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