Merge "Add admin-set default apps to Enterprise Privacy Settings page"

This commit is contained in:
TreeHugger Robot
2017-01-25 03:17:55 +00:00
committed by Android (Google) Code Review
15 changed files with 520 additions and 20 deletions

View File

@@ -18,7 +18,6 @@ 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;
@@ -79,7 +78,7 @@ public final class AppWithAdminGrantedPermissionsCounterTest {
@Mock private Context mContext;
@Mock private PackageManagerWrapper mPackageManager;
@Mock private IPackageManager mPackageManagerService;
@Mock private IPackageManagerWrapper mPackageManagerService;
@Mock private DevicePolicyManagerWrapper mDevicePolicyManager;
private List<UserInfo> mUsersToCount;

View File

@@ -18,12 +18,15 @@ package com.android.settings.applications;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.content.pm.IPackageManager;
import android.content.Intent;
import android.content.pm.ActivityInfo;
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 android.util.ArraySet;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
@@ -39,10 +42,11 @@ import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowApplication;
import java.util.Arrays;
import java.util.Set;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import org.robolectric.shadows.ShadowApplication;
/**
* Tests for {@link ApplicationFeatureProviderImpl}.
*/
@@ -66,7 +70,7 @@ public final class ApplicationFeatureProviderImplTest {
private @Mock UserManager mUserManager;
private @Mock Context mContext;
private @Mock PackageManagerWrapper mPackageManager;
@Mock private IPackageManager mPackageManagerService;
@Mock private IPackageManagerWrapper mPackageManagerService;
@Mock private DevicePolicyManagerWrapper mDevicePolicyManager;
private ApplicationFeatureProvider mProvider;
@@ -139,6 +143,43 @@ public final class ApplicationFeatureProviderImplTest {
}
@Test
public void testFindPersistentPreferredActivities() throws Exception {
when(mUserManager.getUserProfiles()).thenReturn(Arrays.asList(new UserHandle(MAIN_USER_ID),
new UserHandle(MANAGED_PROFILE_ID)));
final Intent viewIntent = new Intent(Intent.ACTION_VIEW);
final Intent editIntent = new Intent(Intent.ACTION_EDIT);
final Intent sendIntent = new Intent(Intent.ACTION_SEND);
final ResolveInfo app1 = createResolveInfo(APP_1);
final ResolveInfo app2 = createResolveInfo(APP_2);
when(mPackageManagerService.findPersistentPreferredActivity(viewIntent, MAIN_USER_ID))
.thenReturn(app1);
when(mPackageManagerService.findPersistentPreferredActivity(viewIntent, MANAGED_PROFILE_ID))
.thenReturn(app1);
when(mPackageManagerService.findPersistentPreferredActivity(editIntent, MAIN_USER_ID))
.thenReturn(null);
when(mPackageManagerService.findPersistentPreferredActivity(editIntent, MANAGED_PROFILE_ID))
.thenReturn(app2);
when(mPackageManagerService.findPersistentPreferredActivity(sendIntent, MAIN_USER_ID))
.thenReturn(app1);
when(mPackageManagerService.findPersistentPreferredActivity(sendIntent, MANAGED_PROFILE_ID))
.thenReturn(null);
final Set<ApplicationFeatureProvider.PersistentPreferredActivityInfo> expectedActivities
= new ArraySet<>();
expectedActivities.add(new ApplicationFeatureProvider.PersistentPreferredActivityInfo(APP_1,
MAIN_USER_ID));
expectedActivities.add(new ApplicationFeatureProvider.PersistentPreferredActivityInfo(APP_1,
MANAGED_PROFILE_ID));
expectedActivities.add(new ApplicationFeatureProvider.PersistentPreferredActivityInfo(APP_2,
MANAGED_PROFILE_ID));
assertThat(mProvider.findPersistentPreferredActivities(
new Intent[] {viewIntent, editIntent, sendIntent})).isEqualTo(expectedActivities);
}
private void setUpUsersAndInstalledApps() {
when(mUserManager.getUsers(true)).thenReturn(Arrays.asList(
new UserInfo(MAIN_USER_ID, "main", UserInfo.FLAG_ADMIN),
@@ -156,4 +197,12 @@ public final class ApplicationFeatureProviderImplTest {
ApplicationTestUtils.buildInfo(APP_2_UID, APP_2, 0 /* flags */,
Build.VERSION_CODES.LOLLIPOP)));
}
private ResolveInfo createResolveInfo(String packageName) {
final ActivityInfo activityInfo = new ActivityInfo();
activityInfo.packageName = packageName;
final ResolveInfo resolveInfo = new ResolveInfo();
resolveInfo.activityInfo = activityInfo;
return resolveInfo;
}
}

View File

@@ -81,6 +81,10 @@ public final class EnterpriseInstalledPackagesPreferenceControllerTest {
final Preference preference = new Preference(mContext, null, 0, 0);
preference.setVisible(true);
setNumberOfEnterpriseInstalledPackages(0);
mController.updateState(preference);
assertThat(preference.isVisible()).isFalse();
setNumberOfEnterpriseInstalledPackages(20);
when(mContext.getResources().getQuantityString(
R.plurals.enterprise_privacy_number_enterprise_installed_packages, 20, 20))
@@ -88,10 +92,6 @@ public final class EnterpriseInstalledPackagesPreferenceControllerTest {
mController.updateState(preference);
assertThat(preference.getTitle()).isEqualTo("20 packages");
assertThat(preference.isVisible()).isTrue();
setNumberOfEnterpriseInstalledPackages(0);
mController.updateState(preference);
assertThat(preference.isVisible()).isFalse();
}
@Test

View File

@@ -73,7 +73,7 @@ public final class EnterprisePrivacySettingsTest {
final List<PreferenceController> controllers = mSettings.getPreferenceControllers(
ShadowApplication.getInstance().getApplicationContext());
assertThat(controllers).isNotNull();
assertThat(controllers.size()).isEqualTo(11);
assertThat(controllers.size()).isEqualTo(12);
assertThat(controllers.get(0)).isInstanceOf(InstalledPackagesPreferenceController.class);
assertThat(controllers.get(1)).isInstanceOf(NetworkLogsPreferenceController.class);
assertThat(controllers.get(2)).isInstanceOf(BugReportsPreferenceController.class);
@@ -87,9 +87,11 @@ public final class EnterprisePrivacySettingsTest {
assertThat(controllers.get(7)).isInstanceOf(
AdminGrantedCameraPermissionPreferenceController.class);
assertThat(controllers.get(8)).isInstanceOf(
AlwaysOnVpnPrimaryUserPreferenceController.class);
EnterpriseSetDefaultAppsPreferenceController.class);
assertThat(controllers.get(9)).isInstanceOf(
AlwaysOnVpnPrimaryUserPreferenceController.class);
assertThat(controllers.get(10)).isInstanceOf(
AlwaysOnVpnManagedProfilePreferenceController.class);
assertThat(controllers.get(10)).isInstanceOf(GlobalHttpProxyPreferenceController.class);
assertThat(controllers.get(11)).isInstanceOf(GlobalHttpProxyPreferenceController.class);
}
}

View File

@@ -0,0 +1,170 @@
/*
* 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.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.support.v7.preference.Preference;
import android.util.ArraySet;
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.testutils.FakeFeatureFactory;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatcher;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;
import java.util.Set;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.argThat;
import static org.mockito.Mockito.anyObject;
import static org.mockito.Mockito.when;
/**
* Tests for {@link EnterpriseSetDefaultAppsPreferenceController}.
*/
@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public final class EnterpriseSetDefaultAppsPreferenceControllerTest {
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Context mContext;
private FakeFeatureFactory mFeatureFactory;
private EnterpriseSetDefaultAppsPreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
FakeFeatureFactory.setupForTest(mContext);
mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
mController = new EnterpriseSetDefaultAppsPreferenceController(mContext);
}
private static Intent buildIntent(String action, String category, String protocol,
String type) {
final Intent intent = new Intent(action);
if (category != null) {
intent.addCategory(category);
}
if (protocol != null) {
intent.setData(Uri.parse(protocol));
}
if (type != null) {
intent.setType(type);
}
return intent;
}
private void setEnterpriseSetDefaultApps(Intent[] intents, int number) {
final Set<ApplicationFeatureProvider.PersistentPreferredActivityInfo> apps
= new ArraySet<>(number);
for (int i = 0; i < number; i++) {
apps.add(new ApplicationFeatureProvider.PersistentPreferredActivityInfo("app", i));
}
when(mFeatureFactory.applicationFeatureProvider.findPersistentPreferredActivities(
argThat(new MatchesIntents(intents)))).thenReturn(apps);
}
@Test
public void testUpdateState() {
final Preference preference = new Preference(mContext, null, 0, 0);
preference.setVisible(true);
when(mFeatureFactory.applicationFeatureProvider.findPersistentPreferredActivities(
anyObject())).thenReturn(
new ArraySet<ApplicationFeatureProvider.PersistentPreferredActivityInfo>());
mController.updateState(preference);
assertThat(preference.isVisible()).isFalse();
setEnterpriseSetDefaultApps(new Intent[] {buildIntent(Intent.ACTION_VIEW,
Intent.CATEGORY_BROWSABLE, "http:", null)}, 1);
setEnterpriseSetDefaultApps(new Intent[] {new Intent(MediaStore.ACTION_IMAGE_CAPTURE),
new Intent(MediaStore.ACTION_VIDEO_CAPTURE)}, 2);
setEnterpriseSetDefaultApps(new Intent[] {buildIntent(Intent.ACTION_VIEW, null, "geo:",
null)}, 4);
setEnterpriseSetDefaultApps(new Intent[] {new Intent(Intent.ACTION_SENDTO),
new Intent(Intent.ACTION_SEND), new Intent(Intent.ACTION_SEND_MULTIPLE)}, 8);
setEnterpriseSetDefaultApps(new Intent[] {buildIntent(Intent.ACTION_INSERT, null, null,
"vnd.android.cursor.dir/event")}, 16);
setEnterpriseSetDefaultApps(new Intent[] {buildIntent(Intent.ACTION_PICK, null, null,
ContactsContract.Contacts.CONTENT_TYPE)}, 32);
setEnterpriseSetDefaultApps(new Intent[] {new Intent(Intent.ACTION_DIAL),
new Intent(Intent.ACTION_CALL)}, 64);
when(mContext.getResources().getQuantityString(
R.plurals.enterprise_privacy_number_enterprise_set_default_apps, 127, 127))
.thenReturn("127 apps");
mController.updateState(preference);
assertThat(preference.getTitle()).isEqualTo("127 apps");
assertThat(preference.isVisible()).isTrue();
}
@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("number_enterprise_set_default_apps");
}
private static class MatchesIntents extends ArgumentMatcher<Intent[]> {
private final Intent[] mExpectedIntents;
MatchesIntents(Intent[] intents) {
mExpectedIntents = intents;
}
@Override
public boolean matches(Object object) {
final Intent[] actualIntents = (Intent[]) object;
if (actualIntents == null) {
return false;
}
if (actualIntents.length != mExpectedIntents.length) {
return false;
}
for (int i = 0; i < mExpectedIntents.length; i++) {
if (!mExpectedIntents[i].filterEquals(actualIntents[i])) {
return false;
}
}
return true;
}
}
}