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