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