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 static com.google.common.truth.Truth.assertThat; 20 import static org.mockito.ArgumentMatchers.any; 21 import static org.mockito.Matchers.anyInt; 22 import static org.mockito.Matchers.argThat; 23 import static org.mockito.Matchers.eq; 24 import static org.mockito.Mockito.when; 25 26 import android.content.Context; 27 import android.content.Intent; 28 import android.content.pm.ApplicationInfo; 29 import android.content.pm.UserInfo; 30 import android.os.UserHandle; 31 import android.support.v7.preference.Preference; 32 33 import com.android.settings.R; 34 import com.android.settings.applications.EnterpriseDefaultApps; 35 import com.android.settings.applications.UserAppInfo; 36 import com.android.settings.testutils.FakeFeatureFactory; 37 import com.android.settings.testutils.SettingsRobolectricTestRunner; 38 39 import org.junit.Before; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 import org.mockito.Answers; 43 import org.mockito.ArgumentMatcher; 44 import org.mockito.Mock; 45 import org.mockito.MockitoAnnotations; 46 47 import java.util.ArrayList; 48 import java.util.List; 49 50 @RunWith(SettingsRobolectricTestRunner.class) 51 public final class EnterpriseSetDefaultAppsPreferenceControllerTest { 52 53 private static final String KEY_DEFAULT_APPS = "number_enterprise_set_default_apps"; 54 55 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 56 private Context mContext; 57 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 58 private FakeFeatureFactory mFeatureFactory; 59 60 private EnterpriseSetDefaultAppsPreferenceController mController; 61 62 @Before setUp()63 public void setUp() { 64 MockitoAnnotations.initMocks(this); 65 mFeatureFactory = FakeFeatureFactory.setupForTest(); 66 mController = new EnterpriseSetDefaultAppsPreferenceController(mContext); 67 } 68 setEnterpriseSetDefaultApps(Intent[] intents, int number)69 private void setEnterpriseSetDefaultApps(Intent[] intents, int number) { 70 final ApplicationInfo appInfo = new ApplicationInfo(); 71 appInfo.packageName = "app"; 72 for (int i = 0; i < number; i++) { 73 final List<UserAppInfo> apps = new ArrayList<>(number); 74 apps.add(new UserAppInfo(new UserInfo(i, "user." + i, UserInfo.FLAG_ADMIN), appInfo)); 75 when(mFeatureFactory.applicationFeatureProvider.findPersistentPreferredActivities(eq(i), 76 argThat(matchesIntents(intents)))).thenReturn(apps); 77 } 78 } 79 configureUsers(int number)80 private void configureUsers(int number) { 81 final List<UserHandle> users = new ArrayList<>(number); 82 for (int i = 0; i < 64; i++) { 83 users.add(new UserHandle(i)); 84 } 85 when(mFeatureFactory.userFeatureProvider.getUserProfiles()).thenReturn(users); 86 } 87 88 @Test testUpdateState()89 public void testUpdateState() { 90 setEnterpriseSetDefaultApps(EnterpriseDefaultApps.BROWSER.getIntents(), 1); 91 setEnterpriseSetDefaultApps(EnterpriseDefaultApps.CAMERA.getIntents(), 2); 92 setEnterpriseSetDefaultApps(EnterpriseDefaultApps.MAP.getIntents(), 4); 93 setEnterpriseSetDefaultApps(EnterpriseDefaultApps.EMAIL.getIntents(), 8); 94 setEnterpriseSetDefaultApps(EnterpriseDefaultApps.CALENDAR.getIntents(), 16); 95 setEnterpriseSetDefaultApps(EnterpriseDefaultApps.CONTACTS.getIntents(), 32); 96 setEnterpriseSetDefaultApps(EnterpriseDefaultApps.PHONE.getIntents(), 64); 97 when(mContext.getResources().getQuantityString(R.plurals.enterprise_privacy_number_packages, 98 127, 127)).thenReturn("127 apps"); 99 100 // As setEnterpriseSetDefaultApps uses fake Users, we need to list them via UserManager. 101 configureUsers(64); 102 103 final Preference preference = new Preference(mContext, null, 0, 0); 104 mController.updateState(preference); 105 assertThat(preference.getSummary()).isEqualTo("127 apps"); 106 } 107 108 @Test testIsAvailable()109 public void testIsAvailable() { 110 when(mFeatureFactory.applicationFeatureProvider.findPersistentPreferredActivities(anyInt(), 111 any(Intent[].class))).thenReturn(new ArrayList<>()); 112 assertThat(mController.isAvailable()).isFalse(); 113 114 setEnterpriseSetDefaultApps(EnterpriseDefaultApps.BROWSER.getIntents(), 1); 115 configureUsers(1); 116 assertThat(mController.isAvailable()).isTrue(); 117 } 118 119 @Test testHandlePreferenceTreeClick()120 public void testHandlePreferenceTreeClick() { 121 assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0))) 122 .isFalse(); 123 } 124 125 @Test testGetPreferenceKey()126 public void testGetPreferenceKey() { 127 assertThat(mController.getPreferenceKey()).isEqualTo(KEY_DEFAULT_APPS); 128 } 129 matchesIntents(Intent[] intents)130 private ArgumentMatcher<Intent[]> matchesIntents(Intent[] intents) { 131 return (Intent[] actualIntents) -> { 132 if (actualIntents == null) { 133 return false; 134 } 135 if (actualIntents.length != intents.length) { 136 return false; 137 } 138 for (int i = 0; i < intents.length; i++) { 139 if (!intents[i].filterEquals(actualIntents[i])) { 140 return false; 141 } 142 } 143 return true; 144 }; 145 } 146 } 147