• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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 
17 package libcore.java.util;
18 
19 import java.text.BreakIterator;
20 import java.text.Collator;
21 import java.text.DateFormat;
22 import java.text.DateFormatSymbols;
23 import java.text.DecimalFormatSymbols;
24 import java.text.NumberFormat;
25 import java.util.Arrays;
26 import java.util.Calendar;
27 import java.util.Locale;
28 import java.util.MissingResourceException;
29 
30 public class LocaleTest extends junit.framework.TestCase {
31     // http://b/2611311; if there's no display language/country/variant, use the raw codes.
test_getDisplayName_invalid()32     public void test_getDisplayName_invalid() throws Exception {
33         Locale invalid = new Locale("AaBbCc", "DdEeFf", "GgHhIi");
34 
35         assertEquals("aabbcc", invalid.getLanguage());
36         assertEquals("DDEEFF", invalid.getCountry());
37         assertEquals("GgHhIi", invalid.getVariant());
38 
39         // Android using icu4c < 49.2 returned empty strings for display language, country,
40         // and variant, but a display name made up of the raw strings.
41         // Newer releases return slightly different results, but no less unreasonable.
42         assertEquals("aabbcc", invalid.getDisplayLanguage());
43         assertEquals("", invalid.getDisplayCountry());
44         assertEquals("DDEEFF_GGHHII", invalid.getDisplayVariant());
45         assertEquals("aabbcc (DDEEFF,DDEEFF_GGHHII)", invalid.getDisplayName());
46     }
47 
48     // http://b/2611311; if there's no display language/country/variant, use the raw codes.
test_getDisplayName_unknown()49     public void test_getDisplayName_unknown() throws Exception {
50         Locale unknown = new Locale("xx", "YY", "Traditional");
51         assertEquals("xx", unknown.getLanguage());
52         assertEquals("YY", unknown.getCountry());
53         assertEquals("Traditional", unknown.getVariant());
54 
55         assertEquals("xx", unknown.getDisplayLanguage());
56         assertEquals("YY", unknown.getDisplayCountry());
57         assertEquals("TRADITIONAL", unknown.getDisplayVariant());
58         assertEquals("xx (YY,TRADITIONAL)", unknown.getDisplayName());
59     }
60 
test_getDisplayName_easy()61     public void test_getDisplayName_easy() throws Exception {
62         assertEquals("English", Locale.ENGLISH.getDisplayLanguage(Locale.ENGLISH));
63         assertEquals("German", Locale.GERMAN.getDisplayLanguage(Locale.ENGLISH));
64         assertEquals("Englisch", Locale.ENGLISH.getDisplayLanguage(Locale.GERMAN));
65         assertEquals("Deutsch", Locale.GERMAN.getDisplayLanguage(Locale.GERMAN));
66     }
67 
test_getDisplayCountry_8870289()68     public void test_getDisplayCountry_8870289() throws Exception {
69         assertEquals("Hong Kong", new Locale("", "HK").getDisplayCountry(Locale.US));
70         assertEquals("Macau", new Locale("", "MO").getDisplayCountry(Locale.US));
71         assertEquals("Palestine", new Locale("", "PS").getDisplayCountry(Locale.US));
72 
73         assertEquals("Cocos [Keeling] Islands", new Locale("", "CC").getDisplayCountry(Locale.US));
74         assertEquals("Congo [DRC]", new Locale("", "CD").getDisplayCountry(Locale.US));
75         assertEquals("Congo [Republic]", new Locale("", "CG").getDisplayCountry(Locale.US));
76         assertEquals("Falkland Islands [Islas Malvinas]", new Locale("", "FK").getDisplayCountry(Locale.US));
77         assertEquals("Macedonia [FYROM]", new Locale("", "MK").getDisplayCountry(Locale.US));
78         assertEquals("Myanmar [Burma]", new Locale("", "MM").getDisplayCountry(Locale.US));
79         assertEquals("Taiwan", new Locale("", "TW").getDisplayCountry(Locale.US));
80     }
81 
test_tl()82     public void test_tl() throws Exception {
83         // In jb-mr1, we had a last-minute hack to always return "Filipino" because
84         // icu4c 4.8 didn't have any localizations for fil. (http://b/7291355)
85         Locale tl = new Locale("tl");
86         Locale tl_PH = new Locale("tl", "PH");
87         assertEquals("Filipino", tl.getDisplayLanguage(Locale.ENGLISH));
88         assertEquals("Filipino", tl_PH.getDisplayLanguage(Locale.ENGLISH));
89         assertEquals("Filipino", tl.getDisplayLanguage(tl));
90         assertEquals("Filipino", tl_PH.getDisplayLanguage(tl_PH));
91 
92         // After the icu4c 4.9 upgrade, we could localize "fil" correctly, though we
93         // needed another hack to supply "fil" instead of "tl" to icu4c. (http://b/8023288)
94         Locale es_MX = new Locale("es", "MX");
95         assertEquals("filipino", tl.getDisplayLanguage(es_MX));
96         assertEquals("filipino", tl_PH.getDisplayLanguage(es_MX));
97       }
98 
99     // http://b/3452611; Locale.getDisplayLanguage fails for the obsolete language codes.
test_getDisplayName_obsolete()100     public void test_getDisplayName_obsolete() throws Exception {
101         // he (new) -> iw (obsolete)
102         assertObsolete("he", "iw", "עברית");
103         // id (new) -> in (obsolete)
104         assertObsolete("id", "in", "Bahasa Indonesia");
105     }
106 
assertObsolete(String newCode, String oldCode, String displayName)107     private static void assertObsolete(String newCode, String oldCode, String displayName) {
108         // Either code should get you the same locale.
109         Locale newLocale = new Locale(newCode);
110         Locale oldLocale = new Locale(oldCode);
111         assertEquals(newLocale, oldLocale);
112 
113         // No matter what code you used to create the locale, you should get the old code back.
114         assertEquals(oldCode, newLocale.getLanguage());
115         assertEquals(oldCode, oldLocale.getLanguage());
116 
117         // Check we get the right display name.
118         assertEquals(displayName, newLocale.getDisplayLanguage(newLocale));
119         assertEquals(displayName, oldLocale.getDisplayLanguage(newLocale));
120         assertEquals(displayName, newLocale.getDisplayLanguage(oldLocale));
121         assertEquals(displayName, oldLocale.getDisplayLanguage(oldLocale));
122 
123         // Check that none of the 'getAvailableLocales' methods are accidentally returning two
124         // equal locales (because to ICU they're different, but we mangle one into the other).
125         assertOnce(newLocale, BreakIterator.getAvailableLocales());
126         assertOnce(newLocale, Calendar.getAvailableLocales());
127         assertOnce(newLocale, Collator.getAvailableLocales());
128         assertOnce(newLocale, DateFormat.getAvailableLocales());
129         assertOnce(newLocale, DateFormatSymbols.getAvailableLocales());
130         assertOnce(newLocale, NumberFormat.getAvailableLocales());
131         assertOnce(newLocale, Locale.getAvailableLocales());
132     }
133 
assertOnce(Locale element, Locale[] array)134     private static void assertOnce(Locale element, Locale[] array) {
135         int count = 0;
136         for (Locale l : array) {
137             if (l.equals(element)) {
138                 ++count;
139             }
140         }
141         assertEquals(1, count);
142     }
143 
test_getISO3Country()144     public void test_getISO3Country() {
145         // Empty country code.
146         assertEquals("", new Locale("en", "").getISO3Country());
147 
148         // Invalid country code.
149         try {
150             assertEquals("", new Locale("en", "XX").getISO3Country());
151             fail();
152         } catch (MissingResourceException expected) {
153             assertEquals("FormatData_en_XX", expected.getClassName());
154             assertEquals("ShortCountry", expected.getKey());
155         }
156 
157         // Valid country code.
158         assertEquals("CAN", new Locale("", "CA").getISO3Country());
159         assertEquals("CAN", new Locale("en", "CA").getISO3Country());
160         assertEquals("CAN", new Locale("xx", "CA").getISO3Country());
161     }
162 
test_getISO3Language()163     public void test_getISO3Language() {
164         // Empty language code.
165         assertEquals("", new Locale("", "US").getISO3Language());
166 
167         // Invalid language code.
168         try {
169             assertEquals("", new Locale("xx", "US").getISO3Language());
170             fail();
171         } catch (MissingResourceException expected) {
172             assertEquals("FormatData_xx_US", expected.getClassName());
173             assertEquals("ShortLanguage", expected.getKey());
174         }
175 
176         // Valid language code.
177         assertEquals("eng", new Locale("en", "").getISO3Language());
178         assertEquals("eng", new Locale("en", "CA").getISO3Language());
179         assertEquals("eng", new Locale("en", "XX").getISO3Language());
180     }
181   }
182