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 package com.android.emergency.preferences; 17 18 import static com.google.common.truth.Truth.assertThat; 19 import static org.mockito.Mockito.anyString; 20 import static org.mockito.Mockito.eq; 21 import static org.mockito.Mockito.spy; 22 import static org.mockito.Mockito.when; 23 24 import android.content.Context; 25 import android.content.SharedPreferences; 26 import android.support.v7.preference.PreferenceGroup; 27 import android.support.v7.preference.PreferenceManager; 28 import android.support.v7.preference.PreferenceScreen; 29 import android.test.suitebuilder.annotation.SmallTest; 30 import android.widget.AutoCompleteTextView; 31 32 import com.android.emergency.PreferenceKeys; 33 import com.android.emergency.TestConfig; 34 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.RobolectricTestRunner; 41 import org.robolectric.RuntimeEnvironment; 42 import org.robolectric.annotation.Config; 43 44 /** Unit tests for {@link NameAutoCompletePreference}. */ 45 @SmallTest 46 @RunWith(RobolectricTestRunner.class) 47 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 48 public class NameAutoCompletePreferenceTest { 49 @Mock private PreferenceManager mPreferenceManager; 50 @Mock private SharedPreferences mSharedPreferences; 51 @Mock private NameAutoCompletePreference.SuggestionProvider mAutoCompleteSuggestionProvider; 52 private NameAutoCompletePreference mPreference; 53 54 @Before setUp()55 public void setUp() { 56 MockitoAnnotations.initMocks(this); 57 58 when(mPreferenceManager.getSharedPreferences()).thenReturn(mSharedPreferences); 59 60 Context context = RuntimeEnvironment.application; 61 62 mPreference = 63 spy(new NameAutoCompletePreference(context, null, mAutoCompleteSuggestionProvider)); 64 65 PreferenceGroup prefRoot = spy(new PreferenceScreen(context, null)); 66 when(prefRoot.getPreferenceManager()).thenReturn(mPreferenceManager); 67 prefRoot.addPreference(mPreference); 68 } 69 70 @Test testProperties()71 public void testProperties() { 72 assertThat(mPreference).isNotNull(); 73 assertThat(mPreference.isEnabled()).isTrue(); 74 assertThat(mPreference.isPersistent()).isTrue(); 75 assertThat(mPreference.isSelectable()).isTrue(); 76 assertThat(mPreference.isNotSet()).isTrue(); 77 } 78 79 @Test testReloadFromPreference()80 public void testReloadFromPreference() throws Throwable { 81 mPreference.setKey(PreferenceKeys.KEY_NAME); 82 83 String name = "John"; 84 when(mSharedPreferences.getString(eq(mPreference.getKey()), anyString())).thenReturn(name); 85 86 mPreference.reloadFromPreference(); 87 assertThat(mPreference.getText()).isEqualTo(name); 88 assertThat(mPreference.isNotSet()).isFalse(); 89 } 90 91 @Test testSetText()92 public void testSetText() throws Throwable { 93 final String name = "John"; 94 mPreference.setText(name); 95 assertThat(mPreference.getText()).isEqualTo(name); 96 assertThat(mPreference.getSummary()).isEqualTo(name); 97 } 98 99 @Test testGetAutoCompleteTextView()100 public void testGetAutoCompleteTextView() { 101 AutoCompleteTextView autoCompleteTextView = mPreference.getAutoCompleteTextView(); 102 assertThat(autoCompleteTextView).isNotNull(); 103 } 104 105 @Test testCreateAutocompleteSuggestions_noNameToSuggest()106 public void testCreateAutocompleteSuggestions_noNameToSuggest() { 107 when(mAutoCompleteSuggestionProvider.hasNameToSuggest()).thenReturn(false); 108 assertThat(mPreference.createAutocompleteSuggestions()).isEqualTo(new String[] {}); 109 } 110 111 @Test testCreateAutocompleteSuggestions_nameToSuggest()112 public void testCreateAutocompleteSuggestions_nameToSuggest() { 113 final String name = "Jane"; 114 when(mAutoCompleteSuggestionProvider.hasNameToSuggest()).thenReturn(true); 115 when(mAutoCompleteSuggestionProvider.getNameSuggestion()).thenReturn(name); 116 assertThat(mPreference.createAutocompleteSuggestions()).isEqualTo(new String[] {name}); 117 } 118 } 119