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.core.PreferenceAvailabilityObserver; 33 import com.android.settings.testutils.FakeFeatureFactory; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.Answers; 39 import org.mockito.ArgumentMatcher; 40 import org.mockito.Mock; 41 import org.mockito.MockitoAnnotations; 42 import org.robolectric.annotation.Config; 43 44 import java.util.ArrayList; 45 import java.util.List; 46 47 import static com.google.common.truth.Truth.assertThat; 48 import static org.mockito.Matchers.anyInt; 49 import static org.mockito.Matchers.argThat; 50 import static org.mockito.Matchers.eq; 51 import static org.mockito.Mockito.anyObject; 52 import static org.mockito.Mockito.verify; 53 import static org.mockito.Mockito.when; 54 55 /** 56 * Tests for {@link EnterpriseSetDefaultAppsPreferenceController}. 57 */ 58 @RunWith(SettingsRobolectricTestRunner.class) 59 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 60 public final class EnterpriseSetDefaultAppsPreferenceControllerTest { 61 62 private static final String KEY_DEFAULT_APPS = "number_enterprise_set_default_apps"; 63 64 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 65 private Context mContext; 66 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 67 private UserManager mUm; 68 private FakeFeatureFactory mFeatureFactory; 69 @Mock private PreferenceAvailabilityObserver mObserver; 70 71 private EnterpriseSetDefaultAppsPreferenceController mController; 72 73 @Before setUp()74 public void setUp() { 75 MockitoAnnotations.initMocks(this); 76 FakeFeatureFactory.setupForTest(mContext); 77 mFeatureFactory = (FakeFeatureFactory) FakeFeatureFactory.getFactory(mContext); 78 mController = new EnterpriseSetDefaultAppsPreferenceController(mContext, 79 null /* lifecycle */); 80 mController.setAvailabilityObserver(mObserver); 81 } 82 83 @Test testGetAvailabilityObserver()84 public void testGetAvailabilityObserver() { 85 assertThat(mController.getAvailabilityObserver()).isEqualTo(mObserver); 86 } 87 setEnterpriseSetDefaultApps(Intent[] intents, int number)88 private void setEnterpriseSetDefaultApps(Intent[] intents, int number) { 89 final ApplicationInfo appInfo = new ApplicationInfo(); 90 appInfo.packageName = "app"; 91 for (int i = 0; i < number; i++) { 92 final List<UserAppInfo> apps = new ArrayList<>(number); 93 apps.add(new UserAppInfo(new UserInfo(i, "user." + i, UserInfo.FLAG_ADMIN), appInfo)); 94 when(mFeatureFactory.applicationFeatureProvider.findPersistentPreferredActivities(eq(i), 95 argThat(new MatchesIntents(intents)))).thenReturn(apps); 96 } 97 } 98 configureUsers(int number)99 private void configureUsers(int number) { 100 final List<UserHandle> users = new ArrayList<>(number); 101 for (int i = 0; i < 64; i++) { 102 users.add(new UserHandle(i)); 103 } 104 when(mFeatureFactory.userFeatureProvider.getUserProfiles()).thenReturn(users); 105 } 106 107 @Test testUpdateState()108 public void testUpdateState() { 109 setEnterpriseSetDefaultApps(EnterpriseDefaultApps.BROWSER.getIntents(), 1); 110 setEnterpriseSetDefaultApps(EnterpriseDefaultApps.CAMERA.getIntents(), 2); 111 setEnterpriseSetDefaultApps(EnterpriseDefaultApps.MAP.getIntents(), 4); 112 setEnterpriseSetDefaultApps(EnterpriseDefaultApps.EMAIL.getIntents(), 8); 113 setEnterpriseSetDefaultApps(EnterpriseDefaultApps.CALENDAR.getIntents(), 16); 114 setEnterpriseSetDefaultApps(EnterpriseDefaultApps.CONTACTS.getIntents(), 32); 115 setEnterpriseSetDefaultApps(EnterpriseDefaultApps.PHONE.getIntents(), 64); 116 when(mContext.getResources().getQuantityString(R.plurals.enterprise_privacy_number_packages, 117 127, 127)).thenReturn("127 apps"); 118 119 // As setEnterpriseSetDefaultApps uses fake Users, we need to list them via UserManager. 120 configureUsers(64); 121 122 final Preference preference = new Preference(mContext, null, 0, 0); 123 mController.updateState(preference); 124 assertThat(preference.getSummary()).isEqualTo("127 apps"); 125 } 126 127 @Test testIsAvailable()128 public void testIsAvailable() { 129 when(mFeatureFactory.applicationFeatureProvider.findPersistentPreferredActivities(anyInt(), 130 anyObject())).thenReturn(new ArrayList<UserAppInfo>()); 131 assertThat(mController.isAvailable()).isFalse(); 132 verify(mObserver).onPreferenceAvailabilityUpdated(KEY_DEFAULT_APPS, false); 133 134 setEnterpriseSetDefaultApps(EnterpriseDefaultApps.BROWSER.getIntents(), 1); 135 configureUsers(1); 136 assertThat(mController.isAvailable()).isTrue(); 137 verify(mObserver).onPreferenceAvailabilityUpdated(KEY_DEFAULT_APPS, true); 138 } 139 140 @Test testHandlePreferenceTreeClick()141 public void testHandlePreferenceTreeClick() { 142 assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0))) 143 .isFalse(); 144 } 145 146 @Test testGetPreferenceKey()147 public void testGetPreferenceKey() { 148 assertThat(mController.getPreferenceKey()).isEqualTo(KEY_DEFAULT_APPS); 149 } 150 151 private static class MatchesIntents extends ArgumentMatcher<Intent[]> { 152 private final Intent[] mExpectedIntents; 153 MatchesIntents(Intent[] intents)154 MatchesIntents(Intent[] intents) { 155 mExpectedIntents = intents; 156 } 157 158 @Override matches(Object object)159 public boolean matches(Object object) { 160 final Intent[] actualIntents = (Intent[]) object; 161 if (actualIntents == null) { 162 return false; 163 } 164 if (actualIntents.length != mExpectedIntents.length) { 165 return false; 166 } 167 for (int i = 0; i < mExpectedIntents.length; i++) { 168 if (!mExpectedIntents[i].filterEquals(actualIntents[i])) { 169 return false; 170 } 171 } 172 return true; 173 } 174 } 175 } 176