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