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