Merge "Add admin-granted permissions to Enterprise Privacy Settings page"

This commit is contained in:
Bartosz Fabianowski
2017-01-18 18:10:34 +00:00
committed by Android (Google) Code Review
27 changed files with 1026 additions and 83 deletions

View File

@@ -0,0 +1,218 @@
/*
* 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.Context;
import android.content.pm.IPackageManager;
import android.content.pm.PackageManager;
import android.content.pm.UserInfo;
import android.os.Build;
import android.os.UserHandle;
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.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 InstalledAppCounter}.
*/
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public final class AppWithAdminGrantedPermissionsCounterTest {
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 Context mContext;
@Mock private PackageManagerWrapper mPackageManager;
@Mock private IPackageManager mPackageManagerService;
@Mock private DevicePolicyManagerWrapper mDevicePolicyManager;
private List<UserInfo> mUsersToCount;
private int mAppCount = -1;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testCountInstalledAppsAcrossAllUsers() throws Exception {
// There are two users.
mUsersToCount = 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);
// Count the number of all apps installed that were granted on or more permissions by the
// admin. Wait for the background task to finish.
(new AppWithAdminGrantedPermissionsCounterTestable(PERMISSIONS)).execute();
ShadowApplication.runBackgroundTasks();
assertThat(mAppCount).isEqualTo(3);
// Verify that installed packages were retrieved for the users returned by
// InstalledAppCounterTestable.getUsersToCount() 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 AppWithAdminGrantedPermissionsCounterTestable extends
AppWithAdminGrantedPermissionsCounter {
public AppWithAdminGrantedPermissionsCounterTestable(String[] permissions) {
super(mContext, permissions, mPackageManager, mPackageManagerService,
mDevicePolicyManager);
}
@Override
protected void onCountComplete(int num) {
mAppCount = num;
}
@Override
protected List<UserInfo> getUsersToCount() {
return mUsersToCount;
}
}
}

View File

