1 /* 2 * Copyright (C) 2009 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.libcore.icu; 18 19 import java.text.BreakIterator; 20 import java.text.Collator; 21 import java.util.Arrays; 22 import java.util.Locale; 23 import libcore.icu.ICU; 24 import libcore.test.annotation.NonCts; 25 import libcore.test.reasons.NonCtsReasons; 26 27 @NonCts(bug = 287231726, reason = NonCtsReasons.INTERNAL_APIS) 28 public class ICUTest extends junit.framework.TestCase { test_getISOLanguages()29 public void test_getISOLanguages() throws Exception { 30 // Check that corrupting our array doesn't affect other callers. 31 assertNotNull(ICU.getISOLanguages()[0]); 32 ICU.getISOLanguages()[0] = null; 33 assertNotNull(ICU.getISOLanguages()[0]); 34 } 35 test_getISOCountries()36 public void test_getISOCountries() throws Exception { 37 // Check that corrupting our array doesn't affect other callers. 38 assertNotNull(ICU.getISOCountries()[0]); 39 ICU.getISOCountries()[0] = null; 40 assertNotNull(ICU.getISOCountries()[0]); 41 } 42 test_getAvailableLocales()43 public void test_getAvailableLocales() throws Exception { 44 // Check that corrupting our array doesn't affect other callers. 45 assertNotNull(ICU.getAvailableLocales()[0]); 46 ICU.getAvailableLocales()[0] = null; 47 assertNotNull(ICU.getAvailableLocales()[0]); 48 } 49 test_getCurrencyCode()50 public void test_getCurrencyCode() { 51 assertEquals("USD", ICU.getCurrencyCode("US")); 52 assertEquals("CAD", ICU.getCurrencyCode("CA")); 53 assertEquals("HKD", ICU.getCurrencyCode("HK")); // a region 54 55 // Test invalid country codes 56 assertNull(ICU.getCurrencyCode("A")); 57 assertNull(ICU.getCurrencyCode(null)); 58 assertNull(ICU.getCurrencyCode("")); 59 assertNull(ICU.getCurrencyCode("AA")); // 2-charcter invalid country code 60 assertNull(ICU.getCurrencyCode("USA")); // 3-character country code 61 assertNull(ICU.getCurrencyCode("ZZZ")); 62 assertNull(ICU.getCurrencyCode("EURO")); 63 assertNull(ICU.getCurrencyCode("PREEURO")); 64 assertNull(ICU.getCurrencyCode("en_EURO")); 65 } 66 test_getBestDateTimePattern()67 public void test_getBestDateTimePattern() throws Exception { 68 assertEquals("d MMMM", ICU.getBestDateTimePattern("MMMMd", new Locale("ca", "ES"))); 69 assertEquals("d 'de' MMMM", ICU.getBestDateTimePattern("MMMMd", new Locale("es", "ES"))); 70 assertEquals("d. MMMM", ICU.getBestDateTimePattern("MMMMd", new Locale("de", "CH"))); 71 assertEquals("MMMM d", ICU.getBestDateTimePattern("MMMMd", new Locale("en", "US"))); 72 assertEquals("d LLLL", ICU.getBestDateTimePattern("MMMMd", new Locale("fa", "IR"))); 73 assertEquals("M月d日", ICU.getBestDateTimePattern("MMMMd", new Locale("ja", "JP"))); 74 } 75 test_localeFromString()76 public void test_localeFromString() throws Exception { 77 // localeFromString is pretty lenient. Some of these can't be round-tripped 78 // through Locale.toString. 79 assertEquals(Locale.ENGLISH, ICU.localeFromIcuLocaleId("en")); 80 assertEquals(Locale.ENGLISH, ICU.localeFromIcuLocaleId("en_")); 81 assertEquals(Locale.ENGLISH, ICU.localeFromIcuLocaleId("en__")); 82 assertEquals(Locale.US, ICU.localeFromIcuLocaleId("en_US")); 83 assertEquals(Locale.US, ICU.localeFromIcuLocaleId("en_US_")); 84 assertEquals(new Locale("", "US", ""), ICU.localeFromIcuLocaleId("_US")); 85 assertEquals(new Locale("", "US", ""), ICU.localeFromIcuLocaleId("_US_")); 86 assertEquals(new Locale("", "", "POSIX"), ICU.localeFromIcuLocaleId("__POSIX")); 87 assertEquals(new Locale("aa", "BB", "CCCCC"), ICU.localeFromIcuLocaleId("aa_BB_CCCCC")); 88 } 89 test_getScript_addLikelySubtags()90 public void test_getScript_addLikelySubtags() throws Exception { 91 assertEquals("Latn", ICU.getScript(ICU.addLikelySubtags("en_US"))); 92 assertEquals("Hebr", ICU.getScript(ICU.addLikelySubtags("he"))); 93 assertEquals("Hebr", ICU.getScript(ICU.addLikelySubtags("he_IL"))); 94 assertEquals("Hebr", ICU.getScript(ICU.addLikelySubtags("iw"))); 95 assertEquals("Hebr", ICU.getScript(ICU.addLikelySubtags("iw_IL"))); 96 } 97 test_addLikelySubtags()98 public void test_addLikelySubtags() throws Exception { 99 assertEquals("Latn", ICU.addLikelySubtags(new Locale("en", "US")).getScript()); 100 assertEquals("Hebr", ICU.addLikelySubtags(new Locale("he")).getScript()); 101 assertEquals("Hebr", ICU.addLikelySubtags(new Locale("he", "IL")).getScript()); 102 assertEquals("Hebr", ICU.addLikelySubtags(new Locale("iw")).getScript()); 103 assertEquals("Hebr", ICU.addLikelySubtags(new Locale("iw", "IL")).getScript()); 104 } 105 best(Locale l, String skeleton)106 private String best(Locale l, String skeleton) { 107 return ICU.getBestDateTimePattern(skeleton, l); 108 } 109 test_getDateFormatOrder()110 public void test_getDateFormatOrder() throws Exception { 111 // lv and fa use differing orders depending on whether you're using numeric or textual months. 112 Locale lv = new Locale("lv"); 113 assertEquals("[d, M, y]", Arrays.toString(ICU.getDateFormatOrder(best(lv, "yyyy-M-dd")))); 114 assertEquals("[y, d, M]", Arrays.toString(ICU.getDateFormatOrder(best(lv, "yyyy-MMM-dd")))); 115 assertEquals("[d, M, \u0000]", Arrays.toString(ICU.getDateFormatOrder(best(lv, "MMM-dd")))); 116 Locale fa = new Locale("fa"); 117 assertEquals("[y, M, d]", Arrays.toString(ICU.getDateFormatOrder(best(fa, "yyyy-M-dd")))); 118 assertEquals("[d, M, y]", Arrays.toString(ICU.getDateFormatOrder(best(fa, "yyyy-MMM-dd")))); 119 assertEquals("[d, M, \u0000]", Arrays.toString(ICU.getDateFormatOrder(best(fa, "MMM-dd")))); 120 121 // English differs on each side of the Atlantic. 122 Locale en_US = Locale.US; 123 assertEquals("[M, d, y]", Arrays.toString(ICU.getDateFormatOrder(best(en_US, "yyyy-M-dd")))); 124 assertEquals("[M, d, y]", Arrays.toString(ICU.getDateFormatOrder(best(en_US, "yyyy-MMM-dd")))); 125 assertEquals("[M, d, \u0000]", Arrays.toString(ICU.getDateFormatOrder(best(en_US, "MMM-dd")))); 126 Locale en_GB = Locale.UK; 127 assertEquals("[d, M, y]", Arrays.toString(ICU.getDateFormatOrder(best(en_GB, "yyyy-M-dd")))); 128 assertEquals("[d, M, y]", Arrays.toString(ICU.getDateFormatOrder(best(en_GB, "yyyy-MMM-dd")))); 129 assertEquals("[d, M, \u0000]", Arrays.toString(ICU.getDateFormatOrder(best(en_GB, "MMM-dd")))); 130 131 assertEquals("[y, M, d]", Arrays.toString(ICU.getDateFormatOrder("yyyy - 'why' '' 'ddd' MMM-dd"))); 132 133 try { 134 ICU.getDateFormatOrder("the quick brown fox jumped over the lazy dog"); 135 fail(); 136 } catch (IllegalArgumentException expected) { 137 } 138 139 try { 140 ICU.getDateFormatOrder("'"); 141 fail(); 142 } catch (IllegalArgumentException expected) { 143 } 144 145 try { 146 ICU.getDateFormatOrder("yyyy'"); 147 fail(); 148 } catch (IllegalArgumentException expected) { 149 } 150 151 try { 152 ICU.getDateFormatOrder("yyyy'MMM"); 153 fail(); 154 } catch (IllegalArgumentException expected) { 155 } 156 } 157 testScriptsPassedToIcu()158 public void testScriptsPassedToIcu() throws Exception { 159 Locale sr_Cyrl_BA = Locale.forLanguageTag("sr-Cyrl-BA"); 160 Locale sr_Cyrl_ME = Locale.forLanguageTag("sr-Cyrl-ME"); 161 Locale sr_Latn_BA = Locale.forLanguageTag("sr-Latn-BA"); 162 Locale sr_Latn_ME = Locale.forLanguageTag("sr-Latn-ME"); 163 164 assertEquals("sr_BA_#Cyrl", sr_Cyrl_BA.toString()); 165 assertEquals("Cyrl", sr_Cyrl_BA.getScript()); 166 167 assertEquals("sr_ME_#Cyrl", sr_Cyrl_ME.toString()); 168 assertEquals("Cyrl", sr_Cyrl_ME.getScript()); 169 170 assertEquals("sr_BA_#Latn", sr_Latn_BA.toString()); 171 assertEquals("Latn", sr_Latn_BA.getScript()); 172 173 assertEquals("sr_ME_#Latn", sr_Latn_ME.toString()); 174 assertEquals("Latn", sr_Latn_ME.getScript()); 175 176 assertEquals("српски", sr_Cyrl_BA.getDisplayLanguage(sr_Cyrl_BA)); 177 assertEquals("Босна и Херцеговина", sr_Cyrl_BA.getDisplayCountry(sr_Cyrl_BA)); 178 assertEquals("ћирилица", sr_Cyrl_BA.getDisplayScript(sr_Cyrl_BA)); 179 assertEquals("", sr_Cyrl_BA.getDisplayVariant(sr_Cyrl_BA)); 180 181 assertEquals("српски", sr_Cyrl_ME.getDisplayLanguage(sr_Cyrl_ME)); 182 assertEquals("Црна Гора", sr_Cyrl_ME.getDisplayCountry(sr_Cyrl_ME)); 183 assertEquals("ћирилица", sr_Cyrl_ME.getDisplayScript(sr_Cyrl_ME)); 184 assertEquals("", sr_Cyrl_ME.getDisplayVariant(sr_Cyrl_ME)); 185 186 assertEquals("srpski", sr_Latn_BA.getDisplayLanguage(sr_Latn_BA)); 187 assertEquals("Bosna i Hercegovina", sr_Latn_BA.getDisplayCountry(sr_Latn_BA)); 188 assertEquals("latinica", sr_Latn_BA.getDisplayScript(sr_Latn_BA)); 189 assertEquals("", sr_Latn_BA.getDisplayVariant(sr_Latn_BA)); 190 191 assertEquals("srpski", sr_Latn_ME.getDisplayLanguage(sr_Latn_ME)); 192 assertEquals("Crna Gora", sr_Latn_ME.getDisplayCountry(sr_Latn_ME)); 193 assertEquals("latinica", sr_Latn_ME.getDisplayScript(sr_Latn_ME)); 194 assertEquals("", sr_Latn_ME.getDisplayVariant(sr_Latn_ME)); 195 196 assertEquals("BIH", sr_Cyrl_BA.getISO3Country()); 197 assertEquals("srp", sr_Cyrl_BA.getISO3Language()); 198 assertEquals("MNE", sr_Cyrl_ME.getISO3Country()); 199 assertEquals("srp", sr_Cyrl_ME.getISO3Language()); 200 assertEquals("BIH", sr_Latn_BA.getISO3Country()); 201 assertEquals("srp", sr_Latn_BA.getISO3Language()); 202 assertEquals("MNE", sr_Latn_ME.getISO3Country()); 203 assertEquals("srp", sr_Latn_ME.getISO3Language()); 204 205 BreakIterator.getCharacterInstance(sr_Cyrl_BA); 206 BreakIterator.getCharacterInstance(sr_Cyrl_ME); 207 BreakIterator.getCharacterInstance(sr_Latn_BA); 208 BreakIterator.getCharacterInstance(sr_Latn_ME); 209 210 BreakIterator.getLineInstance(sr_Cyrl_BA); 211 BreakIterator.getLineInstance(sr_Cyrl_ME); 212 BreakIterator.getLineInstance(sr_Latn_BA); 213 BreakIterator.getLineInstance(sr_Latn_ME); 214 215 BreakIterator.getSentenceInstance(sr_Cyrl_BA); 216 BreakIterator.getSentenceInstance(sr_Cyrl_ME); 217 BreakIterator.getSentenceInstance(sr_Latn_BA); 218 BreakIterator.getSentenceInstance(sr_Latn_ME); 219 220 BreakIterator.getWordInstance(sr_Cyrl_BA); 221 BreakIterator.getWordInstance(sr_Cyrl_ME); 222 BreakIterator.getWordInstance(sr_Latn_BA); 223 BreakIterator.getWordInstance(sr_Latn_ME); 224 225 Collator.getInstance(sr_Cyrl_BA); 226 Collator.getInstance(sr_Cyrl_ME); 227 Collator.getInstance(sr_Latn_BA); 228 Collator.getInstance(sr_Latn_ME); 229 230 Locale l = Locale.forLanguageTag("de-u-co-phonebk-kf-upper-kn"); 231 assertEquals("de__#u-co-phonebk-kf-upper-kn", l.toString()); 232 assertEquals("de-u-co-phonebk-kf-upper-kn", l.toLanguageTag()); 233 234 Collator c = Collator.getInstance(l); 235 assertTrue(c.compare("2", "11") < 0); 236 assertTrue(c.compare("11", "ae") < 0); 237 assertTrue(c.compare("ae", "Ä") < 0); 238 assertTrue(c.compare("Ä", "ä") < 0); 239 assertTrue(c.compare("ä", "AF") < 0); 240 assertTrue(c.compare("AF", "af") < 0); 241 } 242 243 public void testTransformIcuDateTimePattern_forJavaTime() { 244 // Example patterns coming from locale my-MM 245 assertTransformIcuDateTimePatternForJavaTime("B H:mm", "H:mm"); 246 assertTransformIcuDateTimePatternForJavaTime("B HH:mm:ss", "HH:mm:ss"); 247 assertTransformIcuDateTimePatternForJavaTime("dd-MM-yy B HH:mm:ss", "dd-MM-yy HH:mm:ss"); 248 assertTransformIcuDateTimePatternForJavaTime("y၊ MMM d B HH:mm:ss", "y၊ MMM d HH:mm:ss"); 249 250 // Other examples 251 assertTransformIcuDateTimePatternForJavaTime("H:mm B", "H:mm"); 252 assertTransformIcuDateTimePatternForJavaTime("H:mm b", "H:mm"); 253 assertTransformIcuDateTimePatternForJavaTime("b H:mm", "H:mm"); 254 assertTransformIcuDateTimePatternForJavaTime("B H:mm:ss, E", "H:mm:ss, E"); 255 256 // Examples with 'h' 12-hour format 257 assertTransformIcuDateTimePatternForJavaTime("hh:mm a", "hh:mm a"); 258 assertTransformIcuDateTimePatternForJavaTime("hh:mm a", "hh:mm a"); 259 assertTransformIcuDateTimePatternForJavaTime("a h:mm:ss, E", "a h:mm:ss, E"); 260 assertTransformIcuDateTimePatternForJavaTime("dd-MM-yy a", "dd-MM-yy a"); 261 262 assertTransformIcuDateTimePatternForJavaTime("#HH:mm", "'#'HH:mm"); // # sign is reserved for future use 263 assertTransformIcuDateTimePatternForJavaTime("'#'HH:mm", "'#'HH:mm"); 264 assertTransformIcuDateTimePatternForJavaTime("#HH:mm:ss 'lon' v", "'#'HH:mm:ss 'lon' v"); 265 assertTransformIcuDateTimePatternForJavaTime("HH:mm ''", "HH:mm ''"); 266 assertTransformIcuDateTimePatternForJavaTime("H:mm 'B'", "H:mm 'B'"); 267 assertTransformIcuDateTimePatternForJavaTime("H:mm 'b'", "H:mm 'b'"); 268 } 269 270 private static void assertTransformIcuDateTimePatternForJavaTime(String input, String expectedOutput) { 271 String pattern = ICU.transformIcuDateTimePattern_forJavaTime(input); 272 assertEquals("input:" + input, expectedOutput, pattern); 273 } 274 275 public void testTransformIcuDateTimePattern_forJavaText() { 276 // Example patterns coming from locale my-MM 277 assertTransformIcuDateTimePatternForJavaText("B H:mm", "H:mm"); 278 assertTransformIcuDateTimePatternForJavaText("B HH:mm:ss", "HH:mm:ss"); 279 assertTransformIcuDateTimePatternForJavaText("dd-MM-yy B HH:mm:ss", "dd-MM-yy HH:mm:ss"); 280 assertTransformIcuDateTimePatternForJavaText("y၊ MMM d B HH:mm:ss", "y၊ MMM d HH:mm:ss"); 281 282 // Other examples 283 assertTransformIcuDateTimePatternForJavaText("H:mm B", "H:mm"); 284 assertTransformIcuDateTimePatternForJavaText("H:mm b", "H:mm"); 285 assertTransformIcuDateTimePatternForJavaText("b H:mm", "H:mm"); 286 assertTransformIcuDateTimePatternForJavaText("B H:mm:ss, E", "H:mm:ss, E"); 287 288 // Examples with 'h' 12-hour format 289 assertTransformIcuDateTimePatternForJavaText("hh:mm a", "hh:mm a"); 290 assertTransformIcuDateTimePatternForJavaText("hh:mm a", "hh:mm a"); 291 assertTransformIcuDateTimePatternForJavaText("a h:mm:ss, E", "a h:mm:ss, E"); 292 assertTransformIcuDateTimePatternForJavaText("dd-MM-yy a", "dd-MM-yy a"); 293 294 assertTransformIcuDateTimePatternForJavaText("#HH:mm", "#HH:mm"); 295 assertTransformIcuDateTimePatternForJavaText("'#'HH:mm", "'#'HH:mm"); 296 assertTransformIcuDateTimePatternForJavaText("#HH:mm:ss 'lon' v", "#HH:mm:ss 'lon' v"); 297 assertTransformIcuDateTimePatternForJavaText("HH:mm ''", "HH:mm ''"); 298 assertTransformIcuDateTimePatternForJavaText("H:mm 'B'", "H:mm 'B'"); 299 assertTransformIcuDateTimePatternForJavaText("H:mm 'b'", "H:mm 'b'"); 300 } 301 302 private static void assertTransformIcuDateTimePatternForJavaText(String input, String expectedOutput) { 303 String pattern = ICU.transformIcuDateTimePattern_forJavaText(input); 304 assertEquals("input:" + input, expectedOutput, pattern); 305 } 306 307 public void testSetDefault() { 308 String current = ICU.getDefaultLocale(); 309 310 try { 311 assertGetDefault("", ""); 312 assertGetDefault("und", ""); 313 assertGetDefault("en-US", "en_US"); 314 assertGetDefault("uz-CYRL-UZ", "uz_Cyrl_UZ"); 315 assertGetDefault("ca-ES-PREEURO", "ca_ES_PREEURO"); 316 assertGetDefault("es-ES-PREEURO-u-ca-japanese", "es_ES_PREEURO@calendar=japanese"); 317 assertGetDefault("es-ES-PREEURO-u-ca-Japanese", "es_ES_PREEURO@calendar=japanese"); 318 } finally { 319 ICU.setDefaultLocale(current); 320 } 321 } 322 323 private static void assertGetDefault(String inputLangaugeTag, String expectedLanguageTag) { 324 ICU.setDefaultLocale(inputLangaugeTag); 325 assertEquals(expectedLanguageTag, ICU.getDefaultLocale()); 326 } 327 328 } 329