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.applications.appinfo; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.mockito.ArgumentMatchers.anyInt; 21 import static org.mockito.ArgumentMatchers.nullable; 22 import static org.mockito.Mockito.mock; 23 import static org.mockito.Mockito.when; 24 25 import android.content.pm.ApplicationInfo; 26 import android.content.pm.PackageInfo; 27 import android.os.UserManager; 28 29 import com.android.settings.applications.AppStateInstallAppsBridge; 30 import com.android.settings.applications.AppStateInstallAppsBridge.InstallAppsState; 31 import com.android.settings.testutils.SettingsRobolectricTestRunner; 32 import com.android.settingslib.RestrictedSwitchPreference; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Mock; 38 import org.mockito.MockitoAnnotations; 39 import org.robolectric.util.ReflectionHelpers; 40 41 @RunWith(SettingsRobolectricTestRunner.class) 42 public class ExternalSourcesDetailsTest { 43 44 @Mock 45 private UserManager mUserManager; 46 @Mock 47 private RestrictedSwitchPreference mSwitchPref; 48 @Mock 49 private PackageInfo mPackageInfo; 50 51 private ExternalSourcesDetails mFragment; 52 53 @Before 54 public void setUp() { 55 MockitoAnnotations.initMocks(this); 56 57 mFragment = new ExternalSourcesDetails(); 58 ReflectionHelpers.setField(mFragment, "mUserManager", mUserManager); 59 ReflectionHelpers.setField(mFragment, "mSwitchPref", mSwitchPref); 60 } 61 62 @Test 63 public void refreshUi_noPackageInfo_shouldReturnFalseAndNoCrash() { 64 mFragment.refreshUi(); 65 66 assertThat(mFragment.refreshUi()).isFalse(); 67 // should not crash 68 } 69 70 @Test 71 public void refreshUi_noApplicationInfo_shouldReturnFalseAndNoCrash() { 72 ReflectionHelpers.setField(mFragment, "mPackageInfo", mPackageInfo); 73 74 mFragment.refreshUi(); 75 76 assertThat(mFragment.refreshUi()).isFalse(); 77 // should not crash 78 } 79 80 @Test 81 public void refreshUi_hasApplicationInfo_shouldReturnTrue() { 82 ReflectionHelpers.setField(mFragment, "mPackageInfo", mPackageInfo); 83 mPackageInfo.applicationInfo = new ApplicationInfo(); 84 final AppStateInstallAppsBridge appBridge = mock(AppStateInstallAppsBridge.class); 85 ReflectionHelpers.setField(mFragment, "mAppBridge", appBridge); 86 when(appBridge.createInstallAppsStateFor(nullable(String.class), anyInt())) 87 .thenReturn(mock(InstallAppsState.class)); 88 89 mFragment.refreshUi(); 90 91 assertThat(mFragment.refreshUi()).isTrue(); 92 } 93 } 94