2014-02-17 06:37:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.android.inputmethod.keyboard;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.Resources;
|
|
|
|
import android.test.AndroidTestCase;
|
|
|
|
import android.view.ContextThemeWrapper;
|
|
|
|
import android.view.inputmethod.EditorInfo;
|
|
|
|
import android.view.inputmethod.InputMethodInfo;
|
|
|
|
import android.view.inputmethod.InputMethodSubtype;
|
|
|
|
|
|
|
|
import com.android.inputmethod.compat.InputMethodSubtypeCompatUtils;
|
|
|
|
import com.android.inputmethod.keyboard.KeyboardLayoutSet.Builder;
|
|
|
|
import com.android.inputmethod.latin.R;
|
|
|
|
import com.android.inputmethod.latin.RichInputMethodManager;
|
2014-08-25 04:42:15 +00:00
|
|
|
import com.android.inputmethod.latin.RichInputMethodSubtype;
|
2014-10-23 09:37:32 +00:00
|
|
|
import com.android.inputmethod.latin.common.Constants;
|
2014-09-25 06:53:41 +00:00
|
|
|
import com.android.inputmethod.latin.settings.Settings;
|
2014-02-17 06:37:43 +00:00
|
|
|
import com.android.inputmethod.latin.utils.AdditionalSubtypeUtils;
|
|
|
|
import com.android.inputmethod.latin.utils.ResourceUtils;
|
|
|
|
import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Locale;
|
|
|
|
|
2014-06-06 03:11:35 +00:00
|
|
|
public abstract class KeyboardLayoutSetTestsBase extends AndroidTestCase {
|
2014-02-17 06:37:43 +00:00
|
|
|
// All input method subtypes of LatinIME.
|
2014-05-23 11:18:17 +00:00
|
|
|
private final ArrayList<InputMethodSubtype> mAllSubtypesList = new ArrayList<>();
|
2014-02-17 06:37:43 +00:00
|
|
|
|
2014-09-25 06:53:41 +00:00
|
|
|
public interface SubtypeFilter {
|
|
|
|
public boolean accept(final InputMethodSubtype subtype);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final SubtypeFilter FILTER_IS_ASCII_CAPABLE = new SubtypeFilter() {
|
|
|
|
@Override
|
|
|
|
public boolean accept(InputMethodSubtype subtype) {
|
|
|
|
return InputMethodSubtypeCompatUtils.isAsciiCapable(subtype);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public static final SubtypeFilter FILTER_IS_ADDITIONAL_SUBTYPE = new SubtypeFilter() {
|
|
|
|
@Override
|
|
|
|
public boolean accept(InputMethodSubtype subtype) {
|
|
|
|
return AdditionalSubtypeUtils.isAdditionalSubtype(subtype);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-10-06 03:18:45 +00:00
|
|
|
private RichInputMethodManager mRichImm;
|
|
|
|
private InputMethodSubtype[] mSavedAdditionalSubtypes;
|
2014-02-17 06:37:43 +00:00
|
|
|
private int mScreenMetrics;
|
|
|
|
|
2014-06-06 03:11:35 +00:00
|
|
|
protected abstract int getKeyboardThemeForTests();
|
|
|
|
|
2014-02-17 06:37:43 +00:00
|
|
|
@Override
|
|
|
|
protected void setUp() throws Exception {
|
|
|
|
super.setUp();
|
2014-09-25 06:53:41 +00:00
|
|
|
final Context context = getContext();
|
|
|
|
final Resources res = context.getResources();
|
2014-10-06 03:18:45 +00:00
|
|
|
RichInputMethodManager.init(context);
|
|
|
|
mRichImm = RichInputMethodManager.getInstance();
|
2014-09-25 06:53:41 +00:00
|
|
|
|
2014-10-06 03:18:45 +00:00
|
|
|
// Save and reset additional subtypes preference.
|
2014-11-21 03:49:04 +00:00
|
|
|
mSavedAdditionalSubtypes = mRichImm.getAdditionalSubtypes();
|
2014-10-06 03:18:45 +00:00
|
|
|
final InputMethodSubtype[] predefinedAdditionalSubtypes =
|
|
|
|
AdditionalSubtypeUtils.createAdditionalSubtypesArray(
|
|
|
|
AdditionalSubtypeUtils.createPrefSubtypes(
|
|
|
|
res.getStringArray(R.array.predefined_subtypes)));
|
|
|
|
mRichImm.setAdditionalInputMethodSubtypes(predefinedAdditionalSubtypes);
|
2014-09-25 06:53:41 +00:00
|
|
|
|
2014-06-06 03:11:35 +00:00
|
|
|
final KeyboardTheme keyboardTheme = KeyboardTheme.searchKeyboardThemeById(
|
2014-09-17 11:51:09 +00:00
|
|
|
getKeyboardThemeForTests(), KeyboardTheme.KEYBOARD_THEMES);
|
2014-08-27 17:23:31 +00:00
|
|
|
setContext(new ContextThemeWrapper(getContext(), keyboardTheme.mStyleId));
|
2014-09-02 06:55:44 +00:00
|
|
|
KeyboardLayoutSet.onKeyboardThemeChanged();
|
2014-08-27 17:23:31 +00:00
|
|
|
|
2014-10-28 05:40:06 +00:00
|
|
|
mScreenMetrics = Settings.readScreenMetrics(res);
|
2014-02-17 06:37:43 +00:00
|
|
|
|
2014-10-06 03:18:45 +00:00
|
|
|
final InputMethodInfo imi = mRichImm.getInputMethodInfoOfThisIme();
|
2014-02-17 06:37:43 +00:00
|
|
|
final int subtypeCount = imi.getSubtypeCount();
|
|
|
|
for (int index = 0; index < subtypeCount; index++) {
|
2014-09-25 06:53:41 +00:00
|
|
|
mAllSubtypesList.add(imi.getSubtypeAt(index));
|
2014-02-17 06:37:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-25 06:53:41 +00:00
|
|
|
@Override
|
|
|
|
protected void tearDown() throws Exception {
|
|
|
|
// Restore additional subtypes preference.
|
2014-10-06 03:18:45 +00:00
|
|
|
mRichImm.setAdditionalInputMethodSubtypes(mSavedAdditionalSubtypes);
|
2014-09-25 06:53:41 +00:00
|
|
|
super.tearDown();
|
2014-02-28 04:15:27 +00:00
|
|
|
}
|
|
|
|
|
2014-09-25 06:53:41 +00:00
|
|
|
protected final ArrayList<InputMethodSubtype> getAllSubtypesList() {
|
|
|
|
return mAllSubtypesList;
|
2014-02-17 06:37:43 +00:00
|
|
|
}
|
|
|
|
|
2014-09-25 06:53:41 +00:00
|
|
|
protected final ArrayList<InputMethodSubtype> getSubtypesFilteredBy(
|
|
|
|
final SubtypeFilter filter) {
|
|
|
|
final ArrayList<InputMethodSubtype> list = new ArrayList<>();
|
|
|
|
for (final InputMethodSubtype subtype : mAllSubtypesList) {
|
|
|
|
if (filter.accept(subtype)) {
|
|
|
|
list.add(subtype);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
2014-02-17 06:37:43 +00:00
|
|
|
}
|
|
|
|
|
2014-03-04 06:38:33 +00:00
|
|
|
protected final boolean isPhone() {
|
2014-10-28 05:40:06 +00:00
|
|
|
return Constants.isPhone(mScreenMetrics);
|
2014-03-03 08:12:12 +00:00
|
|
|
}
|
|
|
|
|
2014-02-17 06:37:43 +00:00
|
|
|
protected final InputMethodSubtype getSubtype(final Locale locale,
|
|
|
|
final String keyboardLayout) {
|
|
|
|
for (final InputMethodSubtype subtype : mAllSubtypesList) {
|
|
|
|
final Locale subtypeLocale = SubtypeLocaleUtils.getSubtypeLocale(subtype);
|
|
|
|
final String subtypeLayout = SubtypeLocaleUtils.getKeyboardLayoutSetName(subtype);
|
|
|
|
if (locale.equals(subtypeLocale) && keyboardLayout.equals(subtypeLayout)) {
|
|
|
|
// Found subtype that matches locale and keyboard layout.
|
|
|
|
return subtype;
|
|
|
|
}
|
|
|
|
}
|
2014-09-25 06:53:41 +00:00
|
|
|
for (final InputMethodSubtype subtype : getSubtypesFilteredBy(FILTER_IS_ASCII_CAPABLE)) {
|
2014-02-17 06:37:43 +00:00
|
|
|
final Locale subtypeLocale = SubtypeLocaleUtils.getSubtypeLocale(subtype);
|
|
|
|
if (locale.equals(subtypeLocale)) {
|
|
|
|
// Create additional subtype.
|
2014-07-02 04:29:36 +00:00
|
|
|
return AdditionalSubtypeUtils.createAsciiEmojiCapableAdditionalSubtype(
|
|
|
|
locale.toString(), keyboardLayout);
|
2014-02-17 06:37:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new RuntimeException(
|
|
|
|
"Unknown subtype: locale=" + locale + " keyboardLayout=" + keyboardLayout);
|
|
|
|
}
|
|
|
|
|
2014-08-23 10:08:40 +00:00
|
|
|
protected KeyboardLayoutSet createKeyboardLayoutSet(final InputMethodSubtype subtype,
|
2014-02-17 06:37:43 +00:00
|
|
|
final EditorInfo editorInfo) {
|
2014-06-05 16:41:42 +00:00
|
|
|
return createKeyboardLayoutSet(subtype, editorInfo, false /* voiceInputKeyEnabled */,
|
2014-09-10 02:53:26 +00:00
|
|
|
false /* languageSwitchKeyEnabled */, false /* splitLayoutEnabled */);
|
2014-02-17 06:37:43 +00:00
|
|
|
}
|
|
|
|
|
2014-08-23 10:08:40 +00:00
|
|
|
protected KeyboardLayoutSet createKeyboardLayoutSet(final InputMethodSubtype subtype,
|
2014-06-05 16:41:42 +00:00
|
|
|
final EditorInfo editorInfo, final boolean voiceInputKeyEnabled,
|
2014-09-10 02:53:26 +00:00
|
|
|
final boolean languageSwitchKeyEnabled, final boolean splitLayoutEnabled) {
|
2014-08-27 17:23:31 +00:00
|
|
|
final Context context = getContext();
|
2014-02-17 06:37:43 +00:00
|
|
|
final Resources res = context.getResources();
|
2014-12-09 08:53:24 +00:00
|
|
|
final int keyboardWidth = ResourceUtils.getDefaultKeyboardWidth(res);
|
2014-02-17 06:37:43 +00:00
|
|
|
final int keyboardHeight = ResourceUtils.getDefaultKeyboardHeight(res);
|
|
|
|
final Builder builder = new Builder(context, editorInfo);
|
|
|
|
builder.setKeyboardGeometry(keyboardWidth, keyboardHeight)
|
2014-08-25 04:42:15 +00:00
|
|
|
.setSubtype(new RichInputMethodSubtype(subtype))
|
2014-06-05 16:41:42 +00:00
|
|
|
.setVoiceInputKeyEnabled(voiceInputKeyEnabled)
|
2014-09-10 02:53:26 +00:00
|
|
|
.setLanguageSwitchKeyEnabled(languageSwitchKeyEnabled)
|
|
|
|
.setSplitLayoutEnabledByUser(splitLayoutEnabled);
|
2014-02-17 06:37:43 +00:00
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
}
|