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.doReturn; 26 import static org.mockito.Mockito.mock; 27 import static org.mockito.Mockito.when; 28 29 import android.app.admin.DevicePolicyManager; 30 import android.content.Context; 31 import android.content.Intent; 32 import android.content.pm.ApplicationInfo; 33 import android.content.pm.UserInfo; 34 import android.os.UserHandle; 35 36 import androidx.preference.Preference; 37 38 import com.android.settings.R; 39 import com.android.settings.applications.EnterpriseDefaultApps; 40 import com.android.settings.applications.UserAppInfo; 41 import com.android.settings.testutils.FakeFeatureFactory; 42 import com.android.settingslib.utils.StringUtil; 43 44 import org.junit.Before; 45 import org.junit.Test; 46 import org.junit.runner.RunWith; 47 import org.mockito.Answers; 48 import org.mockito.ArgumentMatcher; 49 import org.mockito.Mock; 50 import org.mockito.MockitoAnnotations; 51 52 import java.util.ArrayList; 53 import java.util.List; 54 import org.robolectric.RobolectricTestRunner; 55 56 @RunWith(RobolectricTestRunner.class) 57 public final class EnterpriseSetDefaultAppsPreferenceControllerTest { 58 59 private static final String KEY_DEFAULT_APPS = "number_enterprise_set_default_apps"; 60 61 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 62 private Context mContext; 63 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 64 private FakeFeatureFactory mFeatureFactory; 65 66 private EnterpriseSetDefaultAppsPreferenceController mController; 67 68 @Before setUp()69 public void setUp() { 70 MockitoAnnotations.initMocks(this); 71 doReturn(mock(DevicePolicyManager.class)).when(mContext) 72 .getSystemService(Context.DEVICE_POLICY_SERVICE); 73 mFeatureFactory = FakeFeatureFactory.setupForTest(); 74 mController = new EnterpriseSetDefaultAppsPreferenceController(mContext); 75 } 76 setEnterpriseSetDefaultApps(Intent[] intents, int number)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(matchesIntents(intents)))).thenReturn(apps); 85 } 86 } 87 configureUsers(int number)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 testUpdateState()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().getString(R.string.enterprise_privacy_number_packages)) 106 .thenReturn("# apps"); 107 when(StringUtil.getIcuPluralsString(mContext, 127, 108 R.string.enterprise_privacy_number_packages)).thenReturn("127 apps"); 109 110 // As setEnterpriseSetDefaultApps uses fake Users, we need to list them via UserManager. 111 configureUsers(64); 112 113 final Preference preference = new Preference(mContext, null, 0, 0); 114 mController.updateState(preference); 115 assertThat(preference.getSummary()).isEqualTo("127 apps"); 116 } 117 118 @Test testIsAvailable()119 public void testIsAvailable() { 120 when(mFeatureFactory.applicationFeatureProvider.findPersistentPreferredActivities(anyInt(), 121 any(Intent[].class))).thenReturn(new ArrayList<>()); 122 assertThat(mController.isAvailable()).isFalse(); 123 124 setEnterpriseSetDefaultApps(EnterpriseDefaultApps.BROWSER.getIntents(), 1); 125 configureUsers(1); 126 assertThat(mController.isAvailable()).isTrue(); 127 } 128 129 @Test testHandlePreferenceTreeClick()130 public void testHandlePreferenceTreeClick() { 131 assertThat(mController.handlePreferenceTreeClick(new Preference(mContext, null, 0, 0))) 132 .isFalse(); 133 } 134 135 @Test testGetPreferenceKey()136 public void testGetPreferenceKey() { 137 assertThat(mController.getPreferenceKey()).isEqualTo(KEY_DEFAULT_APPS); 138 } 139 matchesIntents(Intent[] intents)140 private ArgumentMatcher<Intent[]> matchesIntents(Intent[] intents) { 141 return (Intent[] actualIntents) -> { 142 if (actualIntents == null) { 143 return false; 144 } 145 if (actualIntents.length != intents.length) { 146 return false; 147 } 148 for (int i = 0; i < intents.length; i++) { 149 if (!intents[i].filterEquals(actualIntents[i])) { 150 return false; 151 } 152 } 153 return true; 154 }; 155 } 156 } 157