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.internal; 18 19 import android.content.Context; 20 import android.test.AndroidTestCase; 21 import android.test.suitebuilder.annotation.SmallTest; 22 import android.view.inputmethod.InputMethodInfo; 23 import android.view.inputmethod.InputMethodSubtype; 24 25 import com.android.inputmethod.latin.RichInputMethodManager; 26 import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; 27 28 import java.util.ArrayList; 29 import java.util.Collections; 30 import java.util.List; 31 import java.util.Locale; 32 33 @SmallTest 34 public final class KeyboardTextsSetTests extends AndroidTestCase { 35 // All input method subtypes of LatinIME. 36 private List<InputMethodSubtype> mAllSubtypesList; 37 38 @Override 39 protected void setUp() throws Exception { 40 super.setUp(); 41 RichInputMethodManager.init(getContext()); 42 final RichInputMethodManager richImm = RichInputMethodManager.getInstance(); 43 44 final ArrayList<InputMethodSubtype> allSubtypesList = new ArrayList<>(); 45 final InputMethodInfo imi = richImm.getInputMethodInfoOfThisIme(); 46 final int subtypeCount = imi.getSubtypeCount(); 47 for (int index = 0; index < subtypeCount; index++) { 48 final InputMethodSubtype subtype = imi.getSubtypeAt(index); 49 allSubtypesList.add(subtype); 50 } 51 mAllSubtypesList = Collections.unmodifiableList(allSubtypesList); 52 } 53 54 // Test that the text {@link KeyboardTextsSet#SWITCH_TO_ALPHA_KEY_LABEL} exists for all 55 // subtypes. The text is needed to implement Emoji Keyboard, see 56 // {@link KeyboardSwitcher#setEmojiKeyboard()}. 57 public void testSwitchToAlphaKeyLabel() { 58 final Context context = getContext(); 59 final KeyboardTextsSet textsSet = new KeyboardTextsSet(); 60 for (final InputMethodSubtype subtype : mAllSubtypesList) { 61 final Locale locale = SubtypeLocaleUtils.getSubtypeLocale(subtype); 62 textsSet.setLocale(locale, context); 63 final String switchToAlphaKeyLabel = textsSet.getText( 64 KeyboardTextsSet.SWITCH_TO_ALPHA_KEY_LABEL); 65 assertNotNull("Switch to alpha key label of " + locale, switchToAlphaKeyLabel); 66 assertFalse("Switch to alpha key label of " + locale, switchToAlphaKeyLabel.isEmpty()); 67 } 68 } 69 70 private static final String[] TEXT_NAMES_FROM_RESOURCE = { 71 // Labels for action. 72 "label_go_key", 73 "label_send_key", 74 "label_next_key", 75 "label_done_key", 76 "label_previous_key", 77 // Other labels. 78 "label_pause_key", 79 "label_wait_key", 80 }; 81 82 // Test that the text from resources are correctly loaded for all subtypes. 83 public void testTextFromResources() { 84 final Context context = getContext(); 85 final KeyboardTextsSet textsSet = new KeyboardTextsSet(); 86 for (final InputMethodSubtype subtype : mAllSubtypesList) { 87 final Locale locale = SubtypeLocaleUtils.getSubtypeLocale(subtype); 88 textsSet.setLocale(locale, context); 89 for (final String name : TEXT_NAMES_FROM_RESOURCE) { 90 final String text = textsSet.getText(name); 91 assertNotNull(name + " of " + locale, text); 92 assertFalse(name + " of " + locale, text.isEmpty()); 93 } 94 } 95 } 96 } 97