diff options
Diffstat (limited to 'tests/src/com')
3 files changed, 136 insertions, 26 deletions
diff --git a/tests/src/com/android/providers/contacts/ContactLocaleUtilsTest.java b/tests/src/com/android/providers/contacts/ContactLocaleUtilsTest.java index 7213941..394a1aa 100644 --- a/tests/src/com/android/providers/contacts/ContactLocaleUtilsTest.java +++ b/tests/src/com/android/providers/contacts/ContactLocaleUtilsTest.java @@ -19,6 +19,7 @@ package com.android.providers.contacts; import android.provider.ContactsContract.FullNameStyle; import android.test.AndroidTestCase; import android.test.suitebuilder.annotation.SmallTest; +import android.util.Log; import java.text.Collator; import java.util.ArrayList; @@ -30,6 +31,8 @@ import java.util.Locale; @SmallTest public class ContactLocaleUtilsTest extends AndroidTestCase { + private static final String TAG = "ContactLocaleUtilsTest"; + private static final String PHONE_NUMBER_1 = "+1 (650) 555-1212"; private static final String PHONE_NUMBER_2 = "650-555-1212"; private static final String LATIN_NAME = "John Smith"; @@ -94,7 +97,8 @@ public class ContactLocaleUtilsTest extends AndroidTestCase { private static final Locale LOCALE_ARABIC = new Locale("ar"); private static final Locale LOCALE_SERBIAN = new Locale("sr"); private static final Locale LOCALE_UKRAINIAN = new Locale("uk"); - private boolean hasChineseCollator; + private boolean hasSimplifiedChineseCollator; + private boolean hasTraditionalChineseCollator; private boolean hasJapaneseCollator; private boolean hasKoreanCollator; private boolean hasArabicCollator; @@ -107,8 +111,10 @@ public class ContactLocaleUtilsTest extends AndroidTestCase { super.setUp(); final Locale locale[] = Collator.getAvailableLocales(); for (int i = 0; i < locale.length; i++) { - if (locale[i].equals(Locale.CHINA)) { - hasChineseCollator = true; + if (LocaleSet.isLocaleSimplifiedChinese(locale[i])) { + hasSimplifiedChineseCollator = true; + } else if (LocaleSet.isLocaleTraditionalChinese(locale[i])) { + hasTraditionalChineseCollator = true; } else if (locale[i].equals(Locale.JAPAN)) { hasJapaneseCollator = true; } else if (locale[i].equals(Locale.KOREA)) { @@ -166,6 +172,7 @@ public class ContactLocaleUtilsTest extends AndroidTestCase { public void testJapaneseContactLocaleUtils() throws Exception { if (!hasJapaneseCollator) { + Log.w(TAG, "Japanese collator not found; skipping test"); return; } @@ -180,13 +187,13 @@ public class ContactLocaleUtilsTest extends AndroidTestCase { assertNull(getNameLookupKeys(CHINESE_NAME, FullNameStyle.CJK)); assertNull(getNameLookupKeys(CHINESE_NAME, FullNameStyle.CHINESE)); - // Following two tests are broken with ICU 50 - verifyLabels(getLabels(), LABELS_JA_JP); assertEquals("B", getLabel("Bob Smith")); + verifyLabels(getLabels(), LABELS_JA_JP); } public void testChineseContactLocaleUtils() throws Exception { - if (!hasChineseCollator) { + if (!hasSimplifiedChineseCollator) { + Log.w(TAG, "Simplified Chinese collator not found; skipping test"); return; } @@ -199,12 +206,16 @@ public class ContactLocaleUtilsTest extends AndroidTestCase { assertEquals("B", getLabel("Bob Smith")); verifyLabels(getLabels(), LABELS_EN_US); - ContactLocaleUtils.setLocale(Locale.TRADITIONAL_CHINESE); - assertEquals("#", getLabel(PHONE_NUMBER_1)); - assertEquals("#", getLabel(PHONE_NUMBER_2)); - assertEquals("J", getLabel(LATIN_NAME)); - assertEquals("7\u5283", getLabel(CHINESE_NAME)); - assertEquals("D", getLabel(CHINESE_LATIN_MIX_NAME_1)); + if (hasTraditionalChineseCollator) { + ContactLocaleUtils.setLocale(Locale.TRADITIONAL_CHINESE); + assertEquals("#", getLabel(PHONE_NUMBER_1)); + assertEquals("#", getLabel(PHONE_NUMBER_2)); + assertEquals("J", getLabel(LATIN_NAME)); + assertEquals("7\u5283", getLabel(CHINESE_NAME)); + assertEquals("D", getLabel(CHINESE_LATIN_MIX_NAME_1)); + } else { + Log.w(TAG, "Traditional Chinese collator not found"); + } ContactLocaleUtils.setLocale(Locale.SIMPLIFIED_CHINESE); Iterator<String> keys = getNameLookupKeys(CHINESE_NAME, @@ -217,14 +228,45 @@ public class ContactLocaleUtilsTest extends AndroidTestCase { keys = getNameLookupKeys(CHINESE_LATIN_MIX_NAME_2, FullNameStyle.CHINESE); verifyKeys(keys, CHINESE_LATIN_MIX_NAME_2_KEY); - // Following test broken with ICU 50 - ContactLocaleUtils.setLocale(Locale.TRADITIONAL_CHINESE); - verifyLabels(getLabels(), LABELS_ZH_TW); - assertEquals("B", getLabel("Bob Smith")); + if (hasTraditionalChineseCollator) { + ContactLocaleUtils.setLocale(Locale.TRADITIONAL_CHINESE); + assertEquals("B", getLabel("Bob Smith")); + verifyLabels(getLabels(), LABELS_ZH_TW); + } + } + + public void testPinyinEnabledSecondaryLocale() throws Exception { + if (!hasSimplifiedChineseCollator) { + Log.w(TAG, "Simplified Chinese collator not found; skipping test"); + return; + } + + ContactLocaleUtils.setLocales( + new LocaleSet(Locale.ENGLISH, Locale.SIMPLIFIED_CHINESE)); + assertEquals("D", getLabel(CHINESE_NAME)); + + Iterator<String> keys = getNameLookupKeys(CHINESE_NAME, + FullNameStyle.CHINESE); + verifyKeys(keys, CHINESE_NAME_KEY); + } + + public void testPinyinDisabledSecondaryLocale() throws Exception { + if (!hasSimplifiedChineseCollator) { + Log.w(TAG, "Simplified Chinese collator not found; skipping test"); + return; + } + + ContactLocaleUtils.setLocales( + new LocaleSet(Locale.ENGLISH, Locale.JAPAN)); + assertEquals("", getLabel(CHINESE_NAME)); + + assertNull(getNameLookupKeys(CHINESE_NAME, FullNameStyle.CHINESE)); + assertNull(getNameLookupKeys(CHINESE_NAME, FullNameStyle.CJK)); } public void testChineseStyleNameWithDifferentLocale() throws Exception { - if (!hasChineseCollator) { + if (!hasSimplifiedChineseCollator) { + Log.w(TAG, "Simplified Chinese collator not found; skipping test"); return; } @@ -232,7 +274,7 @@ public class ContactLocaleUtilsTest extends AndroidTestCase { assertNull(getNameLookupKeys(CHINESE_NAME, FullNameStyle.CHINESE)); assertNull(getNameLookupKeys(CHINESE_NAME, FullNameStyle.CJK)); - ContactLocaleUtils.setLocale(Locale.CHINA); + ContactLocaleUtils.setLocale(Locale.SIMPLIFIED_CHINESE); Iterator<String> keys = getNameLookupKeys(CHINESE_NAME, FullNameStyle.CJK); verifyKeys(keys, CHINESE_NAME_KEY); @@ -241,12 +283,15 @@ public class ContactLocaleUtilsTest extends AndroidTestCase { keys = getNameLookupKeys(LATIN_NAME_2, FullNameStyle.WESTERN); verifyKeys(keys, LATIN_NAME_KEY_2); - ContactLocaleUtils.setLocale(Locale.TRADITIONAL_CHINESE); - assertNull(getNameLookupKeys(CHINESE_NAME, FullNameStyle.CJK)); + if (hasTraditionalChineseCollator) { + ContactLocaleUtils.setLocale(Locale.TRADITIONAL_CHINESE); + assertNull(getNameLookupKeys(CHINESE_NAME, FullNameStyle.CJK)); + } } public void testKoreanContactLocaleUtils() throws Exception { if (!hasKoreanCollator) { + Log.w(TAG, "Korean collator not found; skipping test"); return; } @@ -261,6 +306,7 @@ public class ContactLocaleUtilsTest extends AndroidTestCase { public void testArabicContactLocaleUtils() throws Exception { if (!hasArabicCollator) { + Log.w(TAG, "Arabic collator not found; skipping test"); return; } @@ -272,6 +318,7 @@ public class ContactLocaleUtilsTest extends AndroidTestCase { public void testSerbianContactLocaleUtils() throws Exception { if (!hasSerbianCollator) { + Log.w(TAG, "Serbian collator not found; skipping test"); return; } @@ -282,6 +329,7 @@ public class ContactLocaleUtilsTest extends AndroidTestCase { public void testUkrainianContactLocaleUtils() throws Exception { if (!hasUkrainianCollator) { + Log.w(TAG, "Ukrainian collator not found; skipping test"); return; } diff --git a/tests/src/com/android/providers/contacts/HanziToPinyinTest.java b/tests/src/com/android/providers/contacts/HanziToPinyinTest.java index e797f2c..a39b5df 100644 --- a/tests/src/com/android/providers/contacts/HanziToPinyinTest.java +++ b/tests/src/com/android/providers/contacts/HanziToPinyinTest.java @@ -40,7 +40,7 @@ public class HanziToPinyinTest extends TestCase { private void test(final char hanzi, final String expectedPinyin) throws Exception { final String hanziString = Character.toString(hanzi); - ArrayList<Token> tokens = HanziToPinyin.getInstance().get(hanziString); + ArrayList<Token> tokens = HanziToPinyin.getInstance().getTokens(hanziString); assertEquals(tokens.size(), 1); final String newString = tokens.get(0).target; if (TextUtils.isEmpty(expectedPinyin)) { @@ -65,27 +65,27 @@ public class HanziToPinyinTest extends TestCase { if (!hasChineseTransliterator()) { return; } - ArrayList<Token> tokens = HanziToPinyin.getInstance().get(ONE_HANZI); + ArrayList<Token> tokens = HanziToPinyin.getInstance().getTokens(ONE_HANZI); assertEquals(tokens.size(), 1); assertEquals(tokens.get(0).type, Token.PINYIN); assertTrue(tokens.get(0).target.equalsIgnoreCase("DU")); - tokens = HanziToPinyin.getInstance().get(TWO_HANZI); + tokens = HanziToPinyin.getInstance().getTokens(TWO_HANZI); assertEquals(tokens.size(), 2); assertEquals(tokens.get(0).type, Token.PINYIN); assertEquals(tokens.get(1).type, Token.PINYIN); assertTrue(tokens.get(0).target.equalsIgnoreCase("DU")); assertTrue(tokens.get(1).target.equalsIgnoreCase("JUAN")); - tokens = HanziToPinyin.getInstance().get(ASSIC); + tokens = HanziToPinyin.getInstance().getTokens(ASSIC); assertEquals(tokens.size(), 1); assertEquals(tokens.get(0).type, Token.LATIN); - tokens = HanziToPinyin.getInstance().get(ONE_UNKNOWN); + tokens = HanziToPinyin.getInstance().getTokens(ONE_UNKNOWN); assertEquals(tokens.size(), 1); assertEquals(tokens.get(0).type, Token.UNKNOWN); - tokens = HanziToPinyin.getInstance().get(MISC); + tokens = HanziToPinyin.getInstance().getTokens(MISC); assertEquals(tokens.size(), 7); assertEquals(tokens.get(0).type, Token.LATIN); assertEquals(tokens.get(1).type, Token.PINYIN); diff --git a/tests/src/com/android/providers/contacts/LocaleSetTest.java b/tests/src/com/android/providers/contacts/LocaleSetTest.java new file mode 100644 index 0000000..4c1b8d3 --- /dev/null +++ b/tests/src/com/android/providers/contacts/LocaleSetTest.java @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.providers.contacts; + +import android.test.suitebuilder.annotation.SmallTest; +import java.util.Locale; +import junit.framework.TestCase; + +@SmallTest +public class LocaleSetTest extends TestCase { + private void testLocaleStringsHelper(Locale primaryLocale, + Locale secondaryLocale, final String expectedString) throws Exception { + final LocaleSet locales = new LocaleSet(primaryLocale, secondaryLocale); + final String localeString = locales.toString(); + assertEquals(expectedString, localeString); + + final LocaleSet parseLocales = LocaleSet.getLocaleSet(localeString); + assertEquals(locales, parseLocales); + } + + @SmallTest + public void testLocaleStrings() throws Exception { + testLocaleStringsHelper(Locale.US, null, "en-US"); + testLocaleStringsHelper(Locale.US, Locale.CHINA, "en-US;zh-CN"); + testLocaleStringsHelper(Locale.JAPAN, Locale.GERMANY, "ja-JP;de-DE"); + } + + private void testNormalizationHelper(String localeString, + Locale expectedPrimary, Locale expectedSecondary) throws Exception { + final LocaleSet expected = new LocaleSet(expectedPrimary, expectedSecondary); + final LocaleSet actual = LocaleSet.getLocaleSet(localeString).normalize(); + assertEquals(expected, actual); + } + + @SmallTest + public void testNormalization() throws Exception { + // Single locale + testNormalizationHelper("en-US", Locale.US, null); + // Disallow secondary with same language as primary + testNormalizationHelper("fr-CA;fr-FR", Locale.CANADA_FRENCH, null); + testNormalizationHelper("en-US;zh-CN", Locale.US, Locale.CHINA); + // Disallow both locales CJK + testNormalizationHelper("ja-JP;zh-CN", Locale.JAPAN, null); + // Disallow en as secondary (happens by default) + testNormalizationHelper("zh-CN;en-US", Locale.CHINA, null); + testNormalizationHelper("zh-CN;de-DE", Locale.CHINA, Locale.GERMANY); + } +} |