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;
18 
19 import android.content.res.Resources;
20 import android.text.InputType;
21 import android.view.inputmethod.EditorInfo;
22 import android.view.inputmethod.InputMethodSubtype;
23 
24 import com.android.inputmethod.keyboard.internal.KeyboardIconsSet;
25 import com.android.inputmethod.latin.Constants;
26 import com.android.inputmethod.latin.utils.RunInLocale;
27 import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
28 
29 import java.util.Locale;
30 
31 abstract class KeyboardLayoutSetActionLabelBase extends KeyboardLayoutSetTestsBase {
testActionUnspecified()32     public void testActionUnspecified() {
33         for (final InputMethodSubtype subtype : getAllSubtypesList()) {
34             final String tag = "unspecifiled "
35                     + SubtypeLocaleUtils.getSubtypeNameForLogging(subtype);
36             doTestActionKeyIcon(tag, subtype, EditorInfo.IME_ACTION_UNSPECIFIED,
37                     KeyboardIconsSet.NAME_ENTER_KEY);
38         }
39     }
40 
testActionNone()41     public void testActionNone() {
42         for (final InputMethodSubtype subtype : getAllSubtypesList()) {
43             final String tag = "none " + SubtypeLocaleUtils.getSubtypeNameForLogging(subtype);
44             doTestActionKeyIcon(tag, subtype, EditorInfo.IME_ACTION_NONE,
45                     KeyboardIconsSet.NAME_ENTER_KEY);
46         }
47     }
48 
testActionSearch()49     public void testActionSearch() {
50         for (final InputMethodSubtype subtype : getAllSubtypesList()) {
51             final String tag = "search " + SubtypeLocaleUtils.getSubtypeNameForLogging(subtype);
52             doTestActionKeyIcon(tag, subtype, EditorInfo.IME_ACTION_SEARCH,
53                     KeyboardIconsSet.NAME_SEARCH_KEY);
54         }
55     }
56 
testActionGo()57     public abstract void testActionGo();
testActionSend()58     public abstract void testActionSend();
testActionNext()59     public abstract void testActionNext();
testActionDone()60     public abstract void testActionDone();
testActionPrevious()61     public abstract void testActionPrevious();
62 
testActionCustom()63     public void testActionCustom() {
64         for (final InputMethodSubtype subtype : getAllSubtypesList()) {
65             final String tag = "custom " + SubtypeLocaleUtils.getSubtypeNameForLogging(subtype);
66             final CharSequence customLabel = "customLabel";
67             final EditorInfo editorInfo = new EditorInfo();
68             editorInfo.imeOptions = EditorInfo.IME_ACTION_UNSPECIFIED;
69             editorInfo.actionLabel = customLabel;
70             doTestActionKeyLabel(tag, subtype, editorInfo, customLabel);
71         }
72     }
73 
doTestActionKey(final String tag, final KeyboardLayoutSet layoutSet, final int elementId, final CharSequence label, final int iconId)74     private static void doTestActionKey(final String tag, final KeyboardLayoutSet layoutSet,
75             final int elementId, final CharSequence label, final int iconId) {
76         final Keyboard keyboard = layoutSet.getKeyboard(elementId);
77         final Key enterKey = keyboard.getKey(Constants.CODE_ENTER);
78         assertNotNull(tag + " enter key on " + keyboard.mId, enterKey);
79         assertEquals(tag + " enter label " + enterKey, label, enterKey.getLabel());
80         assertEquals(tag + " enter icon " + enterKey, iconId, enterKey.getIconId());
81     }
82 
doTestActionKeyLabelResId(final String tag, final InputMethodSubtype subtype, final int actionId, final int labelResId)83     protected void doTestActionKeyLabelResId(final String tag, final InputMethodSubtype subtype,
84             final int actionId, final int labelResId) {
85         final Locale labelLocale = subtype.getLocale().equals(SubtypeLocaleUtils.NO_LANGUAGE)
86                 ? null : SubtypeLocaleUtils.getSubtypeLocale(subtype);
87         doTestActionKeyLabelResIdInLocale(tag, subtype, actionId, labelLocale, labelResId);
88     }
89 
doTestActionKeyLabelResIdInLocale(final String tag, final InputMethodSubtype subtype, final int actionId, final Locale labelLocale, final int labelResId)90     protected void doTestActionKeyLabelResIdInLocale(final String tag,
91             final InputMethodSubtype subtype, final int actionId, final Locale labelLocale,
92             final int labelResId) {
93         final EditorInfo editorInfo = new EditorInfo();
94         editorInfo.imeOptions = actionId;
95         final RunInLocale<String> job = new RunInLocale<String>() {
96             @Override
97             protected String job(final Resources res) {
98                 return res.getString(labelResId);
99             }
100         };
101         final String label = job.runInLocale(getContext().getResources(), labelLocale);
102         doTestActionKeyLabel(tag, subtype, editorInfo, label);
103     }
104 
doTestActionKeyLabel(final String tag, final InputMethodSubtype subtype, final EditorInfo editorInfo, final CharSequence label)105     protected void doTestActionKeyLabel(final String tag, final InputMethodSubtype subtype,
106             final EditorInfo editorInfo, final CharSequence label) {
107         // Test text layouts.
108         editorInfo.inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL;
109         final KeyboardLayoutSet layoutSet = createKeyboardLayoutSet(subtype, editorInfo);
110         doTestActionKey(tag, layoutSet, KeyboardId.ELEMENT_ALPHABET,
111                 label, KeyboardIconsSet.ICON_UNDEFINED);
112         doTestActionKey(tag, layoutSet, KeyboardId.ELEMENT_SYMBOLS,
113                 label, KeyboardIconsSet.ICON_UNDEFINED);
114         doTestActionKey(tag, layoutSet, KeyboardId.ELEMENT_SYMBOLS_SHIFTED,
115                 label, KeyboardIconsSet.ICON_UNDEFINED);
116         // Test phone number layouts.
117         doTestActionKey(tag, layoutSet, KeyboardId.ELEMENT_PHONE,
118                 label, KeyboardIconsSet.ICON_UNDEFINED);
119         doTestActionKey(tag, layoutSet, KeyboardId.ELEMENT_PHONE_SYMBOLS,
120                 label, KeyboardIconsSet.ICON_UNDEFINED);
121         // Test normal number layout.
122         doTestActionKey(tag, layoutSet, KeyboardId.ELEMENT_NUMBER,
123                 label, KeyboardIconsSet.ICON_UNDEFINED);
124         // Test number password layouts.
125         editorInfo.inputType =
126                 InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD;
127         final KeyboardLayoutSet passwordSet = createKeyboardLayoutSet(subtype, editorInfo);
128         doTestActionKey(tag, passwordSet, KeyboardId.ELEMENT_NUMBER,
129                 label, KeyboardIconsSet.ICON_UNDEFINED);
130     }
131 
doTestActionKeyIcon(final String tag, final InputMethodSubtype subtype, final int actionId, final String iconName)132     protected void doTestActionKeyIcon(final String tag, final InputMethodSubtype subtype,
133             final int actionId, final String iconName) {
134         final int iconId = KeyboardIconsSet.getIconId(iconName);
135         final EditorInfo editorInfo = new EditorInfo();
136         editorInfo.imeOptions = actionId;
137         // Test text layouts.
138         editorInfo.inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL;
139         final KeyboardLayoutSet layoutSet = createKeyboardLayoutSet(subtype, editorInfo);
140         doTestActionKey(tag, layoutSet, KeyboardId.ELEMENT_ALPHABET, null /* label */, iconId);
141         doTestActionKey(tag, layoutSet, KeyboardId.ELEMENT_SYMBOLS, null /* label */, iconId);
142         doTestActionKey(
143                 tag, layoutSet, KeyboardId.ELEMENT_SYMBOLS_SHIFTED, null /* label */, iconId);
144         // Test phone number layouts.
145         doTestActionKey(tag, layoutSet, KeyboardId.ELEMENT_PHONE, null /* label */, iconId);
146         doTestActionKey(
147                 tag, layoutSet, KeyboardId.ELEMENT_PHONE_SYMBOLS, null /* label */, iconId);
148         // Test normal number layout.
149         doTestActionKey(tag, layoutSet, KeyboardId.ELEMENT_NUMBER, null /* label */, iconId);
150         // Test number password layout.
151         editorInfo.inputType =
152                 InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD;
153         final KeyboardLayoutSet passwordSet = createKeyboardLayoutSet(subtype, editorInfo);
154         doTestActionKey(tag, passwordSet, KeyboardId.ELEMENT_NUMBER, null /* label */, iconId);
155     }
156 }
157