1 /* GENERATED SOURCE. DO NOT MODIFY. */ 2 // 2016 and later: Unicode, Inc. and others. 3 // License & terms of use: http://www.unicode.org/copyright.html#License 4 /* 5 ******************************************************************************* 6 * Copyright (C) 1996-2008, International Business Machines Corporation and * 7 * others. All Rights Reserved. * 8 ******************************************************************************* 9 */ 10 package android.icu.dev.test.util; 11 12 import java.util.ArrayList; 13 import java.util.Arrays; 14 import java.util.Date; 15 import java.util.Enumeration; 16 import java.util.HashMap; 17 import java.util.Map; 18 import java.util.Set; 19 import java.util.TreeMap; 20 import java.util.TreeSet; 21 22 import org.junit.Ignore; 23 import org.junit.Test; 24 import org.junit.runner.RunWith; 25 import org.junit.runners.JUnit4; 26 27 import android.icu.dev.test.TestFmwk; 28 import android.icu.impl.ICUResourceBundle; 29 import android.icu.util.Calendar; 30 import android.icu.util.Currency; 31 import android.icu.util.GregorianCalendar; 32 import android.icu.util.TimeZone; 33 import android.icu.util.ULocale; 34 import android.icu.util.UResourceBundle; 35 import android.icu.testsharding.MainTestShard; 36 37 // TODO(junit): test is broken in main branch 38 39 @MainTestShard 40 @RunWith(JUnit4.class) 41 public class DisplayNameTest extends TestFmwk { 42 static final boolean SHOW_ALL = false; 43 44 interface DisplayNameGetter { 45 public String get(ULocale locale, String code, Object context); 46 } 47 48 Map[] codeToName = new Map[10]; 49 { 50 for (int k = 0; k < codeToName.length; ++k) codeToName[k] = new HashMap(); 51 } 52 53 static final Object[] zoneFormats = {new Integer(0), new Integer(1), new Integer(2), 54 new Integer(3), new Integer(4), new Integer(5), new Integer(6), new Integer(7)}; 55 static final Object[] currencyFormats = {new Integer(Currency.SYMBOL_NAME), new Integer(Currency.LONG_NAME)}; 56 static final Object[] NO_CONTEXT = {null}; 57 58 static final Date JAN1; 59 static final Date JULY1; 60 61 static { 62 Calendar cal = new GregorianCalendar(2004, Calendar.JANUARY, 1); 63 JAN1 = cal.getTime(); 64 cal.set(Calendar.MONTH, Calendar.JULY); 65 JULY1 = cal.getTime(); 66 } 67 68 String[] countries = addUnknown(ULocale.getISOCountries(),2); 69 String[] languages = addUnknown(ULocale.getISOLanguages(),2); 70 String[] zones = addUnknown(getRealZoneIDs(),5); 71 String[] scripts = addUnknown(getCodes(new ULocale("en","US",""), "Scripts"),4); 72 // TODO fix once there is a way to get a list of all script codes 73 String[] currencies = addUnknown(getCodes(new ULocale("en","",""), "Currencies"),3); 74 // TODO fix once there is a way to get a list of all currency codes 75 76 @Ignore 77 @Test 78 public void TestLocales() { 79 ULocale[] locales = ULocale.getAvailableLocales(); 80 for (int i = 0; i < locales.length; ++i) { 81 checkLocale(locales[i]); 82 } 83 } 84 85 /** 86 * @return 87 */ 88 private String[] getRealZoneIDs() { 89 Set temp = new TreeSet(Arrays.asList(TimeZone.getAvailableIDs())); 90 temp.removeAll(getAliasMap().keySet()); 91 return (String[])temp.toArray(new String[temp.size()]); 92 } 93 94 @Ignore 95 @Test 96 public void TestEnglish() { 97 checkLocale(ULocale.ENGLISH); 98 } 99 100 @Ignore 101 @Test 102 public void TestFrench() { 103 checkLocale(ULocale.FRENCH); 104 } 105 106 private void checkLocale(ULocale locale) { 107 logln("Checking " + locale); 108 check("Language", locale, languages, null, new DisplayNameGetter() { 109 @Override 110 public String get(ULocale loc, String code, Object context) { 111 return ULocale.getDisplayLanguage(code, loc); 112 } 113 }); 114 check("Script", locale, scripts, null, new DisplayNameGetter() { 115 @Override 116 public String get(ULocale loc, String code, Object context) { 117 // TODO This is kinda a hack; ought to be direct way. 118 return ULocale.getDisplayScript("en_"+code, loc); 119 } 120 }); 121 check("Country", locale, countries, null, new DisplayNameGetter() { 122 @Override 123 public String get(ULocale loc, String code, Object context) { 124 // TODO This is kinda a hack; ought to be direct way. 125 return ULocale.getDisplayCountry("en_"+code, loc); 126 } 127 }); 128 check("Currencies", locale, currencies, currencyFormats, new DisplayNameGetter() { 129 @Override 130 public String get(ULocale loc, String code, Object context) { 131 Currency s = Currency.getInstance(code); 132 return s.getName(loc, ((Integer)context).intValue(), new boolean[1]); 133 } 134 }); 135 // comment this out, because the zone string information is lost 136 // we'd have to access the resources directly to test them 137 138 check("Zones", locale, zones, zoneFormats, new DisplayNameGetter() { 139 // TODO replace once we have real API 140 @Override 141 public String get(ULocale loc, String code, Object context) { 142 return getZoneString(loc, code, ((Integer)context).intValue()); 143 } 144 }); 145 146 } 147 148 Map zoneData = new HashMap(); 149 150 private String getZoneString(ULocale locale, String olsonID, int item) { 151 Map data = (Map)zoneData.get(locale); 152 if (data == null) { 153 data = new HashMap(); 154 if (SHOW_ALL) System.out.println(); 155 if (SHOW_ALL) System.out.println("zones for " + locale); 156 ICUResourceBundle bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(locale); 157 ICUResourceBundle table = bundle.getWithFallback("zoneStrings"); 158 for (int i = 0; i < table.getSize(); ++i) { 159 UResourceBundle stringSet = table.get(i); 160 //ICUResourceBundle stringSet = table.getWithFallback(String.valueOf(i)); 161 String key = stringSet.getString(0); 162 if (SHOW_ALL) System.out.println("key: " + key); 163 ArrayList list = new ArrayList(); 164 for (int j = 1; j < stringSet.getSize(); ++j) { 165 String entry = stringSet.getString(j); 166 if (SHOW_ALL) System.out.println(" entry: " + entry); 167 list.add(entry); 168 } 169 data.put(key, list.toArray(new String[list.size()])); 170 } 171 zoneData.put(locale, data); 172 } 173 String[] strings = (String[]) data.get(olsonID); 174 if (strings == null || item >= strings.length) return olsonID; 175 return strings[item]; 176 } 177 178 static String[][] zonesAliases = { 179 {"America/Atka", "America/Atka"}, 180 {"America/Ensenada", "America/Ensenada"}, 181 {"America/Fort_Wayne", "America/Fort_Wayne"}, 182 {"America/Indiana/Indianapolis", "America/Indiana/Indianapolis"}, 183 {"America/Kentucky/Louisville", "America/Kentucky/Louisville"}, 184 {"America/Knox_IN", "America/Knox_IN"}, 185 {"America/Porto_Acre", "America/Porto_Acre"}, 186 {"America/Rosario", "America/Rosario"}, 187 {"America/Shiprock", "America/Shiprock"}, 188 {"America/Virgin", "America/Virgin"}, 189 {"Antarctica/South_Pole", "Antarctica/South_Pole"}, 190 {"Arctic/Longyearbyen", "Arctic/Longyearbyen"}, 191 {"Asia/Ashkhabad", "Asia/Ashkhabad"}, 192 {"Asia/Chungking", "Asia/Chungking"}, 193 {"Asia/Dacca", "Asia/Dacca"}, 194 {"Asia/Istanbul", "Asia/Istanbul"}, 195 {"Asia/Macao", "Asia/Macao"}, 196 {"Asia/Tel_Aviv", "Asia/Tel_Aviv"}, 197 {"Asia/Thimbu", "Asia/Thimbu"}, 198 {"Asia/Ujung_Pandang", "Asia/Ujung_Pandang"}, 199 {"Asia/Ulan_Bator", "Asia/Ulan_Bator"}, 200 {"Australia/ACT", "Australia/ACT"}, 201 {"Australia/Canberra", "Australia/Canberra"}, 202 {"Australia/LHI", "Australia/LHI"}, 203 {"Australia/NSW", "Australia/NSW"}, 204 {"Australia/North", "Australia/North"}, 205 {"Australia/Queensland", "Australia/Queensland"}, 206 {"Australia/South", "Australia/South"}, 207 {"Australia/Tasmania", "Australia/Tasmania"}, 208 {"Australia/Victoria", "Australia/Victoria"}, 209 {"Australia/West", "Australia/West"}, 210 {"Australia/Yancowinna", "Australia/Yancowinna"}, 211 {"Brazil/Acre", "Brazil/Acre"}, 212 {"Brazil/DeNoronha", "Brazil/DeNoronha"}, 213 {"Brazil/East", "Brazil/East"}, 214 {"Brazil/West", "Brazil/West"}, 215 {"CST6CDT", "CST6CDT"}, 216 {"Canada/Atlantic", "Canada/Atlantic"}, 217 {"Canada/Central", "Canada/Central"}, 218 {"Canada/East-Saskatchewan", "Canada/East-Saskatchewan"}, 219 {"Canada/Eastern", "Canada/Eastern"}, 220 {"Canada/Mountain", "Canada/Mountain"}, 221 {"Canada/Newfoundland", "Canada/Newfoundland"}, 222 {"Canada/Pacific", "Canada/Pacific"}, 223 {"Canada/Saskatchewan", "Canada/Saskatchewan"}, 224 {"Canada/Yukon", "Canada/Yukon"}, 225 {"Chile/Continental", "Chile/Continental"}, 226 {"Chile/EasterIsland", "Chile/EasterIsland"}, 227 {"Cuba", "Cuba"}, 228 {"EST", "EST"}, 229 {"EST5EDT", "EST5EDT"}, 230 {"Egypt", "Egypt"}, 231 {"Eire", "Eire"}, 232 {"Etc/GMT+0", "Etc/GMT+0"}, 233 {"Etc/GMT-0", "Etc/GMT-0"}, 234 {"Etc/GMT0", "Etc/GMT0"}, 235 {"Etc/Greenwich", "Etc/Greenwich"}, 236 {"Etc/Universal", "Etc/Universal"}, 237 {"Etc/Zulu", "Etc/Zulu"}, 238 {"Europe/Nicosia", "Europe/Nicosia"}, 239 {"Europe/Tiraspol", "Europe/Tiraspol"}, 240 {"GB", "GB"}, 241 {"GB-Eire", "GB-Eire"}, 242 {"GMT", "GMT"}, 243 {"GMT+0", "GMT+0"}, 244 {"GMT-0", "GMT-0"}, 245 {"GMT0", "GMT0"}, 246 {"Greenwich", "Greenwich"}, 247 {"HST", "HST"}, 248 {"Hongkong", "Hongkong"}, 249 {"Iceland", "Iceland"}, 250 {"Iran", "Iran"}, 251 {"Israel", "Israel"}, 252 {"Jamaica", "Jamaica"}, 253 {"Japan", "Japan"}, 254 {"Kwajalein", "Kwajalein"}, 255 {"Libya", "Libya"}, 256 {"MST", "MST"}, 257 {"MST7MDT", "MST7MDT"}, 258 {"Mexico/BajaNorte", "Mexico/BajaNorte"}, 259 {"Mexico/BajaSur", "Mexico/BajaSur"}, 260 {"Mexico/General", "Mexico/General"}, 261 {"Mideast/Riyadh87", "Mideast/Riyadh87"}, 262 {"Mideast/Riyadh88", "Mideast/Riyadh88"}, 263 {"Mideast/Riyadh89", "Mideast/Riyadh89"}, 264 {"NZ", "NZ"}, 265 {"NZ-CHAT", "NZ-CHAT"}, 266 {"Navajo", "Navajo"}, 267 {"PRC", "PRC"}, 268 {"PST8PDT", "PST8PDT"}, 269 {"Pacific/Samoa", "Pacific/Samoa"}, 270 {"Poland", "Poland"}, 271 {"Portugal", "Portugal"}, 272 {"ROC", "ROC"}, 273 {"ROK", "ROK"}, 274 {"Singapore", "Singapore"}, 275 {"SystemV/AST4", "SystemV/AST4"}, 276 {"SystemV/AST4ADT", "SystemV/AST4ADT"}, 277 {"SystemV/CST6", "SystemV/CST6"}, 278 {"SystemV/CST6CDT", "SystemV/CST6CDT"}, 279 {"SystemV/EST5", "SystemV/EST5"}, 280 {"SystemV/EST5EDT", "SystemV/EST5EDT"}, 281 {"SystemV/HST10", "SystemV/HST10"}, 282 {"SystemV/MST7", "SystemV/MST7"}, 283 {"SystemV/MST7MDT", "SystemV/MST7MDT"}, 284 {"SystemV/PST8", "SystemV/PST8"}, 285 {"SystemV/PST8PDT", "SystemV/PST8PDT"}, 286 {"SystemV/YST9", "SystemV/YST9"}, 287 {"SystemV/YST9YDT", "SystemV/YST9YDT"}, 288 {"Turkey", "Turkey"}, 289 {"UCT", "UCT"}, 290 {"US/Alaska", "US/Alaska"}, 291 {"US/Aleutian", "US/Aleutian"}, 292 {"US/Arizona", "US/Arizona"}, 293 {"US/Central", "US/Central"}, 294 {"US/East-Indiana", "US/East-Indiana"}, 295 {"US/Eastern", "US/Eastern"}, 296 {"US/Hawaii", "US/Hawaii"}, 297 {"US/Indiana-Starke", "US/Indiana-Starke"}, 298 {"US/Michigan", "US/Michigan"}, 299 {"US/Mountain", "US/Mountain"}, 300 {"US/Pacific", "US/Pacific"}, 301 {"US/Pacific-New", "US/Pacific-New"}, 302 {"US/Samoa", "US/Samoa"}, 303 {"UTC", "UTC"}, 304 {"Universal", "Universal"}, 305 {"W-SU", "W-SU"}, 306 {"Zulu", "Zulu"}, 307 {"ACT", "ACT"}, 308 {"AET", "AET"}, 309 {"AGT", "AGT"}, 310 {"ART", "ART"}, 311 {"AST", "AST"}, 312 {"BET", "BET"}, 313 {"BST", "BST"}, 314 {"CAT", "CAT"}, 315 {"CNT", "CNT"}, 316 {"CST", "CST"}, 317 {"CTT", "CTT"}, 318 {"EAT", "EAT"}, 319 {"ECT", "ECT"}, 320 {"IET", "IET"}, 321 {"IST", "IST"}, 322 {"JST", "JST"}, 323 {"MIT", "MIT"}, 324 {"NET", "NET"}, 325 {"NST", "NST"}, 326 {"PLT", "PLT"}, 327 {"PNT", "PNT"}, 328 {"PRT", "PRT"}, 329 {"PST", "PST"}, 330 {"SST", "SST"}, 331 {"VST", "VST"}, 332 }; 333 334 /** 335 * Hack to get code list 336 * @return 337 */ 338 private static String[] getCodes(ULocale locale, String tableName) { 339 // TODO remove Ugly Hack 340 // get stuff 341 ICUResourceBundle bundle = (ICUResourceBundle)UResourceBundle.getBundleInstance(locale); 342 ICUResourceBundle table = bundle.getWithFallback(tableName); 343 // copy into array 344 ArrayList stuff = new ArrayList(); 345 for (Enumeration keys = table.getKeys(); keys.hasMoreElements();) { 346 stuff.add(keys.nextElement()); 347 } 348 String[] result = new String[stuff.size()]; 349 return (String[]) stuff.toArray(result); 350 //return new String[] {"Latn", "Cyrl"}; 351 } 352 353 /** 354 * Add two unknown strings, just to make sure they get passed through without colliding 355 * @param strings 356 * @return 357 */ 358 private String[] addUnknown(String[] strings, int len) { 359 String[] result = new String[strings.length + 2]; 360 result[0] = "x1unknown".substring(0,len); 361 result[1] = "y1nknown".substring(0,len); 362 System.arraycopy(strings,0,result,2,strings.length); 363 return result; 364 } 365 366 Map bogusZones = null; 367 368 private Map getAliasMap() { 369 if (bogusZones == null) { 370 bogusZones = new TreeMap(); 371 for (int i = 0; i < zonesAliases.length; ++i) { 372 bogusZones.put(zonesAliases[i][0], zonesAliases[i][1]); 373 } 374 } 375 return bogusZones; 376 } 377 378 379 private void check(String type, ULocale locale, 380 String[] codes, Object[] contextList, DisplayNameGetter getter) { 381 if (contextList == null) contextList = NO_CONTEXT; 382 for (int k = 0; k < contextList.length; ++k) codeToName[k].clear(); 383 for (int j = 0; j < codes.length; ++j) { 384 String code = codes[j]; 385 for (int k = 0; k < contextList.length; ++k) { 386 Object context = contextList[k]; 387 String name = getter.get(locale, code, context); 388 if (name == null || name.length() == 0) { 389 errln( 390 "Null or Zero-Length Display Name\t" + type 391 + "\t(" + ((context != null) ? context : "") + ")" 392 + ":\t" + locale + " [" + locale.getDisplayName(ULocale.ENGLISH) + "]" 393 + "\t" + code + " [" + getter.get(ULocale.ENGLISH, code, context) + "]" 394 ); 395 continue; 396 } 397 String otherCode = (String) codeToName[k].get(name); 398 if (otherCode != null) { 399 errln( 400 "Display Names collide for\t" + type + "\t(" + ((context != null) ? context : "") + ")" 401 + ":\t" + locale + " [" + locale.getDisplayName(ULocale.ENGLISH) + "]" 402 + "\t" + code + " [" + getter.get(ULocale.ENGLISH, code, context) + "]" 403 + "\t& " + otherCode + " [" + getter.get(ULocale.ENGLISH, otherCode, context) + "]" 404 + "\t=> " + name 405 ); 406 } else { 407 codeToName[k].put(name, code); 408 if (SHOW_ALL) logln( 409 type 410 + " (" + ((context != null) ? context : "") + ")" 411 + "\t" + locale + " [" + locale.getDisplayName(ULocale.ENGLISH) + "]" 412 + "\t" + code + "[" + getter.get(ULocale.ENGLISH, code, context) + "]" 413 + "\t=> " + name 414 ); 415 } 416 } 417 } 418 } 419 } 420