• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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.spy;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.when;
25 
26 import android.app.admin.DevicePolicyManager;
27 import android.content.ComponentName;
28 import android.content.Context;
29 import android.content.pm.PackageManager;
30 import android.view.inputmethod.InputMethodInfo;
31 import android.view.inputmethod.InputMethodManager;
32 
33 import androidx.core.text.BidiFormatter;
34 import androidx.preference.Preference;
35 
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.robolectric.RobolectricTestRunner;
42 import org.robolectric.RuntimeEnvironment;
43 import org.robolectric.annotation.Config;
44 
45 import java.util.ArrayList;
46 import java.util.List;
47 
48 @RunWith(RobolectricTestRunner.class)
49 public class VirtualKeyboardPreferenceControllerTest {
50 
51     @Mock
52     private Context mContext;
53     @Mock
54     private InputMethodManager mImm;
55     @Mock
56     private DevicePolicyManager mDpm;
57     @Mock
58     private PackageManager mPm;
59     @Mock
60     private Preference mPreference;
61 
62     private VirtualKeyboardPreferenceController mController;
63 
64     @Before
setUp()65     public void setUp() {
66         MockitoAnnotations.initMocks(this);
67         when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE)).thenReturn(mDpm);
68         when(mContext.getSystemService(Context.INPUT_METHOD_SERVICE)).thenReturn(mImm);
69         when(mContext.getPackageManager()).thenReturn(mPm);
70         mController = new VirtualKeyboardPreferenceController(mContext);
71     }
72 
73     @Test
testVirtualKeyboard_byDefault_shouldBeShown()74     public void testVirtualKeyboard_byDefault_shouldBeShown() {
75         final Context context = spy(RuntimeEnvironment.application);
76         mController = new VirtualKeyboardPreferenceController(context);
77         assertThat(mController.isAvailable()).isTrue();
78     }
79 
80     @Test
81     @Config(qualifiers = "mcc999")
testVirtualKeyboard_ifDisabled_shouldNotBeShown()82     public void testVirtualKeyboard_ifDisabled_shouldNotBeShown() {
83         final Context context = spy(RuntimeEnvironment.application);
84         mController = new VirtualKeyboardPreferenceController(context);
85         assertThat(mController.isAvailable()).isFalse();
86     }
87 
88     @Test
updateState_noEnabledIMEs_setEmptySummary()89     public void updateState_noEnabledIMEs_setEmptySummary() {
90         mController.updateState(mPreference);
91 
92         verify(mPreference).setSummary(com.android.settingslib.R.string.summary_empty);
93     }
94 
95     @Test
updateState_singleIme_setImeLabelToSummary()96     public void updateState_singleIme_setImeLabelToSummary() {
97         when(mDpm.getPermittedInputMethodsForCurrentUser()).thenReturn(null);
98         final ComponentName componentName = new ComponentName("pkg", "cls");
99         final List<InputMethodInfo> imis = new ArrayList<>();
100         imis.add(mock(InputMethodInfo.class));
101         when(imis.get(0).getPackageName()).thenReturn(componentName.getPackageName());
102         when(mImm.getEnabledInputMethodList()).thenReturn(imis);
103         when(imis.get(0).loadLabel(mPm)).thenReturn("label");
104 
105         mController.updateState(mPreference);
106 
107         verify(mPreference).setSummary("label");
108     }
109 
110     @Test
updateState_multiImeWithMixedLocale_setImeLabelToSummary()111     public void updateState_multiImeWithMixedLocale_setImeLabelToSummary() {
112         final BidiFormatter formatter = BidiFormatter.getInstance();
113         final ComponentName componentName = new ComponentName("pkg", "cls");
114         final List<InputMethodInfo> imis = new ArrayList<>();
115         final String label1 = "label";
116         final String label2 = "Keyboard מִקְלֶדֶת";
117         imis.add(mock(InputMethodInfo.class));
118         imis.add(mock(InputMethodInfo.class));
119 
120         when(mDpm.getPermittedInputMethodsForCurrentUser()).thenReturn(null);
121         when(mImm.getEnabledInputMethodList()).thenReturn(imis);
122         when(imis.get(0).getPackageName()).thenReturn(componentName.getPackageName());
123         when(imis.get(0).loadLabel(mPm)).thenReturn(label1);
124         when(imis.get(1).getPackageName()).thenReturn(componentName.getPackageName());
125         when(imis.get(1).loadLabel(mPm)).thenReturn(label2);
126 
127         mController.updateState(mPreference);
128 
129         verify(mPreference).setSummary(formatter.unicodeWrap(label1) + " and " + formatter.unicodeWrap(label2));
130     }
131 }
132