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.applications.defaultapps; 18 19 20import android.content.Context; 21import android.content.Intent; 22import android.content.pm.ResolveInfo; 23import android.os.UserManager; 24import android.support.v7.preference.Preference; 25 26import com.android.settings.R; 27import com.android.settings.testutils.SettingsRobolectricTestRunner; 28import com.android.settings.TestConfig; 29import com.android.settings.applications.PackageManagerWrapper; 30 31import org.junit.Before; 32import org.junit.Test; 33import org.junit.runner.RunWith; 34import org.mockito.Answers; 35import org.mockito.Mock; 36import org.mockito.MockitoAnnotations; 37import org.robolectric.annotation.Config; 38import org.robolectric.util.ReflectionHelpers; 39 40import java.util.ArrayList; 41import java.util.Arrays; 42import java.util.List; 43 44import static com.google.common.truth.Truth.assertThat; 45import static org.mockito.Matchers.any; 46import static org.mockito.Matchers.anyInt; 47import static org.mockito.Mockito.mock; 48import static org.mockito.Mockito.verify; 49import static org.mockito.Mockito.when; 50 51@RunWith(SettingsRobolectricTestRunner.class) 52@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 53public class DefaultBrowserPreferenceControllerTest { 54 55 @Mock 56 private Context mContext; 57 @Mock 58 private UserManager mUserManager; 59 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 60 private PackageManagerWrapper mPackageManager; 61 62 private DefaultBrowserPreferenceController mController; 63 64 @Before 65 public void setUp() { 66 MockitoAnnotations.initMocks(this); 67 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 68 69 mController = new DefaultBrowserPreferenceController(mContext); 70 ReflectionHelpers.setField(mController, "mPackageManager", mPackageManager); 71 } 72 73 @Test 74 public void isAvailable_noBrowser_shouldReturnFalse() { 75 when(mPackageManager.queryIntentActivitiesAsUser(any(Intent.class), anyInt(), anyInt())) 76 .thenReturn(null); 77 assertThat(mController.isAvailable()).isFalse(); 78 } 79 80 @Test 81 public void isAvailable_hasBrowser_shouldReturnTrue() { 82 final List<ResolveInfo> candidates = new ArrayList<>(); 83 candidates.add(new ResolveInfo()); 84 when(mPackageManager.queryIntentActivitiesAsUser(any(Intent.class), anyInt(), anyInt())) 85 .thenReturn(candidates); 86 assertThat(mController.isAvailable()).isTrue(); 87 } 88 89 @Test 90 public void getSoleAppLabel_hasNoApp_shouldNotReturnLabel() { 91 when(mPackageManager.queryIntentActivitiesAsUser(any(Intent.class), anyInt(), anyInt())) 92 .thenReturn(null); 93 final Preference pref = mock(Preference.class); 94 95 mController.updateState(pref); 96 verify(pref).setSummary(R.string.app_list_preference_none); 97 } 98 99 @Test 100 public void getDefaultApp_shouldGetDefaultBrowserPackage() { 101 mController.getDefaultAppInfo(); 102 103 verify(mPackageManager).getDefaultBrowserPackageNameAsUser(anyInt()); 104 } 105 106 @Test 107 public void isBrowserDefault_onlyApp_shouldReturnTrue() { 108 final String testPkg = "pkg"; 109 when(mPackageManager.getDefaultBrowserPackageNameAsUser(anyInt())) 110 .thenReturn(null); 111 when(mPackageManager.queryIntentActivitiesAsUser(any(Intent.class), anyInt(), anyInt())) 112 .thenReturn(Arrays.asList(new ResolveInfo())); 113 114 assertThat(mController.isBrowserDefault(testPkg, 0)).isTrue(); 115 } 116} 117