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.res.Resources;
20 import android.test.suitebuilder.annotation.MediumTest;
21 import android.view.inputmethod.EditorInfo;
22 import android.view.inputmethod.InputMethodSubtype;
23 
24 import com.android.inputmethod.keyboard.KeyboardLayoutSet;
25 import com.android.inputmethod.keyboard.internal.KeyboardIconsSet;
26 import com.android.inputmethod.keyboard.internal.KeyboardTextsSet;
27 import com.android.inputmethod.latin.R;
28 import com.android.inputmethod.latin.RichInputMethodManager;
29 import com.android.inputmethod.latin.utils.RunInLocale;
30 import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
31 
32 import java.util.Locale;
33 
34 @MediumTest
35 public class KlpActionLabelTests extends KlpActionTestsBase {
doTestActionKeys(final InputMethodSubtype subtype, final String tag, final ExpectedActionKey unspecifiedKey, final ExpectedActionKey noneKey, final ExpectedActionKey goKey, final ExpectedActionKey searchKey, final ExpectedActionKey sendKey, final ExpectedActionKey nextKey, final ExpectedActionKey doneKey, final ExpectedActionKey previousKey)36     void doTestActionKeys(final InputMethodSubtype subtype, final String tag,
37             final ExpectedActionKey unspecifiedKey, final ExpectedActionKey noneKey,
38             final ExpectedActionKey goKey, final ExpectedActionKey searchKey,
39             final ExpectedActionKey sendKey, final ExpectedActionKey nextKey,
40             final ExpectedActionKey doneKey, final ExpectedActionKey previousKey) {
41         doTestActionKey(
42                 tag + " unspecified", subtype, EditorInfo.IME_ACTION_UNSPECIFIED, unspecifiedKey);
43         doTestActionKey(tag + " none", subtype, EditorInfo.IME_ACTION_NONE, noneKey);
44         doTestActionKey(tag + " go", subtype, EditorInfo.IME_ACTION_GO, goKey);
45         doTestActionKey(tag + " search", subtype, EditorInfo.IME_ACTION_SEARCH, searchKey);
46         doTestActionKey(tag + " send", subtype, EditorInfo.IME_ACTION_SEND, sendKey);
47         doTestActionKey(tag + " next", subtype, EditorInfo.IME_ACTION_NEXT, nextKey);
48         doTestActionKey(tag + " done", subtype, EditorInfo.IME_ACTION_DONE, doneKey);
49         doTestActionKey(tag + " previous", subtype, EditorInfo.IME_ACTION_PREVIOUS, previousKey);
50     }
51 
52     // Working variable to simulate system locale changing.
53     private Locale mSystemLocale = Locale.getDefault();
54 
doTestActionKeysInLocaleWithStringResources(final InputMethodSubtype subtype, final Locale labelLocale, final Locale systemLocale)55     private void doTestActionKeysInLocaleWithStringResources(final InputMethodSubtype subtype,
56             final Locale labelLocale, final Locale systemLocale) {
57         // Simulate system locale changing, see {@link SystemBroadcastReceiver}.
58         if (!systemLocale.equals(mSystemLocale)) {
59             KeyboardLayoutSet.onSystemLocaleChanged();
60             mSystemLocale = systemLocale;
61         }
62         final ExpectedActionKey enterKey = ExpectedActionKey.newIconKey(
63                 KeyboardIconsSet.NAME_ENTER_KEY);
64         final ExpectedActionKey goKey = ExpectedActionKey.newLabelKey(
65                 R.string.label_go_key, labelLocale, getContext());
66         final ExpectedActionKey searchKey = ExpectedActionKey.newIconKey(
67                 KeyboardIconsSet.NAME_SEARCH_KEY);
68         final ExpectedActionKey sendKey = ExpectedActionKey.newLabelKey(
69                 R.string.label_send_key, labelLocale, getContext());
70         final ExpectedActionKey nextKey = ExpectedActionKey.newLabelKey(
71                 R.string.label_next_key, labelLocale, getContext());
72         final ExpectedActionKey doneKey = ExpectedActionKey.newLabelKey(
73                 R.string.label_done_key, labelLocale, getContext());
74         final ExpectedActionKey previousKey = ExpectedActionKey.newLabelKey(
75                 R.string.label_previous_key, labelLocale, getContext());
76         final String tag = "label=" + labelLocale + " system=" + systemLocale
77                 + " " + SubtypeLocaleUtils.getSubtypeNameForLogging(subtype);
78         final RunInLocale<Void> job = new RunInLocale<Void>() {
79             @Override
80             public Void job(final Resources res) {
81                 doTestActionKeys(subtype, tag, enterKey, enterKey, goKey, searchKey, sendKey,
82                         nextKey, doneKey, previousKey);
83                 return null;
84             }
85         };
86         job.runInLocale(getContext().getResources(), systemLocale);
87     }
88 
testActionLabelInOtherLocale()89     public void testActionLabelInOtherLocale() {
90         final RichInputMethodManager richImm = RichInputMethodManager.getInstance();
91         final InputMethodSubtype italian = richImm.findSubtypeByLocaleAndKeyboardLayoutSet(
92                 Locale.ITALIAN.toString(), SubtypeLocaleUtils.QWERTY);
93         // An action label should be displayed in subtype's locale regardless of the system locale.
94         doTestActionKeysInLocaleWithStringResources(italian, Locale.ITALIAN, Locale.US);
95         doTestActionKeysInLocaleWithStringResources(italian, Locale.ITALIAN, Locale.FRENCH);
96         doTestActionKeysInLocaleWithStringResources(italian, Locale.ITALIAN, Locale.ITALIAN);
97         doTestActionKeysInLocaleWithStringResources(italian, Locale.ITALIAN, Locale.JAPANESE);
98     }
99 
testNoLanguageSubtypeActionLabel()100     public void testNoLanguageSubtypeActionLabel() {
101         final RichInputMethodManager richImm = RichInputMethodManager.getInstance();
102         final InputMethodSubtype noLanguage = richImm.findSubtypeByLocaleAndKeyboardLayoutSet(
103                 SubtypeLocaleUtils.NO_LANGUAGE, SubtypeLocaleUtils.QWERTY);
104         // An action label of no language keyboard should be displayed in the system locale.
105         doTestActionKeysInLocaleWithStringResources(noLanguage, Locale.US, Locale.US);
106         doTestActionKeysInLocaleWithStringResources(noLanguage, Locale.FRENCH, Locale.FRENCH);
107         doTestActionKeysInLocaleWithStringResources(noLanguage, Locale.ITALIAN, Locale.ITALIAN);
108         doTestActionKeysInLocaleWithStringResources(noLanguage, Locale.JAPANESE, Locale.JAPANESE);
109     }
110 
doTestActionKeysInLocaleWithKeyboardTextsSet(final InputMethodSubtype subtype, final Locale labelLocale, final Locale systemLocale)111     private void doTestActionKeysInLocaleWithKeyboardTextsSet(final InputMethodSubtype subtype,
112             final Locale labelLocale, final Locale systemLocale) {
113         // Simulate system locale changing, see {@link SystemBroadcastReceiver}.
114         if (!systemLocale.equals(mSystemLocale)) {
115             KeyboardLayoutSet.onSystemLocaleChanged();
116             mSystemLocale = systemLocale;
117         }
118         final KeyboardTextsSet textsSet = new KeyboardTextsSet();
119         textsSet.setLocale(labelLocale, getContext());
120         final ExpectedActionKey enterKey = ExpectedActionKey.newIconKey(
121                 KeyboardIconsSet.NAME_ENTER_KEY);
122         final ExpectedActionKey goKey = ExpectedActionKey.newLabelKey(
123                 textsSet.getText("label_go_key"));
124         final ExpectedActionKey searchKey = ExpectedActionKey.newIconKey(
125                 KeyboardIconsSet.NAME_SEARCH_KEY);
126         final ExpectedActionKey sendKey = ExpectedActionKey.newLabelKey(
127                 textsSet.getText("label_send_key"));
128         final ExpectedActionKey nextKey = ExpectedActionKey.newLabelKey(
129                 textsSet.getText("label_next_key"));
130         final ExpectedActionKey doneKey = ExpectedActionKey.newLabelKey(
131                 textsSet.getText("label_done_key"));
132         final ExpectedActionKey previousKey = ExpectedActionKey.newLabelKey(
133                 textsSet.getText("label_previous_key"));
134         final String tag = "label=" + subtype.getLocale() + " system=" + systemLocale
135                 + " " + SubtypeLocaleUtils.getSubtypeNameForLogging(subtype);
136         final RunInLocale<Void> job = new RunInLocale<Void>() {
137             @Override
138             public Void job(final Resources res) {
139                 doTestActionKeys(subtype, tag, enterKey, enterKey, goKey, searchKey, sendKey,
140                         nextKey, doneKey, previousKey);
141                 return null;
142             }
143         };
144         job.runInLocale(getContext().getResources(), systemLocale);
145     }
146 
testHinglishActionLabel()147     public void testHinglishActionLabel() {
148         final RichInputMethodManager richImm = RichInputMethodManager.getInstance();
149         final Locale hi_ZZ = new Locale("hi", "ZZ");
150         final InputMethodSubtype hiLatn = richImm.findSubtypeByLocaleAndKeyboardLayoutSet(
151                 hi_ZZ.toString(), SubtypeLocaleUtils.QWERTY);
152         // This is a preliminary subtype and may not exist.
153         if (hiLatn == null) {
154             return;
155         }
156         // An action label should be displayed in subtype's locale regardless of the system locale.
157         doTestActionKeysInLocaleWithKeyboardTextsSet(hiLatn, hi_ZZ, new Locale("hi"));
158         doTestActionKeysInLocaleWithKeyboardTextsSet(hiLatn, hi_ZZ, Locale.US);
159         doTestActionKeysInLocaleWithKeyboardTextsSet(hiLatn, hi_ZZ, Locale.FRENCH);
160         doTestActionKeysInLocaleWithKeyboardTextsSet(hiLatn, hi_ZZ, Locale.ITALIAN);
161         doTestActionKeysInLocaleWithKeyboardTextsSet(hiLatn, hi_ZZ, Locale.JAPANESE);
162     }
163 
testSerbianLatinActionLabel()164     public void testSerbianLatinActionLabel() {
165         final RichInputMethodManager richImm = RichInputMethodManager.getInstance();
166         final Locale sr_ZZ = new Locale("sr", "ZZ");
167         final InputMethodSubtype srLatn = richImm.findSubtypeByLocaleAndKeyboardLayoutSet(
168                 sr_ZZ.toString(), "serbian_qwertz");
169         // This is a preliminary subtype and may not exist.
170         if (srLatn == null) {
171             return;
172         }
173         // An action label should be displayed in subtype's locale regardless of the system locale.
174         doTestActionKeysInLocaleWithKeyboardTextsSet(srLatn, sr_ZZ, new Locale("sr"));
175         doTestActionKeysInLocaleWithKeyboardTextsSet(srLatn, sr_ZZ, Locale.US);
176         doTestActionKeysInLocaleWithKeyboardTextsSet(srLatn, sr_ZZ, Locale.FRENCH);
177         doTestActionKeysInLocaleWithKeyboardTextsSet(srLatn, sr_ZZ, Locale.ITALIAN);
178         doTestActionKeysInLocaleWithKeyboardTextsSet(srLatn, sr_ZZ, Locale.JAPANESE);
179     }
180 }
181