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