InputMethodSubtypeSwitchingControllerTest.java revision 5a647b69be8ac8d40c33ed9abe63e41514699e5b
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 android.os;
18
19import android.content.pm.ApplicationInfo;
20import android.content.pm.ResolveInfo;
21import android.content.pm.ServiceInfo;
22import android.test.InstrumentationTestCase;
23import android.test.suitebuilder.annotation.SmallTest;
24import android.view.inputmethod.InputMethodInfo;
25import android.view.inputmethod.InputMethodSubtype;
26import android.view.inputmethod.InputMethodSubtype.InputMethodSubtypeBuilder;
27
28import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController;
29import com.android.internal.inputmethod.InputMethodSubtypeSwitchingController.ImeSubtypeListItem;
30
31import java.util.ArrayList;
32import java.util.Arrays;
33import java.util.List;
34
35public class InputMethodSubtypeSwitchingControllerTest extends InstrumentationTestCase {
36    private static final String DUMMY_PACKAGE_NAME = "dymmy package name";
37    private static final String DUMMY_SETTING_ACTIVITY_NAME = "";
38    private static final boolean DUMMY_IS_AUX_IME = false;
39    private static final boolean DUMMY_FORCE_DEFAULT = false;
40    private static final int DUMMY_IS_DEFAULT_RES_ID = 0;
41    private static final String SYSTEM_LOCALE = "en_US";
42
43    private static InputMethodSubtype createDummySubtype(final String locale) {
44        final InputMethodSubtypeBuilder builder = new InputMethodSubtypeBuilder();
45        return builder.setSubtypeNameResId(0)
46                .setSubtypeIconResId(0)
47                .setSubtypeLocale(locale)
48                .setIsAsciiCapable(true)
49                .build();
50    }
51
52    private static void addDummyImeSubtypeListItems(List<ImeSubtypeListItem> items,
53            String imeName, String imeLabel, List<String> subtypeLocales,
54            boolean supportsSwitchingToNextInputMethod) {
55        final ResolveInfo ri = new ResolveInfo();
56        final ServiceInfo si = new ServiceInfo();
57        final ApplicationInfo ai = new ApplicationInfo();
58        ai.packageName = DUMMY_PACKAGE_NAME;
59        ai.enabled = true;
60        si.applicationInfo = ai;
61        si.enabled = true;
62        si.packageName = DUMMY_PACKAGE_NAME;
63        si.name = imeName;
64        si.exported = true;
65        si.nonLocalizedLabel = imeLabel;
66        ri.serviceInfo = si;
67        final List<InputMethodSubtype> subtypes = new ArrayList<InputMethodSubtype>();
68        for (String subtypeLocale : subtypeLocales) {
69            subtypes.add(createDummySubtype(subtypeLocale));
70        }
71        final InputMethodInfo imi = new InputMethodInfo(ri, DUMMY_IS_AUX_IME,
72                DUMMY_SETTING_ACTIVITY_NAME, subtypes, DUMMY_IS_DEFAULT_RES_ID,
73                DUMMY_FORCE_DEFAULT, supportsSwitchingToNextInputMethod);
74        for (int i = 0; i < subtypes.size(); ++i) {
75            final String subtypeLocale = subtypeLocales.get(i);
76            items.add(new ImeSubtypeListItem(imeName, subtypeLocale, imi, i, subtypeLocale,
77                    SYSTEM_LOCALE));
78        }
79    }
80
81    private static List<ImeSubtypeListItem> createTestData() {
82        final List<ImeSubtypeListItem> items = new ArrayList<ImeSubtypeListItem>();
83        addDummyImeSubtypeListItems(items, "switchAwareLatinIme", "switchAwareLatinIme",
84                Arrays.asList("en_US", "es_US", "fr"),
85                true /* supportsSwitchingToNextInputMethod*/);
86        addDummyImeSubtypeListItems(items, "nonSwitchAwareLatinIme", "nonSwitchAwareLatinIme",
87                Arrays.asList("en_UK", "hi"),
88                false /* supportsSwitchingToNextInputMethod*/);
89        addDummyImeSubtypeListItems(items, "switchAwareJapaneseIme", "switchAwareJapaneseIme",
90                Arrays.asList("ja_JP"),
91                true /* supportsSwitchingToNextInputMethod*/);
92        addDummyImeSubtypeListItems(items, "nonSwitchAwareJapaneseIme", "nonSwitchAwareJapaneseIme",
93                Arrays.asList("ja_JP"),
94                false /* supportsSwitchingToNextInputMethod*/);
95        return items;
96    }
97
98    @SmallTest
99    public void testGetNextInputMethodImplWithNotOnlyCurrentIme() throws Exception {
100        final List<ImeSubtypeListItem> imList = createTestData();
101
102        final boolean ONLY_CURRENT_IME = false;
103        ImeSubtypeListItem currentIme;
104        ImeSubtypeListItem nextIme;
105
106        // "switchAwareLatinIme/en_US" -> "switchAwareLatinIme/es_US"
107        currentIme = imList.get(0);
108        nextIme = InputMethodSubtypeSwitchingController.getNextInputMethodLockedImpl(
109                imList, ONLY_CURRENT_IME, currentIme.mImi, createDummySubtype(
110                        currentIme.mSubtypeName.toString()));
111        assertEquals(imList.get(1), nextIme);
112        // "switchAwareLatinIme/es_US" -> "switchAwareLatinIme/fr"
113        currentIme = imList.get(1);
114        nextIme = InputMethodSubtypeSwitchingController.getNextInputMethodLockedImpl(
115                imList, ONLY_CURRENT_IME, currentIme.mImi, createDummySubtype(
116                        currentIme.mSubtypeName.toString()));
117        assertEquals(imList.get(2), nextIme);
118        // "switchAwareLatinIme/fr" -> "switchAwareJapaneseIme/ja_JP"
119        currentIme = imList.get(2);
120        nextIme = InputMethodSubtypeSwitchingController.getNextInputMethodLockedImpl(
121                imList, ONLY_CURRENT_IME, currentIme.mImi, createDummySubtype(
122                        currentIme.mSubtypeName.toString()));
123        assertEquals(imList.get(5), nextIme);
124        // "switchAwareJapaneseIme/ja_JP" -> "switchAwareLatinIme/en_US"
125        currentIme = imList.get(5);
126        nextIme = InputMethodSubtypeSwitchingController.getNextInputMethodLockedImpl(
127                imList, ONLY_CURRENT_IME, currentIme.mImi, createDummySubtype(
128                        currentIme.mSubtypeName.toString()));
129        assertEquals(imList.get(0), nextIme);
130
131        // "nonSwitchAwareLatinIme/en_UK" -> "nonSwitchAwareLatinIme/hi"
132        currentIme = imList.get(3);
133        nextIme = InputMethodSubtypeSwitchingController.getNextInputMethodLockedImpl(
134                imList, ONLY_CURRENT_IME, currentIme.mImi, createDummySubtype(
135                        currentIme.mSubtypeName.toString()));
136        assertEquals(imList.get(4), nextIme);
137        // "nonSwitchAwareLatinIme/hi" -> "nonSwitchAwareJapaneseIme/ja_JP"
138        currentIme = imList.get(4);
139        nextIme = InputMethodSubtypeSwitchingController.getNextInputMethodLockedImpl(
140                imList, ONLY_CURRENT_IME, currentIme.mImi, createDummySubtype(
141                        currentIme.mSubtypeName.toString()));
142        assertEquals(imList.get(6), nextIme);
143        // "nonSwitchAwareJapaneseIme/ja_JP" -> "nonSwitchAwareLatinIme/en_UK"
144        currentIme = imList.get(6);
145        nextIme = InputMethodSubtypeSwitchingController.getNextInputMethodLockedImpl(
146                imList, ONLY_CURRENT_IME, currentIme.mImi, createDummySubtype(
147                        currentIme.mSubtypeName.toString()));
148        assertEquals(imList.get(3), nextIme);
149    }
150
151    @SmallTest
152    public void testGetNextInputMethodImplWithOnlyCurrentIme() throws Exception {
153        final List<ImeSubtypeListItem> imList = createTestData();
154
155        final boolean ONLY_CURRENT_IME = true;
156        ImeSubtypeListItem currentIme;
157        ImeSubtypeListItem nextIme;
158
159        // "switchAwareLatinIme/en_US" -> "switchAwareLatinIme/es_US"
160        currentIme = imList.get(0);
161        nextIme = InputMethodSubtypeSwitchingController.getNextInputMethodLockedImpl(
162                imList, ONLY_CURRENT_IME, currentIme.mImi, createDummySubtype(
163                        currentIme.mSubtypeName.toString()));
164        assertEquals(imList.get(1), nextIme);
165        // "switchAwareLatinIme/es_US" -> "switchAwareLatinIme/fr"
166        currentIme = imList.get(1);
167        nextIme = InputMethodSubtypeSwitchingController.getNextInputMethodLockedImpl(
168                imList, ONLY_CURRENT_IME, currentIme.mImi, createDummySubtype(
169                        currentIme.mSubtypeName.toString()));
170        assertEquals(imList.get(2), nextIme);
171        // "switchAwareLatinIme/fr" -> "switchAwareLatinIme/en_US"
172        currentIme = imList.get(2);
173        nextIme = InputMethodSubtypeSwitchingController.getNextInputMethodLockedImpl(
174                imList, ONLY_CURRENT_IME, currentIme.mImi, createDummySubtype(
175                        currentIme.mSubtypeName.toString()));
176        assertEquals(imList.get(0), nextIme);
177
178        // "nonSwitchAwareLatinIme/en_UK" -> "nonSwitchAwareLatinIme/hi"
179        currentIme = imList.get(3);
180        nextIme = InputMethodSubtypeSwitchingController.getNextInputMethodLockedImpl(
181                imList, ONLY_CURRENT_IME, currentIme.mImi, createDummySubtype(
182                        currentIme.mSubtypeName.toString()));
183        assertEquals(imList.get(4), nextIme);
184        // "nonSwitchAwareLatinIme/hi" -> "switchAwareLatinIme/en_UK"
185        currentIme = imList.get(4);
186        nextIme = InputMethodSubtypeSwitchingController.getNextInputMethodLockedImpl(
187                imList, ONLY_CURRENT_IME, currentIme.mImi, createDummySubtype(
188                        currentIme.mSubtypeName.toString()));
189        assertEquals(imList.get(3), nextIme);
190
191        // "switchAwareJapaneseIme/ja_JP" -> null
192        currentIme = imList.get(5);
193        nextIme = InputMethodSubtypeSwitchingController.getNextInputMethodLockedImpl(
194                imList, ONLY_CURRENT_IME, currentIme.mImi, createDummySubtype(
195                        currentIme.mSubtypeName.toString()));
196        assertNull(nextIme);
197
198        // "nonSwitchAwareJapaneseIme/ja_JP" -> null
199        currentIme = imList.get(6);
200        nextIme = InputMethodSubtypeSwitchingController.getNextInputMethodLockedImpl(
201                imList, ONLY_CURRENT_IME, currentIme.mImi, createDummySubtype(
202                        currentIme.mSubtypeName.toString()));
203        assertNull(nextIme);
204    }
205 }
206