ActionTestsBase.java revision 9342484e8d573a40f470b6a593df31c602fa4076
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.action;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.text.InputType;
22import android.view.inputmethod.EditorInfo;
23import android.view.inputmethod.InputMethodSubtype;
24
25import com.android.inputmethod.keyboard.Key;
26import com.android.inputmethod.keyboard.Keyboard;
27import com.android.inputmethod.keyboard.KeyboardId;
28import com.android.inputmethod.keyboard.KeyboardLayoutSet;
29import com.android.inputmethod.keyboard.KeyboardLayoutSetTestsBase;
30import com.android.inputmethod.keyboard.internal.KeyboardIconsSet;
31import com.android.inputmethod.keyboard.layout.expected.ExpectedKeyVisual;
32import com.android.inputmethod.latin.common.Constants;
33import com.android.inputmethod.latin.utils.LocaleUtils;
34import com.android.inputmethod.latin.utils.RunInLocale;
35import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
36
37import java.util.Locale;
38
39abstract class ActionTestsBase extends KeyboardLayoutSetTestsBase {
40    static class ExpectedActionKey {
41        static ExpectedActionKey newIconKey(final String iconName) {
42            final int iconId = KeyboardIconsSet.getIconId(iconName);
43            return new ExpectedActionKey(ExpectedKeyVisual.newInstance(iconId));
44        }
45
46        static ExpectedActionKey newLabelKey(final String label) {
47            return new ExpectedActionKey(ExpectedKeyVisual.newInstance(label));
48        }
49
50        static ExpectedActionKey newLabelKey(final int labelResId,
51                final Locale labelLocale, final Context context) {
52            final RunInLocale<String> getString = new RunInLocale<String>() {
53                @Override
54                protected String job(final Resources res) {
55                    return res.getString(labelResId);
56                }
57            };
58            return newLabelKey(getString.runInLocale(context.getResources(), labelLocale));
59        }
60
61        private final ExpectedKeyVisual mVisual;
62
63        private ExpectedActionKey(final ExpectedKeyVisual visual) {
64            mVisual = visual;
65        }
66
67        public int getIconId() { return mVisual.getIconId(); }
68
69        public String getLabel() { return mVisual.getLabel(); }
70    }
71
72    protected static Locale getLabelLocale(final InputMethodSubtype subtype) {
73        final String localeString = subtype.getLocale();
74        if (localeString.equals(SubtypeLocaleUtils.NO_LANGUAGE)) {
75            return null;
76        }
77        return LocaleUtils.constructLocaleFromString(localeString);
78    }
79
80    private static void assertActionKey(final String tag, final KeyboardLayoutSet layoutSet,
81            final int elementId, final ExpectedActionKey expectedKey) {
82        final Keyboard keyboard = layoutSet.getKeyboard(elementId);
83        final Key actualKey = keyboard.getKey(Constants.CODE_ENTER);
84        assertNotNull(tag + " enter key on " + keyboard.mId, actualKey);
85        assertEquals(tag + " label " + expectedKey, expectedKey.getLabel(), actualKey.getLabel());
86        assertEquals(tag + " icon " + expectedKey, expectedKey.getIconId(), actualKey.getIconId());
87    }
88
89    protected void doTestActionKey(final String tag, final InputMethodSubtype subtype,
90            final int actionId, final ExpectedActionKey expectedKey) {
91        final EditorInfo editorInfo = new EditorInfo();
92        editorInfo.imeOptions = actionId;
93        doTestActionKey(tag, subtype, editorInfo, expectedKey);
94    }
95
96    protected void doTestActionKey(final String tag, final InputMethodSubtype subtype,
97            final EditorInfo editorInfo, final ExpectedActionKey expectedKey) {
98        // Test text layouts.
99        editorInfo.inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL;
100        final KeyboardLayoutSet layoutSet = createKeyboardLayoutSet(subtype, editorInfo);
101        assertActionKey(tag, layoutSet, KeyboardId.ELEMENT_ALPHABET, expectedKey);
102        assertActionKey(tag, layoutSet, KeyboardId.ELEMENT_SYMBOLS, expectedKey);
103        assertActionKey(tag, layoutSet, KeyboardId.ELEMENT_SYMBOLS_SHIFTED, expectedKey);
104        // Test phone number layouts.
105        assertActionKey(tag, layoutSet, KeyboardId.ELEMENT_PHONE, expectedKey);
106        assertActionKey(tag, layoutSet, KeyboardId.ELEMENT_PHONE_SYMBOLS, expectedKey);
107        // Test normal number layout.
108        assertActionKey(tag, layoutSet, KeyboardId.ELEMENT_NUMBER, expectedKey);
109        // Test number password layout.
110        editorInfo.inputType =
111                InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD;
112        final KeyboardLayoutSet passwordSet = createKeyboardLayoutSet(subtype, editorInfo);
113        assertActionKey(tag, passwordSet, KeyboardId.ELEMENT_NUMBER, expectedKey);
114    }
115}
116