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.Answers.RETURNS_DEEP_STUBS; 22 import static org.mockito.ArgumentMatchers.any; 23 import static org.mockito.ArgumentMatchers.anyInt; 24 import static org.mockito.ArgumentMatchers.eq; 25 import static org.mockito.Mockito.spy; 26 import static org.mockito.Mockito.times; 27 import static org.mockito.Mockito.verify; 28 import static org.mockito.Mockito.when; 29 30 import android.content.Context; 31 import android.content.pm.ApplicationInfo; 32 import android.content.pm.PackageManager; 33 import android.content.pm.UserInfo; 34 import android.content.res.Resources; 35 import android.os.UserHandle; 36 37 import androidx.preference.Preference; 38 import androidx.preference.PreferenceManager; 39 import androidx.preference.PreferenceScreen; 40 41 import com.android.settings.R; 42 import com.android.settings.SettingsPreferenceFragment; 43 import com.android.settings.applications.EnterpriseDefaultApps; 44 import com.android.settings.applications.UserAppInfo; 45 import com.android.settings.testutils.ApplicationTestUtils; 46 import com.android.settings.testutils.FakeFeatureFactory; 47 48 import org.junit.Before; 49 import org.junit.Test; 50 import org.junit.runner.RunWith; 51 import org.mockito.ArgumentCaptor; 52 import org.mockito.Mock; 53 import org.mockito.MockitoAnnotations; 54 import org.robolectric.RobolectricTestRunner; 55 import org.robolectric.RuntimeEnvironment; 56 import org.robolectric.shadows.ShadowApplication; 57 58 import java.util.Arrays; 59 import java.util.Collections; 60 61 @RunWith(RobolectricTestRunner.class) 62 public class EnterpriseSetDefaultAppsListPreferenceControllerTest { 63 64 private static final int USER_ID = 0; 65 private static final int APP_UID = 0; 66 67 private static final String APP_1 = "APP_1"; 68 private static final String APP_2 = "APP_2"; 69 private static final String BROWSER_TITLE = "Browser app"; 70 private static final String PHONE_TITLE = "Phone apps"; 71 72 @Mock(answer = RETURNS_DEEP_STUBS) 73 private PreferenceScreen mScreen; 74 @Mock(answer = RETURNS_DEEP_STUBS) 75 private PreferenceManager mPrefenceManager; 76 @Mock(answer = RETURNS_DEEP_STUBS) 77 private PackageManager mPackageManager; 78 @Mock(answer = RETURNS_DEEP_STUBS) 79 private SettingsPreferenceFragment mFragment; 80 81 private Context mContext; 82 private FakeFeatureFactory mFeatureFactory; 83 84 @Before setUp()85 public void setUp() { 86 MockitoAnnotations.initMocks(this); 87 mContext = spy(RuntimeEnvironment.application); 88 mFeatureFactory = FakeFeatureFactory.setupForTest(); 89 when(mFragment.getPreferenceScreen()).thenReturn(mScreen); 90 when(mPrefenceManager.getContext()).thenReturn(mContext); 91 when(mFragment.getPreferenceManager()).thenReturn(mPrefenceManager); 92 93 when(mContext.getString(R.string.default_browser_title)).thenReturn(BROWSER_TITLE); 94 Resources resources = spy(mContext.getResources()); 95 when(mContext.getResources()).thenReturn(resources); 96 when(resources.getQuantityString(R.plurals.default_phone_app_title, 2)) 97 .thenReturn(PHONE_TITLE); 98 when(mContext.getString(R.string.app_names_concatenation_template_2)) 99 .thenReturn("%1$s, %2$s"); 100 101 when(mPackageManager.getText(eq(APP_1), anyInt(), any())).thenReturn(APP_1); 102 when(mPackageManager.getText(eq(APP_2), anyInt(), any())).thenReturn(APP_2); 103 } 104 105 @Test testMultipleAppsForOneTypeOfDefault()106 public void testMultipleAppsForOneTypeOfDefault() { 107 final UserInfo user = new UserInfo(USER_ID, "main", UserInfo.FLAG_ADMIN); 108 final ApplicationInfo appInfo1 = ApplicationTestUtils.buildInfo(APP_UID, APP_1, 0, 0); 109 final ApplicationInfo appInfo2 = ApplicationTestUtils.buildInfo(APP_UID, APP_2, 0, 0); 110 111 when(mFeatureFactory.userFeatureProvider.getUserProfiles()) 112 .thenReturn(Collections.singletonList(new UserHandle(USER_ID))); 113 when(mFeatureFactory.enterprisePrivacyFeatureProvider.isInCompMode()).thenReturn(false); 114 when(mFeatureFactory.applicationFeatureProvider 115 .findPersistentPreferredActivities(anyInt(), any())) 116 .thenReturn(Collections.emptyList()); 117 when(mFeatureFactory.applicationFeatureProvider 118 .findPersistentPreferredActivities(eq(USER_ID), 119 eq(EnterpriseDefaultApps.BROWSER.getIntents()))) 120 .thenReturn(Collections.singletonList(new UserAppInfo(user, appInfo1))); 121 when(mFeatureFactory.applicationFeatureProvider 122 .findPersistentPreferredActivities(eq(USER_ID), 123 eq(EnterpriseDefaultApps.PHONE.getIntents()))).thenReturn( 124 Arrays.asList(new UserAppInfo(user, appInfo1), 125 new UserAppInfo(user, appInfo2))); 126 127 new EnterpriseSetDefaultAppsListPreferenceController(mContext, mFragment, mPackageManager); 128 ShadowApplication.runBackgroundTasks(); 129 130 ArgumentCaptor<Preference> apps = ArgumentCaptor.forClass(Preference.class); 131 verify(mScreen, times(2)).addPreference(apps.capture()); 132 133 assertThat(apps.getAllValues().get(0).getTitle()).isEqualTo(BROWSER_TITLE); 134 assertThat(apps.getAllValues().get(0).getSummary()).isEqualTo(APP_1); 135 136 assertThat(apps.getAllValues().get(1).getTitle()).isEqualTo(PHONE_TITLE); 137 assertThat(apps.getAllValues().get(1).getSummary()).isEqualTo(APP_1 + ", " + APP_2); 138 } 139 140 @Test isAvailable()141 public void isAvailable() { 142 when(mFeatureFactory.userFeatureProvider.getUserProfiles()) 143 .thenReturn(Collections.singletonList(new UserHandle(USER_ID))); 144 when(mFeatureFactory.applicationFeatureProvider 145 .findPersistentPreferredActivities(anyInt(), any())) 146 .thenReturn(Collections.emptyList()); 147 final EnterpriseSetDefaultAppsListPreferenceController controller = 148 new EnterpriseSetDefaultAppsListPreferenceController(mContext, mFragment, 149 mPackageManager); 150 assertThat(controller.isAvailable()).isTrue(); 151 } 152 153 @Test getPreferenceKey()154 public void getPreferenceKey() { 155 when(mFeatureFactory.userFeatureProvider.getUserProfiles()) 156 .thenReturn(Collections.singletonList(new UserHandle(USER_ID))); 157 when(mFeatureFactory.applicationFeatureProvider 158 .findPersistentPreferredActivities(anyInt(), any())) 159 .thenReturn(Collections.emptyList()); 160 final EnterpriseSetDefaultAppsListPreferenceController controller = 161 new EnterpriseSetDefaultAppsListPreferenceController(mContext, mFragment, 162 mPackageManager); 163 assertThat(controller.getPreferenceKey()).isNull(); 164 } 165 }