LanguageOnSpacebarHelperTests.java revision 3895d7f8dc2e4999947f61220b86fa148f433413
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
17package com.android.inputmethod.keyboard.internal;
18
19import static com.android.inputmethod.keyboard.internal.LanguageOnSpacebarHelper.FORMAT_TYPE_FULL_LOCALE;
20import static com.android.inputmethod.keyboard.internal.LanguageOnSpacebarHelper.FORMAT_TYPE_LANGUAGE_ONLY;
21import static com.android.inputmethod.keyboard.internal.LanguageOnSpacebarHelper.FORMAT_TYPE_NONE;
22
23import android.content.Context;
24import android.test.AndroidTestCase;
25import android.test.suitebuilder.annotation.SmallTest;
26import android.view.inputmethod.InputMethodSubtype;
27
28import com.android.inputmethod.latin.RichInputMethodManager;
29import com.android.inputmethod.latin.utils.AdditionalSubtypeUtils;
30import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
31
32import java.util.Arrays;
33import java.util.List;
34import java.util.Locale;
35
36@SmallTest
37public class LanguageOnSpacebarHelperTests extends AndroidTestCase {
38    private final LanguageOnSpacebarHelper mLanguageOnSpacebarHelper =
39            new LanguageOnSpacebarHelper();
40
41    private RichInputMethodManager mRichImm;
42
43    InputMethodSubtype EN_US_QWERTY;
44    InputMethodSubtype EN_GB_QWERTY;
45    InputMethodSubtype FR_AZERTY;
46    InputMethodSubtype FR_CA_QWERTY;
47    InputMethodSubtype FR_CH_SWISS;
48    InputMethodSubtype FR_CH_QWERTY;
49    InputMethodSubtype FR_CH_QWERTZ;
50    InputMethodSubtype ZZ_QWERTY;
51
52    @Override
53    protected void setUp() throws Exception {
54        super.setUp();
55        final Context context = getContext();
56        RichInputMethodManager.init(context);
57        mRichImm = RichInputMethodManager.getInstance();
58        SubtypeLocaleUtils.init(context);
59
60        EN_US_QWERTY = mRichImm.findSubtypeByLocaleAndKeyboardLayoutSet(
61                Locale.US.toString(), "qwerty");
62        EN_GB_QWERTY = mRichImm.findSubtypeByLocaleAndKeyboardLayoutSet(
63                Locale.UK.toString(), "qwerty");
64        FR_AZERTY = mRichImm.findSubtypeByLocaleAndKeyboardLayoutSet(
65                Locale.FRENCH.toString(), "azerty");
66        FR_CA_QWERTY = mRichImm.findSubtypeByLocaleAndKeyboardLayoutSet(
67                Locale.CANADA_FRENCH.toString(), "qwerty");
68        FR_CH_SWISS = mRichImm.findSubtypeByLocaleAndKeyboardLayoutSet(
69                "fr_CH", "swiss");
70        FR_CH_QWERTZ = AdditionalSubtypeUtils.createAsciiEmojiCapableAdditionalSubtype(
71                "fr_CH", "qwertz");
72        FR_CH_QWERTY = AdditionalSubtypeUtils.createAsciiEmojiCapableAdditionalSubtype(
73                "fr_CH", "qwerty");
74        ZZ_QWERTY = mRichImm.findSubtypeByLocaleAndKeyboardLayoutSet(
75                SubtypeLocaleUtils.NO_LANGUAGE, "qwerty");
76    }
77
78    private static List<InputMethodSubtype> asList(final InputMethodSubtype ... subtypes) {
79        return Arrays.asList(subtypes);
80    }
81
82    public void testOneSubtype() {
83        mLanguageOnSpacebarHelper.updateEnabledSubtypes(asList(EN_US_QWERTY));
84        mLanguageOnSpacebarHelper.updateIsSystemLanguageSameAsInputLanguage(true /* isSame */);
85        assertEquals("one same English (US)", FORMAT_TYPE_NONE,
86                mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(EN_US_QWERTY));
87        assertEquals("one same NoLanguage", FORMAT_TYPE_FULL_LOCALE,
88                mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(ZZ_QWERTY));
89
90        mLanguageOnSpacebarHelper.updateEnabledSubtypes(asList(FR_AZERTY));
91        mLanguageOnSpacebarHelper.updateIsSystemLanguageSameAsInputLanguage(false /* isSame */);
92        assertEquals("one diff English (US)", FORMAT_TYPE_LANGUAGE_ONLY,
93                mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(EN_US_QWERTY));
94        assertEquals("one diff NoLanguage", FORMAT_TYPE_FULL_LOCALE,
95                mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(ZZ_QWERTY));
96    }
97
98    public void testTwoSubtypes() {
99        mLanguageOnSpacebarHelper.updateEnabledSubtypes(asList(EN_US_QWERTY, FR_AZERTY));
100
101        mLanguageOnSpacebarHelper.updateIsSystemLanguageSameAsInputLanguage(true /* isSame */);
102        assertEquals("two same English (US)", FORMAT_TYPE_LANGUAGE_ONLY,
103                mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(EN_US_QWERTY));
104        assertEquals("two same French)", FORMAT_TYPE_LANGUAGE_ONLY,
105                mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(FR_AZERTY));
106        assertEquals("two same NoLanguage", FORMAT_TYPE_FULL_LOCALE,
107                mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(ZZ_QWERTY));
108
109        mLanguageOnSpacebarHelper.updateIsSystemLanguageSameAsInputLanguage(false /* isSame */);
110        assertEquals("two diff English (US)", FORMAT_TYPE_LANGUAGE_ONLY,
111                mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(EN_US_QWERTY));
112        assertEquals("two diff French", FORMAT_TYPE_LANGUAGE_ONLY,
113                mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(FR_AZERTY));
114        assertEquals("two diff NoLanguage", FORMAT_TYPE_FULL_LOCALE,
115                mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(ZZ_QWERTY));
116    }
117
118    public void testSameLanuageSubtypes() {
119        mLanguageOnSpacebarHelper.updateEnabledSubtypes(
120                asList(EN_US_QWERTY, EN_GB_QWERTY, FR_AZERTY, ZZ_QWERTY));
121
122        mLanguageOnSpacebarHelper.updateIsSystemLanguageSameAsInputLanguage(true /* isSame */);
123        assertEquals("two same English (US)", FORMAT_TYPE_FULL_LOCALE,
124                mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(EN_US_QWERTY));
125        assertEquals("two same English (UK)", FORMAT_TYPE_FULL_LOCALE,
126                mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(EN_GB_QWERTY));
127        assertEquals("two same NoLanguage", FORMAT_TYPE_FULL_LOCALE,
128                mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(ZZ_QWERTY));
129
130        mLanguageOnSpacebarHelper.updateIsSystemLanguageSameAsInputLanguage(false /* isSame */);
131        assertEquals("two diff English (US)", FORMAT_TYPE_FULL_LOCALE,
132                mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(EN_US_QWERTY));
133        assertEquals("two diff English (UK)", FORMAT_TYPE_FULL_LOCALE,
134                mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(EN_GB_QWERTY));
135        assertEquals("two diff NoLanguage", FORMAT_TYPE_FULL_LOCALE,
136                mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(ZZ_QWERTY));
137    }
138
139    public void testMultiSameLanuageSubtypes() {
140        mLanguageOnSpacebarHelper.updateEnabledSubtypes(
141                asList(FR_AZERTY, FR_CA_QWERTY, FR_CH_SWISS, FR_CH_QWERTY, FR_CH_QWERTZ));
142
143        mLanguageOnSpacebarHelper.updateIsSystemLanguageSameAsInputLanguage(true /* isSame */);
144        assertEquals("multi same French", FORMAT_TYPE_LANGUAGE_ONLY,
145                mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(FR_AZERTY));
146        assertEquals("multi same French (CA)", FORMAT_TYPE_FULL_LOCALE,
147                mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(FR_CA_QWERTY));
148        assertEquals("multi same French (CH)", FORMAT_TYPE_LANGUAGE_ONLY,
149                mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(FR_CH_SWISS));
150        assertEquals("multi same French (CH) (QWERTY)", FORMAT_TYPE_FULL_LOCALE,
151                mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(FR_CH_QWERTY));
152        assertEquals("multi same French (CH) (QWERTZ)", FORMAT_TYPE_LANGUAGE_ONLY,
153                mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(FR_CH_QWERTZ));
154
155        mLanguageOnSpacebarHelper.updateIsSystemLanguageSameAsInputLanguage(false /* isSame */);
156        assertEquals("multi diff French", FORMAT_TYPE_LANGUAGE_ONLY,
157                mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(FR_AZERTY));
158        assertEquals("multi diff French (CA)", FORMAT_TYPE_FULL_LOCALE,
159                mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(FR_CA_QWERTY));
160        assertEquals("multi diff French (CH)", FORMAT_TYPE_LANGUAGE_ONLY,
161                mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(FR_CH_SWISS));
162        assertEquals("multi diff French (CH) (QWERTY)", FORMAT_TYPE_FULL_LOCALE,
163                mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(FR_CH_QWERTY));
164        assertEquals("multi diff French (CH) (QWERTZ)", FORMAT_TYPE_LANGUAGE_ONLY,
165                mLanguageOnSpacebarHelper.getLanguageOnSpacebarFormatType(FR_CH_QWERTZ));
166    }
167}
168