@@ -16,14 +16,18 @@
package com.android.settings.applications;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.content.pm.IPackageManager;
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 com.android.settings.testutils.ApplicationTestUtils;
import com.android.settings.testutils.shadow.ShadowUserManager;
import org.junit.Before;
@@ -50,15 +54,25 @@ public final class ApplicationFeatureProviderImplTest {
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 = MANAGED_PROFILE_ID * PER_USER_UID_RANGE + 1;
private final String APP_1 = "app1";
private final String APP_2 = "app2";
private final String PERMISSION = "some.permission";
private @Mock UserManager mUserManager;
private @Mock Context mContext;
private @Mock PackageManagerWrapper mPackageManager;
@Mock private IPackageManager mPackageManagerService;
@Mock private DevicePolicyManagerWrapper mDevicePolicyManager;
private ApplicationFeatureProvider mProvider;
private int mAppCount = -1;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
@@ -66,14 +80,66 @@ public final class ApplicationFeatureProviderImplTest {
when(mContext.getApplicationContext()).thenReturn(mContext);
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
mProvider = new ApplicationFeatureProviderImpl(mContext, mPackageManager);
mProvider = new ApplicationFeatureProviderImpl(mContext, mPackageManager,
mPackageManagerService, mDevicePolicyManager);
}
@Test
public void testCalculateNumberOfInstalledApps() {
final Integer[] numberOfInstalledApps = new Integer[1];
numberOfInstalledApps[0] = null;
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);
// Count all installed apps.
mAppCount = -1;
mProvider.calculateNumberOfInstalledApps(ApplicationFeatureProvider.IGNORE_INSTALL_REASON,
(num) -> {
mAppCount = num;
});
ShadowApplication.runBackgroundTasks();
assertThat(mAppCount).isEqualTo(2);
// Count apps with specific install reason only.
mAppCount = -1;
mProvider.calculateNumberOfInstalledApps(PackageManager.INSTALL_REASON_POLICY,
(num) -> {
mAppCount = num;
});
ShadowApplication.runBackgroundTasks();
assertThat(mAppCount).isEqualTo(1);
}
@Test
public void testCalculateNumberOfAppsWithAdminGrantedPermissions() 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);
mAppCount = -1;
mProvider.calculateNumberOfAppsWithAdminGrantedPermissions(new String[] {PERMISSION},
(num) -> {
mAppCount = num;
});
ShadowApplication.runBackgroundTasks();
assertThat(mAppCount).isEqualTo(2);
}
private void setUpUsersAndInstalledApps() {
when(mUserManager.getUsers(true)).thenReturn(Arrays.asList(
new UserInfo(MAIN_USER_ID, "main", UserInfo.FLAG_ADMIN),
new UserInfo(MANAGED_PROFILE_ID, "managed profile", 0)));
@@ -82,32 +148,12 @@ public final class ApplicationFeatureProviderImplTest {
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS
| PackageManager.MATCH_ANY_USER,
MAIN_USER_ID)).thenReturn(Arrays.asList(
ApplicationTestUtils.buildInfo(MAIN_USER_ID, APP_1, 0 /* flags */)));
when(mPackageManager.getInstallReason(APP_1, new UserHandle(MAIN_USER_ID)))
.thenReturn(PackageManager.INSTALL_REASON_UNKNOWN);
ApplicationTestUtils.buildInfo(APP_1_UID, APP_1, 0 /* flags */,
Build.VERSION_CODES.M)));
when(mPackageManager.getInstalledApplicationsAsUser(PackageManager.GET_DISABLED_COMPONENTS
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS,
MANAGED_PROFILE_ID)).thenReturn(Arrays.asList(
ApplicationTestUtils.buildInfo(MANAGED_PROFILE_ID, APP_2, 0 /* flags */)));
when(mPackageManager.getInstallReason(APP_2, new UserHandle(MANAGED_PROFILE_ID)))
.thenReturn(PackageManager.INSTALL_REASON_POLICY);
// Count all installed apps.
mProvider.calculateNumberOfInstalledApps(ApplicationFeatureProvider.IGNORE_INSTALL_REASON,
(num) -> {
numberOfInstalledApps[0] = num;
});
ShadowApplication.runBackgroundTasks();
assertThat(numberOfInstalledApps[0]).isEqualTo(2);
// Count apps with specific install reason only.
numberOfInstalledApps[0] = null;
mProvider.calculateNumberOfInstalledApps(PackageManager.INSTALL_REASON_POLICY,
(num) -> {
numberOfInstalledApps[0] = num;
});
ShadowApplication.runBackgroundTasks();
assertThat(numberOfInstalledApps[0]).isEqualTo(1);
ApplicationTestUtils.buildInfo(APP_2_UID, APP_2, 0 /* flags */,
Build.VERSION_CODES.LOLLIPOP)));
}
}

View File

