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