1/*
2 * Copyright (C) 2015 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 */
16package android.os;
17
18import android.test.suitebuilder.annotation.SmallTest;
19
20import java.util.Locale;
21
22import junit.framework.TestCase;
23
24public class LocaleListTest extends TestCase {
25    @SmallTest
26    public void testConstructor() throws Exception {
27        LocaleList ll;
28        ll = new LocaleList(Locale.forLanguageTag("fr"), null);
29        assertEquals("fr", ll.toLanguageTags());
30
31        ll = new LocaleList(Locale.forLanguageTag("fr"), LocaleList.getEmptyLocaleList());
32        assertEquals("fr", ll.toLanguageTags());
33
34        ll = new LocaleList(Locale.forLanguageTag("fr"), LocaleList.forLanguageTags("fr"));
35        assertEquals("fr", ll.toLanguageTags());
36
37        ll = new LocaleList(Locale.forLanguageTag("fr"), LocaleList.forLanguageTags("de"));
38        assertEquals("fr,de", ll.toLanguageTags());
39
40        ll = new LocaleList(Locale.forLanguageTag("fr"), LocaleList.forLanguageTags("de,ja"));
41        assertEquals("fr,de,ja", ll.toLanguageTags());
42
43        ll = new LocaleList(Locale.forLanguageTag("fr"), LocaleList.forLanguageTags("de,fr,ja"));
44        assertEquals("fr,de,ja", ll.toLanguageTags());
45
46        ll = new LocaleList(Locale.forLanguageTag("fr"), LocaleList.forLanguageTags("de,fr"));
47        assertEquals("fr,de", ll.toLanguageTags());
48
49        ll = new LocaleList(Locale.forLanguageTag("fr"), LocaleList.forLanguageTags("fr,de"));
50        assertEquals("fr,de", ll.toLanguageTags());
51    }
52
53    @SmallTest
54    public void testConstructor_nullThrows() throws Exception {
55        try {
56            final LocaleList ll = new LocaleList(null, LocaleList.getEmptyLocaleList());
57            fail("Constructing with locale and locale list should throw with a null locale.");
58        } catch (Throwable e) {
59            assertEquals(NullPointerException.class, e.getClass());
60        }
61    }
62
63    @SmallTest
64    public void testGetDefault_localeSetDefaultCalledButNoChangeNecessary() throws Exception {
65        final Locale originalLocale = Locale.getDefault();
66        final LocaleList originalLocaleList = LocaleList.getDefault();
67        final int originalLocaleIndex = originalLocaleList.indexOf(originalLocale);
68
69        // This simulates a situation potentially set by the system processes
70        LocaleList.setDefault(LocaleList.forLanguageTags("ae,en,ja"), 1 /* en */);
71
72        // check our assumptions about input
73        assertEquals("en", Locale.getDefault().toLanguageTag());
74        final LocaleList firstResult = LocaleList.getDefault();
75        assertEquals("ae,en,ja", LocaleList.getDefault().toLanguageTags());
76
77        Locale.setDefault(Locale.forLanguageTag("ae"));
78        assertSame(firstResult, LocaleList.getDefault());
79
80        // restore the original values
81        LocaleList.setDefault(originalLocaleList, originalLocaleIndex);
82    }
83}
84