1 /* 2 * Copyright (C) 2014 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.inputmethod.keyboard; 18 19 import android.content.Context; 20 import android.content.res.Resources; 21 import android.test.AndroidTestCase; 22 import android.view.ContextThemeWrapper; 23 import android.view.inputmethod.EditorInfo; 24 import android.view.inputmethod.InputMethodInfo; 25 import android.view.inputmethod.InputMethodSubtype; 26 27 import com.android.inputmethod.compat.InputMethodSubtypeCompatUtils; 28 import com.android.inputmethod.keyboard.KeyboardLayoutSet.Builder; 29 import com.android.inputmethod.latin.Constants; 30 import com.android.inputmethod.latin.R; 31 import com.android.inputmethod.latin.RichInputMethodManager; 32 import com.android.inputmethod.latin.utils.AdditionalSubtypeUtils; 33 import com.android.inputmethod.latin.utils.ResourceUtils; 34 import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; 35 36 import java.util.ArrayList; 37 import java.util.Locale; 38 39 public abstract class KeyboardLayoutSetTestsBase extends AndroidTestCase { 40 // All input method subtypes of LatinIME. 41 private final ArrayList<InputMethodSubtype> mAllSubtypesList = new ArrayList<>(); 42 private final ArrayList<InputMethodSubtype> mAsciiCapableSubtypesList = new ArrayList<>(); 43 private final ArrayList<InputMethodSubtype> mAdditionalSubtypesList = new ArrayList<>(); 44 45 private int mScreenMetrics; 46 getKeyboardThemeForTests()47 protected abstract int getKeyboardThemeForTests(); 48 49 @Override setUp()50 protected void setUp() throws Exception { 51 super.setUp(); 52 final KeyboardTheme keyboardTheme = KeyboardTheme.searchKeyboardThemeById( 53 getKeyboardThemeForTests()); 54 setContext(new ContextThemeWrapper(getContext(), keyboardTheme.mStyleId)); 55 KeyboardLayoutSet.onKeyboardThemeChanged(); 56 57 final Context context = getContext(); 58 mScreenMetrics = context.getResources().getInteger(R.integer.config_screen_metrics); 59 RichInputMethodManager.init(context); 60 final RichInputMethodManager richImm = RichInputMethodManager.getInstance(); 61 62 final InputMethodInfo imi = richImm.getInputMethodInfoOfThisIme(); 63 final int subtypeCount = imi.getSubtypeCount(); 64 for (int index = 0; index < subtypeCount; index++) { 65 final InputMethodSubtype subtype = imi.getSubtypeAt(index); 66 if (AdditionalSubtypeUtils.isAdditionalSubtype(subtype)) { 67 mAdditionalSubtypesList.add(subtype); 68 continue; 69 } 70 mAllSubtypesList.add(subtype); 71 if (InputMethodSubtypeCompatUtils.isAsciiCapable(subtype)) { 72 mAsciiCapableSubtypesList.add(subtype); 73 } 74 } 75 } 76 getAllSubtypesList()77 protected final ArrayList<InputMethodSubtype> getAllSubtypesList() { 78 return mAllSubtypesList; 79 } 80 getAsciiCapableSubtypesList()81 protected final ArrayList<InputMethodSubtype> getAsciiCapableSubtypesList() { 82 return mAsciiCapableSubtypesList; 83 } 84 getAdditionalSubtypesList()85 protected final ArrayList<InputMethodSubtype> getAdditionalSubtypesList() { 86 return mAdditionalSubtypesList; 87 } 88 isPhone()89 protected final boolean isPhone() { 90 return mScreenMetrics == Constants.SCREEN_METRICS_SMALL_PHONE 91 || mScreenMetrics == Constants.SCREEN_METRICS_LARGE_PHONE; 92 } 93 getSubtype(final Locale locale, final String keyboardLayout)94 protected final InputMethodSubtype getSubtype(final Locale locale, 95 final String keyboardLayout) { 96 for (final InputMethodSubtype subtype : mAllSubtypesList) { 97 final Locale subtypeLocale = SubtypeLocaleUtils.getSubtypeLocale(subtype); 98 final String subtypeLayout = SubtypeLocaleUtils.getKeyboardLayoutSetName(subtype); 99 if (locale.equals(subtypeLocale) && keyboardLayout.equals(subtypeLayout)) { 100 // Found subtype that matches locale and keyboard layout. 101 return subtype; 102 } 103 } 104 for (final InputMethodSubtype subtype : mAsciiCapableSubtypesList) { 105 final Locale subtypeLocale = SubtypeLocaleUtils.getSubtypeLocale(subtype); 106 if (locale.equals(subtypeLocale)) { 107 // Create additional subtype. 108 return AdditionalSubtypeUtils.createAsciiEmojiCapableAdditionalSubtype( 109 locale.toString(), keyboardLayout); 110 } 111 } 112 throw new RuntimeException( 113 "Unknown subtype: locale=" + locale + " keyboardLayout=" + keyboardLayout); 114 } 115 createKeyboardLayoutSet(final InputMethodSubtype subtype, final EditorInfo editorInfo)116 protected KeyboardLayoutSet createKeyboardLayoutSet(final InputMethodSubtype subtype, 117 final EditorInfo editorInfo) { 118 return createKeyboardLayoutSet(subtype, editorInfo, false /* voiceInputKeyEnabled */, 119 false /* languageSwitchKeyEnabled */); 120 } 121 createKeyboardLayoutSet(final InputMethodSubtype subtype, final EditorInfo editorInfo, final boolean voiceInputKeyEnabled, final boolean languageSwitchKeyEnabled)122 protected KeyboardLayoutSet createKeyboardLayoutSet(final InputMethodSubtype subtype, 123 final EditorInfo editorInfo, final boolean voiceInputKeyEnabled, 124 final boolean languageSwitchKeyEnabled) { 125 final Context context = getContext(); 126 final Resources res = context.getResources(); 127 final int keyboardWidth = ResourceUtils.getDefaultKeyboardWidth(res); 128 final int keyboardHeight = ResourceUtils.getDefaultKeyboardHeight(res); 129 final Builder builder = new Builder(context, editorInfo); 130 builder.setKeyboardGeometry(keyboardWidth, keyboardHeight) 131 .setSubtype(subtype) 132 .setVoiceInputKeyEnabled(voiceInputKeyEnabled) 133 .setLanguageSwitchKeyEnabled(languageSwitchKeyEnabled); 134 return builder.build(); 135 } 136 } 137