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