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