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 17 package com.android.settings.inputmethod; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.mockito.Mockito.mock; 21 import static org.mockito.Mockito.when; 22 23 import android.content.Context; 24 import android.content.res.Resources; 25 import android.support.v7.preference.Preference; 26 import android.view.textservice.SpellCheckerInfo; 27 import android.view.textservice.TextServicesManager; 28 29 import com.android.settings.R; 30 import com.android.settings.testutils.SettingsRobolectricTestRunner; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 import org.mockito.Answers; 36 import org.mockito.Mock; 37 import org.mockito.MockitoAnnotations; 38 import org.robolectric.RuntimeEnvironment; 39 40 @RunWith(SettingsRobolectricTestRunner.class) 41 public class SpellCheckerPreferenceControllerTest { 42 43 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 44 private Context mContext; 45 @Mock 46 private TextServicesManager mTextServicesManager; 47 @Mock 48 private Resources mResources; 49 50 private Context mAppContext; 51 private Preference mPreference; 52 private SpellCheckerPreferenceController mController; 53 54 @Before setUp()55 public void setUp() { 56 MockitoAnnotations.initMocks(this); 57 mAppContext = RuntimeEnvironment.application; 58 when(mContext.getSystemService(Context.TEXT_SERVICES_MANAGER_SERVICE)) 59 .thenReturn(mTextServicesManager); 60 when(mContext.getResources()).thenReturn(mResources); 61 when(mResources.getBoolean(R.bool.config_show_spellcheckers_settings)).thenReturn(true); 62 mPreference = new Preference(mAppContext); 63 mController = new SpellCheckerPreferenceController(mContext); 64 } 65 66 @Test testSpellChecker_byDefault_shouldBeShown()67 public void testSpellChecker_byDefault_shouldBeShown() { 68 assertThat(mController.isAvailable()).isTrue(); 69 } 70 71 @Test testSpellChecker_ifDisabled_shouldNotBeShown()72 public void testSpellChecker_ifDisabled_shouldNotBeShown() { 73 when(mResources.getBoolean(R.bool.config_show_spellcheckers_settings)).thenReturn(false); 74 assertThat(mController.isAvailable()).isFalse(); 75 } 76 77 @Test updateState_NoSpellerChecker_shouldSetSummaryToDefault()78 public void updateState_NoSpellerChecker_shouldSetSummaryToDefault() { 79 when(mTextServicesManager.isSpellCheckerEnabled()).thenReturn(true); 80 when(mTextServicesManager.getCurrentSpellChecker()).thenReturn(null); 81 82 mController.updateState(mPreference); 83 84 assertThat(mPreference.getSummary()) 85 .isEqualTo(mAppContext.getString(R.string.spell_checker_not_selected)); 86 } 87 88 @Test updateState_spellerCheckerDisabled_shouldSetSummaryToDisabledText()89 public void updateState_spellerCheckerDisabled_shouldSetSummaryToDisabledText() { 90 when(mTextServicesManager.isSpellCheckerEnabled()).thenReturn(false); 91 92 mController.updateState(mPreference); 93 assertThat(mPreference.getSummary()) 94 .isEqualTo(mAppContext.getString(R.string.switch_off_text)); 95 } 96 97 @Test updateState_hasSpellerChecker_shouldSetSummaryToAppName()98 public void updateState_hasSpellerChecker_shouldSetSummaryToAppName() { 99 final String spellCheckerAppLabel = "test"; 100 final SpellCheckerInfo sci = mock(SpellCheckerInfo.class); 101 when(mTextServicesManager.isSpellCheckerEnabled()).thenReturn(true); 102 when(mTextServicesManager.getCurrentSpellChecker()).thenReturn(sci); 103 when(sci.loadLabel(mContext.getPackageManager())).thenReturn(spellCheckerAppLabel); 104 105 mController.updateState(mPreference); 106 107 assertThat(mPreference.getSummary()).isEqualTo(spellCheckerAppLabel); 108 } 109 } 110