1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17package com.android.inputmethod.latin; 18 19import com.android.inputmethod.latin.LocaleUtils; 20 21import android.content.Context; 22import android.content.res.Resources; 23import android.test.AndroidTestCase; 24import android.view.inputmethod.InputMethodInfo; 25import android.view.inputmethod.InputMethodManager; 26import android.view.inputmethod.InputMethodSubtype; 27 28import java.util.ArrayList; 29import java.util.List; 30import java.util.Locale; 31 32public class SubtypeLocaleTests extends AndroidTestCase { 33 private static final String PACKAGE = LatinIME.class.getPackage().getName(); 34 35 private Resources mRes; 36 private List<InputMethodSubtype> mKeyboardSubtypes = new ArrayList<InputMethodSubtype>(); 37 38 @Override 39 protected void setUp() throws Exception { 40 super.setUp(); 41 42 final Context context = getContext(); 43 mRes = context.getResources(); 44 45 SubtypeLocale.init(context); 46 47 final InputMethodManager imm = (InputMethodManager) context.getSystemService( 48 Context.INPUT_METHOD_SERVICE); 49 for (final InputMethodInfo imi : imm.getInputMethodList()) { 50 if (imi.getPackageName().equals(PACKAGE)) { 51 final int subtypeCount = imi.getSubtypeCount(); 52 for (int i = 0; i < subtypeCount; ++i) { 53 InputMethodSubtype subtype = imi.getSubtypeAt(i); 54 if (subtype.getMode().equals("keyboard")) { 55 mKeyboardSubtypes.add(subtype); 56 } 57 } 58 break; 59 } 60 } 61 assertNotNull("Can not find input method " + PACKAGE, mKeyboardSubtypes); 62 assertTrue("Can not find keyboard subtype", mKeyboardSubtypes.size() > 0); 63 } 64 65 private String getStringWithLocale(int resId, Locale locale) { 66 final Locale savedLocale = Locale.getDefault(); 67 try { 68 Locale.setDefault(locale); 69 return mRes.getString(resId); 70 } finally { 71 Locale.setDefault(savedLocale); 72 } 73 } 74 75 public void testSubtypeLocale() { 76 final StringBuilder messages = new StringBuilder(); 77 int failedCount = 0; 78 for (final InputMethodSubtype subtype : mKeyboardSubtypes) { 79 final String localeCode = subtype.getLocale(); 80 final Locale locale = LocaleUtils.constructLocaleFromString(localeCode); 81 // The locale name which will be displayed on spacebar. For example 'English (US)' or 82 // 'Francais (Canada)'. (c=\u008d) 83 final String displayName = SubtypeLocale.getFullDisplayName(locale); 84 // The subtype name in its locale. For example 'English (US) Keyboard' or 85 // 'Clavier Francais (Canada)'. (c=\u008d) 86 final String subtypeName = getStringWithLocale(subtype.getNameResId(), locale); 87 if (subtypeName.contains(displayName)) { 88 failedCount++; 89 messages.append(String.format( 90 "subtype name is '%s' and should contain locale '%s' name '%s'\n", 91 subtypeName, localeCode, displayName)); 92 } 93 } 94 assertEquals(messages.toString(), 0, failedCount); 95 } 96} 97