• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.settings.applications;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.mockito.Mockito.when;
21 
22 import android.app.admin.DevicePolicyManager;
23 import android.content.ComponentName;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.content.pm.ActivityInfo;
27 import android.content.pm.ApplicationInfo;
28 import android.content.pm.IPackageManager;
29 import android.content.pm.PackageManager;
30 import android.content.pm.ResolveInfo;
31 import android.content.pm.UserInfo;
32 import android.os.Build;
33 import android.os.UserHandle;
34 import android.os.UserManager;
35 
36 import com.android.settings.testutils.ApplicationTestUtils;
37 import com.android.settings.testutils.SettingsRobolectricTestRunner;
38 import com.android.settings.testutils.shadow.ShadowDefaultDialerManager;
39 import com.android.settings.testutils.shadow.ShadowSmsApplication;
40 import com.android.settingslib.wrapper.PackageManagerWrapper;
41 
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.mockito.Mock;
46 import org.mockito.MockitoAnnotations;
47 import org.robolectric.RuntimeEnvironment;
48 import org.robolectric.annotation.Config;
49 import org.robolectric.shadows.ShadowApplication;
50 import org.robolectric.util.ReflectionHelpers;
51 
52 import java.util.ArrayList;
53 import java.util.Arrays;
54 import java.util.List;
55 import java.util.Set;
56 
57 /**
58  * Tests for {@link ApplicationFeatureProviderImpl}.
59  */
60 @RunWith(SettingsRobolectricTestRunner.class)
61 public final class ApplicationFeatureProviderImplTest {
62 
63     private final int MAIN_USER_ID = 0;
64     private final int MANAGED_PROFILE_ID = 10;
65 
66     private final int PER_USER_UID_RANGE = 100000;
67     private final int APP_1_UID = MAIN_USER_ID * PER_USER_UID_RANGE + 1;
68     private final int APP_2_UID = MANAGED_PROFILE_ID * PER_USER_UID_RANGE + 1;
69 
70     private final String APP_1 = "app1";
71     private final String APP_2 = "app2";
72 
73     private final String PERMISSION = "some.permission";
74 
75     @Mock
76     private UserManager mUserManager;
77     @Mock
78     private Context mContext;
79     @Mock
80     private PackageManagerWrapper mPackageManager;
81     @Mock
82     private IPackageManager mPackageManagerService;
83     @Mock
84     private DevicePolicyManager mDevicePolicyManager;
85 
86     private ApplicationFeatureProvider mProvider;
87 
88     private int mAppCount = -1;
89     private List<UserAppInfo> mAppList = null;
90 
91     @Before
setUp()92     public void setUp() {
93         MockitoAnnotations.initMocks(this);
94 
95         when(mContext.getApplicationContext()).thenReturn(mContext);
96         when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
97 
98         mProvider = new ApplicationFeatureProviderImpl(mContext, mPackageManager,
99                 mPackageManagerService, mDevicePolicyManager);
100     }
101 
verifyCalculateNumberOfPolicyInstalledApps(boolean async)102     private void verifyCalculateNumberOfPolicyInstalledApps(boolean async) {
103         setUpUsersAndInstalledApps();
104 
105         when(mPackageManager.getInstallReason(APP_1, new UserHandle(MAIN_USER_ID)))
106                 .thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
107         when(mPackageManager.getInstallReason(APP_2, new UserHandle(MANAGED_PROFILE_ID)))
108                 .thenReturn(PackageManager.INSTALL_REASON_POLICY);
109 
110         mAppCount = -1;
111         mProvider.calculateNumberOfPolicyInstalledApps(async,
112                 (num) -> mAppCount = num);
113         if (async) {
114             ShadowApplication.runBackgroundTasks();
115         }
116         assertThat(mAppCount).isEqualTo(1);
117     }
118 
119     @Test
testListPolicyInstalledApps()120     public void testListPolicyInstalledApps() {
121         setUpUsersAndInstalledApps();
122 
123         when(mPackageManager.getInstallReason(APP_1, new UserHandle(MAIN_USER_ID)))
124                 .thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
125         when(mPackageManager.getInstallReason(APP_2, new UserHandle(MANAGED_PROFILE_ID)))
126                 .thenReturn(PackageManager.INSTALL_REASON_POLICY);
127 
128         mAppList = null;
129         mProvider.listPolicyInstalledApps((list) -> mAppList = list);
130         assertThat(mAppList).isNotNull();
131         assertThat(mAppList.size()).isEqualTo(1);
132         assertThat(mAppList.get(0).appInfo.packageName).isEqualTo(APP_2);
133     }
134 
135     @Test
testCalculateNumberOfInstalledAppsSync()136     public void testCalculateNumberOfInstalledAppsSync() {
137         verifyCalculateNumberOfPolicyInstalledApps(false /* async */);
138     }
139 
140     @Test
testCalculateNumberOfInstalledAppsAsync()141     public void testCalculateNumberOfInstalledAppsAsync() {
142         verifyCalculateNumberOfPolicyInstalledApps(true /* async */);
143     }
144 
verifyCalculateNumberOfAppsWithAdminGrantedPermissions(boolean async)145     private void verifyCalculateNumberOfAppsWithAdminGrantedPermissions(boolean async)
146             throws Exception {
147         setUpUsersAndInstalledApps();
148 
149         when(mDevicePolicyManager.getPermissionGrantState(null, APP_1, PERMISSION))
150                 .thenReturn(DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED);
151         when(mDevicePolicyManager.getPermissionGrantState(null, APP_2, PERMISSION))
152                 .thenReturn(DevicePolicyManager.PERMISSION_GRANT_STATE_DENIED);
153         when(mPackageManagerService.checkUidPermission(PERMISSION, APP_1_UID))
154                 .thenReturn(PackageManager.PERMISSION_DENIED);
155         when(mPackageManagerService.checkUidPermission(PERMISSION, APP_2_UID))
156                 .thenReturn(PackageManager.PERMISSION_GRANTED);
157         when(mPackageManager.getInstallReason(APP_1, new UserHandle(MAIN_USER_ID)))
158                 .thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
159         when(mPackageManager.getInstallReason(APP_2, new UserHandle(MANAGED_PROFILE_ID)))
160                 .thenReturn(PackageManager.INSTALL_REASON_POLICY);
161 
162         mAppCount = -1;
163         mProvider.calculateNumberOfAppsWithAdminGrantedPermissions(new String[] {PERMISSION}, async,
164                 (num) -> mAppCount = num);
165         if (async) {
166             ShadowApplication.runBackgroundTasks();
167         }
168         assertThat(mAppCount).isEqualTo(2);
169     }
170 
171     @Test
testCalculateNumberOfAppsWithAdminGrantedPermissionsSync()172     public void testCalculateNumberOfAppsWithAdminGrantedPermissionsSync() throws Exception {
173         verifyCalculateNumberOfAppsWithAdminGrantedPermissions(false /* async */);
174     }
175 
176     @Test
testCalculateNumberOfAppsWithAdminGrantedPermissionsAsync()177     public void testCalculateNumberOfAppsWithAdminGrantedPermissionsAsync() throws Exception {
178         verifyCalculateNumberOfAppsWithAdminGrantedPermissions(true /* async */);
179     }
180 
181     @Test
testListAppsWithAdminGrantedPermissions()182     public void testListAppsWithAdminGrantedPermissions()
183             throws Exception {
184         setUpUsersAndInstalledApps();
185 
186         when(mDevicePolicyManager.getPermissionGrantState(null, APP_1, PERMISSION))
187                 .thenReturn(DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED);
188         when(mDevicePolicyManager.getPermissionGrantState(null, APP_2, PERMISSION))
189                 .thenReturn(DevicePolicyManager.PERMISSION_GRANT_STATE_DENIED);
190         when(mPackageManagerService.checkUidPermission(PERMISSION, APP_1_UID))
191                 .thenReturn(PackageManager.PERMISSION_DENIED);
192         when(mPackageManagerService.checkUidPermission(PERMISSION, APP_2_UID))
193                 .thenReturn(PackageManager.PERMISSION_GRANTED);
194         when(mPackageManager.getInstallReason(APP_1, new UserHandle(MAIN_USER_ID)))
195                 .thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
196         when(mPackageManager.getInstallReason(APP_2, new UserHandle(MANAGED_PROFILE_ID)))
197                 .thenReturn(PackageManager.INSTALL_REASON_POLICY);
198 
199         mAppList = null;
200         mProvider.listAppsWithAdminGrantedPermissions(new String[] {PERMISSION},
201                 (list) -> mAppList = list);
202         assertThat(mAppList).isNotNull();
203         assertThat(mAppList.size()).isEqualTo(2);
204         assertThat(Arrays.asList(mAppList.get(0).appInfo.packageName,
205                 mAppList.get(1).appInfo.packageName).containsAll(Arrays.asList(APP_1, APP_2)))
206                 .isTrue();
207     }
208 
209     @Test
testFindPersistentPreferredActivities()210     public void testFindPersistentPreferredActivities() throws Exception {
211         final UserInfo mainUser = new UserInfo(MAIN_USER_ID, "main", UserInfo.FLAG_ADMIN);
212         final UserInfo managedUser = new UserInfo(MANAGED_PROFILE_ID, "managed",
213                 UserInfo.FLAG_MANAGED_PROFILE);
214 
215         when(mUserManager.getUserProfiles()).thenReturn(Arrays.asList(new UserHandle(MAIN_USER_ID),
216                 new UserHandle(MANAGED_PROFILE_ID)));
217         when(mUserManager.getUserInfo(MAIN_USER_ID)).thenReturn(mainUser);
218         when(mUserManager.getUserInfo(MANAGED_PROFILE_ID)).thenReturn(managedUser);
219 
220         final Intent viewIntent = new Intent(Intent.ACTION_VIEW);
221         final Intent editIntent = new Intent(Intent.ACTION_EDIT);
222         final Intent sendIntent = new Intent(Intent.ACTION_SEND);
223 
224         final ResolveInfo app1 = createResolveInfo(APP_1);
225         final ResolveInfo app2 = createResolveInfo(APP_2);
226         when(mPackageManagerService.findPersistentPreferredActivity(viewIntent, MAIN_USER_ID))
227                 .thenReturn(app1);
228         when(mPackageManagerService.findPersistentPreferredActivity(viewIntent, MANAGED_PROFILE_ID))
229                 .thenReturn(app1);
230         when(mPackageManagerService.findPersistentPreferredActivity(editIntent, MAIN_USER_ID))
231                 .thenReturn(null);
232         when(mPackageManagerService.findPersistentPreferredActivity(editIntent, MANAGED_PROFILE_ID))
233                 .thenReturn(app2);
234         when(mPackageManagerService.findPersistentPreferredActivity(sendIntent, MAIN_USER_ID))
235                 .thenReturn(app1);
236         when(mPackageManagerService.findPersistentPreferredActivity(sendIntent, MANAGED_PROFILE_ID))
237                 .thenReturn(null);
238 
239         final List<UserAppInfo> expectedMainUserActivities = new ArrayList<>();
240         expectedMainUserActivities.add(new UserAppInfo(mainUser,
241                 new ApplicationInfo(app1.activityInfo.applicationInfo)));
242         final List<UserAppInfo> expectedManagedUserActivities = new ArrayList<>();
243         expectedManagedUserActivities.add(new UserAppInfo(managedUser,
244                 new ApplicationInfo(app1.activityInfo.applicationInfo)));
245         expectedManagedUserActivities.add(new UserAppInfo(managedUser,
246                 new ApplicationInfo(app2.activityInfo.applicationInfo)));
247 
248         assertThat(mProvider.findPersistentPreferredActivities(MAIN_USER_ID,
249                 new Intent[] {viewIntent, editIntent, sendIntent}))
250                 .isEqualTo(expectedMainUserActivities);
251         assertThat(mProvider.findPersistentPreferredActivities(MANAGED_PROFILE_ID,
252                 new Intent[] {viewIntent, editIntent, sendIntent}))
253                 .isEqualTo(expectedManagedUserActivities);
254     }
255 
256     @Test
257     @Config(shadows = {ShadowSmsApplication.class, ShadowDefaultDialerManager.class})
getKeepEnabledPackages_shouldContainDefaultPhoneAndSms()258     public void getKeepEnabledPackages_shouldContainDefaultPhoneAndSms() {
259         final String testDialer = "com.android.test.defaultdialer";
260         final String testSms = "com.android.test.defaultsms";
261         ShadowSmsApplication.setDefaultSmsApplication(new ComponentName(testSms, "receiver"));
262         ShadowDefaultDialerManager.setDefaultDialerApplication(testDialer);
263         ReflectionHelpers.setField(mProvider, "mContext", RuntimeEnvironment.application);
264 
265         final Set<String> keepEnabledPackages = mProvider.getKeepEnabledPackages();
266 
267         final List<String> expectedPackages = Arrays.asList(testDialer, testSms);
268         assertThat(keepEnabledPackages).containsExactlyElementsIn(expectedPackages);
269     }
270 
setUpUsersAndInstalledApps()271     private void setUpUsersAndInstalledApps() {
272         when(mUserManager.getProfiles(UserHandle.myUserId())).thenReturn(Arrays.asList(
273                 new UserInfo(MAIN_USER_ID, "main", UserInfo.FLAG_ADMIN),
274                 new UserInfo(MANAGED_PROFILE_ID, "managed profile", 0)));
275 
276         when(mPackageManager.getInstalledApplicationsAsUser(PackageManager.GET_DISABLED_COMPONENTS
277                         | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS
278                         | PackageManager.MATCH_ANY_USER,
279                 MAIN_USER_ID)).thenReturn(Arrays.asList(
280                 ApplicationTestUtils.buildInfo(APP_1_UID, APP_1, 0 /* flags */,
281                         Build.VERSION_CODES.M)));
282         when(mPackageManager.getInstalledApplicationsAsUser(PackageManager.GET_DISABLED_COMPONENTS
283                         | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
284                 MANAGED_PROFILE_ID)).thenReturn(Arrays.asList(
285                 ApplicationTestUtils.buildInfo(APP_2_UID, APP_2, 0 /* flags */,
286                         Build.VERSION_CODES.LOLLIPOP)));
287     }
288 
createResolveInfo(String packageName)289     private ResolveInfo createResolveInfo(String packageName) {
290         final ApplicationInfo applicationInfo = new ApplicationInfo();
291         applicationInfo.packageName = packageName;
292         final ActivityInfo activityInfo = new ActivityInfo();
293         activityInfo.packageName = packageName;
294         activityInfo.applicationInfo = applicationInfo;
295         final ResolveInfo resolveInfo = new ResolveInfo();
296         resolveInfo.activityInfo = activityInfo;
297         return resolveInfo;
298     }
299 }
300