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;
18
19import static com.android.inputmethod.keyboard.KeyboardTheme.THEME_ID_ICS;
20import static com.android.inputmethod.keyboard.KeyboardTheme.THEME_ID_KLP;
21import static com.android.inputmethod.keyboard.KeyboardTheme.THEME_ID_LXX_DARK;
22import static com.android.inputmethod.keyboard.KeyboardTheme.THEME_ID_LXX_LIGHT;
23
24import android.content.SharedPreferences;
25import android.os.Build;
26import android.os.Build.VERSION_CODES;
27import android.preference.PreferenceManager;
28import android.test.AndroidTestCase;
29import android.test.suitebuilder.annotation.SmallTest;
30
31import java.util.Arrays;
32
33@SmallTest
34public class KeyboardThemeTests extends AndroidTestCase {
35    private SharedPreferences mPrefs;
36
37    private static final int THEME_ID_NULL = -1;
38    private static final int THEME_ID_UNKNOWN = -2;
39    private static final int THEME_ID_ILLEGAL = -3;
40    private static final String ILLEGAL_THEME_ID_STRING = "ThisCausesNumberFormatExecption";
41
42    @Override
43    protected void setUp() throws Exception {
44        super.setUp();
45        mPrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
46    }
47
48    /*
49     * Helper functions.
50     */
51
52    private static boolean isValidKeyboardThemeId(final int themeId) {
53        switch (themeId) {
54        case THEME_ID_ICS:
55        case THEME_ID_KLP:
56        case THEME_ID_LXX_LIGHT:
57        case THEME_ID_LXX_DARK:
58            return true;
59        default:
60            return false;
61        }
62    }
63
64    private void setKeyboardThemePreference(final String prefKey, final int themeId) {
65        final String themeIdString = Integer.toString(themeId);
66        if (isValidKeyboardThemeId(themeId) || themeId == THEME_ID_UNKNOWN) {
67            // Set valid theme id to preference.
68            mPrefs.edit().putString(prefKey, themeIdString).apply();
69            return;
70        }
71        if (themeId == THEME_ID_NULL) {
72            // Simulate undefined preference.
73            mPrefs.edit().remove(prefKey).apply();
74            return;
75        }
76        // themeId == THEME_ID_ILLEGAL
77        // Simulate illegal format theme id in preference.
78        mPrefs.edit().putString(prefKey, ILLEGAL_THEME_ID_STRING).apply();
79    }
80
81    private void assertKeyboardTheme(final int sdkVersion, final int expectedThemeId) {
82        final KeyboardTheme actualTheme = KeyboardTheme.getKeyboardTheme(
83                mPrefs, sdkVersion, KeyboardTheme.KEYBOARD_THEMES);
84        assertEquals(expectedThemeId, actualTheme.mThemeId);
85    }
86
87    /*
88     * Test keyboard theme preference on the same platform version and the same keyboard version.
89     */
90
91    private void assertKeyboardThemePreference(final int sdkVersion, final int previousThemeId,
92            final int expectedThemeId) {
93        // Clear preferences before testing.
94        setKeyboardThemePreference(KeyboardTheme.KLP_KEYBOARD_THEME_KEY, THEME_ID_NULL);
95        setKeyboardThemePreference(KeyboardTheme.LXX_KEYBOARD_THEME_KEY, THEME_ID_NULL);
96        // Set the preference of the sdkVersion to previousThemeId.
97        final String prefKey = KeyboardTheme.getPreferenceKey(sdkVersion);
98        setKeyboardThemePreference(prefKey, previousThemeId);
99        assertKeyboardTheme(sdkVersion, expectedThemeId);
100    }
101
102    private void assertKeyboardThemePreferenceOnKlp(final int sdkVersion) {
103        final int defaultThemeId = THEME_ID_KLP;
104        assertKeyboardThemePreference(sdkVersion, THEME_ID_NULL, defaultThemeId);
105        assertKeyboardThemePreference(sdkVersion, THEME_ID_ICS, THEME_ID_ICS);
106        assertKeyboardThemePreference(sdkVersion, THEME_ID_KLP, THEME_ID_KLP);
107        assertKeyboardThemePreference(sdkVersion, THEME_ID_LXX_LIGHT, THEME_ID_LXX_LIGHT);
108        assertKeyboardThemePreference(sdkVersion, THEME_ID_LXX_DARK, THEME_ID_LXX_DARK);
109        assertKeyboardThemePreference(sdkVersion, THEME_ID_UNKNOWN, defaultThemeId);
110        assertKeyboardThemePreference(sdkVersion, THEME_ID_ILLEGAL, defaultThemeId);
111    }
112
113    public void testKeyboardThemePreferenceOnKlp() {
114        assertKeyboardThemePreferenceOnKlp(VERSION_CODES.ICE_CREAM_SANDWICH);
115        assertKeyboardThemePreferenceOnKlp(VERSION_CODES.ICE_CREAM_SANDWICH_MR1);
116        assertKeyboardThemePreferenceOnKlp(VERSION_CODES.JELLY_BEAN);
117        assertKeyboardThemePreferenceOnKlp(VERSION_CODES.JELLY_BEAN_MR1);
118        assertKeyboardThemePreferenceOnKlp(VERSION_CODES.JELLY_BEAN_MR2);
119        assertKeyboardThemePreferenceOnKlp(VERSION_CODES.KITKAT);
120    }
121
122    private void assertKeyboardThemePreferenceOnLxx(final int sdkVersion) {
123        final int defaultThemeId = THEME_ID_LXX_LIGHT;
124        assertKeyboardThemePreference(sdkVersion, THEME_ID_NULL, defaultThemeId);
125        assertKeyboardThemePreference(sdkVersion, THEME_ID_ICS, THEME_ID_ICS);
126        assertKeyboardThemePreference(sdkVersion, THEME_ID_KLP, THEME_ID_KLP);
127        assertKeyboardThemePreference(sdkVersion, THEME_ID_LXX_LIGHT, THEME_ID_LXX_LIGHT);
128        assertKeyboardThemePreference(sdkVersion, THEME_ID_LXX_DARK, THEME_ID_LXX_DARK);
129        assertKeyboardThemePreference(sdkVersion, THEME_ID_UNKNOWN, defaultThemeId);
130        assertKeyboardThemePreference(sdkVersion, THEME_ID_ILLEGAL, defaultThemeId);
131    }
132
133    public void testKeyboardThemePreferenceOnLxx() {
134        assertKeyboardThemePreferenceOnLxx(Build.VERSION_CODES.LOLLIPOP);
135    }
136
137    /*
138     * Test default keyboard theme based on the platform version.
139     */
140
141    private void assertDefaultKeyboardTheme(final int sdkVersion, final int previousThemeId,
142            final int expectedThemeId) {
143        final String oldPrefKey = KeyboardTheme.KLP_KEYBOARD_THEME_KEY;
144        setKeyboardThemePreference(oldPrefKey, previousThemeId);
145
146        final KeyboardTheme defaultTheme = KeyboardTheme.getDefaultKeyboardTheme(
147                mPrefs, sdkVersion, KeyboardTheme.KEYBOARD_THEMES);
148
149        assertNotNull(defaultTheme);
150        assertEquals(expectedThemeId, defaultTheme.mThemeId);
151        if (sdkVersion <= VERSION_CODES.KITKAT) {
152            // Old preference must be retained if it is valid. Otherwise it must be pruned.
153            assertEquals(isValidKeyboardThemeId(previousThemeId), mPrefs.contains(oldPrefKey));
154            return;
155        }
156        // Old preference must be removed.
157        assertFalse(mPrefs.contains(oldPrefKey));
158    }
159
160    private void assertDefaultKeyboardThemeOnKlp(final int sdkVersion) {
161        assertDefaultKeyboardTheme(sdkVersion, THEME_ID_NULL, THEME_ID_KLP);
162        assertDefaultKeyboardTheme(sdkVersion, THEME_ID_ICS, THEME_ID_ICS);
163        assertDefaultKeyboardTheme(sdkVersion, THEME_ID_KLP, THEME_ID_KLP);
164        assertDefaultKeyboardTheme(sdkVersion, THEME_ID_UNKNOWN, THEME_ID_KLP);
165        assertDefaultKeyboardTheme(sdkVersion, THEME_ID_ILLEGAL, THEME_ID_KLP);
166    }
167
168    public void testDefaultKeyboardThemeOnKlp() {
169        assertDefaultKeyboardThemeOnKlp(VERSION_CODES.ICE_CREAM_SANDWICH);
170        assertDefaultKeyboardThemeOnKlp(VERSION_CODES.ICE_CREAM_SANDWICH_MR1);
171        assertDefaultKeyboardThemeOnKlp(VERSION_CODES.JELLY_BEAN);
172        assertDefaultKeyboardThemeOnKlp(VERSION_CODES.JELLY_BEAN_MR1);
173        assertDefaultKeyboardThemeOnKlp(VERSION_CODES.JELLY_BEAN_MR2);
174        assertDefaultKeyboardThemeOnKlp(VERSION_CODES.KITKAT);
175    }
176
177    private void assertDefaultKeyboardThemeOnLxx(final int sdkVersion) {
178        // Forced to switch to LXX theme.
179        assertDefaultKeyboardTheme(sdkVersion, THEME_ID_NULL, THEME_ID_LXX_LIGHT);
180        assertDefaultKeyboardTheme(sdkVersion, THEME_ID_ICS, THEME_ID_LXX_LIGHT);
181        assertDefaultKeyboardTheme(sdkVersion, THEME_ID_KLP, THEME_ID_LXX_LIGHT);
182        assertDefaultKeyboardTheme(sdkVersion, THEME_ID_UNKNOWN, THEME_ID_LXX_LIGHT);
183        assertDefaultKeyboardTheme(sdkVersion, THEME_ID_ILLEGAL, THEME_ID_LXX_LIGHT);
184    }
185
186    public void testDefaultKeyboardThemeOnLxx() {
187        assertDefaultKeyboardThemeOnLxx(Build.VERSION_CODES.LOLLIPOP);
188    }
189
190    /*
191     * Test keyboard theme preference while upgrading the keyboard that doesn't support LXX theme
192     * to the keyboard that supports LXX theme.
193     */
194
195    private void assertUpgradeKeyboardToLxxOn(final int sdkVersion, final int previousThemeId,
196            final int expectedThemeId) {
197        setKeyboardThemePreference(KeyboardTheme.KLP_KEYBOARD_THEME_KEY, previousThemeId);
198        // Clean up new keyboard theme preference to simulate "upgrade to LXX keyboard".
199        setKeyboardThemePreference(KeyboardTheme.LXX_KEYBOARD_THEME_KEY, THEME_ID_NULL);
200
201        final KeyboardTheme theme = KeyboardTheme.getKeyboardTheme(
202                mPrefs, sdkVersion, KeyboardTheme.KEYBOARD_THEMES);
203
204        assertNotNull(theme);
205        assertEquals(expectedThemeId, theme.mThemeId);
206        if (sdkVersion <= VERSION_CODES.KITKAT) {
207            // New preference must not exist.
208            assertFalse(mPrefs.contains(KeyboardTheme.LXX_KEYBOARD_THEME_KEY));
209            // Old preference must be retained if it is valid. Otherwise it must be pruned.
210            assertEquals(isValidKeyboardThemeId(previousThemeId),
211                    mPrefs.contains(KeyboardTheme.KLP_KEYBOARD_THEME_KEY));
212            if (isValidKeyboardThemeId(previousThemeId)) {
213                // Old preference must have an expected value.
214                assertEquals(mPrefs.getString(KeyboardTheme.KLP_KEYBOARD_THEME_KEY, null),
215                        Integer.toString(expectedThemeId));
216            }
217            return;
218        }
219        // Old preference must be removed.
220        assertFalse(mPrefs.contains(KeyboardTheme.KLP_KEYBOARD_THEME_KEY));
221        // New preference must not exist.
222        assertFalse(mPrefs.contains(KeyboardTheme.LXX_KEYBOARD_THEME_KEY));
223    }
224
225    private void assertUpgradeKeyboardToLxxOnKlp(final int sdkVersion) {
226        assertUpgradeKeyboardToLxxOn(sdkVersion, THEME_ID_NULL, THEME_ID_KLP);
227        assertUpgradeKeyboardToLxxOn(sdkVersion, THEME_ID_ICS, THEME_ID_ICS);
228        assertUpgradeKeyboardToLxxOn(sdkVersion, THEME_ID_KLP, THEME_ID_KLP);
229        assertUpgradeKeyboardToLxxOn(sdkVersion, THEME_ID_UNKNOWN, THEME_ID_KLP);
230        assertUpgradeKeyboardToLxxOn(sdkVersion, THEME_ID_ILLEGAL, THEME_ID_KLP);
231    }
232
233    // Upgrading keyboard on I,J and K.
234    public void testUpgradeKeyboardToLxxOnKlp() {
235        assertUpgradeKeyboardToLxxOnKlp(VERSION_CODES.ICE_CREAM_SANDWICH);
236        assertUpgradeKeyboardToLxxOnKlp(VERSION_CODES.ICE_CREAM_SANDWICH_MR1);
237        assertUpgradeKeyboardToLxxOnKlp(VERSION_CODES.JELLY_BEAN);
238        assertUpgradeKeyboardToLxxOnKlp(VERSION_CODES.JELLY_BEAN_MR1);
239        assertUpgradeKeyboardToLxxOnKlp(VERSION_CODES.JELLY_BEAN_MR2);
240        assertUpgradeKeyboardToLxxOnKlp(VERSION_CODES.KITKAT);
241    }
242
243    private void assertUpgradeKeyboardToLxxOnLxx(final int sdkVersion) {
244        // Forced to switch to LXX theme.
245        assertUpgradeKeyboardToLxxOn(sdkVersion, THEME_ID_NULL, THEME_ID_LXX_LIGHT);
246        assertUpgradeKeyboardToLxxOn(sdkVersion, THEME_ID_ICS, THEME_ID_LXX_LIGHT);
247        assertUpgradeKeyboardToLxxOn(sdkVersion, THEME_ID_KLP, THEME_ID_LXX_LIGHT);
248        assertUpgradeKeyboardToLxxOn(sdkVersion, THEME_ID_UNKNOWN, THEME_ID_LXX_LIGHT);
249        assertUpgradeKeyboardToLxxOn(sdkVersion, THEME_ID_ILLEGAL, THEME_ID_LXX_LIGHT);
250    }
251
252    // Upgrading keyboard on L.
253    public void testUpgradeKeyboardToLxxOnLxx() {
254        assertUpgradeKeyboardToLxxOnLxx(Build.VERSION_CODES.LOLLIPOP);
255    }
256
257    /*
258     * Test keyboard theme preference while upgrading platform version.
259     */
260
261    private void assertUpgradePlatformFromTo(final int oldSdkVersion, final int newSdkVersion,
262            final int previousThemeId, final int expectedThemeId) {
263        if (newSdkVersion < oldSdkVersion) {
264            // No need to test.
265            return;
266        }
267        // Clean up preferences.
268        setKeyboardThemePreference(KeyboardTheme.KLP_KEYBOARD_THEME_KEY, THEME_ID_NULL);
269        setKeyboardThemePreference(KeyboardTheme.LXX_KEYBOARD_THEME_KEY, THEME_ID_NULL);
270
271        final String oldPrefKey = KeyboardTheme.getPreferenceKey(oldSdkVersion);
272        setKeyboardThemePreference(oldPrefKey, previousThemeId);
273
274        assertKeyboardTheme(newSdkVersion, expectedThemeId);
275    }
276
277    private void assertUpgradePlatformFromKlpToKlp(final int oldSdkVersion,
278            final int newSdkVersion) {
279        assertUpgradePlatformFromTo(oldSdkVersion, newSdkVersion, THEME_ID_NULL, THEME_ID_KLP);
280        assertUpgradePlatformFromTo(oldSdkVersion, newSdkVersion, THEME_ID_ICS, THEME_ID_ICS);
281        assertUpgradePlatformFromTo(oldSdkVersion, newSdkVersion, THEME_ID_KLP, THEME_ID_KLP);
282        assertUpgradePlatformFromTo(oldSdkVersion, newSdkVersion, THEME_ID_UNKNOWN, THEME_ID_KLP);
283        assertUpgradePlatformFromTo(oldSdkVersion, newSdkVersion, THEME_ID_ILLEGAL, THEME_ID_KLP);
284    }
285
286    private void assertUpgradePlatformToKlpFrom(final int oldSdkVersion) {
287        assertUpgradePlatformFromKlpToKlp(oldSdkVersion, VERSION_CODES.ICE_CREAM_SANDWICH);
288        assertUpgradePlatformFromKlpToKlp(oldSdkVersion, VERSION_CODES.ICE_CREAM_SANDWICH_MR1);
289        assertUpgradePlatformFromKlpToKlp(oldSdkVersion, VERSION_CODES.JELLY_BEAN);
290        assertUpgradePlatformFromKlpToKlp(oldSdkVersion, VERSION_CODES.JELLY_BEAN_MR1);
291        assertUpgradePlatformFromKlpToKlp(oldSdkVersion, VERSION_CODES.JELLY_BEAN_MR2);
292        assertUpgradePlatformFromKlpToKlp(oldSdkVersion, VERSION_CODES.KITKAT);
293    }
294
295    // Update platform from I,J, and K to I,J, and K
296    public void testUpgradePlatformToKlpFromKlp() {
297        assertUpgradePlatformToKlpFrom(VERSION_CODES.ICE_CREAM_SANDWICH);
298        assertUpgradePlatformToKlpFrom(VERSION_CODES.ICE_CREAM_SANDWICH_MR1);
299        assertUpgradePlatformToKlpFrom(VERSION_CODES.JELLY_BEAN);
300        assertUpgradePlatformToKlpFrom(VERSION_CODES.JELLY_BEAN_MR1);
301        assertUpgradePlatformToKlpFrom(VERSION_CODES.JELLY_BEAN_MR2);
302        assertUpgradePlatformToKlpFrom(VERSION_CODES.KITKAT);
303    }
304
305    private void assertUpgradePlatformToLxxFrom(final int oldSdkVersion) {
306        // Forced to switch to LXX theme.
307        final int newSdkVersion = Build.VERSION_CODES.LOLLIPOP;
308        assertUpgradePlatformFromTo(
309                oldSdkVersion, newSdkVersion, THEME_ID_NULL, THEME_ID_LXX_LIGHT);
310        assertUpgradePlatformFromTo(
311                oldSdkVersion, newSdkVersion, THEME_ID_ICS, THEME_ID_LXX_LIGHT);
312        assertUpgradePlatformFromTo(
313                oldSdkVersion, newSdkVersion, THEME_ID_KLP, THEME_ID_LXX_LIGHT);
314        assertUpgradePlatformFromTo(
315                oldSdkVersion, newSdkVersion, THEME_ID_UNKNOWN, THEME_ID_LXX_LIGHT);
316        assertUpgradePlatformFromTo(
317                oldSdkVersion, newSdkVersion, THEME_ID_ILLEGAL, THEME_ID_LXX_LIGHT);
318    }
319
320    // Update platform from I,J, and K to L
321    public void testUpgradePlatformToLxx() {
322        assertUpgradePlatformToLxxFrom(VERSION_CODES.ICE_CREAM_SANDWICH);
323        assertUpgradePlatformToLxxFrom(VERSION_CODES.ICE_CREAM_SANDWICH_MR1);
324        assertUpgradePlatformToLxxFrom(VERSION_CODES.JELLY_BEAN);
325        assertUpgradePlatformToLxxFrom(VERSION_CODES.JELLY_BEAN_MR1);
326        assertUpgradePlatformToLxxFrom(VERSION_CODES.JELLY_BEAN_MR2);
327        assertUpgradePlatformToLxxFrom(VERSION_CODES.KITKAT);
328    }
329
330    // Update platform from L to L.
331    public void testUpgradePlatformToLxxFromLxx() {
332        final int oldSdkVersion = Build.VERSION_CODES.LOLLIPOP;
333        final int newSdkVersion = Build.VERSION_CODES.LOLLIPOP;
334        assertUpgradePlatformFromTo(
335                oldSdkVersion, newSdkVersion, THEME_ID_NULL, THEME_ID_LXX_LIGHT);
336        assertUpgradePlatformFromTo(
337                oldSdkVersion, newSdkVersion, THEME_ID_ICS, THEME_ID_ICS);
338        assertUpgradePlatformFromTo(
339                oldSdkVersion, newSdkVersion, THEME_ID_KLP, THEME_ID_KLP);
340        assertUpgradePlatformFromTo(
341                oldSdkVersion, newSdkVersion, THEME_ID_LXX_LIGHT, THEME_ID_LXX_LIGHT);
342        assertUpgradePlatformFromTo(
343                oldSdkVersion, newSdkVersion, THEME_ID_LXX_DARK, THEME_ID_LXX_DARK);
344        assertUpgradePlatformFromTo(
345                oldSdkVersion, newSdkVersion, THEME_ID_UNKNOWN, THEME_ID_LXX_LIGHT);
346        assertUpgradePlatformFromTo(
347                oldSdkVersion, newSdkVersion, THEME_ID_ILLEGAL, THEME_ID_LXX_LIGHT);
348    }
349
350    /*
351     * Test that KeyboardTheme array should be sorted by descending order of
352     * {@link KeyboardTheme#mMinApiVersion}.
353     */
354    private static void assertSortedKeyboardThemeArray(final KeyboardTheme[] array) {
355        assertNotNull(array);
356        final int length = array.length;
357        assertTrue("array length=" + length, length > 0);
358        for (int index = 0; index < length - 1; index++) {
359            final KeyboardTheme theme = array[index];
360            final KeyboardTheme nextTheme = array[index + 1];
361            assertTrue("sorted MinApiVersion: "
362                    + theme.mThemeName + ": minApiVersion=" + theme.mMinApiVersion,
363                    theme.mMinApiVersion >= nextTheme.mMinApiVersion);
364        }
365    }
366
367    public void testSortedKeyboardTheme() {
368        assertSortedKeyboardThemeArray(KeyboardTheme.KEYBOARD_THEMES);
369    }
370
371    public void testSortedAvailableKeyboardTheme() {
372        assertSortedKeyboardThemeArray(KeyboardTheme.getAvailableThemeArray(getContext()));
373    }
374
375    /*
376     * Test for missing selected theme.
377     */
378    private static KeyboardTheme[] LIMITED_THEMES = {
379        KeyboardTheme.searchKeyboardThemeById(THEME_ID_ICS, KeyboardTheme.KEYBOARD_THEMES),
380        KeyboardTheme.searchKeyboardThemeById(THEME_ID_KLP, KeyboardTheme.KEYBOARD_THEMES)
381    };
382    static {
383        Arrays.sort(LIMITED_THEMES);
384        assertSortedKeyboardThemeArray(LIMITED_THEMES);
385    }
386
387    public void testMissingSelectedThemeIcs() {
388        // Clean up preferences.
389        setKeyboardThemePreference(KeyboardTheme.KLP_KEYBOARD_THEME_KEY, THEME_ID_NULL);
390        setKeyboardThemePreference(KeyboardTheme.LXX_KEYBOARD_THEME_KEY, THEME_ID_NULL);
391
392        final int sdkVersion = VERSION_CODES.ICE_CREAM_SANDWICH;
393        final String oldPrefKey = KeyboardTheme.getPreferenceKey(sdkVersion);
394        setKeyboardThemePreference(oldPrefKey, THEME_ID_LXX_LIGHT);
395
396        final KeyboardTheme actualTheme = KeyboardTheme.getKeyboardTheme(
397                mPrefs, sdkVersion, LIMITED_THEMES);
398        // LXX_LIGHT is missing, fall-back to KLP.
399        assertEquals(THEME_ID_KLP, actualTheme.mThemeId);
400    }
401
402    public void testMissingSelectedThemeKlp() {
403        // Clean up preferences.
404        setKeyboardThemePreference(KeyboardTheme.KLP_KEYBOARD_THEME_KEY, THEME_ID_NULL);
405        setKeyboardThemePreference(KeyboardTheme.LXX_KEYBOARD_THEME_KEY, THEME_ID_NULL);
406
407        final int sdkVersion = VERSION_CODES.KITKAT;
408        final String oldPrefKey = KeyboardTheme.getPreferenceKey(sdkVersion);
409        setKeyboardThemePreference(oldPrefKey, THEME_ID_LXX_LIGHT);
410
411        final KeyboardTheme actualTheme = KeyboardTheme.getKeyboardTheme(
412                mPrefs, sdkVersion, LIMITED_THEMES);
413        // LXX_LIGHT is missing, fall-back to KLP.
414        assertEquals(THEME_ID_KLP, actualTheme.mThemeId);
415    }
416
417    public void testMissingSelectedThemeLxx() {
418        // Clean up preferences.
419        setKeyboardThemePreference(KeyboardTheme.KLP_KEYBOARD_THEME_KEY, THEME_ID_NULL);
420        setKeyboardThemePreference(KeyboardTheme.LXX_KEYBOARD_THEME_KEY, THEME_ID_NULL);
421
422        final int sdkVersion = Build.VERSION_CODES.LOLLIPOP;
423        final String oldPrefKey = KeyboardTheme.getPreferenceKey(sdkVersion);
424        setKeyboardThemePreference(oldPrefKey, THEME_ID_LXX_DARK);
425
426        final KeyboardTheme actualTheme = KeyboardTheme.getKeyboardTheme(
427                mPrefs, sdkVersion, LIMITED_THEMES);
428        // LXX_DARK is missing, fall-back to KLP.
429        assertEquals(THEME_ID_KLP, actualTheme.mThemeId);
430    }
431}
432