LocaleListCompatTest.java revision 01f7e32cdf00e64181661c34a3190b70ad4c79af
1/*
2 * Copyright (C) 2017 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 android.support.v4.os;
18
19
20import static junit.framework.Assert.assertEquals;
21import static junit.framework.TestCase.assertFalse;
22import static junit.framework.TestCase.assertSame;
23import static junit.framework.TestCase.fail;
24
25import static org.junit.Assert.assertNotNull;
26import static org.junit.Assert.assertNull;
27import static org.junit.Assert.assertTrue;
28
29import android.annotation.TargetApi;
30import android.os.Build;
31import android.support.test.filters.SdkSuppress;
32import android.support.test.filters.SmallTest;
33import android.support.test.runner.AndroidJUnit4;
34
35import org.junit.Test;
36import org.junit.runner.RunWith;
37
38import java.util.Locale;
39
40/**
41 * Tests for {@link LocaleListCompat}.
42 */
43@RunWith(AndroidJUnit4.class)
44@SmallTest
45public class LocaleListCompatTest {
46
47    @Test
48    public void testEmptyLocaleList() {
49        LocaleListCompat ll = LocaleListCompat.create();
50        assertNotNull(ll);
51        assertTrue(ll.isEmpty());
52        assertEquals(0, ll.size());
53        assertNull(ll.get(0));
54        assertNull(ll.get(1));
55        assertNull(ll.get(10));
56
57        ll = LocaleListCompat.create(new Locale[0]);
58        assertNotNull(ll);
59        assertTrue(ll.isEmpty());
60        assertEquals(0, ll.size());
61        assertNull(ll.get(0));
62        assertNull(ll.get(1));
63        assertNull(ll.get(10));
64    }
65
66    @Test
67    public void testOneMemberLocaleList() {
68        final LocaleListCompat ll = LocaleListCompat.create(Locale.US);
69        assertNotNull(ll);
70        assertFalse(ll.isEmpty());
71        assertEquals(1, ll.size());
72        assertEquals(Locale.US, ll.get(0));
73        assertNull(ll.get(10));
74    }
75
76    @Test
77    public void testTwoMemberLocaleList() {
78        final Locale enPH = forLanguageTag("en-PH");
79        final Locale[] la = {enPH, Locale.US};
80        final LocaleListCompat ll = LocaleListCompat.create(la);
81        assertNotNull(ll);
82        assertFalse(ll.isEmpty());
83        assertEquals(2, ll.size());
84        assertEquals(enPH, ll.get(0));
85        assertEquals(Locale.US, ll.get(1));
86        assertNull(ll.get(10));
87    }
88
89    @Test
90    public void testNullArgument() {
91        try {
92            LocaleListCompat.create((Locale) null);
93            fail("Initializing a LocaleListCompat with a null argument should throw.");
94        } catch (Throwable e) {
95            assertEquals(NullPointerException.class, e.getClass());
96        }
97        try {
98            LocaleListCompat.create((Locale[]) null);
99            fail("Initializing a LocaleListCompat with a null array should throw.");
100        } catch (Throwable e) {
101            assertEquals(NullPointerException.class, e.getClass());
102        }
103    }
104
105    @Test
106    public void testNullArguments() {
107        final Locale[] la = {Locale.US, null};
108        try {
109            LocaleListCompat.create(la);
110            fail("Initializing a LocaleListCompat with an array containing null should throw.");
111        } catch (Throwable e) {
112            assertEquals(NullPointerException.class, e.getClass());
113        }
114    }
115
116    @Test
117    public void testRepeatedArguments() {
118        final Locale[] la = {Locale.US, Locale.US};
119        try {
120            LocaleListCompat.create(la);
121            fail("Initializing a LocaleListCompat with an array containing duplicates should "
122                    + "throw.");
123        } catch (Throwable e) {
124            assertEquals(IllegalArgumentException.class, e.getClass());
125        }
126    }
127
128    @Test
129    public void testIndexOf() {
130        final LocaleListCompat empty = LocaleListCompat.create();
131        assertEquals(-1, empty.indexOf(Locale.US));
132
133        final LocaleListCompat oneMember = LocaleListCompat.create(Locale.US);
134        assertEquals(0, oneMember.indexOf(Locale.US));
135        assertEquals(-1, oneMember.indexOf(Locale.ENGLISH));
136
137        final LocaleListCompat twoMember = LocaleListCompat.forLanguageTags("en,fr");
138        assertEquals(0, twoMember.indexOf(forLanguageTag("en")));
139        assertEquals(1, twoMember.indexOf(forLanguageTag("fr")));
140        assertEquals(-1, twoMember.indexOf(forLanguageTag("en-US")));
141    }
142
143    @Test
144    public void testToString() {
145        LocaleListCompat ll = LocaleListCompat.create();
146        assertEquals("[]", ll.toString());
147
148        final Locale[] la1 = {Locale.US};
149        ll = LocaleListCompat.create(la1);
150        assertEquals("[" + Locale.US.toString() + "]", ll.toString());
151
152        final Locale[] la2 = {Locale.US, Locale.FRENCH};
153        ll = LocaleListCompat.create(la2);
154        assertEquals("[" + Locale.US.toString() + "," + Locale.FRENCH.toString() + "]",
155                ll.toString());
156    }
157
158    @Test
159    public void testToLanguageTags() {
160        LocaleListCompat ll = LocaleListCompat.create();
161        assertEquals("", ll.toLanguageTags());
162
163        final Locale[] la1 = {Locale.US};
164        ll = LocaleListCompat.create(la1);
165        assertEquals(toLanguageTag(Locale.US), ll.toLanguageTags());
166
167        final Locale[] la2 = {Locale.US, Locale.FRENCH};
168        ll = LocaleListCompat.create(la2);
169        assertEquals(toLanguageTag(Locale.US) + "," + toLanguageTag(Locale.FRENCH),
170                ll.toLanguageTags());
171    }
172
173    @Test
174    public void testGetEmptyLocaleList() {
175        LocaleListCompat empty = LocaleListCompat.getEmptyLocaleList();
176        LocaleListCompat anotherEmpty = LocaleListCompat.getEmptyLocaleList();
177        LocaleListCompat constructedEmpty = LocaleListCompat.create();
178
179        assertEquals(constructedEmpty, empty);
180        assertSame(empty, anotherEmpty);
181    }
182
183    @Test
184    public void testForLanguageTags() {
185        assertEquals(LocaleListCompat.getEmptyLocaleList(), LocaleListCompat.forLanguageTags(null));
186        assertEquals(LocaleListCompat.getEmptyLocaleList(), LocaleListCompat.forLanguageTags(""));
187
188        assertEquals(LocaleListCompat.create(forLanguageTag("en-US")),
189                LocaleListCompat.forLanguageTags("en-US"));
190
191        final Locale[] la = {forLanguageTag("en-PH"), forLanguageTag("en-US")};
192        assertEquals(LocaleListCompat.create(la),
193                LocaleListCompat.forLanguageTags("en-PH,en-US"));
194    }
195
196    @Test
197    public void testGetDefault() {
198        final LocaleListCompat ll = LocaleListCompat.getDefault();
199        assertNotNull(ll);
200        assertTrue(ll.size() >= 1);
201
202        final Locale defaultLocale = Locale.getDefault();
203        assertTrue(ll.indexOf(defaultLocale) != -1);
204    }
205
206    @Test
207    public void testGetAdjustedDefault() {
208        final LocaleListCompat ll = LocaleListCompat.getDefault();
209        assertNotNull(ll);
210        assertTrue(ll.size() >= 1);
211
212        final Locale defaultLocale = Locale.getDefault();
213        assertTrue(ll.indexOf(defaultLocale) == 0);
214    }
215
216    @Test
217    public void testGetFirstMatch_noAssets() {
218        String[] noAssets = {};
219        assertNull(LocaleListCompat.getEmptyLocaleList().getFirstMatch(noAssets));
220        assertEquals(
221                forLanguageTag("fr-BE"),
222                LocaleListCompat.forLanguageTags("fr-BE").getFirstMatch(noAssets));
223        assertEquals(
224                forLanguageTag("fr-BE"),
225                LocaleListCompat.forLanguageTags("fr-BE,nl-BE").getFirstMatch(noAssets));
226    }
227
228    @Test
229    public void testGetFirstMatch_oneAsset() {
230        String[] oneDutchAsset = {"nl"};
231        assertEquals(
232                forLanguageTag("fr-BE"),
233                LocaleListCompat.forLanguageTags("fr-BE").getFirstMatch(oneDutchAsset));
234        assertEquals(
235                forLanguageTag("nl-BE"),
236                LocaleListCompat.forLanguageTags("nl-BE").getFirstMatch(oneDutchAsset));
237        assertEquals(
238                forLanguageTag("nl-BE"),
239                LocaleListCompat.forLanguageTags("fr-BE,nl-BE").getFirstMatch(oneDutchAsset));
240        assertEquals(
241                forLanguageTag("nl-BE"),
242                LocaleListCompat.forLanguageTags("nl-BE,fr-BE").getFirstMatch(oneDutchAsset));
243    }
244
245    @Test
246    public void testGetFirstMatch_twoAssets() {
247        String[] FrenchAndDutchAssets = {"fr", "nl"};
248        assertEquals(
249                forLanguageTag("fr-BE"),
250                LocaleListCompat.forLanguageTags("fr-BE").getFirstMatch(FrenchAndDutchAssets));
251        assertEquals(
252                forLanguageTag("nl-BE"),
253                LocaleListCompat.forLanguageTags("nl-BE").getFirstMatch(FrenchAndDutchAssets));
254        assertEquals(
255                forLanguageTag("fr-BE"),
256                LocaleListCompat.forLanguageTags("fr-BE,nl-BE")
257                        .getFirstMatch(FrenchAndDutchAssets));
258        assertEquals(
259                forLanguageTag("nl-BE"),
260                LocaleListCompat.forLanguageTags("nl-BE,fr-BE")
261                        .getFirstMatch(FrenchAndDutchAssets));
262    }
263
264    @SdkSuppress(minSdkVersion = 24)
265    @TargetApi(24)
266    @Test
267    public void testGetFirstMatch_oneChineseAsset() {
268        String[] oneChineseAsset = {"zh-CN"};  // Assumed to mean zh-Hans-CN
269        // The following Chinese examples would all match, so they will be chosen.
270        assertEquals(
271                forLanguageTag("zh"),
272                LocaleListCompat.forLanguageTags("ko-KR,zh").getFirstMatch(oneChineseAsset));
273        assertEquals(
274                forLanguageTag("zh-CN"),
275                LocaleListCompat.forLanguageTags("ko-KR,zh-CN").getFirstMatch(oneChineseAsset));
276        assertEquals(
277                forLanguageTag("zh-Hans"),
278                LocaleListCompat.forLanguageTags("ko-KR,zh-Hans").getFirstMatch(oneChineseAsset));
279        assertEquals(
280                forLanguageTag("zh-Hans-CN"),
281                LocaleListCompat.forLanguageTags("ko-KR,zh-Hans-CN")
282                        .getFirstMatch(oneChineseAsset));
283        assertEquals(
284                forLanguageTag("zh-Hans-HK"),
285                LocaleListCompat.forLanguageTags("ko-KR,zh-Hans-HK")
286                        .getFirstMatch(oneChineseAsset));
287
288        // The following Chinese examples wouldn't match, so the first locale will be chosen
289        // instead.
290        assertEquals(
291                forLanguageTag("ko-KR"),
292                LocaleListCompat.forLanguageTags("ko-KR,zh-TW").getFirstMatch(oneChineseAsset));
293        assertEquals(
294                forLanguageTag("ko-KR"),
295                LocaleListCompat.forLanguageTags("ko-KR,zh-Hant").getFirstMatch(oneChineseAsset));
296        assertEquals(
297                forLanguageTag("ko-KR"),
298                LocaleListCompat.forLanguageTags("ko-KR,zh-Hant-TW")
299                        .getFirstMatch(oneChineseAsset));
300    }
301
302    @SdkSuppress(minSdkVersion = 24)
303    @TargetApi(24)
304    @Test
305    public void testGetFirstMatch_serbianCyrillic() {
306        String[] oneSerbianAsset = {"sr"};  // Assumed to mean sr-Cyrl-RS
307        // The following Serbian examples would all match, so they will be chosen.
308        assertEquals(
309                forLanguageTag("sr"),
310                LocaleListCompat.forLanguageTags("hr-HR,sr").getFirstMatch(oneSerbianAsset));
311        assertEquals(
312                forLanguageTag("sr-RS"),
313                LocaleListCompat.forLanguageTags("hr-HR,sr-RS").getFirstMatch(oneSerbianAsset));
314        assertEquals(
315                forLanguageTag("sr-Cyrl"),
316                LocaleListCompat.forLanguageTags("hr-HR,sr-Cyrl").getFirstMatch(oneSerbianAsset));
317        assertEquals(
318                forLanguageTag("sr-Cyrl-RS"),
319                LocaleListCompat.forLanguageTags("hr-HR,sr-Cyrl-RS")
320                        .getFirstMatch(oneSerbianAsset));
321        assertEquals(
322                forLanguageTag("sr-Cyrl-ME"),
323                LocaleListCompat.forLanguageTags("hr-HR,sr-Cyrl-ME")
324                        .getFirstMatch(oneSerbianAsset));
325
326        // The following Serbian examples wouldn't match, so the first locale will be chosen
327        // instead.
328        assertEquals(
329                forLanguageTag("hr-HR"),
330                LocaleListCompat.forLanguageTags("hr-HR,sr-ME").getFirstMatch(oneSerbianAsset));
331        assertEquals(
332                forLanguageTag("hr-HR"),
333                LocaleListCompat.forLanguageTags("hr-HR,sr-Latn").getFirstMatch(oneSerbianAsset));
334        assertEquals(
335                forLanguageTag("hr-HR"),
336                LocaleListCompat.forLanguageTags("hr-HR,sr-Latn-ME")
337                        .getFirstMatch(oneSerbianAsset));
338    }
339
340    @Test
341    public void testGetFirstMatch_LtrPseudoLocale() {
342        String[] onePseudoLocale = {"en-XA"};
343        // "en-XA" matches itself
344        assertEquals(
345                forLanguageTag("en-XA"),
346                LocaleListCompat.forLanguageTags("sr,en-XA").getFirstMatch(onePseudoLocale));
347
348        // "en-XA" doesn't match "en" or "en-US"
349        assertEquals(
350                forLanguageTag("sr"),
351                LocaleListCompat.forLanguageTags("sr,en").getFirstMatch(onePseudoLocale));
352        assertEquals(
353                forLanguageTag("sr"),
354                LocaleListCompat.forLanguageTags("sr,en-US").getFirstMatch(onePseudoLocale));
355    }
356
357    @Test
358    public void testGetFirstMatch_RtlPseudoLocale() {
359        String[] onePseudoLocale = {"ar-XB"};
360        // "ar-XB" matches itself
361        assertEquals(
362                forLanguageTag("ar-XB"),
363                LocaleListCompat.forLanguageTags("sr,ar-XB").getFirstMatch(onePseudoLocale));
364
365        // "ar-XB" doesn't match "ar" or "ar-EG"
366        assertEquals(
367                forLanguageTag("sr"),
368                LocaleListCompat.forLanguageTags("sr,ar").getFirstMatch(onePseudoLocale));
369        assertEquals(
370                forLanguageTag("sr"),
371                LocaleListCompat.forLanguageTags("sr,ar-EG").getFirstMatch(onePseudoLocale));
372    }
373
374    @Test
375    public void testGetFirstMatch_privateUseWithoutCountry() {
376        String[] onePrivateLocale = {"qaa"};
377        // "qaa" supports itself and "qaa-CA"
378        assertEquals(
379                forLanguageTag("qaa"),
380                LocaleListCompat.forLanguageTags("sr,qaa").getFirstMatch(onePrivateLocale));
381        assertEquals(
382                forLanguageTag("qaa-CA"),
383                LocaleListCompat.forLanguageTags("sr,qaa-CA").getFirstMatch(onePrivateLocale));
384    }
385
386    @Test
387    public void testGetFirstMatch_privateUseWithCountry() {
388        String[] onePrivateLocale = {"qaa-US"};
389        // "qaa-US" supports itself
390        assertEquals(
391                forLanguageTag("qaa-US"),
392                LocaleListCompat.forLanguageTags("sr,qaa-US").getFirstMatch(onePrivateLocale));
393
394        // "qaa-US" doesn't support "qaa" or "qaa-CA"
395        assertEquals(
396                forLanguageTag("sr"),
397                LocaleListCompat.forLanguageTags("sr,qaa-CA").getFirstMatch(onePrivateLocale));
398        assertEquals(
399                forLanguageTag("sr"),
400                LocaleListCompat.forLanguageTags("sr,qaa").getFirstMatch(onePrivateLocale));
401    }
402
403    private Locale forLanguageTag(String str) {
404        if (Build.VERSION.SDK_INT >= 21) {
405            return Locale.forLanguageTag(str);
406        } else {
407            return LocaleHelper.forLanguageTag(str);
408        }
409    }
410
411    private String toLanguageTag(Locale locale) {
412        if (Build.VERSION.SDK_INT >= 21) {
413            return locale.toLanguageTag();
414        } else {
415            return LocaleHelper.toLanguageTag(locale);
416        }
417    }
418
419}
420