9974a6d31fa246a10b8d6252bc3609e9efeb9eee
[android/platform/frameworks/opt/telephony.git] / tests / telephonytests / src / com / android / internal / telephony / CarrierAppUtilsTest.java
1 /*
2  * Copyright (C) 2015 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.internal.telephony;
18
19 import android.content.pm.ApplicationInfo;
20 import android.content.pm.IPackageManager;
21 import android.content.pm.PackageManager;
22 import android.os.Bundle;
23 import android.provider.Settings;
24 import android.telephony.TelephonyManager;
25 import android.test.InstrumentationTestCase;
26 import android.test.mock.MockContentProvider;
27 import android.test.mock.MockContentResolver;
28 import android.test.suitebuilder.annotation.SmallTest;
29 import android.util.ArrayMap;
30
31 import org.junit.Test;
32 import org.mockito.Mock;
33 import org.mockito.Mockito;
34 import org.mockito.MockitoAnnotations;
35
36 import java.util.ArrayList;
37 import java.util.List;
38
39 public class CarrierAppUtilsTest extends InstrumentationTestCase {
40     private static final String CARRIER_APP = "com.example.carrier";
41     private static final String[] CARRIER_APPS = new String[] { CARRIER_APP };
42     private static final String ASSOCIATED_APP = "com.example.associated";
43     private static final ArrayMap<String, List<String>> ASSOCIATED_APPS = new ArrayMap<>();
44     static {
45         List<String> associatedAppList = new ArrayList<>();
46         associatedAppList.add(ASSOCIATED_APP);
47         ASSOCIATED_APPS.put(CARRIER_APP, associatedAppList);
48     }
49     private static final int USER_ID = 12345;
50     private static final String CALLING_PACKAGE = "phone";
51
52     @Mock private IPackageManager mPackageManager;
53     @Mock private TelephonyManager mTelephonyManager;
54     private SettingsMockContentProvider mContentProvider;
55     private MockContentResolver mContentResolver;
56
57     @Override
58     public void setUp() throws Exception {
59         super.setUp();
60         System.setProperty("dexmaker.dexcache",
61                 getInstrumentation().getTargetContext().getCacheDir().getPath());
62         Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
63         MockitoAnnotations.initMocks(this);
64
65         mContentResolver = new MockContentResolver();
66         mContentProvider = new SettingsMockContentProvider();
67         mContentResolver.addProvider(Settings.AUTHORITY, mContentProvider);
68         Settings.Secure.putIntForUser(
69                 mContentResolver, Settings.Secure.CARRIER_APPS_HANDLED, 0, USER_ID);
70     }
71
72     /** No apps configured - should do nothing. */
73     @Test @SmallTest
74     public void testDisableCarrierAppsUntilPrivileged_EmptyList() {
75         CarrierAppUtils.disableCarrierAppsUntilPrivileged(CALLING_PACKAGE, mPackageManager,
76                 mTelephonyManager, mContentResolver, USER_ID, new String[0],
77                 ASSOCIATED_APPS);
78         Mockito.verifyNoMoreInteractions(mPackageManager, mTelephonyManager);
79     }
80
81     /** Configured app is missing - should do nothing. */
82     @Test @SmallTest
83     public void testDisableCarrierAppsUntilPrivileged_MissingApp() throws Exception {
84         Mockito.when(mPackageManager.getApplicationInfo("com.example.missing.app",
85                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID)).thenReturn(null);
86         CarrierAppUtils.disableCarrierAppsUntilPrivileged(CALLING_PACKAGE, mPackageManager,
87                 mTelephonyManager, mContentResolver, USER_ID,
88                 new String[] { "com.example.missing.app" }, ASSOCIATED_APPS);
89         Mockito.verify(mPackageManager, Mockito.never()).setApplicationEnabledSetting(
90                 Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt(),
91                 Mockito.anyString());
92         Mockito.verify(mPackageManager, Mockito.never())
93                 .grantDefaultPermissionsToEnabledCarrierApps(Mockito.any(String[].class),
94                         Mockito.anyInt());
95         Mockito.verifyNoMoreInteractions(mTelephonyManager);
96     }
97
98     /** Configured app is not bundled with the system - should do nothing. */
99     @Test @SmallTest
100     public void testDisableCarrierAppsUntilPrivileged_NonSystemApp() throws Exception {
101         ApplicationInfo appInfo = new ApplicationInfo();
102         Mockito.when(mPackageManager.getApplicationInfo(CARRIER_APP,
103                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID)).thenReturn(appInfo);
104         CarrierAppUtils.disableCarrierAppsUntilPrivileged(CALLING_PACKAGE, mPackageManager,
105                 mTelephonyManager, mContentResolver, USER_ID, CARRIER_APPS, ASSOCIATED_APPS);
106         Mockito.verify(mPackageManager, Mockito.never()).setApplicationEnabledSetting(
107                 Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt(),
108                 Mockito.anyString());
109         Mockito.verify(mPackageManager, Mockito.never())
110                 .grantDefaultPermissionsToEnabledCarrierApps(
111                         Mockito.any(String[].class), Mockito.anyInt());
112         Mockito.verifyNoMoreInteractions(mTelephonyManager);
113     }
114
115     /**
116      * Configured app has privileges, but was disabled by the user - should only grant
117      * permissions.
118      */
119     @Test @SmallTest
120     public void testDisableCarrierAppsUntilPrivileged_HasPrivileges_DisabledUser()
121             throws Exception {
122         ApplicationInfo appInfo = new ApplicationInfo();
123         appInfo.packageName = CARRIER_APP;
124         appInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
125         appInfo.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER;
126         Mockito.when(mPackageManager.getApplicationInfo(CARRIER_APP,
127                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID)).thenReturn(appInfo);
128         Mockito.when(mTelephonyManager.checkCarrierPrivilegesForPackageAnyPhone(CARRIER_APP))
129                 .thenReturn(TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS);
130         CarrierAppUtils.disableCarrierAppsUntilPrivileged(CALLING_PACKAGE, mPackageManager,
131                 mTelephonyManager, mContentResolver, USER_ID, CARRIER_APPS, ASSOCIATED_APPS);
132         Mockito.verify(mPackageManager, Mockito.never()).setApplicationEnabledSetting(
133                 Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt(),
134                 Mockito.anyString());
135         Mockito.verify(mPackageManager).grantDefaultPermissionsToEnabledCarrierApps(
136                 new String[] {appInfo.packageName}, USER_ID);
137     }
138
139     /** Configured app has privileges, but was disabled - should only grant permissions. */
140     @Test @SmallTest
141     public void testDisableCarrierAppsUntilPrivileged_HasPrivileges_Disabled() throws Exception {
142         ApplicationInfo appInfo = new ApplicationInfo();
143         appInfo.packageName = CARRIER_APP;
144         appInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
145         appInfo.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
146         Mockito.when(mPackageManager.getApplicationInfo(CARRIER_APP,
147                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID)).thenReturn(appInfo);
148         Mockito.when(mTelephonyManager.checkCarrierPrivilegesForPackageAnyPhone(CARRIER_APP))
149                 .thenReturn(TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS);
150         CarrierAppUtils.disableCarrierAppsUntilPrivileged(CALLING_PACKAGE, mPackageManager,
151                 mTelephonyManager, mContentResolver, USER_ID, CARRIER_APPS, ASSOCIATED_APPS);
152         Mockito.verify(mPackageManager, Mockito.never()).setApplicationEnabledSetting(
153                 Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt(),
154                 Mockito.anyString());
155         Mockito.verify(mPackageManager).grantDefaultPermissionsToEnabledCarrierApps(
156                 new String[] {appInfo.packageName}, USER_ID);
157     }
158
159     /** Configured app has privileges, and is already enabled - should only grant permissions. */
160     @Test @SmallTest
161     public void testDisableCarrierAppsUntilPrivileged_HasPrivileges_Enabled() throws Exception {
162         ApplicationInfo appInfo = new ApplicationInfo();
163         appInfo.packageName = CARRIER_APP;
164         appInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
165         appInfo.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
166         Mockito.when(mPackageManager.getApplicationInfo(CARRIER_APP,
167                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID)).thenReturn(appInfo);
168         Mockito.when(mTelephonyManager.checkCarrierPrivilegesForPackageAnyPhone(CARRIER_APP))
169                 .thenReturn(TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS);
170         CarrierAppUtils.disableCarrierAppsUntilPrivileged(CALLING_PACKAGE, mPackageManager,
171                 mTelephonyManager, mContentResolver, USER_ID, CARRIER_APPS, ASSOCIATED_APPS);
172         Mockito.verify(mPackageManager, Mockito.never()).setApplicationEnabledSetting(
173                 Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt(),
174                 Mockito.anyString());
175         Mockito.verify(mPackageManager).grantDefaultPermissionsToEnabledCarrierApps(
176                 new String[] {appInfo.packageName}, USER_ID);
177     }
178
179     /** Configured /data app has privileges - should only grant permissions. */
180     @Test @SmallTest
181     public void testDisableCarrierAppsUntilPrivileged_HasPrivileges_UpdatedApp() throws Exception {
182         ApplicationInfo appInfo = new ApplicationInfo();
183         appInfo.packageName = CARRIER_APP;
184         appInfo.flags |= ApplicationInfo.FLAG_SYSTEM | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
185         appInfo.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
186         Mockito.when(mPackageManager.getApplicationInfo(CARRIER_APP,
187                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID)).thenReturn(appInfo);
188         Mockito.when(mTelephonyManager.checkCarrierPrivilegesForPackageAnyPhone(CARRIER_APP))
189                 .thenReturn(TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS);
190         CarrierAppUtils.disableCarrierAppsUntilPrivileged(CALLING_PACKAGE, mPackageManager,
191                 mTelephonyManager, mContentResolver, USER_ID, CARRIER_APPS, ASSOCIATED_APPS);
192         Mockito.verify(mPackageManager, Mockito.never()).setApplicationEnabledSetting(
193                 Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt(),
194                 Mockito.anyString());
195         Mockito.verify(mPackageManager).grantDefaultPermissionsToEnabledCarrierApps(
196                 new String[] {appInfo.packageName}, USER_ID);
197     }
198
199     /**
200      * Configured app has privileges, and is in the default state - should enable. Associated app
201      * is missing and should not be touched.
202      */
203     @Test @SmallTest
204     public void testDisableCarrierAppsUntilPrivileged_HasPrivileges_MissingAssociated_Default()
205             throws Exception {
206         ApplicationInfo appInfo = new ApplicationInfo();
207         appInfo.packageName = CARRIER_APP;
208         appInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
209         appInfo.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
210         Mockito.when(mPackageManager.getApplicationInfo(CARRIER_APP,
211                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID))
212                 .thenReturn(appInfo);
213         Mockito.when(mTelephonyManager.checkCarrierPrivilegesForPackageAnyPhone(CARRIER_APP))
214                 .thenReturn(TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS);
215         CarrierAppUtils.disableCarrierAppsUntilPrivileged(CALLING_PACKAGE, mPackageManager,
216                 mTelephonyManager, mContentResolver, USER_ID, CARRIER_APPS, ASSOCIATED_APPS);
217         Mockito.verify(mPackageManager).setApplicationEnabledSetting(
218                 CARRIER_APP, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
219                 PackageManager.DONT_KILL_APP, USER_ID, CALLING_PACKAGE);
220         Mockito.verify(mPackageManager, Mockito.never()).setApplicationEnabledSetting(
221                 ASSOCIATED_APP, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
222                 PackageManager.DONT_KILL_APP, USER_ID, CALLING_PACKAGE);
223         Mockito.verify(mPackageManager).grantDefaultPermissionsToEnabledCarrierApps(
224                 new String[] {appInfo.packageName}, USER_ID);
225     }
226
227     /**
228      * Configured app has privileges, and is in the default state along with associated app - should
229      * enable both.
230      */
231     @Test @SmallTest
232     public void testDisableCarrierAppsUntilPrivileged_HasPrivileges_Associated_Default()
233             throws Exception {
234         // Enabling should be done even if this isn't the first run.
235         Settings.Secure.putIntForUser(
236                 mContentResolver, Settings.Secure.CARRIER_APPS_HANDLED, 1, USER_ID);
237         ApplicationInfo appInfo = new ApplicationInfo();
238         appInfo.packageName = CARRIER_APP;
239         appInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
240         appInfo.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
241         Mockito.when(mPackageManager.getApplicationInfo(CARRIER_APP,
242                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID))
243                 .thenReturn(appInfo);
244         ApplicationInfo associatedAppInfo = new ApplicationInfo();
245         associatedAppInfo.packageName = ASSOCIATED_APP;
246         associatedAppInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
247         associatedAppInfo.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
248         Mockito.when(mPackageManager.getApplicationInfo(ASSOCIATED_APP,
249                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID))
250                 .thenReturn(associatedAppInfo);
251         Mockito.when(mTelephonyManager.checkCarrierPrivilegesForPackageAnyPhone(CARRIER_APP))
252                 .thenReturn(TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS);
253         CarrierAppUtils.disableCarrierAppsUntilPrivileged(CALLING_PACKAGE, mPackageManager,
254                 mTelephonyManager, mContentResolver, USER_ID, CARRIER_APPS, ASSOCIATED_APPS);
255         Mockito.verify(mPackageManager).setApplicationEnabledSetting(
256                 CARRIER_APP, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
257                 PackageManager.DONT_KILL_APP, USER_ID, CALLING_PACKAGE);
258         Mockito.verify(mPackageManager).setApplicationEnabledSetting(
259                 ASSOCIATED_APP, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
260                 PackageManager.DONT_KILL_APP, USER_ID, CALLING_PACKAGE);
261         Mockito.verify(mPackageManager).grantDefaultPermissionsToEnabledCarrierApps(
262                 new String[] {appInfo.packageName}, USER_ID);
263     }
264
265     /**
266      * Configured app has privileges, and is disabled until used - should enable. Associated app has
267      * been updated and should not be touched.
268      */
269     @Test @SmallTest
270     public void testDisableCarrierAppsUntilPrivileged_HasPrivileges_UpdatedAssociated_DisabledUntilUsed()
271             throws Exception {
272         ApplicationInfo appInfo = new ApplicationInfo();
273         appInfo.packageName = CARRIER_APP;
274         appInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
275         appInfo.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED;
276         Mockito.when(mPackageManager.getApplicationInfo(CARRIER_APP,
277                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID)).thenReturn(appInfo);
278         ApplicationInfo associatedAppInfo = new ApplicationInfo();
279         associatedAppInfo.packageName = ASSOCIATED_APP;
280         associatedAppInfo.flags |=
281                 ApplicationInfo.FLAG_SYSTEM | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
282         associatedAppInfo.enabledSetting =
283                 PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED;
284         Mockito.when(mPackageManager.getApplicationInfo(ASSOCIATED_APP,
285                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID))
286                 .thenReturn(associatedAppInfo);
287         Mockito.when(mTelephonyManager.checkCarrierPrivilegesForPackageAnyPhone(CARRIER_APP))
288                 .thenReturn(TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS);
289         CarrierAppUtils.disableCarrierAppsUntilPrivileged(CALLING_PACKAGE, mPackageManager,
290                 mTelephonyManager, mContentResolver, USER_ID, CARRIER_APPS, ASSOCIATED_APPS);
291         Mockito.verify(mPackageManager).setApplicationEnabledSetting(
292                 CARRIER_APP, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
293                 PackageManager.DONT_KILL_APP, USER_ID, CALLING_PACKAGE);
294         Mockito.verify(mPackageManager, Mockito.never()).setApplicationEnabledSetting(
295                 ASSOCIATED_APP, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
296                 PackageManager.DONT_KILL_APP, USER_ID, CALLING_PACKAGE);
297         Mockito.verify(mPackageManager).grantDefaultPermissionsToEnabledCarrierApps(
298                 new String[] {appInfo.packageName}, USER_ID);
299     }
300
301     /**
302      * Configured app has privileges, and is disabled until used along with associated app - should
303      * enable both.
304      */
305     @Test @SmallTest
306     public void testDisableCarrierAppsUntilPrivileged_HasPrivileges_Associated_DisabledUntilUsed()
307             throws Exception {
308         ApplicationInfo appInfo = new ApplicationInfo();
309         appInfo.packageName = CARRIER_APP;
310         appInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
311         appInfo.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED;
312         Mockito.when(mPackageManager.getApplicationInfo(CARRIER_APP,
313                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID)).thenReturn(appInfo);
314         ApplicationInfo associatedAppInfo = new ApplicationInfo();
315         associatedAppInfo.packageName = ASSOCIATED_APP;
316         associatedAppInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
317         associatedAppInfo.enabledSetting =
318                 PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED;
319         Mockito.when(mPackageManager.getApplicationInfo(ASSOCIATED_APP,
320                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID))
321                 .thenReturn(associatedAppInfo);
322         Mockito.when(mTelephonyManager.checkCarrierPrivilegesForPackageAnyPhone(CARRIER_APP))
323                 .thenReturn(TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS);
324         CarrierAppUtils.disableCarrierAppsUntilPrivileged(CALLING_PACKAGE, mPackageManager,
325                 mTelephonyManager, mContentResolver, USER_ID, CARRIER_APPS, ASSOCIATED_APPS);
326         Mockito.verify(mPackageManager).setApplicationEnabledSetting(
327                 CARRIER_APP, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
328                 PackageManager.DONT_KILL_APP, USER_ID, CALLING_PACKAGE);
329         Mockito.verify(mPackageManager).setApplicationEnabledSetting(
330                 ASSOCIATED_APP, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
331                 PackageManager.DONT_KILL_APP, USER_ID, CALLING_PACKAGE);
332         Mockito.verify(mPackageManager).grantDefaultPermissionsToEnabledCarrierApps(
333                 new String[] {appInfo.packageName}, USER_ID);
334     }
335
336     /** Configured app has no privileges, and was disabled by the user - should do nothing. */
337     @Test @SmallTest
338     public void testDisableCarrierAppsUntilPrivileged_NoPrivileges_DisabledUser() throws Exception {
339         ApplicationInfo appInfo = new ApplicationInfo();
340         appInfo.packageName = CARRIER_APP;
341         appInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
342         appInfo.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER;
343         Mockito.when(mPackageManager.getApplicationInfo(CARRIER_APP,
344                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID)).thenReturn(appInfo);
345         Mockito.when(mTelephonyManager.checkCarrierPrivilegesForPackageAnyPhone(CARRIER_APP))
346                 .thenReturn(TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS);
347         CarrierAppUtils.disableCarrierAppsUntilPrivileged(CALLING_PACKAGE, mPackageManager,
348                 mTelephonyManager, mContentResolver, USER_ID, CARRIER_APPS, ASSOCIATED_APPS);
349         Mockito.verify(mPackageManager, Mockito.never()).setApplicationEnabledSetting(
350                 Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt(),
351                 Mockito.anyString());
352         Mockito.verify(mPackageManager, Mockito.never())
353                 .grantDefaultPermissionsToEnabledCarrierApps(
354                         Mockito.any(String[].class), Mockito.anyInt());
355     }
356
357     /** Telephony is not initialized, and app was disabled by the user - should do nothing. */
358     @Test @SmallTest
359     public void testDisableCarrierAppsUntilPrivileged_NullPrivileges_DisabledUser()
360             throws Exception {
361         ApplicationInfo appInfo = new ApplicationInfo();
362         appInfo.packageName = CARRIER_APP;
363         appInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
364         appInfo.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER;
365         Mockito.when(mPackageManager.getApplicationInfo(CARRIER_APP,
366                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID)).thenReturn(appInfo);
367         CarrierAppUtils.disableCarrierAppsUntilPrivileged(CALLING_PACKAGE, mPackageManager,
368                 null /* telephonyManager */, mContentResolver, USER_ID, CARRIER_APPS,
369                 ASSOCIATED_APPS);
370         Mockito.verify(mPackageManager, Mockito.never()).setApplicationEnabledSetting(
371                 Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt(),
372                 Mockito.anyString());
373         Mockito.verify(mPackageManager, Mockito.never())
374                 .grantDefaultPermissionsToEnabledCarrierApps(
375                         Mockito.any(String[].class), Mockito.anyInt());
376     }
377
378     /** Configured app has no privileges, and was disabled - should do nothing. */
379     @Test @SmallTest
380     public void testDisableCarrierAppsUntilPrivileged_NoPrivileges_Disabled() throws Exception {
381         ApplicationInfo appInfo = new ApplicationInfo();
382         appInfo.packageName = CARRIER_APP;
383         appInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
384         appInfo.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
385         Mockito.when(mPackageManager.getApplicationInfo(CARRIER_APP,
386                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID)).thenReturn(appInfo);
387         Mockito.when(mTelephonyManager.checkCarrierPrivilegesForPackageAnyPhone(CARRIER_APP))
388                 .thenReturn(TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS);
389         CarrierAppUtils.disableCarrierAppsUntilPrivileged(CALLING_PACKAGE, mPackageManager,
390                 mTelephonyManager, mContentResolver, USER_ID, CARRIER_APPS, ASSOCIATED_APPS);
391         Mockito.verify(mPackageManager, Mockito.never()).setApplicationEnabledSetting(
392                 Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt(),
393                 Mockito.anyString());
394         Mockito.verify(mPackageManager, Mockito.never())
395                 .grantDefaultPermissionsToEnabledCarrierApps(
396                         Mockito.any(String[].class), Mockito.anyInt());
397     }
398
399     /** Telephony is not initialized, and app was disabled - should do nothing. */
400     @Test @SmallTest
401     public void testDisableCarrierAppsUntilPrivileged_NullPrivileges_Disabled() throws Exception {
402         ApplicationInfo appInfo = new ApplicationInfo();
403         appInfo.packageName = CARRIER_APP;
404         appInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
405         appInfo.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
406         Mockito.when(mPackageManager.getApplicationInfo(CARRIER_APP,
407                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID)).thenReturn(appInfo);
408         CarrierAppUtils.disableCarrierAppsUntilPrivileged(CALLING_PACKAGE, mPackageManager,
409                 null /* telephonyManager */, mContentResolver, USER_ID, CARRIER_APPS,
410                 ASSOCIATED_APPS);
411         Mockito.verify(mPackageManager, Mockito.never()).setApplicationEnabledSetting(
412                 Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt(),
413                 Mockito.anyString());
414         Mockito.verify(mPackageManager, Mockito.never())
415                 .grantDefaultPermissionsToEnabledCarrierApps(
416                         Mockito.any(String[].class), Mockito.anyInt());
417     }
418
419     /** Configured app has no privileges, and is explicitly enabled - should do nothing. */
420     @Test @SmallTest
421     public void testDisableCarrierAppsUntilPrivileged_NoPrivileges_Enabled() throws Exception {
422         ApplicationInfo appInfo = new ApplicationInfo();
423         appInfo.packageName = CARRIER_APP;
424         appInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
425         appInfo.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
426         Mockito.when(mPackageManager.getApplicationInfo(CARRIER_APP,
427                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID)).thenReturn(appInfo);
428         Mockito.when(mTelephonyManager.checkCarrierPrivilegesForPackageAnyPhone(CARRIER_APP))
429                 .thenReturn(TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS);
430         CarrierAppUtils.disableCarrierAppsUntilPrivileged(CALLING_PACKAGE, mPackageManager,
431                 mTelephonyManager, mContentResolver, USER_ID, CARRIER_APPS, ASSOCIATED_APPS);
432         Mockito.verify(mPackageManager, Mockito.never()).setApplicationEnabledSetting(
433                 Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt(),
434                 Mockito.anyString());
435         Mockito.verify(mPackageManager, Mockito.never())
436                 .grantDefaultPermissionsToEnabledCarrierApps(
437                         Mockito.any(String[].class), Mockito.anyInt());
438     }
439
440     /** Telephony is not initialized, and app is explicitly enabled - should do nothing. */
441     @Test @SmallTest
442     public void testDisableCarrierAppsUntilPrivileged_NullPrivileges_Enabled() throws Exception {
443         ApplicationInfo appInfo = new ApplicationInfo();
444         appInfo.packageName = CARRIER_APP;
445         appInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
446         appInfo.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
447         Mockito.when(mPackageManager.getApplicationInfo(CARRIER_APP,
448                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID)).thenReturn(appInfo);
449         CarrierAppUtils.disableCarrierAppsUntilPrivileged(CALLING_PACKAGE, mPackageManager,
450                 null /* telephonyManager */, mContentResolver, USER_ID, CARRIER_APPS,
451                 ASSOCIATED_APPS);
452         Mockito.verify(mPackageManager, Mockito.never()).setApplicationEnabledSetting(
453                 Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt(),
454                 Mockito.anyString());
455         Mockito.verify(mPackageManager, Mockito.never())
456                 .grantDefaultPermissionsToEnabledCarrierApps(
457                         Mockito.any(String[].class), Mockito.anyInt());
458     }
459
460     /** Configured /data app has no privileges - should do nothing. */
461     @Test @SmallTest
462     public void testDisableCarrierAppsUntilPrivileged_NoPrivileges_UpdatedApp() throws Exception {
463         ApplicationInfo appInfo = new ApplicationInfo();
464         appInfo.packageName = CARRIER_APP;
465         appInfo.flags |= ApplicationInfo.FLAG_SYSTEM | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
466         appInfo.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
467         Mockito.when(mPackageManager.getApplicationInfo(CARRIER_APP,
468                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID)).thenReturn(appInfo);
469         Mockito.when(mTelephonyManager.checkCarrierPrivilegesForPackageAnyPhone(CARRIER_APP))
470                 .thenReturn(TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS);
471         CarrierAppUtils.disableCarrierAppsUntilPrivileged(CALLING_PACKAGE, mPackageManager,
472                 mTelephonyManager, mContentResolver, USER_ID, CARRIER_APPS, ASSOCIATED_APPS);
473         Mockito.verify(mPackageManager, Mockito.never()).setApplicationEnabledSetting(
474                 Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt(),
475                 Mockito.anyString());
476         Mockito.verify(mPackageManager, Mockito.never())
477                 .grantDefaultPermissionsToEnabledCarrierApps(
478                         Mockito.any(String[].class), Mockito.anyInt());
479     }
480
481     /** Telephony is not initialized and app is in /data - should do nothing. */
482     @Test @SmallTest
483     public void testDisableCarrierAppsUntilPrivileged_NullPrivileges_UpdatedApp() throws Exception {
484         ApplicationInfo appInfo = new ApplicationInfo();
485         appInfo.packageName = CARRIER_APP;
486         appInfo.flags |= ApplicationInfo.FLAG_SYSTEM | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
487         appInfo.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
488         Mockito.when(mPackageManager.getApplicationInfo(CARRIER_APP,
489                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID)).thenReturn(appInfo);
490         CarrierAppUtils.disableCarrierAppsUntilPrivileged(CALLING_PACKAGE, mPackageManager,
491                 null /* telephonyManager */, mContentResolver, USER_ID, CARRIER_APPS,
492                 ASSOCIATED_APPS);
493         Mockito.verify(mPackageManager, Mockito.never()).setApplicationEnabledSetting(
494                 Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt(),
495                 Mockito.anyString());
496         Mockito.verify(mPackageManager, Mockito.never())
497                 .grantDefaultPermissionsToEnabledCarrierApps(
498                         Mockito.any(String[].class), Mockito.anyInt());
499     }
500
501     /**
502      * Configured app has no privileges, and is in the default state - should disable until use.
503      * Associated app is enabled and should not be touched.
504      */
505     @Test @SmallTest
506     public void testDisableCarrierAppsUntilPrivileged_NoPrivileges_EnabledAssociated_Default()
507             throws Exception {
508         ApplicationInfo appInfo = new ApplicationInfo();
509         appInfo.packageName = CARRIER_APP;
510         appInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
511         appInfo.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
512         Mockito.when(mPackageManager.getApplicationInfo(CARRIER_APP,
513                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID)).thenReturn(appInfo);
514         ApplicationInfo associatedAppInfo = new ApplicationInfo();
515         associatedAppInfo.packageName = ASSOCIATED_APP;
516         associatedAppInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
517         associatedAppInfo.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
518         Mockito.when(mPackageManager.getApplicationInfo(ASSOCIATED_APP,
519                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID))
520                 .thenReturn(associatedAppInfo);
521         Mockito.when(mTelephonyManager.checkCarrierPrivilegesForPackageAnyPhone(CARRIER_APP))
522                 .thenReturn(TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS);
523         CarrierAppUtils.disableCarrierAppsUntilPrivileged(CALLING_PACKAGE, mPackageManager,
524                 mTelephonyManager, mContentResolver, USER_ID, CARRIER_APPS, ASSOCIATED_APPS);
525         Mockito.verify(mPackageManager).setApplicationEnabledSetting(
526                 CARRIER_APP, PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED, 0, USER_ID,
527                 CALLING_PACKAGE);
528         Mockito.verify(mPackageManager, Mockito.never()).setApplicationEnabledSetting(
529                 ASSOCIATED_APP, PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED, 0,
530                 USER_ID, CALLING_PACKAGE);
531         Mockito.verify(mPackageManager, Mockito.never())
532                 .grantDefaultPermissionsToEnabledCarrierApps(
533                         Mockito.any(String[].class), Mockito.anyInt());
534     }
535
536     /**
537      * Configured app has no privileges, and is in the default state along with associated app -
538      * should disable both.
539      */
540     @Test @SmallTest
541     public void testDisableCarrierAppsUntilPrivileged_NoPrivileges_Associated_Default()
542             throws Exception {
543         ApplicationInfo appInfo = new ApplicationInfo();
544         appInfo.packageName = CARRIER_APP;
545         appInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
546         appInfo.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
547         Mockito.when(mPackageManager.getApplicationInfo(CARRIER_APP,
548                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID)).thenReturn(appInfo);
549         ApplicationInfo associatedAppInfo = new ApplicationInfo();
550         associatedAppInfo.packageName = ASSOCIATED_APP;
551         associatedAppInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
552         associatedAppInfo.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
553         Mockito.when(mPackageManager.getApplicationInfo(ASSOCIATED_APP,
554                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID))
555                 .thenReturn(associatedAppInfo);
556         Mockito.when(mTelephonyManager.checkCarrierPrivilegesForPackageAnyPhone(CARRIER_APP))
557                 .thenReturn(TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS);
558         CarrierAppUtils.disableCarrierAppsUntilPrivileged(CALLING_PACKAGE, mPackageManager,
559                 mTelephonyManager, mContentResolver, USER_ID, CARRIER_APPS, ASSOCIATED_APPS);
560         Mockito.verify(mPackageManager).setApplicationEnabledSetting(
561                 CARRIER_APP, PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED, 0, USER_ID,
562                 CALLING_PACKAGE);
563         Mockito.verify(mPackageManager).setApplicationEnabledSetting(
564                 ASSOCIATED_APP, PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED, 0,
565                 USER_ID, CALLING_PACKAGE);
566         Mockito.verify(mPackageManager, Mockito.never())
567                 .grantDefaultPermissionsToEnabledCarrierApps(
568                         Mockito.any(String[].class), Mockito.anyInt());
569     }
570
571     /**
572      * Configured app has no privileges, and is in the default state along with associated app, and
573      * disabling has already occurred - should only disable configured app.
574      */
575     @Test @SmallTest
576     public void testDisableCarrierAppsUntilPrivileged_NoPrivileges_Associated_Default_AlreadyRun()
577             throws Exception {
578         Settings.Secure.putIntForUser(
579                 mContentResolver, Settings.Secure.CARRIER_APPS_HANDLED, 1, USER_ID);
580         ApplicationInfo appInfo = new ApplicationInfo();
581         appInfo.packageName = CARRIER_APP;
582         appInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
583         appInfo.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
584         Mockito.when(mPackageManager.getApplicationInfo(CARRIER_APP,
585                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID)).thenReturn(appInfo);
586         ApplicationInfo associatedAppInfo = new ApplicationInfo();
587         associatedAppInfo.packageName = ASSOCIATED_APP;
588         associatedAppInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
589         associatedAppInfo.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
590         Mockito.when(mPackageManager.getApplicationInfo(ASSOCIATED_APP,
591                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID))
592                 .thenReturn(associatedAppInfo);
593         Mockito.when(mTelephonyManager.checkCarrierPrivilegesForPackageAnyPhone(CARRIER_APP))
594                 .thenReturn(TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS);
595         CarrierAppUtils.disableCarrierAppsUntilPrivileged(CALLING_PACKAGE, mPackageManager,
596                 mTelephonyManager, mContentResolver, USER_ID, CARRIER_APPS, ASSOCIATED_APPS);
597         Mockito.verify(mPackageManager).setApplicationEnabledSetting(
598                 CARRIER_APP, PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED, 0, USER_ID,
599                 CALLING_PACKAGE);
600         Mockito.verify(mPackageManager, Mockito.never()).setApplicationEnabledSetting(
601                 Mockito.eq(ASSOCIATED_APP), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt(),
602                 Mockito.anyString());
603         Mockito.verify(mPackageManager, Mockito.never())
604                 .grantDefaultPermissionsToEnabledCarrierApps(
605                         Mockito.any(String[].class), Mockito.anyInt());
606     }
607
608     /** Telephony is not initialized, and app is in the default state - should disable until use. */
609     @Test @SmallTest
610     public void testDisableCarrierAppsUntilPrivileged_NullPrivileges_Default() throws Exception {
611         ApplicationInfo appInfo = new ApplicationInfo();
612         appInfo.packageName = CARRIER_APP;
613         appInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
614         appInfo.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
615         Mockito.when(mPackageManager.getApplicationInfo(CARRIER_APP,
616                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID)).thenReturn(appInfo);
617         CarrierAppUtils.disableCarrierAppsUntilPrivileged(CALLING_PACKAGE, mPackageManager,
618                 null /* telephonyManager */, mContentResolver, USER_ID, CARRIER_APPS,
619                 ASSOCIATED_APPS);
620         Mockito.verify(mPackageManager).setApplicationEnabledSetting(
621                 CARRIER_APP, PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED, 0, USER_ID,
622                 CALLING_PACKAGE);
623         Mockito.verify(mPackageManager, Mockito.never())
624                 .grantDefaultPermissionsToEnabledCarrierApps(
625                         Mockito.any(String[].class), Mockito.anyInt());
626     }
627
628     /** Configured app has no privileges, and is disabled until used - should do nothing. */
629     @Test @SmallTest
630     public void testDisableCarrierAppsUntilPrivileged_NoPrivileges_DisabledUntilUsed()
631             throws Exception {
632         ApplicationInfo appInfo = new ApplicationInfo();
633         appInfo.packageName = CARRIER_APP;
634         appInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
635         appInfo.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED;
636         Mockito.when(mPackageManager.getApplicationInfo(CARRIER_APP,
637                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID)).thenReturn(appInfo);
638         Mockito.when(mTelephonyManager.checkCarrierPrivilegesForPackageAnyPhone(CARRIER_APP))
639                 .thenReturn(TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS);
640         CarrierAppUtils.disableCarrierAppsUntilPrivileged(CALLING_PACKAGE, mPackageManager,
641                 mTelephonyManager, mContentResolver, USER_ID, CARRIER_APPS, ASSOCIATED_APPS);
642         Mockito.verify(mPackageManager, Mockito.never()).setApplicationEnabledSetting(
643                 Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt(),
644                 Mockito.anyString());
645         Mockito.verify(mPackageManager, Mockito.never())
646                 .grantDefaultPermissionsToEnabledCarrierApps(
647                         Mockito.any(String[].class), Mockito.anyInt());
648     }
649
650     /** Telephony is not initialized, and app is disabled until used - should do nothing. */
651     @Test @SmallTest
652     public void testDisableCarrierAppsUntilPrivileged_NullPrivileges_DisabledUntilUsed()
653             throws Exception {
654         ApplicationInfo appInfo = new ApplicationInfo();
655         appInfo.packageName = CARRIER_APP;
656         appInfo.flags |= ApplicationInfo.FLAG_SYSTEM;
657         appInfo.enabledSetting = PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED;
658         Mockito.when(mPackageManager.getApplicationInfo(CARRIER_APP,
659                 PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS, USER_ID)).thenReturn(appInfo);
660         CarrierAppUtils.disableCarrierAppsUntilPrivileged(CALLING_PACKAGE, mPackageManager,
661                 null /* telephonyManager */, mContentResolver, USER_ID, CARRIER_APPS,
662                 ASSOCIATED_APPS);
663         Mockito.verify(mPackageManager, Mockito.never()).setApplicationEnabledSetting(
664                 Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt(), Mockito.anyInt(),
665                 Mockito.anyString());
666         Mockito.verify(mPackageManager, Mockito.never())
667                 .grantDefaultPermissionsToEnabledCarrierApps(
668                         Mockito.any(String[].class), Mockito.anyInt());
669     }
670
671     class SettingsMockContentProvider extends MockContentProvider {
672         private int mExpectedValue;
673
674         @Override
675         public Bundle call(String method, String request, Bundle args) {
676             Bundle result = new Bundle();
677             if (Settings.CALL_METHOD_GET_SECURE.equals(method)) {
678                 result.putString(Settings.NameValueTable.VALUE, Integer.toString(mExpectedValue));
679             } else {
680                 mExpectedValue = Integer.parseInt(args.getString(Settings.NameValueTable.VALUE));
681             }
682             return result;
683         }
684     }
685
686 }
687