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