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.providers.contacts;
18
19import android.test.suitebuilder.annotation.SmallTest;
20import java.util.Locale;
21import junit.framework.TestCase;
22
23@SmallTest
24public class LocaleSetTest extends TestCase {
25    private void testLocaleStringsHelper(Locale primaryLocale,
26            Locale secondaryLocale, final String expectedString) throws Exception {
27        final LocaleSet locales = new LocaleSet(primaryLocale, secondaryLocale);
28        final String localeString = locales.toString();
29        assertEquals(expectedString, localeString);
30
31        final LocaleSet parseLocales = LocaleSet.getLocaleSet(localeString);
32        assertEquals(locales, parseLocales);
33    }
34
35    @SmallTest
36    public void testLocaleStrings() throws Exception {
37        testLocaleStringsHelper(Locale.US, null, "en-US");
38        testLocaleStringsHelper(Locale.US, Locale.CHINA, "en-US;zh-CN");
39        testLocaleStringsHelper(Locale.JAPAN, Locale.GERMANY, "ja-JP;de-DE");
40    }
41
42    private void testNormalizationHelper(String localeString,
43            Locale expectedPrimary, Locale expectedSecondary) throws Exception {
44        final LocaleSet expected = new LocaleSet(expectedPrimary, expectedSecondary);
45        final LocaleSet actual = LocaleSet.getLocaleSet(localeString).normalize();
46        assertEquals(expected, actual);
47    }
48
49    @SmallTest
50    public void testNormalization() throws Exception {
51        // Single locale
52        testNormalizationHelper("en-US", Locale.US, null);
53        // Disallow secondary with same language as primary
54        testNormalizationHelper("fr-CA;fr-FR", Locale.CANADA_FRENCH, null);
55        testNormalizationHelper("en-US;zh-CN", Locale.US, Locale.CHINA);
56        // Disallow both locales CJK
57        testNormalizationHelper("ja-JP;zh-CN", Locale.JAPAN, null);
58        // Disallow en as secondary (happens by default)
59        testNormalizationHelper("zh-CN;en-US", Locale.CHINA, null);
60        testNormalizationHelper("zh-CN;de-DE", Locale.CHINA, Locale.GERMANY);
61    }
62}
63