Home | History | Annotate | Download | only in test
      1 //  2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html#License
      3 /*
      4  *******************************************************************************
      5  * Copyright (C) 2009-2014, International Business Machines Corporation and    *
      6  * others. All Rights Reserved.                                                *
      7  *******************************************************************************
      8  */
      9 package com.ibm.icu.dev.test;
     10 
     11 import static com.ibm.icu.impl.LocaleDisplayNamesImpl.DataTableType.LANG;
     12 import static com.ibm.icu.impl.LocaleDisplayNamesImpl.DataTableType.REGION;
     13 
     14 import java.util.Locale;
     15 
     16 import org.junit.Test;
     17 import org.junit.runner.RunWith;
     18 import org.junit.runners.JUnit4;
     19 
     20 import com.ibm.icu.impl.LocaleDisplayNamesImpl;
     21 import com.ibm.icu.text.LocaleDisplayNames;
     22 import com.ibm.icu.text.LocaleDisplayNames.DialectHandling;
     23 import com.ibm.icu.util.ULocale;
     24 
     25 @RunWith(JUnit4.class)
     26 public class TestLocaleNamePackaging extends TestFmwk {
     27     public TestLocaleNamePackaging() {
     28     }
     29 
     30     public boolean validate() {
     31         logln("language data: " + LocaleDisplayNamesImpl.haveData(LANG));
     32         logln("  region data: " + LocaleDisplayNamesImpl.haveData(REGION));
     33         return true;
     34     }
     35 
     36     private static ULocale[] locales = {
     37         ULocale.ROOT, ULocale.US, new ULocale("es_ES"), ULocale.GERMANY,
     38         new ULocale("und_TH")
     39     };
     40 
     41     // Java Locales equivalent to above
     42     private static Locale[] javaLocales = {
     43         new Locale(""), Locale.US, new Locale("es", "ES"), Locale.GERMANY,
     44         new Locale("und", "TH")
     45     };
     46 
     47     @Test
     48     public void testRegionDisplayNames() {
     49         String[] expectedWithRegionData = {
     50             "",
     51             "US",
     52             "ES",
     53             "DE",
     54             "TH",
     55             "",
     56             "United States",
     57             "Spain",
     58             "Germany",
     59             "Thailand",
     60             "",
     61             "Estados Unidos",
     62             "Espa\u00f1a",
     63             "Alemania",
     64             "Tailandia",
     65             "",
     66             "Vereinigte Staaten",
     67             "Spanien",
     68             "Deutschland",
     69             "Thailand",
     70             "",
     71             "United States",
     72             "Spain",
     73             "Germany",
     74             "Thailand",
     75         };
     76         String[] expectedWithoutRegionData = {
     77             "",
     78             "US",
     79             "ES",
     80             "DE",
     81             "TH",
     82         };
     83         String[] expected = LocaleDisplayNamesImpl.haveData(REGION) ?
     84             expectedWithRegionData : expectedWithoutRegionData;
     85 
     86         int n = 0;
     87         for (ULocale displayLocale : locales) {
     88             LocaleDisplayNames dn = LocaleDisplayNames.getInstance(displayLocale);
     89             for (ULocale targetLocale : locales) {
     90                 String result = dn.regionDisplayName(targetLocale.getCountry());
     91                 assertEquals(targetLocale + " in " + displayLocale, expected[n++], result);
     92                 if (n == expected.length) {
     93                     n = 0;
     94                 }
     95             }
     96         }
     97 
     98         // Same test with Java Locale
     99         n = 0;
    100         for (Locale displayJavaLocale : javaLocales) {
    101             LocaleDisplayNames dn = LocaleDisplayNames.getInstance(displayJavaLocale);
    102             for (Locale targetLocale : javaLocales) {
    103                 String result = dn.regionDisplayName(targetLocale.getCountry());
    104                 assertEquals(targetLocale + " in " + displayJavaLocale, expected[n++], result);
    105                 if (n == expected.length) {
    106                     n = 0;
    107                 }
    108             }
    109         }
    110 
    111     }
    112 
    113     @Test
    114     public void testLanguageDisplayNames() {
    115         String[] expectedWithLanguageData = {
    116             "",
    117             "en",
    118             "es",
    119             "de",
    120             "und",
    121             "",
    122             "English",
    123             "Spanish",
    124             "German",
    125             "Unknown Language",
    126             "",
    127             "ingl\u00E9s",
    128             "espa\u00F1ol",
    129             "alem\u00E1n",
    130             "lengua desconocida",
    131             "",
    132             "Englisch",
    133             "Spanisch",
    134             "Deutsch",
    135             "Unbestimmte Sprache",
    136             "",
    137             "English",
    138             "Spanish",
    139             "German",
    140             "Unknown Language",
    141         };
    142         String[] expectedWithoutLanguageData = {
    143             "",
    144             "en",
    145             "es",
    146             "de",
    147             "und"
    148         };
    149         String[] expected = LocaleDisplayNamesImpl.haveData(LANG) ?
    150             expectedWithLanguageData : expectedWithoutLanguageData;
    151 
    152         int n = 0;
    153         for (ULocale displayLocale : locales) {
    154             LocaleDisplayNames dn = LocaleDisplayNames.getInstance(displayLocale);
    155             for (ULocale targetLocale : locales) {
    156                 String result = dn.languageDisplayName(targetLocale.getLanguage());
    157                 assertEquals(targetLocale + " in " + displayLocale, expected[n++], result);
    158                 if (n == expected.length) {
    159                     n = 0;
    160                 }
    161             }
    162         }
    163 
    164         // Same test with Java Locale
    165         n = 0;
    166         for (Locale displayJavaLocale : javaLocales) {
    167             LocaleDisplayNames dn = LocaleDisplayNames.getInstance(displayJavaLocale);
    168             for (Locale targetLocale : javaLocales) {
    169                 String result = dn.languageDisplayName(targetLocale.getLanguage());
    170                 assertEquals(targetLocale + " in " + displayJavaLocale, expected[n++], result);
    171                 if (n == expected.length) {
    172                     n = 0;
    173                 }
    174             }
    175         }
    176 
    177     }
    178 
    179     // test a 'root' locale, with keywords
    180     @Test
    181     public void testLocaleDisplayNameWithKeywords() {
    182         String[] expectedWithLanguageData = {
    183             "root (collation=phonebook)",
    184             "Root (Phonebook Sort Order)",
    185             "ra\u00EDz (orden de list\u00EDn telef\u00F3nico)",
    186             "Root (Telefonbuch-Sortierung)",
    187             "Root (Phonebook Sort Order)",
    188         };
    189         String[] expectedWithoutLanguageData = {
    190             "root (collation=phonebook)",
    191         };
    192         String[] expected = LocaleDisplayNamesImpl.haveData(LANG) ?
    193             expectedWithLanguageData : expectedWithoutLanguageData;
    194 
    195         ULocale kl = new ULocale("@collation=phonebook");
    196 
    197         int n = 0;
    198         for (ULocale displayLocale : locales) {
    199             LocaleDisplayNames dn = LocaleDisplayNames.getInstance(displayLocale);
    200             String result = dn.localeDisplayName(kl);
    201             assertEquals(kl + " in " + displayLocale, expected[n++], result);
    202             if (n == expected.length) {
    203                 n = 0;
    204             }
    205         }
    206     }
    207 
    208     @Test
    209     public void testLanguageDisplayNameDoesNotTranslateRoot() {
    210         // "root" is not a language code-- the fact that we have our data organized this
    211         // way is immaterial.  "root" remains untranslated whether we have data or not.
    212         LocaleDisplayNames dn = LocaleDisplayNames.getInstance(ULocale.US);
    213         assertEquals("root", "root", dn.languageDisplayName("root"));
    214     }
    215 
    216     @Test
    217     public void testLanguageDisplayNameDoesNotTranslateDialects() {
    218         // Dialect ids are also not language codes.
    219         LocaleDisplayNames dn = LocaleDisplayNames.getInstance(ULocale.US,
    220                                                                DialectHandling.DIALECT_NAMES);
    221         assertEquals("dialect", "en_GB", dn.languageDisplayName("en_GB"));
    222 
    223         String target = LocaleDisplayNamesImpl.haveData(LANG)
    224             ? "British English"
    225             : (LocaleDisplayNamesImpl.haveData(REGION)
    226                ? "en (United Kingdom)"
    227                : "en (GB)");
    228         assertEquals("dialect 2", target, dn.localeDisplayName("en_GB"));
    229     }
    230 
    231     @Test
    232     public void testLocaleKeywords() {
    233         LocaleDisplayNames dn = LocaleDisplayNames.getInstance(ULocale.US,
    234                 DialectHandling.DIALECT_NAMES);
    235         String name = dn.localeDisplayName("de@collation=phonebook");
    236         String target = LocaleDisplayNamesImpl.haveData(LANG) ?
    237                 "German (Phonebook Sort Order)" : "de (collation=phonebook)";
    238         assertEquals("collation", target, name);
    239 
    240     }
    241 }
    242