1 /* 2 * Copyright (C) 2016 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 package com.android.settings.accounts; 17 18 import static com.google.common.truth.Truth.assertThat; 19 20 import static org.mockito.Answers.RETURNS_DEEP_STUBS; 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.ArgumentMatchers.anyInt; 23 import static org.mockito.Mockito.mock; 24 import static org.mockito.Mockito.never; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 import android.app.Activity; 29 import android.content.Context; 30 import android.content.Intent; 31 import android.content.pm.ResolveInfo; 32 import android.content.pm.UserInfo; 33 import android.os.UserManager; 34 35 import androidx.preference.Preference; 36 import androidx.preference.PreferenceScreen; 37 38 import com.android.settings.R; 39 import com.android.settings.testutils.shadow.ShadowAccountManager; 40 import com.android.settings.testutils.shadow.ShadowContentResolver; 41 import com.android.settingslib.search.SearchIndexableRaw; 42 43 import org.junit.After; 44 import org.junit.Before; 45 import org.junit.Test; 46 import org.junit.runner.RunWith; 47 import org.mockito.Mock; 48 import org.mockito.MockitoAnnotations; 49 import org.robolectric.Robolectric; 50 import org.robolectric.RobolectricTestRunner; 51 import org.robolectric.annotation.Config; 52 import org.robolectric.shadows.ShadowApplication; 53 54 import java.util.ArrayList; 55 import java.util.List; 56 57 @RunWith(RobolectricTestRunner.class) 58 public class EmergencyInfoPreferenceControllerTest { 59 60 @Mock(answer = RETURNS_DEEP_STUBS) 61 private Context mContext; 62 @Mock(answer = RETURNS_DEEP_STUBS) 63 private PreferenceScreen mScreen; 64 @Mock(answer = RETURNS_DEEP_STUBS) 65 private UserManager mUserManager; 66 67 private EmergencyInfoPreferenceController mController; 68 private Preference mPreference; 69 70 @Before setUp()71 public void setUp() { 72 MockitoAnnotations.initMocks(this); 73 mController = new EmergencyInfoPreferenceController(mContext, "test_key"); 74 mPreference = new Preference(Robolectric.setupActivity(Activity.class)); 75 mPreference.setKey(mController.getPreferenceKey()); 76 when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference); 77 when(mContext.getResources().getBoolean(R.bool.config_show_emergency_info_in_device_info)) 78 .thenReturn(true); 79 } 80 81 @After tearDown()82 public void tearDown() { 83 ShadowContentResolver.reset(); 84 } 85 86 @Test updateRawDataToIndex_prefUnavailable_shouldNotUpdate()87 public void updateRawDataToIndex_prefUnavailable_shouldNotUpdate() { 88 final List<SearchIndexableRaw> data = new ArrayList<>(); 89 when(mContext.getPackageManager().queryIntentActivities( 90 any(Intent.class), anyInt())) 91 .thenReturn(null); 92 93 mController.updateRawDataToIndex(data); 94 95 assertThat(data).isEmpty(); 96 } 97 98 @Test updateRawDataToIndex_prefAvailable_shouldUpdate()99 public void updateRawDataToIndex_prefAvailable_shouldUpdate() { 100 final List<SearchIndexableRaw> data = new ArrayList<>(); 101 final List<ResolveInfo> infos = new ArrayList<>(); 102 infos.add(new ResolveInfo()); 103 when(mContext.getPackageManager().queryIntentActivities( 104 any(Intent.class), anyInt())) 105 .thenReturn(infos); 106 107 mController.updateRawDataToIndex(data); 108 109 assertThat(mController.isAvailable()).isTrue(); 110 assertThat(data).isNotEmpty(); 111 } 112 113 @Test displayPref_prefUnAvailable_shouldNotDisplay()114 public void displayPref_prefUnAvailable_shouldNotDisplay() { 115 when(mContext.getPackageManager().queryIntentActivities( 116 any(Intent.class), anyInt())) 117 .thenReturn(null); 118 119 mController.displayPreference(mScreen); 120 121 assertThat(mPreference.isVisible()).isFalse(); 122 } 123 124 @Test displayPref_prefAvailable_shouldDisplay()125 public void displayPref_prefAvailable_shouldDisplay() { 126 final List<ResolveInfo> infos = new ArrayList<>(); 127 infos.add(new ResolveInfo()); 128 when(mContext.getPackageManager().queryIntentActivities( 129 any(Intent.class), anyInt())) 130 .thenReturn(infos); 131 132 mController.displayPreference(mScreen); 133 134 verify(mScreen, never()).removePreference(any(Preference.class)); 135 } 136 137 @Test 138 @Config(shadows = {ShadowAccountManager.class, ShadowContentResolver.class}) updateState_shouldSetSummary()139 public void updateState_shouldSetSummary() { 140 final List<UserInfo> infos = new ArrayList<>(); 141 infos.add(new UserInfo(1, "user 1", UserInfo.FLAG_MANAGED_PROFILE)); 142 when((Object) mContext.getSystemService(UserManager.class)).thenReturn(mUserManager); 143 when(mUserManager.getProfiles(anyInt())).thenReturn(infos); 144 final Preference preference = mock(Preference.class); 145 146 mController.updateState(preference); 147 148 verify(preference).setSummary( 149 mContext.getString(R.string.emergency_info_summary, "user 1")); 150 } 151 152 @Test handlePreferenceTreeClick_shouldStartActivity()153 public void handlePreferenceTreeClick_shouldStartActivity() { 154 final ShadowApplication application = ShadowApplication.getInstance(); 155 final Activity activity = Robolectric.setupActivity(Activity.class); 156 final Preference preference = new Preference(activity); 157 preference.setKey("emergency_info"); 158 mController = new EmergencyInfoPreferenceController(activity, preference.getKey()); 159 mController.mIntent = new Intent("com.example.action.new").setPackage("com.example.test"); 160 161 mController.handlePreferenceTreeClick(preference); 162 163 assertThat(application.getNextStartedActivity().getAction()) 164 .isEqualTo("com.example.action.new"); 165 } 166 } 167