@@ -71,6 +71,10 @@ public final class InstalledAppCounterTest {
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 Context mContext;
@Mock private PackageManagerWrapper mPackageManager;
@@ -109,10 +113,14 @@ public final class InstalledAppCounterTest {
| PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS
| PackageManager.MATCH_ANY_USER,
MAIN_USER_ID)).thenReturn(Arrays.asList(
buildInfo(MAIN_USER_ID, APP_1, ApplicationInfo.FLAG_UPDATED_SYSTEM_APP),
buildInfo(MAIN_USER_ID, APP_2, 0 /* flags */),
buildInfo(MAIN_USER_ID, APP_3, ApplicationInfo.FLAG_SYSTEM),
buildInfo(MAIN_USER_ID, APP_4, ApplicationInfo.FLAG_SYSTEM)));
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 */);
@@ -129,14 +137,16 @@ public final class InstalledAppCounterTest {
when(mPackageManager.getInstallReason(APP_4, mainUser))
.thenReturn(PackageManager.INSTALL_REASON_POLICY);
// The second user has four apps installed:
// 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_ID, APP_5, 0 /* flags */),
buildInfo(MANAGED_PROFILE_ID, APP_6, ApplicationInfo.FLAG_SYSTEM)));
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.
@@ -165,7 +175,7 @@ public final class InstalledAppCounterTest {
// Count once more, considering apps installed by enterprise policy only. Wait for the
// background task to finish.
mInstalledAppCount = 0;
mInstalledAppCount = -1;
(new InstalledAppCounterTestable(PackageManager.INSTALL_REASON_POLICY)).execute();
ShadowApplication.runBackgroundTasks();

View File

@@ -0,0 +1,46 @@
/*
* 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.Manifest;
import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
/**
* Tests for {@link AdminGrantedCameraPermissionPreferenceController}.
*/
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public final class AdminGrantedCameraPermissionPreferenceControllerTest extends
AdminGrantedPermissionsPreferenceControllerTestBase {
public AdminGrantedCameraPermissionPreferenceControllerTest() {
super("enterprise_privacy_number_camera_access_packages",
new String[] {Manifest.permission.CAMERA},
R.plurals.enterprise_privacy_number_camera_access_packages);
}
@Override
public void setUp() {
super.setUp();
mController = new AdminGrantedCameraPermissionPreferenceController(mContext);
}
}

View File

@@ -0,0 +1,47 @@
/*
* 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.Manifest;
import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
/**
* Tests for {@link AdminGrantedLocationPermissionsPreferenceController}.
*/
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public final class AdminGrantedLocationPermissionsPreferenceControllerTest extends
AdminGrantedPermissionsPreferenceControllerTestBase {
public AdminGrantedLocationPermissionsPreferenceControllerTest() {
super("enterprise_privacy_number_location_access_packages",
new String[] {Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION},
R.plurals.enterprise_privacy_number_location_access_packages);
}
@Override
public void setUp() {
super.setUp();
mController = new AdminGrantedLocationPermissionsPreferenceController(mContext);
}
}

View File

@@ -0,0 +1,46 @@
/*
* 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.Manifest;
import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
/**
* Tests for {@link AdminGrantedMicrophonePermissionPreferenceController}.
*/
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public final class AdminGrantedMicrophonePermissionPreferenceControllerTest extends
AdminGrantedPermissionsPreferenceControllerTestBase {
public AdminGrantedMicrophonePermissionPreferenceControllerTest() {
super("enterprise_privacy_number_microphone_access_packages",
new String[] {Manifest.permission.RECORD_AUDIO},
R.plurals.enterprise_privacy_number_microphone_access_packages);
}
@Override
public void setUp() {
super.setUp();
mController = new AdminGrantedMicrophonePermissionPreferenceController(mContext);
}
}

View File

@@ -0,0 +1,55 @@
/*
* 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 com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
/**
* Tests for {@link AdminGrantedPermissionsPreferenceControllerBase}.
*/
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public final class AdminGrantedPermissionsPreferenceControllerBaseTest extends
AdminGrantedPermissionsPreferenceControllerTestBase {
public AdminGrantedPermissionsPreferenceControllerBaseTest() {
super(null, new String[] {"some.permission"}, 123 /* resourceStringId */);
}
@Override
public void setUp() {
super.setUp();
mController = new AdminGrantedPermissionsPreferenceControllerBaseTestable();
}
private class AdminGrantedPermissionsPreferenceControllerBaseTestable extends
AdminGrantedPermissionsPreferenceControllerBase {
AdminGrantedPermissionsPreferenceControllerBaseTestable() {
super(AdminGrantedPermissionsPreferenceControllerBaseTest.this.mContext, mPermissions,
mStringResourceId);
}
@Override
public String getPreferenceKey() {
return null;
}
}
}

View File

@@ -0,0 +1,112 @@
/*
* 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.res.Resources;
import android.support.v7.preference.Preference;
import com.android.settings.applications.ApplicationFeatureProvider;
import com.android.settings.testutils.FakeFeatureFactory;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Answers;
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.Mockito.doAnswer;
import static org.mockito.Mockito.eq;
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 int mStringResourceId;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
protected Context mContext;
private FakeFeatureFactory mFeatureFactory;
protected AdminGrantedPermissionsPreferenceControllerBase mController;
public AdminGrantedPermissionsPreferenceControllerTestBase(String key, String[] permissions,
int stringResourceId) {
mKey = key;
mPermissions = permissions;
mStringResourceId = stringResourceId;
}
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
FakeFeatureFactory.setupForTest(mContext);
mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
}
private void setNumberOfPackagesWithAdminGrantedPermissions(int number) {
doAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) {
((ApplicationFeatureProvider.NumberOfAppsCallback)
invocation.getArguments()[1]).onNumberOfAppsResult(number);
return null;
}}).when(mFeatureFactory.applicationFeatureProvider)
.calculateNumberOfAppsWithAdminGrantedPermissions(eq(mPermissions),
anyObject());
}
@Test
public void testUpdateState() {
final Preference preference = new Preference(mContext, null, 0, 0);
preference.setVisible(true);
setNumberOfPackagesWithAdminGrantedPermissions(20);
when(mContext.getResources().getQuantityString(mStringResourceId, 20, 20))
.thenReturn("20 packages");
mController.updateState(preference);
assertThat(preference.getTitle()).isEqualTo("20 packages");
assertThat(preference.isVisible()).isTrue();
setNumberOfPackagesWithAdminGrantedPermissions(0);
mController.updateState(preference);
assertThat(preference.isVisible()).isFalse();
}
@Test
public void testIsAvailable() {
assertThat(mController.isAvailable()).isTrue();
}
@Test
public void testHandlePreferenceTreeClick() {
assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0)))
.isFalse();
}
@Test
public void testGetPreferenceKey() {
assertThat(mController.getPreferenceKey()).isEqualTo(mKey);
}
}

View File

@@ -68,8 +68,8 @@ public final class EnterpriseInstalledPackagesPreferenceControllerTest {
private void setNumberOfEnterpriseInstalledPackages(int number) {
doAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) {
((ApplicationFeatureProvider.NumberOfInstalledAppsCallback)
invocation.getArguments()[1]).onNumberOfInstalledAppsResult(number);
((ApplicationFeatureProvider.NumberOfAppsCallback)
invocation.getArguments()[1]).onNumberOfAppsResult(number);
return null;
}}).when(mFeatureFactory.applicationFeatureProvider)
.calculateNumberOfInstalledApps(eq(PackageManager.INSTALL_REASON_POLICY),

View File

@@ -73,17 +73,23 @@ public final class EnterprisePrivacySettingsTest {
final List<PreferenceController> controllers = mSettings.getPreferenceControllers(
ShadowApplication.getInstance().getApplicationContext());
assertThat(controllers).isNotNull();
assertThat(controllers.size()).isEqualTo(8);
assertThat(controllers.size()).isEqualTo(11);
assertThat(controllers.get(0)).isInstanceOf(InstalledPackagesPreferenceController.class);
assertThat(controllers.get(1)).isInstanceOf(NetworkLogsPreferenceController.class);
assertThat(controllers.get(2)).isInstanceOf(BugReportsPreferenceController.class);
assertThat(controllers.get(3)).isInstanceOf(SecurityLogsPreferenceController.class);
assertThat(controllers.get(4)).isInstanceOf(
AlwaysOnVpnPrimaryUserPreferenceController.class);
assertThat(controllers.get(5)).isInstanceOf(
AlwaysOnVpnManagedProfilePreferenceController.class);
assertThat(controllers.get(6)).isInstanceOf(GlobalHttpProxyPreferenceController.class);
assertThat(controllers.get(7)).isInstanceOf(
EnterpriseInstalledPackagesPreferenceController.class);
assertThat(controllers.get(5)).isInstanceOf(
AdminGrantedLocationPermissionsPreferenceController.class);
assertThat(controllers.get(6)).isInstanceOf(
AdminGrantedMicrophonePermissionPreferenceController.class);
assertThat(controllers.get(7)).isInstanceOf(
AdminGrantedCameraPermissionPreferenceController.class);
assertThat(controllers.get(8)).isInstanceOf(
AlwaysOnVpnPrimaryUserPreferenceController.class);
assertThat(controllers.get(9)).isInstanceOf(
AlwaysOnVpnManagedProfilePreferenceController.class);
assertThat(controllers.get(10)).isInstanceOf(GlobalHttpProxyPreferenceController.class);
}
}

View File

@@ -69,8 +69,8 @@ public final class InstalledPackagesPreferenceControllerTest {
final Preference preference = new Preference(mContext, null, 0, 0);
doAnswer(new Answer() {
public Object answer(InvocationOnMock invocation) {
((ApplicationFeatureProvider.NumberOfInstalledAppsCallback)
invocation.getArguments()[1]).onNumberOfInstalledAppsResult(20);
((ApplicationFeatureProvider.NumberOfAppsCallback)
invocation.getArguments()[1]).onNumberOfAppsResult(20);
return null;
}}).when(mFeatureFactory.applicationFeatureProvider)
.calculateNumberOfInstalledApps(

View File

@@ -64,11 +64,16 @@ public class InstalledAppResultLoaderTest {
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
when(mPackageManagerWrapper.getInstalledApplicationsAsUser(anyInt(), anyInt()))
.thenReturn(Arrays.asList(
ApplicationTestUtils.buildInfo(0 /* uid */, "app1", FLAG_SYSTEM),
ApplicationTestUtils.buildInfo(0 /* uid */, "app2", FLAG_SYSTEM),
ApplicationTestUtils.buildInfo(0 /* uid */, "app3", FLAG_SYSTEM),
ApplicationTestUtils.buildInfo(0 /* uid */, "app4", 0 /* flags */),
ApplicationTestUtils.buildInfo(0 /* uid */, "app", 0 /* flags */)));
ApplicationTestUtils.buildInfo(0 /* uid */, "app1", FLAG_SYSTEM,
0 /* targetSdkVersion */),
ApplicationTestUtils.buildInfo(0 /* uid */, "app2", FLAG_SYSTEM,
0 /* targetSdkVersion */),
ApplicationTestUtils.buildInfo(0 /* uid */, "app3", FLAG_SYSTEM,
0 /* targetSdkVersion */),
ApplicationTestUtils.buildInfo(0 /* uid */, "app4", 0 /* flags */,
0 /* targetSdkVersion */),
ApplicationTestUtils.buildInfo(0 /* uid */, "app", 0 /* flags */,
0 /* targetSdkVersion */)));
}
@Test

View File

@@ -15,7 +15,6 @@
package com.android.settings.testutils;
import android.content.pm.ApplicationInfo;
import android.os.UserHandle;
/**
* Helper for mocking installed applications.
@@ -25,19 +24,21 @@ public class ApplicationTestUtils {
* Create and populate an {@link android.content.pm.ApplicationInfo} object that describes an
* installed app.
*
* @param userId The user id that this app is installed for. Typical values are 0 for the
* system user and 10, 11, 12... for secondary users.
* @param uid The app's uid
* @param packageName The app's package name.
* @param flags Flags describing the app. See {@link android.content.pm.ApplicationInfo#flags}
* for possible values.
* @param targetSdkVersion The app's target SDK version
*
* @see android.content.pm.ApplicationInfo
*/
public static ApplicationInfo buildInfo(int userId, String packageName, int flags) {
public static ApplicationInfo buildInfo(int uid, String packageName, int flags,
int targetSdkVersion) {
final ApplicationInfo info = new ApplicationInfo();
info.uid = UserHandle.getUid(userId, 1);
info.uid = uid;
info.packageName = packageName;
info.flags = flags;
info.targetSdkVersion = targetSdkVersion;
return info;
}
}