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 20 import static com.google.common.truth.Truth.assertThat; 21 import static org.mockito.Matchers.anyString; 22 import static org.mockito.Matchers.eq; 23 import static org.mockito.Mockito.mock; 24 import static org.mockito.Mockito.verify; 25 import static org.mockito.Mockito.when; 26 27 import android.app.admin.DevicePolicyManager; 28 import android.content.ComponentName; 29 import android.content.Context; 30 import android.content.pm.PackageManager; 31 import android.support.v4.text.BidiFormatter; 32 import android.support.v7.preference.Preference; 33 import android.view.inputmethod.InputMethodInfo; 34 import android.view.inputmethod.InputMethodManager; 35 36 import com.android.settings.R; 37 import com.android.settings.testutils.SettingsRobolectricTestRunner; 38 import com.android.settings.TestConfig; 39 40 import org.junit.Before; 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 import org.mockito.Mock; 44 import org.mockito.MockitoAnnotations; 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(SettingsRobolectricTestRunner.class) 52 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 53 public class VirtualKeyboardPreferenceControllerTest { 54 55 @Mock 56 private Context mContext; 57 @Mock 58 private InputMethodManager mImm; 59 @Mock 60 private DevicePolicyManager mDpm; 61 @Mock 62 private PackageManager mPm; 63 @Mock 64 private Preference mPreference; 65 66 private VirtualKeyboardPreferenceController mController; 67 68 @Before setUp()69 public void setUp() { 70 MockitoAnnotations.initMocks(this); 71 when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE)).thenReturn(mDpm); 72 when(mContext.getSystemService(Context.INPUT_METHOD_SERVICE)).thenReturn(mImm); 73 when(mContext.getPackageManager()).thenReturn(mPm); 74 mController = new VirtualKeyboardPreferenceController(mContext); 75 } 76 77 @Test shouldAlwaysBeAvailable()78 public void shouldAlwaysBeAvailable() { 79 assertThat(mController.isAvailable()).isTrue(); 80 } 81 82 @Test updateState_noEnabledIMEs_setEmptySummary()83 public void updateState_noEnabledIMEs_setEmptySummary() { 84 mController.updateState(mPreference); 85 86 verify(mPreference).setSummary(R.string.summary_empty); 87 } 88 89 @Test updateState_singleIme_setImeLabelToSummary()90 public void updateState_singleIme_setImeLabelToSummary() { 91 when(mDpm.getPermittedInputMethodsForCurrentUser()).thenReturn(null); 92 final ComponentName componentName = new ComponentName("pkg", "cls"); 93 final List<InputMethodInfo> imis = new ArrayList<>(); 94 imis.add(mock(InputMethodInfo.class)); 95 when(imis.get(0).getPackageName()).thenReturn(componentName.getPackageName()); 96 when(mImm.getEnabledInputMethodList()).thenReturn(imis); 97 when(imis.get(0).loadLabel(mPm)).thenReturn("label"); 98 99 mController.updateState(mPreference); 100 101 verify(mPreference).setSummary("label"); 102 } 103 104 @Test updateState_multiImeWithMixedLocale_setImeLabelToSummary()105 public void updateState_multiImeWithMixedLocale_setImeLabelToSummary() { 106 final BidiFormatter formatter = BidiFormatter.getInstance(); 107 final ComponentName componentName = new ComponentName("pkg", "cls"); 108 final List<InputMethodInfo> imis = new ArrayList<>(); 109 final String label1 = "label"; 110 final String label2 = "Keyboard מִקְלֶדֶת"; 111 imis.add(mock(InputMethodInfo.class)); 112 imis.add(mock(InputMethodInfo.class)); 113 114 when(mDpm.getPermittedInputMethodsForCurrentUser()).thenReturn(null); 115 when(mImm.getEnabledInputMethodList()).thenReturn(imis); 116 when(imis.get(0).getPackageName()).thenReturn(componentName.getPackageName()); 117 when(imis.get(0).loadLabel(mPm)).thenReturn(label1); 118 when(imis.get(1).getPackageName()).thenReturn(componentName.getPackageName()); 119 when(imis.get(1).loadLabel(mPm)).thenReturn(label2); 120 when(mContext.getString(eq(R.string.join_many_items_middle), anyString(), anyString())) 121 .thenAnswer(invocation -> { 122 final Object[] args = invocation.getArguments(); 123 final String str1 = (String) args[1]; 124 final String str2 = (String) args[2]; 125 return RuntimeEnvironment.application.getString(R.string.join_many_items_middle, 126 str1, str2); 127 }); 128 129 mController.updateState(mPreference); 130 131 verify(mPreference).setSummary( 132 formatter.unicodeWrap(label1) + ", " + formatter.unicodeWrap(label2)); 133 } 134 } 135