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