OSDN Git Service

Restart loader in onResume
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / enterprise / EnterpriseSetDefaultAppsPreferenceControllerTest.java
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.android.settings.enterprise;
18
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;
26
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;
33
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;
42
43 import java.util.ArrayList;
44 import java.util.List;
45
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;
52
53 /**
54  * Tests for {@link EnterpriseSetDefaultAppsPreferenceController}.
55  */
56 @RunWith(SettingsRobolectricTestRunner.class)
57 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
58 public final class EnterpriseSetDefaultAppsPreferenceControllerTest {
59
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;
65
66     private EnterpriseSetDefaultAppsPreferenceController mController;
67
68     @Before
69     public void setUp() {
70         MockitoAnnotations.initMocks(this);
71         FakeFeatureFactory.setupForTest(mContext);
72         mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext);
73         mController = new EnterpriseSetDefaultAppsPreferenceController(mContext,
74                 null /* lifecycle */);
75     }
76
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);
85         }
86     }
87
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));
92         }
93         when(mFeatureFactory.userFeatureProvider.getUserProfiles()).thenReturn(users);
94     }
95
96     @Test
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");
107
108         // As setEnterpriseSetDefaultApps uses fake Users, we need to list them via UserManager.
109         configureUsers(64);
110
111         final Preference preference = new Preference(mContext, null, 0, 0);
112         mController.updateState(preference);
113         assertThat(preference.getSummary()).isEqualTo("127 apps");
114     }
115
116     @Test
117     public void testIsAvailable() {
118         when(mFeatureFactory.applicationFeatureProvider.findPersistentPreferredActivities(anyInt(),
119                 anyObject())).thenReturn(new ArrayList<UserAppInfo>());
120         assertThat(mController.isAvailable()).isFalse();
121
122         setEnterpriseSetDefaultApps(EnterpriseDefaultApps.BROWSER.getIntents(), 1);
123         configureUsers(1);
124         assertThat(mController.isAvailable()).isTrue();
125     }
126
127     @Test
128     public void testHandlePreferenceTreeClick() {
129         assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0)))
130                 .isFalse();
131     }
132
133     @Test
134     public void testGetPreferenceKey() {
135         assertThat(mController.getPreferenceKey())
136                 .isEqualTo("number_enterprise_set_default_apps");
137     }
138
139     private static class MatchesIntents extends ArgumentMatcher<Intent[]> {
140         private final Intent[] mExpectedIntents;
141
142         MatchesIntents(Intent[] intents) {
143             mExpectedIntents = intents;
144         }
145
146         @Override
147         public boolean matches(Object object) {
148             final Intent[] actualIntents = (Intent[]) object;
149             if (actualIntents == null) {
150                 return false;
151             }
152             if (actualIntents.length != mExpectedIntents.length) {
153                 return false;
154             }
155             for (int i = 0; i < mExpectedIntents.length; i++) {
156                 if (!mExpectedIntents[i].filterEquals(actualIntents[i])) {
157                     return false;
158                 }
159             }
160             return true;
161         }
162     }
163 }