DO Disclosure: add UI that lists apps that were managed by owner:

- had permissions granted by admin
- were installed by owner via policy

Bug: 32692748
Test: m RunSettingsRoboTests
Change-Id: I365e2f8f351671e68f83cceb7c0ca241d7a5a588
This commit is contained in:
Denis Kuznetsov
2017-04-12 15:15:53 +02:00
parent 10ad029318
commit 60b2960cbb
22 changed files with 1544 additions and 186 deletions

View File

@@ -18,15 +18,18 @@ package com.android.settings.applications;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.UserInfo;
import android.os.Build;
import android.os.RemoteException;
import android.os.UserHandle;
import android.os.UserManager;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.enterprise.DevicePolicyManagerWrapper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -83,102 +86,31 @@ public final class AppWithAdminGrantedPermissionsCounterTest {
@Mock private DevicePolicyManagerWrapper mDevicePolicyManager;
private int mAppCount = -1;
private ApplicationInfo mApp1;
private ApplicationInfo mApp2;
private ApplicationInfo mApp3;
private ApplicationInfo mApp4;
private ApplicationInfo mApp5;
private ApplicationInfo mApp6;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
mApp1 = buildInfo(APP_1_UID, APP_1, 0 /* flags */, Build.VERSION_CODES.M);
mApp2 = buildInfo(APP_2_UID, APP_2, 0 /* flags */, Build.VERSION_CODES.M);
mApp3 = buildInfo(APP_3_UID, APP_3, 0 /* flags */, Build.VERSION_CODES.LOLLIPOP);
mApp4 = buildInfo(APP_4_UID, APP_4, 0 /* flags */, Build.VERSION_CODES.LOLLIPOP);
mApp5 = buildInfo(APP_5_UID, APP_5, 0 /* flags */, Build.VERSION_CODES.LOLLIPOP);
mApp6 = buildInfo(APP_6_UID, APP_6, 0 /* flags */, Build.VERSION_CODES.M);
}
private void verifyCountInstalledApps(boolean async) throws Exception {
// There are two users.
when(mUserManager.getProfiles(UserHandle.myUserId())).thenReturn(Arrays.asList(
new UserInfo(MAIN_USER_ID, "main", UserInfo.FLAG_ADMIN),
new UserInfo(MANAGED_PROFILE_ID, "managed profile", 0)));
// The first user has five apps installed:
// * app1 uses run-time permissions. It has been granted one of the permissions by the
// admin. It should be counted.
// * app2 uses run-time permissions. It has not been granted any of the permissions by the
// admin. It should not be counted.
// * app3 uses install-time permissions. It was installed by the admin and requested one of
// the permissions. It should be counted.
// * app4 uses install-time permissions. It was not installed by the admin but did request
// one of the permissions. It should not be counted.
// * app5 uses install-time permissions. It was installed by the admin but did not request
// any of the permissions. It should not be counted.
when(mPackageManager.getInstalledApplicationsAsUser(PackageManager.GET_DISABLED_COMPONENTS
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS
| PackageManager.MATCH_ANY_USER,
MAIN_USER_ID)).thenReturn(Arrays.asList(
buildInfo(APP_1_UID, APP_1, 0 /* flags */, Build.VERSION_CODES.M),
buildInfo(APP_2_UID, APP_2, 0 /* flags */, Build.VERSION_CODES.M),
buildInfo(APP_3_UID, APP_3, 0 /* flags */, Build.VERSION_CODES.LOLLIPOP),
buildInfo(APP_4_UID, APP_4, 0 /* flags */, Build.VERSION_CODES.LOLLIPOP),
buildInfo(APP_5_UID, APP_5, 0 /* flags */, Build.VERSION_CODES.LOLLIPOP)));
// Grant run-time permissions as appropriate.
when(mDevicePolicyManager.getPermissionGrantState(null, APP_1, PERMISSION_1))
.thenReturn(DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED);
when(mDevicePolicyManager.getPermissionGrantState(null, APP_1, PERMISSION_2))
.thenReturn(DevicePolicyManager.PERMISSION_GRANT_STATE_DENIED);
when(mDevicePolicyManager.getPermissionGrantState(eq(null), eq(APP_2), anyObject()))
.thenReturn(DevicePolicyManager.PERMISSION_GRANT_STATE_DENIED);
when(mDevicePolicyManager.getPermissionGrantState(eq(null), eq(APP_3), anyObject()))
.thenReturn(DevicePolicyManager.PERMISSION_GRANT_STATE_DENIED);
when(mDevicePolicyManager.getPermissionGrantState(eq(null), eq(APP_4), anyObject()))
.thenReturn(DevicePolicyManager.PERMISSION_GRANT_STATE_DENIED);
when(mDevicePolicyManager.getPermissionGrantState(eq(null), eq(APP_5), anyObject()))
.thenReturn(DevicePolicyManager.PERMISSION_GRANT_STATE_DENIED);
// Grant install-time permissions as appropriate.
when(mPackageManagerService.checkUidPermission(anyObject(), eq(APP_1_UID)))
.thenReturn(PackageManager.PERMISSION_DENIED);
when(mPackageManagerService.checkUidPermission(anyObject(), eq(APP_2_UID)))
.thenReturn(PackageManager.PERMISSION_DENIED);
when(mPackageManagerService.checkUidPermission(PERMISSION_1, APP_3_UID))
.thenReturn(PackageManager.PERMISSION_DENIED);
when(mPackageManagerService.checkUidPermission(PERMISSION_2, APP_3_UID))
.thenReturn(PackageManager.PERMISSION_GRANTED);
when(mPackageManagerService.checkUidPermission(PERMISSION_1, APP_4_UID))
.thenReturn(PackageManager.PERMISSION_DENIED);
when(mPackageManagerService.checkUidPermission(PERMISSION_2, APP_4_UID))
.thenReturn(PackageManager.PERMISSION_GRANTED);
when(mPackageManagerService.checkUidPermission(anyObject(), eq(APP_5_UID)))
.thenReturn(PackageManager.PERMISSION_DENIED);
// app3 and app5 were installed by enterprise policy.
final UserHandle mainUser = new UserHandle(MAIN_USER_ID);
when(mPackageManager.getInstallReason(APP_1, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
when(mPackageManager.getInstallReason(APP_2, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
when(mPackageManager.getInstallReason(APP_3, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_POLICY);
when(mPackageManager.getInstallReason(APP_4, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
when(mPackageManager.getInstallReason(APP_5, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_POLICY);
// The second user has one app installed. This app uses run-time permissions. It has been
// granted both permissions by the admin. It should be counted.
when(mPackageManager.getInstalledApplicationsAsUser(PackageManager.GET_DISABLED_COMPONENTS
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
MANAGED_PROFILE_ID)).thenReturn(Arrays.asList(
buildInfo(APP_6_UID, APP_6, 0 /* flags */, Build.VERSION_CODES.M)));
// Grant run-time permissions as appropriate.
when(mDevicePolicyManager.getPermissionGrantState(eq(null), eq(APP_6), anyObject()))
.thenReturn(DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED);
// Grant install-time permissions as appropriate.
when(mPackageManagerService.checkUidPermission(anyObject(), eq(APP_6_UID)))
.thenReturn(PackageManager.PERMISSION_DENIED);
// app6 was not installed by enterprise policy.
final UserHandle managedProfileUser = new UserHandle(MANAGED_PROFILE_ID);
when(mPackageManager.getInstallReason(APP_6, managedProfileUser))
.thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
configureUserManager();
configurePackageManager();
configureRunTimePermissions();
configureInstallTimePermissions();
// Count the number of all apps installed that were granted on or more permissions by the
// admin.
@@ -200,6 +132,28 @@ public final class AppWithAdminGrantedPermissionsCounterTest {
verifyNoMoreInteractions(mPackageManager);
}
@Test
public void testIncludeInCount() throws Exception {
configurePackageManager();
configureRunTimePermissions();
configureInstallTimePermissions();
assertThat(AppWithAdminGrantedPermissionsCounter.includeInCount(PERMISSIONS,
mDevicePolicyManager, mPackageManager, mPackageManagerService, mApp1)).isTrue();
assertThat(AppWithAdminGrantedPermissionsCounter.includeInCount(PERMISSIONS,
mDevicePolicyManager, mPackageManager, mPackageManagerService, mApp2)).isFalse();
assertThat(AppWithAdminGrantedPermissionsCounter.includeInCount(PERMISSIONS,
mDevicePolicyManager, mPackageManager, mPackageManagerService, mApp3)).isTrue();
assertThat(AppWithAdminGrantedPermissionsCounter.includeInCount(PERMISSIONS,
mDevicePolicyManager, mPackageManager, mPackageManagerService, mApp4)).isFalse();
assertThat(AppWithAdminGrantedPermissionsCounter.includeInCount(PERMISSIONS,
mDevicePolicyManager, mPackageManager, mPackageManagerService, mApp5)).isFalse();
}
@Test
public void testCountInstalledAppsSync() throws Exception {
verifyCountInstalledApps(false /* async */);
@@ -210,6 +164,90 @@ public final class AppWithAdminGrantedPermissionsCounterTest {
verifyCountInstalledApps(true /* async */);
}
private void configureInstallTimePermissions() throws RemoteException {
when(mPackageManagerService.checkUidPermission(anyObject(), eq(APP_1_UID)))
.thenReturn(PackageManager.PERMISSION_DENIED);
when(mPackageManagerService.checkUidPermission(anyObject(), eq(APP_2_UID)))
.thenReturn(PackageManager.PERMISSION_DENIED);
when(mPackageManagerService.checkUidPermission(PERMISSION_1, APP_3_UID))
.thenReturn(PackageManager.PERMISSION_DENIED);
when(mPackageManagerService.checkUidPermission(PERMISSION_2, APP_3_UID))
.thenReturn(PackageManager.PERMISSION_GRANTED);
when(mPackageManagerService.checkUidPermission(PERMISSION_1, APP_4_UID))
.thenReturn(PackageManager.PERMISSION_DENIED);
when(mPackageManagerService.checkUidPermission(PERMISSION_2, APP_4_UID))
.thenReturn(PackageManager.PERMISSION_GRANTED);
when(mPackageManagerService.checkUidPermission(anyObject(), eq(APP_5_UID)))
.thenReturn(PackageManager.PERMISSION_DENIED);
when(mPackageManagerService.checkUidPermission(anyObject(), eq(APP_6_UID)))
.thenReturn(PackageManager.PERMISSION_DENIED);
}
private void configureRunTimePermissions() {
when(mDevicePolicyManager.getPermissionGrantState(null, APP_1, PERMISSION_1))
.thenReturn(DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED);
when(mDevicePolicyManager.getPermissionGrantState(null, APP_1, PERMISSION_2))
.thenReturn(DevicePolicyManager.PERMISSION_GRANT_STATE_DENIED);
when(mDevicePolicyManager.getPermissionGrantState(eq(null), eq(APP_2), anyObject()))
.thenReturn(DevicePolicyManager.PERMISSION_GRANT_STATE_DENIED);
when(mDevicePolicyManager.getPermissionGrantState(eq(null), eq(APP_3), anyObject()))
.thenReturn(DevicePolicyManager.PERMISSION_GRANT_STATE_DENIED);
when(mDevicePolicyManager.getPermissionGrantState(eq(null), eq(APP_4), anyObject()))
.thenReturn(DevicePolicyManager.PERMISSION_GRANT_STATE_DENIED);
when(mDevicePolicyManager.getPermissionGrantState(eq(null), eq(APP_5), anyObject()))
.thenReturn(DevicePolicyManager.PERMISSION_GRANT_STATE_DENIED);
when(mDevicePolicyManager.getPermissionGrantState(eq(null), eq(APP_6), anyObject()))
.thenReturn(DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED);
}
private void configurePackageManager() {
// The first user has five apps installed:
// * app1 uses run-time permissions. It has been granted one of the permissions by the
// admin. It should be counted.
// * app2 uses run-time permissions. It has not been granted any of the permissions by the
// admin. It should not be counted.
// * app3 uses install-time permissions. It was installed by the admin and requested one of
// the permissions. It should be counted.
// * app4 uses install-time permissions. It was not installed by the admin but did request
// one of the permissions. It should not be counted.
// * app5 uses install-time permissions. It was installed by the admin but did not request
// any of the permissions. It should not be counted.
when(mPackageManager.getInstalledApplicationsAsUser(PackageManager.GET_DISABLED_COMPONENTS
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS
| PackageManager.MATCH_ANY_USER,
MAIN_USER_ID)).thenReturn(Arrays.asList(mApp1, mApp2, mApp3, mApp4, mApp5));
// The second user has one app installed. This app uses run-time permissions. It has been
// granted both permissions by the admin. It should be counted.
when(mPackageManager.getInstalledApplicationsAsUser(PackageManager.GET_DISABLED_COMPONENTS
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
MANAGED_PROFILE_ID)).thenReturn(Arrays.asList(mApp6));
// app3 and app5 were installed by enterprise policy.
final UserHandle mainUser = new UserHandle(MAIN_USER_ID);
when(mPackageManager.getInstallReason(APP_1, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
when(mPackageManager.getInstallReason(APP_2, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
when(mPackageManager.getInstallReason(APP_3, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_POLICY);
when(mPackageManager.getInstallReason(APP_4, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
when(mPackageManager.getInstallReason(APP_5, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_POLICY);
// app6 was not installed by enterprise policy.
final UserHandle managedProfileUser = new UserHandle(MANAGED_PROFILE_ID);
when(mPackageManager.getInstallReason(APP_6, managedProfileUser))
.thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
}
private void configureUserManager() {
// There are two users.
when(mUserManager.getProfiles(UserHandle.myUserId())).thenReturn(Arrays.asList(
new UserInfo(MAIN_USER_ID, "main", UserInfo.FLAG_ADMIN),
new UserInfo(MANAGED_PROFILE_ID, "managed profile", 0)));
}
private class AppWithAdminGrantedPermissionsCounterTestable extends
AppWithAdminGrantedPermissionsCounter {
public AppWithAdminGrantedPermissionsCounterTestable(String[] permissions) {

View File

@@ -0,0 +1,223 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.settings.applications;
import android.app.admin.DevicePolicyManager;
import android.content.pm.PackageManager;
import android.content.pm.UserInfo;
import android.os.Build;
import android.os.UserHandle;
import android.os.UserManager;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.enterprise.DevicePolicyManagerWrapper;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static com.android.settings.testutils.ApplicationTestUtils.buildInfo;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
/**
* Tests for {@link AppWithAdminGrantedPermissionsLister}.
*/
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public final class AppWithAdminGrantedPermissionsListerTest {
private final String APP_1 = "app1";
private final String APP_2 = "app2";
private final String APP_3 = "app3";
private final String APP_4 = "app4";
private final String APP_5 = "app5";
private final String APP_6 = "app6";
private final int MAIN_USER_ID = 0;
private final int MANAGED_PROFILE_ID = 10;
private final int PER_USER_UID_RANGE = 100000;
private final int APP_1_UID = MAIN_USER_ID * PER_USER_UID_RANGE + 1;
private final int APP_2_UID = MAIN_USER_ID * PER_USER_UID_RANGE + 2;
private final int APP_3_UID = MAIN_USER_ID * PER_USER_UID_RANGE + 3;
private final int APP_4_UID = MAIN_USER_ID * PER_USER_UID_RANGE + 4;
private final int APP_5_UID = MAIN_USER_ID * PER_USER_UID_RANGE + 5;
private final int APP_6_UID = MANAGED_PROFILE_ID * PER_USER_UID_RANGE + 1;
private final String PERMISSION_1 = "some.permission.1";
private final String PERMISSION_2 = "some.permission.2";
private final String[] PERMISSIONS = {PERMISSION_1, PERMISSION_2};
@Mock private UserManager mUserManager;
@Mock private PackageManagerWrapper mPackageManager;
@Mock private IPackageManagerWrapper mPackageManagerService;
@Mock private DevicePolicyManagerWrapper mDevicePolicyManager;
private List<UserAppInfo> mAppList = Collections.emptyList();
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void verifyListInstalledApps() throws Exception {
// There are two users.
when(mUserManager.getProfiles(UserHandle.myUserId())).thenReturn(Arrays.asList(
new UserInfo(MAIN_USER_ID, "main", UserInfo.FLAG_ADMIN),
new UserInfo(MANAGED_PROFILE_ID, "managed profile", 0)));
// The first user has five apps installed:
// * app1 uses run-time permissions. It has been granted one of the permissions by the
// admin. It should be listed.
// * app2 uses run-time permissions. It has not been granted any of the permissions by the
// admin. It should not be listed.
// * app3 uses install-time permissions. It was installed by the admin and requested one of
// the permissions. It should be listed.
// * app4 uses install-time permissions. It was not installed by the admin but did request
// one of the permissions. It should not be listed.
// * app5 uses install-time permissions. It was installed by the admin but did not request
// any of the permissions. It should not be listed.
when(mPackageManager.getInstalledApplicationsAsUser(PackageManager.GET_DISABLED_COMPONENTS
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS
| PackageManager.MATCH_ANY_USER,
MAIN_USER_ID)).thenReturn(Arrays.asList(
buildInfo(APP_1_UID, APP_1, 0 /* flags */, Build.VERSION_CODES.M),
buildInfo(APP_2_UID, APP_2, 0 /* flags */, Build.VERSION_CODES.M),
buildInfo(APP_3_UID, APP_3, 0 /* flags */, Build.VERSION_CODES.LOLLIPOP),
buildInfo(APP_4_UID, APP_4, 0 /* flags */, Build.VERSION_CODES.LOLLIPOP),
buildInfo(APP_5_UID, APP_5, 0 /* flags */, Build.VERSION_CODES.LOLLIPOP)));
// Grant run-time permissions as appropriate.
when(mDevicePolicyManager.getPermissionGrantState(null, APP_1, PERMISSION_1))
.thenReturn(DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED);
when(mDevicePolicyManager.getPermissionGrantState(null, APP_1, PERMISSION_2))
.thenReturn(DevicePolicyManager.PERMISSION_GRANT_STATE_DENIED);
when(mDevicePolicyManager.getPermissionGrantState(eq(null), eq(APP_2), anyObject()))
.thenReturn(DevicePolicyManager.PERMISSION_GRANT_STATE_DENIED);
when(mDevicePolicyManager.getPermissionGrantState(eq(null), eq(APP_3), anyObject()))
.thenReturn(DevicePolicyManager.PERMISSION_GRANT_STATE_DENIED);
when(mDevicePolicyManager.getPermissionGrantState(eq(null), eq(APP_4), anyObject()))
.thenReturn(DevicePolicyManager.PERMISSION_GRANT_STATE_DENIED);
when(mDevicePolicyManager.getPermissionGrantState(eq(null), eq(APP_5), anyObject()))
.thenReturn(DevicePolicyManager.PERMISSION_GRANT_STATE_DENIED);
// Grant install-time permissions as appropriate.
when(mPackageManagerService.checkUidPermission(anyObject(), eq(APP_1_UID)))
.thenReturn(PackageManager.PERMISSION_DENIED);
when(mPackageManagerService.checkUidPermission(anyObject(), eq(APP_2_UID)))
.thenReturn(PackageManager.PERMISSION_DENIED);
when(mPackageManagerService.checkUidPermission(PERMISSION_1, APP_3_UID))
.thenReturn(PackageManager.PERMISSION_DENIED);
when(mPackageManagerService.checkUidPermission(PERMISSION_2, APP_3_UID))
.thenReturn(PackageManager.PERMISSION_GRANTED);
when(mPackageManagerService.checkUidPermission(PERMISSION_1, APP_4_UID))
.thenReturn(PackageManager.PERMISSION_DENIED);
when(mPackageManagerService.checkUidPermission(PERMISSION_2, APP_4_UID))
.thenReturn(PackageManager.PERMISSION_GRANTED);
when(mPackageManagerService.checkUidPermission(anyObject(), eq(APP_5_UID)))
.thenReturn(PackageManager.PERMISSION_DENIED);
// app3 and app5 were installed by enterprise policy.
final UserHandle mainUser = new UserHandle(MAIN_USER_ID);
when(mPackageManager.getInstallReason(APP_1, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
when(mPackageManager.getInstallReason(APP_2, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
when(mPackageManager.getInstallReason(APP_3, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_POLICY);
when(mPackageManager.getInstallReason(APP_4, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
when(mPackageManager.getInstallReason(APP_5, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_POLICY);
// The second user has one app installed. This app uses run-time permissions. It has been
// granted both permissions by the admin. It should be listed.
when(mPackageManager.getInstalledApplicationsAsUser(PackageManager.GET_DISABLED_COMPONENTS
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
MANAGED_PROFILE_ID)).thenReturn(Arrays.asList(
buildInfo(APP_6_UID, APP_6, 0 /* flags */, Build.VERSION_CODES.M)));
// Grant run-time permissions as appropriate.
when(mDevicePolicyManager.getPermissionGrantState(eq(null), eq(APP_6), anyObject()))
.thenReturn(DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED);
// Grant install-time permissions as appropriate.
when(mPackageManagerService.checkUidPermission(anyObject(), eq(APP_6_UID)))
.thenReturn(PackageManager.PERMISSION_DENIED);
// app6 was not installed by enterprise policy.
final UserHandle managedProfileUser = new UserHandle(MANAGED_PROFILE_ID);
when(mPackageManager.getInstallReason(APP_6, managedProfileUser))
.thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
// List all apps installed that were granted one or more permissions by the
// admin.
(new AppWithAdminGrantedPermissionsListerTestable(PERMISSIONS)).execute();
// Wait for the background task to finish.
ShadowApplication.runBackgroundTasks();
assertThat(mAppList.size()).isEqualTo(3);
InstalledAppListerTest.verifyListUniqueness(mAppList);
assertThat(InstalledAppListerTest.checkAppFound(mAppList, APP_1, MAIN_USER_ID)).isTrue();
assertThat(InstalledAppListerTest.checkAppFound(mAppList, APP_2, MAIN_USER_ID)).isFalse();
assertThat(InstalledAppListerTest.checkAppFound(mAppList, APP_3, MAIN_USER_ID)).isTrue();
assertThat(InstalledAppListerTest.checkAppFound(mAppList, APP_4, MAIN_USER_ID)).isFalse();
assertThat(InstalledAppListerTest.checkAppFound(mAppList, APP_5, MAIN_USER_ID)).isFalse();
assertThat(InstalledAppListerTest.checkAppFound(mAppList, APP_6, MANAGED_PROFILE_ID)).
isTrue();
// Verify that installed packages were retrieved the current user and the user's managed
// profile only.
verify(mPackageManager).getInstalledApplicationsAsUser(anyInt(), eq(MAIN_USER_ID));
verify(mPackageManager).getInstalledApplicationsAsUser(anyInt(),
eq(MANAGED_PROFILE_ID));
verify(mPackageManager, atLeast(0)).getInstallReason(anyObject(), anyObject());
verifyNoMoreInteractions(mPackageManager);
}
private class AppWithAdminGrantedPermissionsListerTestable extends
AppWithAdminGrantedPermissionsLister {
public AppWithAdminGrantedPermissionsListerTestable(String[] permissions) {
super(permissions, mPackageManager, mPackageManagerService,
mDevicePolicyManager, mUserManager);
}
@Override
protected void onAppListBuilt(List<UserAppInfo> list) {
mAppList = list;
}
}
}

View File

@@ -33,6 +33,7 @@ import com.android.settings.TestConfig;
import com.android.settings.enterprise.DevicePolicyManagerWrapper;
import com.android.settings.testutils.ApplicationTestUtils;
import com.android.settings.testutils.shadow.ShadowUserManager;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -42,6 +43,7 @@ import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import static com.google.common.truth.Truth.assertThat;
@@ -76,6 +78,7 @@ public final class ApplicationFeatureProviderImplTest {
private ApplicationFeatureProvider mProvider;
private int mAppCount = -1;
private List<UserAppInfo> mAppList = null;
@Before
public void setUp() {
@@ -105,6 +108,22 @@ public final class ApplicationFeatureProviderImplTest {
assertThat(mAppCount).isEqualTo(1);
}
@Test
public void testListPolicyInstalledApps() {
setUpUsersAndInstalledApps();
when(mPackageManager.getInstallReason(APP_1, new UserHandle(MAIN_USER_ID)))
.thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
when(mPackageManager.getInstallReason(APP_2, new UserHandle(MANAGED_PROFILE_ID)))
.thenReturn(PackageManager.INSTALL_REASON_POLICY);
mAppList = null;
mProvider.listPolicyInstalledApps((list) -> mAppList = list);
assertThat(mAppList).isNotNull();
assertThat(mAppList.size()).isEqualTo(1);
assertThat(mAppList.get(0).appInfo.packageName).isEqualTo(APP_2);
}
@Test
public void testCalculateNumberOfInstalledAppsSync() {
verifyCalculateNumberOfPolicyInstalledApps(false /* async */);
@@ -139,7 +158,6 @@ public final class ApplicationFeatureProviderImplTest {
ShadowApplication.runBackgroundTasks();
}
assertThat(mAppCount).isEqualTo(2);
}
@Test
@@ -152,6 +170,34 @@ public final class ApplicationFeatureProviderImplTest {
verifyCalculateNumberOfAppsWithAdminGrantedPermissions(true /* async */);
}
@Test
public void testListAppsWithAdminGrantedPermissions()
throws Exception {
setUpUsersAndInstalledApps();
when(mDevicePolicyManager.getPermissionGrantState(null, APP_1, PERMISSION))
.thenReturn(DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED);
when(mDevicePolicyManager.getPermissionGrantState(null, APP_2, PERMISSION))
.thenReturn(DevicePolicyManager.PERMISSION_GRANT_STATE_DENIED);
when(mPackageManagerService.checkUidPermission(PERMISSION, APP_1_UID))
.thenReturn(PackageManager.PERMISSION_DENIED);
when(mPackageManagerService.checkUidPermission(PERMISSION, APP_2_UID))
.thenReturn(PackageManager.PERMISSION_GRANTED);
when(mPackageManager.getInstallReason(APP_1, new UserHandle(MAIN_USER_ID)))
.thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
when(mPackageManager.getInstallReason(APP_2, new UserHandle(MANAGED_PROFILE_ID)))
.thenReturn(PackageManager.INSTALL_REASON_POLICY);
mAppList = null;
mProvider.listAppsWithAdminGrantedPermissions(new String[] {PERMISSION},
(list) -> mAppList = list);
assertThat(mAppList).isNotNull();
assertThat(mAppList.size()).isEqualTo(2);
assertThat(Arrays.asList(mAppList.get(0).appInfo.packageName,
mAppList.get(1).appInfo.packageName).containsAll(Arrays.asList(APP_1, APP_2)))
.isTrue();
}
@Test
public void testFindPersistentPreferredActivities() throws Exception {
when(mUserManager.getUserProfiles()).thenReturn(Arrays.asList(new UserHandle(MAIN_USER_ID),

View File

@@ -22,12 +22,14 @@ import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.UserInfo;
import android.os.Build;
import android.os.UserHandle;
import android.os.UserManager;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.testutils.shadow.ShadowUserManager;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -75,16 +77,38 @@ public final class InstalledAppCounterTest {
private final int MAIN_USER_APP_UID = MAIN_USER_ID * PER_USER_UID_RANGE;
private final int MANAGED_PROFILE_APP_UID = MANAGED_PROFILE_ID * PER_USER_UID_RANGE;
@Mock private UserManager mUserManager;
@Mock private Context mContext;
@Mock private PackageManagerWrapper mPackageManager;
@Mock
private UserManager mUserManager;
@Mock
private Context mContext;
@Mock
private PackageManagerWrapper mPackageManager;
private int mInstalledAppCount = -1;
private ApplicationInfo mApp1;
private ApplicationInfo mApp2;
private ApplicationInfo mApp3;
private ApplicationInfo mApp4;
private ApplicationInfo mApp5;
private ApplicationInfo mApp6;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
mApp1 = buildInfo(MAIN_USER_APP_UID, APP_1,
ApplicationInfo.FLAG_UPDATED_SYSTEM_APP, 0 /* targetSdkVersion */);
mApp2 = buildInfo(MAIN_USER_APP_UID, APP_2, 0 /* flags */,
0 /* targetSdkVersion */);
mApp3 = buildInfo(MAIN_USER_APP_UID, APP_3, ApplicationInfo.FLAG_SYSTEM,
0 /* targetSdkVersion */);
mApp4 = buildInfo(MAIN_USER_APP_UID, APP_4, ApplicationInfo.FLAG_SYSTEM,
0 /* targetSdkVersion */);
mApp5 = buildInfo(MANAGED_PROFILE_APP_UID, APP_5, 0 /* flags */,
0 /* targetSdkVersion */);
mApp6 = buildInfo(MANAGED_PROFILE_APP_UID, APP_6, ApplicationInfo.FLAG_SYSTEM,
0 /* targetSdkVersion */);
}
private void expectQueryIntentActivities(int userId, String packageName, boolean launchable) {
@@ -93,7 +117,7 @@ public final class InstalledAppCounterTest {
eq(PackageManager.GET_DISABLED_COMPONENTS | PackageManager.MATCH_DIRECT_BOOT_AWARE
| PackageManager.MATCH_DIRECT_BOOT_UNAWARE),
eq(userId))).thenReturn(launchable ? Arrays.asList(new ResolveInfo())
: new ArrayList<ResolveInfo>());
: new ArrayList<ResolveInfo>());
}
private void testCountInstalledAppsAcrossAllUsers(boolean async) {
@@ -101,58 +125,7 @@ public final class InstalledAppCounterTest {
when(mUserManager.getProfiles(UserHandle.myUserId())).thenReturn(Arrays.asList(
new UserInfo(MAIN_USER_ID, "main", UserInfo.FLAG_ADMIN),
new UserInfo(MANAGED_PROFILE_ID, "managed profile", 0)));
// The first user has four apps installed:
// * app1 is an updated system app. It should be counted.
// * app2 is a user-installed app. It should be counted.
// * app3 is a system app that provides a launcher icon. It should be counted.
// * app4 is a system app that provides no launcher icon. It should not be counted.
when(mPackageManager.getInstalledApplicationsAsUser(PackageManager.GET_DISABLED_COMPONENTS
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS
| PackageManager.MATCH_ANY_USER,
MAIN_USER_ID)).thenReturn(Arrays.asList(
buildInfo(MAIN_USER_APP_UID, APP_1,
ApplicationInfo.FLAG_UPDATED_SYSTEM_APP, 0 /* targetSdkVersion */),
buildInfo(MAIN_USER_APP_UID, APP_2, 0 /* flags */,
0 /* targetSdkVersion */),
buildInfo(MAIN_USER_APP_UID, APP_3, ApplicationInfo.FLAG_SYSTEM,
0 /* targetSdkVersion */),
buildInfo(MAIN_USER_APP_UID, APP_4, ApplicationInfo.FLAG_SYSTEM,
0 /* targetSdkVersion */)));
// For system apps, InstalledAppCounter checks whether they handle the default launcher
// intent to decide whether to include them in the count of installed apps or not.
expectQueryIntentActivities(MAIN_USER_ID, APP_3, true /* launchable */);
expectQueryIntentActivities(MAIN_USER_ID, APP_4, false /* launchable */);
// app1, app3 and app4 are installed by enterprise policy.
final UserHandle mainUser = new UserHandle(MAIN_USER_ID);
when(mPackageManager.getInstallReason(APP_1, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_POLICY);
when(mPackageManager.getInstallReason(APP_2, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
when(mPackageManager.getInstallReason(APP_3, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_POLICY);
when(mPackageManager.getInstallReason(APP_4, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_POLICY);
// The second user has two apps installed:
// * app5 is a user-installed app. It should be counted.
// * app6 is a system app that provides a launcher icon. It should be counted.
when(mPackageManager.getInstalledApplicationsAsUser(PackageManager.GET_DISABLED_COMPONENTS
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
MANAGED_PROFILE_ID)).thenReturn(Arrays.asList(
buildInfo(MANAGED_PROFILE_APP_UID, APP_5, 0 /* flags */,
0 /* targetSdkVersion */),
buildInfo(MANAGED_PROFILE_APP_UID, APP_6, ApplicationInfo.FLAG_SYSTEM,
0 /* targetSdkVersion */)));
expectQueryIntentActivities(MANAGED_PROFILE_ID, APP_6, true /* launchable */);
// app5 is installed by enterprise policy.
final UserHandle managedProfileUser = new UserHandle(MANAGED_PROFILE_ID);
when(mPackageManager.getInstallReason(APP_5, managedProfileUser))
.thenReturn(PackageManager.INSTALL_REASON_POLICY);
when(mPackageManager.getInstallReason(APP_6, managedProfileUser))
.thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
configurePackageManager();
// Count the number of all apps installed, irrespective of install reason.
count(InstalledAppCounter.IGNORE_INSTALL_REASON, async);
@@ -172,6 +145,36 @@ public final class InstalledAppCounterTest {
assertThat(mInstalledAppCount).isEqualTo(3);
}
@Test
public void testIncludeInCount() {
configurePackageManager();
assertThat(InstalledAppCounter.includeInCount(InstalledAppCounter.IGNORE_INSTALL_REASON,
mPackageManager, mApp1)).isTrue();
assertThat(InstalledAppCounter.includeInCount(InstalledAppCounter.IGNORE_INSTALL_REASON,
mPackageManager, mApp2)).isTrue();
assertThat(InstalledAppCounter.includeInCount(InstalledAppCounter.IGNORE_INSTALL_REASON,
mPackageManager, mApp3)).isTrue();
assertThat(InstalledAppCounter.includeInCount(InstalledAppCounter.IGNORE_INSTALL_REASON,
mPackageManager, mApp4)).isFalse();
assertThat(InstalledAppCounter.includeInCount(InstalledAppCounter.IGNORE_INSTALL_REASON,
mPackageManager, mApp5)).isTrue();
assertThat(InstalledAppCounter.includeInCount(InstalledAppCounter.IGNORE_INSTALL_REASON,
mPackageManager, mApp6)).isTrue();
assertThat(InstalledAppCounter.includeInCount(PackageManager.INSTALL_REASON_POLICY,
mPackageManager, mApp1)).isTrue();
assertThat(InstalledAppCounter.includeInCount(PackageManager.INSTALL_REASON_POLICY,
mPackageManager, mApp2)).isFalse();
assertThat(InstalledAppCounter.includeInCount(PackageManager.INSTALL_REASON_POLICY,
mPackageManager, mApp3)).isTrue();
assertThat(InstalledAppCounter.includeInCount(PackageManager.INSTALL_REASON_POLICY,
mPackageManager, mApp4)).isFalse();
assertThat(InstalledAppCounter.includeInCount(PackageManager.INSTALL_REASON_POLICY,
mPackageManager, mApp5)).isTrue();
assertThat(InstalledAppCounter.includeInCount(PackageManager.INSTALL_REASON_POLICY,
mPackageManager, mApp6)).isFalse();
}
@Test
public void testCountInstalledAppsAcrossAllUsersSync() {
testCountInstalledAppsAcrossAllUsers(false /* async */);
@@ -194,6 +197,48 @@ public final class InstalledAppCounterTest {
}
}
private void configurePackageManager() {
// The first user has four apps installed:
// * app1 is an updated system app. It should be counted.
// * app2 is a user-installed app. It should be counted.
// * app3 is a system app that provides a launcher icon. It should be counted.
// * app4 is a system app that provides no launcher icon. It should not be counted.
when(mPackageManager.getInstalledApplicationsAsUser(PackageManager.GET_DISABLED_COMPONENTS
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS
| PackageManager.MATCH_ANY_USER,
MAIN_USER_ID)).thenReturn(Arrays.asList(mApp1, mApp2, mApp3, mApp4));
// For system apps, InstalledAppCounter checks whether they handle the default launcher
// intent to decide whether to include them in the count of installed apps or not.
expectQueryIntentActivities(MAIN_USER_ID, APP_3, true /* launchable */);
expectQueryIntentActivities(MAIN_USER_ID, APP_4, false /* launchable */);
// app1, app3 and app4 are installed by enterprise policy.
final UserHandle mainUser = new UserHandle(MAIN_USER_ID);
when(mPackageManager.getInstallReason(APP_1, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_POLICY);
when(mPackageManager.getInstallReason(APP_2, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
when(mPackageManager.getInstallReason(APP_3, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_POLICY);
when(mPackageManager.getInstallReason(APP_4, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_POLICY);
// The second user has two apps installed:
// * app5 is a user-installed app. It should be counted.
// * app6 is a system app that provides a launcher icon. It should be counted.
when(mPackageManager.getInstalledApplicationsAsUser(PackageManager.GET_DISABLED_COMPONENTS
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,MANAGED_PROFILE_ID))
.thenReturn(Arrays.asList(mApp5, mApp6));
expectQueryIntentActivities(MANAGED_PROFILE_ID, APP_6, true /* launchable */);
// app5 is installed by enterprise policy.
final UserHandle managedProfileUser = new UserHandle(MANAGED_PROFILE_ID);
when(mPackageManager.getInstallReason(APP_5, managedProfileUser))
.thenReturn(PackageManager.INSTALL_REASON_POLICY);
when(mPackageManager.getInstallReason(APP_6, managedProfileUser))
.thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
}
private class InstalledAppCounterTestable extends InstalledAppCounter {
public InstalledAppCounterTestable(int installReason) {

View File

@@ -0,0 +1,233 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.settings.applications;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.UserInfo;
import android.os.UserHandle;
import android.os.UserManager;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatcher;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static com.android.settings.testutils.ApplicationTestUtils.buildInfo;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
/**
* Tests for {@link InstalledAppLister}.
*/
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public final class InstalledAppListerTest {
private final String APP_1 = "app1";
private final String APP_2 = "app2";
private final String APP_3 = "app3";
private final String APP_4 = "app4";
private final String APP_5 = "app5";
private final String APP_6 = "app6";
private final int MAIN_USER_ID = 0;
private final int MANAGED_PROFILE_ID = 10;
private final int PER_USER_UID_RANGE = 100000;
private final int MAIN_USER_APP_UID = MAIN_USER_ID * PER_USER_UID_RANGE;
private final int MANAGED_PROFILE_APP_UID = MANAGED_PROFILE_ID * PER_USER_UID_RANGE;
@Mock private UserManager mUserManager;
@Mock private PackageManagerWrapper mPackageManager;
private List<UserAppInfo> mInstalledAppList = Collections.emptyList();
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
private void expectQueryIntentActivities(int userId, String packageName, boolean launchable) {
when(mPackageManager.queryIntentActivitiesAsUser(
argThat(new IsLaunchIntentFor(packageName)),
eq(PackageManager.GET_DISABLED_COMPONENTS | PackageManager.MATCH_DIRECT_BOOT_AWARE
| PackageManager.MATCH_DIRECT_BOOT_UNAWARE),
eq(userId))).thenReturn(launchable ? Arrays.asList(new ResolveInfo())
: new ArrayList<ResolveInfo>());
}
@Test
public void testCountInstalledAppsAcrossAllUsers() {
// There are two users.
when(mUserManager.getProfiles(UserHandle.myUserId())).thenReturn(Arrays.asList(
new UserInfo(MAIN_USER_ID, "main", UserInfo.FLAG_ADMIN),
new UserInfo(MANAGED_PROFILE_ID, "managed profile", 0)));
// The first user has four apps installed:
// * app1 is an updated system app. It should be listed.
// * app2 is a user-installed app. It should be listed.
// * app3 is a system app that provides a launcher icon. It should be listed.
// * app4 is a system app that provides no launcher icon. It should not be listed.
when(mPackageManager.getInstalledApplicationsAsUser(PackageManager.GET_DISABLED_COMPONENTS
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS
| PackageManager.MATCH_ANY_USER,
MAIN_USER_ID)).thenReturn(Arrays.asList(
buildInfo(MAIN_USER_APP_UID, APP_1,
ApplicationInfo.FLAG_UPDATED_SYSTEM_APP, 0 /* targetSdkVersion */),
buildInfo(MAIN_USER_APP_UID, APP_2, 0 /* flags */,
0 /* targetSdkVersion */),
buildInfo(MAIN_USER_APP_UID, APP_3, ApplicationInfo.FLAG_SYSTEM,
0 /* targetSdkVersion */),
buildInfo(MAIN_USER_APP_UID, APP_4, ApplicationInfo.FLAG_SYSTEM,
0 /* targetSdkVersion */)));
// For system apps, InstalledAppLister checks whether they handle the default launcher
// intent to decide whether to include them in the list of installed apps or not.
expectQueryIntentActivities(MAIN_USER_ID, APP_3, true /* launchable */);
expectQueryIntentActivities(MAIN_USER_ID, APP_4, false /* launchable */);
// app1, app3 and app4 are installed by enterprise policy.
final UserHandle mainUser = new UserHandle(MAIN_USER_ID);
when(mPackageManager.getInstallReason(APP_1, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_POLICY);
when(mPackageManager.getInstallReason(APP_2, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
when(mPackageManager.getInstallReason(APP_3, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_POLICY);
when(mPackageManager.getInstallReason(APP_4, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_POLICY);
// The second user has two apps installed:
// * app5 is a user-installed app. It should be listed.
// * app6 is a system app that provides a launcher icon. It should be listed.
when(mPackageManager.getInstalledApplicationsAsUser(PackageManager.GET_DISABLED_COMPONENTS
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
MANAGED_PROFILE_ID)).thenReturn(Arrays.asList(
buildInfo(MANAGED_PROFILE_APP_UID, APP_5, 0 /* flags */,
0 /* targetSdkVersion */),
buildInfo(MANAGED_PROFILE_APP_UID, APP_6, ApplicationInfo.FLAG_SYSTEM,
0 /* targetSdkVersion */)));
expectQueryIntentActivities(MANAGED_PROFILE_ID, APP_6, true /* launchable */);
// app5 is installed by enterprise policy.
final UserHandle managedProfileUser = new UserHandle(MANAGED_PROFILE_ID);
when(mPackageManager.getInstallReason(APP_5, managedProfileUser))
.thenReturn(PackageManager.INSTALL_REASON_POLICY);
when(mPackageManager.getInstallReason(APP_6, managedProfileUser))
.thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
// List apps, considering apps installed by enterprise policy only.
mInstalledAppList = Collections.emptyList();
final InstalledAppListerTestable counter = new InstalledAppListerTestable();
counter.execute();
// Wait for the background task to finish.
ShadowApplication.runBackgroundTasks();
assertThat(mInstalledAppList.size()).isEqualTo(3);
assertThat(checkAppFound(mInstalledAppList, APP_1, MAIN_USER_ID)).isTrue();
assertThat(checkAppFound(mInstalledAppList, APP_2, MAIN_USER_ID)).isFalse();
assertThat(checkAppFound(mInstalledAppList, APP_3, MAIN_USER_ID)).isTrue();
assertThat(checkAppFound(mInstalledAppList, APP_4, MAIN_USER_ID)).isFalse();
assertThat(checkAppFound(mInstalledAppList, APP_5, MANAGED_PROFILE_ID)).isTrue();
assertThat(checkAppFound(mInstalledAppList, APP_6, MANAGED_PROFILE_ID)).isFalse();
// Verify that installed packages were retrieved for the current user and the user's
// managed profile.
verify(mPackageManager).getInstalledApplicationsAsUser(anyInt(), eq(MAIN_USER_ID));
verify(mPackageManager).getInstalledApplicationsAsUser(anyInt(),
eq(MANAGED_PROFILE_ID));
verify(mPackageManager, atLeast(0)).queryIntentActivitiesAsUser(anyObject(), anyInt(),
anyInt());
}
public static boolean checkAppFound(List<UserAppInfo> mInstalledAppList, String appId,
int userId) {
for (UserAppInfo info : mInstalledAppList) {
if (appId.equals(info.appInfo.packageName) && (info.userInfo.id == userId)) {
return true;
}
}
return false;
}
public static void verifyListUniqueness(List<UserAppInfo> list) {
assertThat((new HashSet<>(list)).size()).isEqualTo(list.size());
}
private class InstalledAppListerTestable extends InstalledAppLister {
public InstalledAppListerTestable() {
super(mPackageManager, mUserManager);
}
@Override
protected void onAppListBuilt(List<UserAppInfo> list) {
mInstalledAppList = list;
}
}
private static class IsLaunchIntentFor extends ArgumentMatcher<Intent> {
private final String mPackageName;
IsLaunchIntentFor(String packageName) {
mPackageName = packageName;
}
@Override
public boolean matches(Object i) {
final Intent intent = (Intent) i;
if (intent == null) {
return false;
}
if (!Intent.ACTION_MAIN.equals(intent.getAction())) {
return false;
}
final Set<String> categories = intent.getCategories();
if (categories == null || categories.size() != 1 ||
!categories.contains(Intent.CATEGORY_LAUNCHER)) {
return false;
}
if (!mPackageName.equals(intent.getPackage())) {
return false;
}
return true;
}
}
}

View File

@@ -17,8 +17,6 @@
package com.android.settings.enterprise;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.support.v7.preference.Preference;
import com.android.settings.R;
@@ -28,24 +26,21 @@ import com.android.settings.testutils.FakeFeatureFactory;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Answers;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.anyObject;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* Common base for testing subclasses of {@link AdminGrantedPermissionsPreferenceControllerBase}.
*/
public abstract class AdminGrantedPermissionsPreferenceControllerTestBase {
protected final String mKey;
protected final String[] mPermissions;
protected final String mPermissionGroup;
@@ -123,19 +118,8 @@ public abstract class AdminGrantedPermissionsPreferenceControllerTestBase {
@Test
public void testHandlePreferenceTreeClick() {
final Preference preference = new Preference(mContext, null, 0, 0);
preference.setKey(mKey);
assertThat(mController.handlePreferenceTreeClick(preference)).isTrue();
final ArgumentCaptor<Intent> argumentCaptor = ArgumentCaptor.forClass(Intent.class);
verify(mContext).startActivity(argumentCaptor.capture());
final Intent intent = argumentCaptor.getValue();
assertThat(intent.getAction()).isEqualTo(Intent.ACTION_MANAGE_PERMISSION_APPS);
assertThat(intent.getStringExtra(Intent.EXTRA_PERMISSION_NAME)).
isEqualTo(mPermissionGroup);
assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0)))
.isFalse();
}
@Test

View File

@@ -0,0 +1,131 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.settings.enterprise;
import android.content.Context;
import android.content.pm.UserInfo;
import android.support.v7.preference.PreferenceManager;
import android.support.v7.preference.PreferenceScreen;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.applications.ApplicationFeatureProvider;
import com.android.settings.applications.UserAppInfo;
import com.android.settings.core.PreferenceController;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import java.util.ArrayList;
import java.util.List;
import static com.android.settings.testutils.ApplicationTestUtils.buildInfo;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class ApplicationListFragmentTest {
private static final int USER_ID = 0;
private static final int USER_APP_UID = 0;
private static final String APP = "APP";
@Mock(answer = RETURNS_DEEP_STUBS)
private PreferenceScreen mScreen;
@Mock(answer = RETURNS_DEEP_STUBS)
private PreferenceManager mPreferenceManager;
private ApplicationListFragment mFragment;
private Context mContext;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = ShadowApplication.getInstance().getApplicationContext();
when(mPreferenceManager.getContext()).thenReturn(mContext);
mFragment = new ApplicationListFragmentTestable(mPreferenceManager, mScreen);
}
@Test
public void getMetricsCategory() {
assertThat(mFragment.getMetricsCategory())
.isEqualTo(MetricsEvent.ENTERPRISE_PRIVACY_SETTINGS);
}
@Test
public void getLogTag() {
assertThat(mFragment.getLogTag())
.isEqualTo("EnterprisePrivacySettings");
}
@Test
public void getScreenResource() {
assertThat(mFragment.getPreferenceScreenResId())
.isEqualTo(R.xml.app_list_disclosure_settings);
}
@Test
public void getPreferenceControllers() {
final List<PreferenceController> controllers = mFragment.getPreferenceControllers(mContext);
assertThat(controllers).isNotNull();
assertThat(controllers.size()).isEqualTo(1);
int position = 0;
assertThat(controllers.get(position++)).isInstanceOf(
ApplicationListPreferenceController.class);
}
private static class ApplicationListFragmentTestable extends ApplicationListFragment {
private final PreferenceManager mPreferenceManager;
private final PreferenceScreen mPreferenceScreen;
public ApplicationListFragmentTestable(PreferenceManager preferenceManager,
PreferenceScreen screen) {
this.mPreferenceManager = preferenceManager;
this.mPreferenceScreen = screen;
}
@Override
public void buildApplicationList(Context context,
ApplicationFeatureProvider.ListOfAppsCallback callback) {
final List<UserAppInfo> apps = new ArrayList<>();
final UserInfo user = new UserInfo(USER_ID, "main", UserInfo.FLAG_ADMIN);
apps.add(new UserAppInfo(user, buildInfo(USER_APP_UID, APP, 0, 0)));
callback.onListOfAppsResult(apps);
}
@Override
public PreferenceManager getPreferenceManager() {
return mPreferenceManager;
}
@Override
public PreferenceScreen getPreferenceScreen() {
return mPreferenceScreen;
}
}
}

View File

@@ -0,0 +1,132 @@
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.settings.enterprise;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.UserInfo;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceScreen;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.applications.ApplicationFeatureProvider;
import com.android.settings.applications.UserAppInfo;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static com.android.settings.testutils.ApplicationTestUtils.buildInfo;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Answers.RETURNS_DEEP_STUBS;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class ApplicationListPreferenceControllerTest {
private static final int MAIN_USER_ID = 0;
private static final int MANAGED_PROFILE_ID = 10;
private static final int PER_USER_UID_RANGE = 100000;
private static final int MAIN_USER_APP_UID = MAIN_USER_ID * PER_USER_UID_RANGE;
private static final int MANAGED_PROFILE_APP_UID = MANAGED_PROFILE_ID * PER_USER_UID_RANGE;
private static final String APP_1 = "APP_1";
private static final String APP_2 = "APP_2";
private static final String APP_3 = "APP_3";
@Mock(answer = RETURNS_DEEP_STUBS)
private PreferenceScreen mScreen;
@Mock(answer = RETURNS_DEEP_STUBS)
private PackageManager mPackageManager;
@Mock(answer = RETURNS_DEEP_STUBS)
private SettingsPreferenceFragment mFragment;
private Context mContext;
private ApplicationListPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
final ShadowApplication shadowContext = ShadowApplication.getInstance();
mContext = shadowContext.getApplicationContext();
when(mFragment.getPreferenceScreen()).thenReturn(mScreen);
when(mFragment.getPreferenceManager().getContext()).thenReturn(mContext);
when(mPackageManager.getText(eq(APP_1), anyInt(), any())).thenReturn(APP_1);
when(mPackageManager.getText(eq(APP_2), anyInt(), any())).thenReturn(APP_2);
when(mPackageManager.getText(eq(APP_3), anyInt(), any())).thenReturn(APP_3);
mController = new ApplicationListPreferenceController(mContext, new ThreeAppsBuilder(),
mPackageManager, mFragment);
}
@Test
public void checkNumberAndTitlesOfApps() {
ArgumentCaptor<Preference> apps = ArgumentCaptor.forClass(Preference.class);
verify(mScreen, times(3)).addPreference(apps.capture());
final Set<String> expectedPackages = new HashSet<>(Arrays.asList(APP_1, APP_2, APP_3));
final Set<String> packages = new HashSet<>();
for (Preference p : apps.getAllValues()) {
packages.add(p.getTitle().toString());
}
assertThat(packages).isEqualTo(expectedPackages);
}
@Test
public void isAvailable() {
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void getPreferenceKey() {
assertThat(mController.getPreferenceKey()).isNull();
}
private static class ThreeAppsBuilder
implements ApplicationListPreferenceController.ApplicationListBuilder {
@Override
public void buildApplicationList(Context context,
ApplicationFeatureProvider.ListOfAppsCallback callback) {
final List<UserAppInfo> apps = new ArrayList<>();
final UserInfo user = new UserInfo(MAIN_USER_ID, "main", UserInfo.FLAG_ADMIN);
apps.add(new UserAppInfo(user, buildInfo(MAIN_USER_APP_UID, APP_1, 0, 0)));
apps.add(new UserAppInfo(user, buildInfo(MAIN_USER_APP_UID, APP_2, 0, 0)));
apps.add(new UserAppInfo(user, buildInfo(MANAGED_PROFILE_APP_UID, APP_3, 0, 0)));
callback.onListOfAppsResult(apps);
}
}
}