• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.action;
18 
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.text.InputType;
22 import android.view.inputmethod.EditorInfo;
23 import android.view.inputmethod.InputMethodSubtype;
24 
25 import com.android.inputmethod.keyboard.Key;
26 import com.android.inputmethod.keyboard.Keyboard;
27 import com.android.inputmethod.keyboard.KeyboardId;
28 import com.android.inputmethod.keyboard.KeyboardLayoutSet;
29 import com.android.inputmethod.keyboard.KeyboardLayoutSetTestsBase;
30 import com.android.inputmethod.keyboard.internal.KeyboardIconsSet;
31 import com.android.inputmethod.keyboard.layout.expected.ExpectedKeyVisual;
32 import com.android.inputmethod.latin.common.Constants;
33 import com.android.inputmethod.latin.common.LocaleUtils;
34 import com.android.inputmethod.latin.utils.RunInLocale;
35 import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
36 
37 import java.util.Locale;
38 
39 abstract class ActionTestsBase extends KeyboardLayoutSetTestsBase {
40     static class ExpectedActionKey {
newIconKey(final String iconName)41         static ExpectedActionKey newIconKey(final String iconName) {
42             final int iconId = KeyboardIconsSet.getIconId(iconName);
43             return new ExpectedActionKey(ExpectedKeyVisual.newInstance(iconId));
44         }
45 
newLabelKey(final String label)46         static ExpectedActionKey newLabelKey(final String label) {
47             return new ExpectedActionKey(ExpectedKeyVisual.newInstance(label));
48         }
49 
newLabelKey(final int labelResId, final Locale labelLocale, final Context context)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 
ExpectedActionKey(final ExpectedKeyVisual visual)63         private ExpectedActionKey(final ExpectedKeyVisual visual) {
64             mVisual = visual;
65         }
66 
getIconId()67         public int getIconId() { return mVisual.getIconId(); }
68 
getLabel()69         public String getLabel() { return mVisual.getLabel(); }
70     }
71 
getLabelLocale(final InputMethodSubtype subtype)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 
assertActionKey(final String tag, final KeyboardLayoutSet layoutSet, final int elementId, final ExpectedActionKey expectedKey)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 
doTestActionKey(final String tag, final InputMethodSubtype subtype, final int actionId, final ExpectedActionKey expectedKey)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 
doTestActionKey(final String tag, final InputMethodSubtype subtype, final EditorInfo editorInfo, final ExpectedActionKey expectedKey)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