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