1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.phone;
18 
19 import static org.junit.Assert.fail;
20 
21 import android.os.Build;
22 import android.telephony.LocationAccessPolicy;
23 
24 import androidx.test.runner.AndroidJUnit4;
25 
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 
29 @RunWith(AndroidJUnit4.class)
30 public class LocationAccessPolicyBuilderTest {
31     @Test
testBuilderMissingMinCoarse()32     public void testBuilderMissingMinCoarse() {
33         try {
34             new LocationAccessPolicy.LocationPermissionQuery.Builder()
35                     .setMethod("test")
36                     .setCallingPackage("com.android.test")
37                     .setCallingFeatureId(null)
38                     .setCallingPid(0)
39                     .setCallingUid(0)
40                     .setMinSdkVersionForFine(Build.VERSION_CODES.N)
41                     .build();
42             fail("Should have failed without specifying min version for coarse");
43         } catch (IllegalArgumentException e) {
44             // expected
45         }
46     }
47 
48     @Test
testBuilderMissingMinFine()49     public void testBuilderMissingMinFine() {
50         try {
51             new LocationAccessPolicy.LocationPermissionQuery.Builder()
52                     .setMethod("test")
53                     .setCallingPackage("com.android.test")
54                     .setCallingFeatureId(null)
55                     .setCallingPid(0)
56                     .setCallingUid(0)
57                     .setMinSdkVersionForCoarse(Build.VERSION_CODES.N)
58                     .build();
59             fail("Should have failed without specifying min version for fine");
60         } catch (IllegalArgumentException e) {
61             // expected
62         }
63     }
64 
65     @Test
testBuilderMissingMinEnforcement()66     public void testBuilderMissingMinEnforcement() {
67         try {
68             new LocationAccessPolicy.LocationPermissionQuery.Builder()
69                     .setMethod("test")
70                     .setCallingPackage("com.android.test")
71                     .setCallingFeatureId(null)
72                     .setCallingPid(0)
73                     .setCallingUid(0)
74                     .setMinSdkVersionForFine(Build.VERSION_CODES.N)
75                     .setMinSdkVersionForCoarse(Build.VERSION_CODES.N)
76                     .build();
77             fail("Should have failed without specifying min version for any enforcement");
78         } catch (IllegalArgumentException e) {
79             // expected
80         }
81     }
82 }
83