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