2 * Copyright (C) 2017 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.android.settings.enterprise;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.pm.ApplicationInfo;
22 import android.content.pm.UserInfo;
23 import android.os.UserHandle;
24 import android.os.UserManager;
25 import android.support.v7.preference.Preference;
27 import com.android.settings.R;
28 import com.android.settings.SettingsRobolectricTestRunner;
29 import com.android.settings.TestConfig;
30 import com.android.settings.applications.EnterpriseDefaultApps;
31 import com.android.settings.applications.UserAppInfo;
32 import com.android.settings.testutils.FakeFeatureFactory;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Answers;
38 import org.mockito.ArgumentMatcher;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.robolectric.annotation.Config;
43 import java.util.ArrayList;
44 import java.util.List;
46 import static com.google.common.truth.Truth.assertThat;
47 import static org.mockito.Matchers.anyInt;
48 import static org.mockito.Matchers.argThat;
49 import static org.mockito.Matchers.eq;
50 import static org.mockito.Mockito.anyObject;
51 import static org.mockito.Mockito.when;
54 * Tests for {@link EnterpriseSetDefaultAppsPreferenceController}.
56 @RunWith(SettingsRobolectricTestRunner.class)
57 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
58 public final class EnterpriseSetDefaultAppsPreferenceControllerTest {
60 @Mock(answer = Answers.RETURNS_DEEP_STUBS)
61 private Context mContext;
62 @Mock(answer = Answers.RETURNS_DEEP_STUBS)
63 private UserManager mUm;
64 private FakeFeatureFactory mFeatureFactory;
66 private EnterpriseSetDefaultAppsPreferenceController mController;
70 MockitoAnnotations.initMocks(this);
71 FakeFeatureFactory.setupForTest(mContext);
72 mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
73 mController = new EnterpriseSetDefaultAppsPreferenceController(mContext,
74 null /* lifecycle */);
77 private void setEnterpriseSetDefaultApps(Intent[] intents, int number) {
78 final ApplicationInfo appInfo = new ApplicationInfo();
79 appInfo.packageName = "app";
80 for (int i = 0; i < number; i++) {
81 final List<UserAppInfo> apps = new ArrayList<>(number);
82 apps.add(new UserAppInfo(new UserInfo(i, "user." + i, UserInfo.FLAG_ADMIN), appInfo));
83 when(mFeatureFactory.applicationFeatureProvider.findPersistentPreferredActivities(eq(i),
84 argThat(new MatchesIntents(intents)))).thenReturn(apps);
88 private void configureUsers(int number) {
89 final List<UserHandle> users = new ArrayList<>(number);
90 for (int i = 0; i < 64; i++) {
91 users.add(new UserHandle(i));
93 when(mFeatureFactory.userFeatureProvider.getUserProfiles()).thenReturn(users);
97 public void testUpdateState() {
98 setEnterpriseSetDefaultApps(EnterpriseDefaultApps.BROWSER.getIntents(), 1);
99 setEnterpriseSetDefaultApps(EnterpriseDefaultApps.CAMERA.getIntents(), 2);
100 setEnterpriseSetDefaultApps(EnterpriseDefaultApps.MAP.getIntents(), 4);
101 setEnterpriseSetDefaultApps(EnterpriseDefaultApps.EMAIL.getIntents(), 8);
102 setEnterpriseSetDefaultApps(EnterpriseDefaultApps.CALENDAR.getIntents(), 16);
103 setEnterpriseSetDefaultApps(EnterpriseDefaultApps.CONTACTS.getIntents(), 32);
104 setEnterpriseSetDefaultApps(EnterpriseDefaultApps.PHONE.getIntents(), 64);
105 when(mContext.getResources().getQuantityString(R.plurals.enterprise_privacy_number_packages,
106 127, 127)).thenReturn("127 apps");
108 // As setEnterpriseSetDefaultApps uses fake Users, we need to list them via UserManager.
111 final Preference preference = new Preference(mContext, null, 0, 0);
112 mController.updateState(preference);
113 assertThat(preference.getSummary()).isEqualTo("127 apps");
117 public void testIsAvailable() {
118 when(mFeatureFactory.applicationFeatureProvider.findPersistentPreferredActivities(anyInt(),
119 anyObject())).thenReturn(new ArrayList<UserAppInfo>());
120 assertThat(mController.isAvailable()).isFalse();
122 setEnterpriseSetDefaultApps(EnterpriseDefaultApps.BROWSER.getIntents(), 1);
124 assertThat(mController.isAvailable()).isTrue();
128 public void testHandlePreferenceTreeClick() {
129 assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0)))
134 public void testGetPreferenceKey() {
135 assertThat(mController.getPreferenceKey())
136 .isEqualTo("number_enterprise_set_default_apps");
139 private static class MatchesIntents extends ArgumentMatcher<Intent[]> {
140 private final Intent[] mExpectedIntents;
142 MatchesIntents(Intent[] intents) {
143 mExpectedIntents = intents;
147 public boolean matches(Object object) {
148 final Intent[] actualIntents = (Intent[]) object;
149 if (actualIntents == null) {
152 if (actualIntents.length != mExpectedIntents.length) {
155 for (int i = 0; i < mExpectedIntents.length; i++) {
156 if (!mExpectedIntents[i].filterEquals(actualIntents[i])) {