1 /*
2  * Copyright (C) 2011 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.latin.utils;
18 
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.test.AndroidTestCase;
22 import android.test.suitebuilder.annotation.SmallTest;
23 import android.view.inputmethod.InputMethodInfo;
24 import android.view.inputmethod.InputMethodSubtype;
25 
26 import com.android.inputmethod.latin.RichInputMethodManager;
27 
28 import java.util.ArrayList;
29 import java.util.Locale;
30 
31 @SmallTest
32 public class SpacebarLanguagetUtilsTests extends AndroidTestCase {
33     // All input method subtypes of LatinIME.
34     private final ArrayList<InputMethodSubtype> mSubtypesList = new ArrayList<>();
35 
36     private RichInputMethodManager mRichImm;
37     private Resources mRes;
38 
39     InputMethodSubtype EN_US;
40     InputMethodSubtype EN_GB;
41     InputMethodSubtype ES_US;
42     InputMethodSubtype FR;
43     InputMethodSubtype FR_CA;
44     InputMethodSubtype FR_CH;
45     InputMethodSubtype DE;
46     InputMethodSubtype DE_CH;
47     InputMethodSubtype ZZ;
48     InputMethodSubtype DE_QWERTY;
49     InputMethodSubtype FR_QWERTZ;
50     InputMethodSubtype EN_US_AZERTY;
51     InputMethodSubtype EN_UK_DVORAK;
52     InputMethodSubtype ES_US_COLEMAK;
53     InputMethodSubtype ZZ_AZERTY;
54     InputMethodSubtype ZZ_PC;
55 
56     @Override
setUp()57     protected void setUp() throws Exception {
58         super.setUp();
59         final Context context = getContext();
60         RichInputMethodManager.init(context);
61         mRichImm = RichInputMethodManager.getInstance();
62         mRes = context.getResources();
63         SubtypeLocaleUtils.init(context);
64 
65         final InputMethodInfo imi = mRichImm.getInputMethodInfoOfThisIme();
66         final int subtypeCount = imi.getSubtypeCount();
67         for (int index = 0; index < subtypeCount; index++) {
68             final InputMethodSubtype subtype = imi.getSubtypeAt(index);
69             mSubtypesList.add(subtype);
70         }
71 
72         EN_US = mRichImm.findSubtypeByLocaleAndKeyboardLayoutSet(
73                 Locale.US.toString(), "qwerty");
74         EN_GB = mRichImm.findSubtypeByLocaleAndKeyboardLayoutSet(
75                 Locale.UK.toString(), "qwerty");
76         ES_US = mRichImm.findSubtypeByLocaleAndKeyboardLayoutSet(
77                 "es_US", "spanish");
78         FR = mRichImm.findSubtypeByLocaleAndKeyboardLayoutSet(
79                 Locale.FRENCH.toString(), "azerty");
80         FR_CA = mRichImm.findSubtypeByLocaleAndKeyboardLayoutSet(
81                 Locale.CANADA_FRENCH.toString(), "qwerty");
82         FR_CH = mRichImm.findSubtypeByLocaleAndKeyboardLayoutSet(
83                 "fr_CH", "swiss");
84         DE = mRichImm.findSubtypeByLocaleAndKeyboardLayoutSet(
85                 Locale.GERMAN.toString(), "qwertz");
86         DE_CH = mRichImm.findSubtypeByLocaleAndKeyboardLayoutSet(
87                 "de_CH", "swiss");
88         ZZ = mRichImm.findSubtypeByLocaleAndKeyboardLayoutSet(
89                 SubtypeLocaleUtils.NO_LANGUAGE, "qwerty");
90         DE_QWERTY = AdditionalSubtypeUtils.createAsciiEmojiCapableAdditionalSubtype(
91                 Locale.GERMAN.toString(), "qwerty");
92         FR_QWERTZ = AdditionalSubtypeUtils.createAsciiEmojiCapableAdditionalSubtype(
93                 Locale.FRENCH.toString(), "qwertz");
94         EN_US_AZERTY = AdditionalSubtypeUtils.createAsciiEmojiCapableAdditionalSubtype(
95                 Locale.US.toString(), "azerty");
96         EN_UK_DVORAK = AdditionalSubtypeUtils.createAsciiEmojiCapableAdditionalSubtype(
97                 Locale.UK.toString(), "dvorak");
98         ES_US_COLEMAK = AdditionalSubtypeUtils.createAsciiEmojiCapableAdditionalSubtype(
99                 "es_US", "colemak");
100         ZZ_AZERTY = AdditionalSubtypeUtils.createAsciiEmojiCapableAdditionalSubtype(
101                 SubtypeLocaleUtils.NO_LANGUAGE, "azerty");
102         ZZ_PC = AdditionalSubtypeUtils.createAsciiEmojiCapableAdditionalSubtype(
103                 SubtypeLocaleUtils.NO_LANGUAGE, "pcqwerty");
104     }
105 
testAllFullDisplayNameForSpacebar()106     public void testAllFullDisplayNameForSpacebar() {
107         for (final InputMethodSubtype subtype : mSubtypesList) {
108             final String subtypeName = SubtypeLocaleUtils
109                     .getSubtypeDisplayNameInSystemLocale(subtype);
110             final String spacebarText = SpacebarLanguageUtils.getFullDisplayName(subtype);
111             final String languageName = SubtypeLocaleUtils
112                     .getSubtypeLocaleDisplayName(subtype.getLocale());
113             if (SubtypeLocaleUtils.isNoLanguage(subtype)) {
114                 assertFalse(subtypeName, spacebarText.contains(languageName));
115             } else {
116                 assertTrue(subtypeName, spacebarText.contains(languageName));
117             }
118         }
119     }
120 
testAllMiddleDisplayNameForSpacebar()121    public void testAllMiddleDisplayNameForSpacebar() {
122         for (final InputMethodSubtype subtype : mSubtypesList) {
123             final String subtypeName = SubtypeLocaleUtils
124                     .getSubtypeDisplayNameInSystemLocale(subtype);
125             final String spacebarText = SpacebarLanguageUtils.getMiddleDisplayName(subtype);
126             if (SubtypeLocaleUtils.isNoLanguage(subtype)) {
127                 assertEquals(subtypeName,
128                         SubtypeLocaleUtils.getKeyboardLayoutSetDisplayName(subtype), spacebarText);
129             } else {
130                 final Locale locale = SubtypeLocaleUtils.getSubtypeLocale(subtype);
131                 assertEquals(subtypeName,
132                         SubtypeLocaleUtils.getSubtypeLocaleDisplayName(locale.getLanguage()),
133                         spacebarText);
134             }
135         }
136     }
137 
138     // InputMethodSubtype's display name for spacebar text in its locale.
139     //        isAdditionalSubtype (T=true, F=false)
140     // locale layout  |  Middle     Full
141     // ------ ------- - --------- ----------------------
142     //  en_US qwerty  F  English   English (US)           exception
143     //  en_GB qwerty  F  English   English (UK)           exception
144     //  es_US spanish F  Español   Español (EE.UU.)       exception
145     //  fr    azerty  F  Français  Français
146     //  fr_CA qwerty  F  Français  Français (Canada)
147     //  fr_CH swiss   F  Français  Français (Suisse)
148     //  de    qwertz  F  Deutsch   Deutsch
149     //  de_CH swiss   F  Deutsch   Deutsch (Schweiz)
150     //  zz    qwerty  F  QWERTY    QWERTY
151     //  fr    qwertz  T  Français  Français
152     //  de    qwerty  T  Deutsch   Deutsch
153     //  en_US azerty  T  English   English (US)
154     //  zz    azerty  T  AZERTY    AZERTY
155 
156     private final RunInLocale<Void> testsPredefinedSubtypesForSpacebar = new RunInLocale<Void>() {
157         @Override
158         protected Void job(final Resources res) {
159             assertEquals("en_US", "English (US)",
160                     SpacebarLanguageUtils.getFullDisplayName(EN_US));
161             assertEquals("en_GB", "English (UK)",
162                     SpacebarLanguageUtils.getFullDisplayName(EN_GB));
163             assertEquals("es_US", "Español (EE.UU.)",
164                     SpacebarLanguageUtils.getFullDisplayName(ES_US));
165             assertEquals("fr", "Français",
166                     SpacebarLanguageUtils.getFullDisplayName(FR));
167             assertEquals("fr_CA", "Français (Canada)",
168                     SpacebarLanguageUtils.getFullDisplayName(FR_CA));
169             assertEquals("fr_CH", "Français (Suisse)",
170                     SpacebarLanguageUtils.getFullDisplayName(FR_CH));
171             assertEquals("de", "Deutsch",
172                     SpacebarLanguageUtils.getFullDisplayName(DE));
173             assertEquals("de_CH", "Deutsch (Schweiz)",
174                     SpacebarLanguageUtils.getFullDisplayName(DE_CH));
175             assertEquals("zz", "QWERTY",
176                     SpacebarLanguageUtils.getFullDisplayName(ZZ));
177 
178             assertEquals("en_US", "English",
179                     SpacebarLanguageUtils.getMiddleDisplayName(EN_US));
180             assertEquals("en_GB", "English",
181                     SpacebarLanguageUtils.getMiddleDisplayName(EN_GB));
182             assertEquals("es_US", "Español",
183                     SpacebarLanguageUtils.getMiddleDisplayName(ES_US));
184             assertEquals("fr", "Français",
185                     SpacebarLanguageUtils.getMiddleDisplayName(FR));
186             assertEquals("fr_CA", "Français",
187                     SpacebarLanguageUtils.getMiddleDisplayName(FR_CA));
188             assertEquals("fr_CH", "Français",
189                     SpacebarLanguageUtils.getMiddleDisplayName(FR_CH));
190             assertEquals("de", "Deutsch",
191                     SpacebarLanguageUtils.getMiddleDisplayName(DE));
192             assertEquals("de_CH", "Deutsch",
193                     SpacebarLanguageUtils.getMiddleDisplayName(DE_CH));
194             assertEquals("zz", "QWERTY",
195                     SpacebarLanguageUtils.getMiddleDisplayName(ZZ));
196             return null;
197         }
198     };
199 
200     private final RunInLocale<Void> testsAdditionalSubtypesForSpacebar = new RunInLocale<Void>() {
201         @Override
202         protected Void job(final Resources res) {
203             assertEquals("fr qwertz", "Français",
204                     SpacebarLanguageUtils.getFullDisplayName(FR_QWERTZ));
205             assertEquals("de qwerty", "Deutsch",
206                     SpacebarLanguageUtils.getFullDisplayName(DE_QWERTY));
207             assertEquals("en_US azerty", "English (US)",
208                     SpacebarLanguageUtils.getFullDisplayName(EN_US_AZERTY));
209             assertEquals("en_UK dvorak", "English (UK)",
210                     SpacebarLanguageUtils.getFullDisplayName(EN_UK_DVORAK));
211             assertEquals("es_US colemak", "Español (EE.UU.)",
212                     SpacebarLanguageUtils.getFullDisplayName(ES_US_COLEMAK));
213             assertEquals("zz azerty", "AZERTY",
214                     SpacebarLanguageUtils.getFullDisplayName(ZZ_AZERTY));
215             assertEquals("zz pc", "PC",
216                     SpacebarLanguageUtils.getFullDisplayName(ZZ_PC));
217 
218             assertEquals("fr qwertz", "Français",
219                     SpacebarLanguageUtils.getMiddleDisplayName(FR_QWERTZ));
220             assertEquals("de qwerty", "Deutsch",
221                     SpacebarLanguageUtils.getMiddleDisplayName(DE_QWERTY));
222             assertEquals("en_US azerty", "English",
223                     SpacebarLanguageUtils.getMiddleDisplayName(EN_US_AZERTY));
224             assertEquals("en_UK dvorak", "English",
225                     SpacebarLanguageUtils.getMiddleDisplayName(EN_UK_DVORAK));
226             assertEquals("es_US colemak", "Español",
227                     SpacebarLanguageUtils.getMiddleDisplayName(ES_US_COLEMAK));
228             assertEquals("zz azerty", "AZERTY",
229                     SpacebarLanguageUtils.getMiddleDisplayName(ZZ_AZERTY));
230             assertEquals("zz pc", "PC",
231                     SpacebarLanguageUtils.getMiddleDisplayName(ZZ_PC));
232             return null;
233         }
234     };
235 
testPredefinedSubtypesForSpacebarInEnglish()236     public void testPredefinedSubtypesForSpacebarInEnglish() {
237         testsPredefinedSubtypesForSpacebar.runInLocale(mRes, Locale.ENGLISH);
238     }
239 
testAdditionalSubtypeForSpacebarInEnglish()240     public void testAdditionalSubtypeForSpacebarInEnglish() {
241         testsAdditionalSubtypesForSpacebar.runInLocale(mRes, Locale.ENGLISH);
242     }
243 
testPredefinedSubtypesForSpacebarInFrench()244     public void testPredefinedSubtypesForSpacebarInFrench() {
245         testsPredefinedSubtypesForSpacebar.runInLocale(mRes, Locale.FRENCH);
246     }
247 
testAdditionalSubtypeForSpacebarInFrench()248     public void testAdditionalSubtypeForSpacebarInFrench() {
249         testsAdditionalSubtypesForSpacebar.runInLocale(mRes, Locale.FRENCH);
250     }
251 }
252