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