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