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