Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2008 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.nio.charset.Charset;
     20 import java.text.DateFormatSymbols;
     21 import java.util.Calendar;
     22 import java.util.Currency;
     23 import java.util.Locale;
     24 import java.util.Set;
     25 import java.util.TimeZone;
     26 import junit.framework.TestCase;
     27 
     28 /**
     29  * Test some locale-dependent stuff for Android. This test mainly ensures that
     30  * our ICU configuration is correct and contains all the needed locales and
     31  * resource bundles.
     32  */
     33 public class OldAndroidLocaleTest extends TestCase {
     34     // Test basic Locale infrastructure.
     35     public void testLocale() throws Exception {
     36         Locale locale = new Locale("en");
     37         assertEquals("en", locale.toString());
     38 
     39         locale = new Locale("en", "US");
     40         assertEquals("en_US", locale.toString());
     41 
     42         locale = new Locale("en", "", "POSIX");
     43         assertEquals("en__POSIX", locale.toString());
     44 
     45         locale = new Locale("en", "US", "POSIX");
     46         assertEquals("en_US_POSIX", locale.toString());
     47     }
     48 
     49     public void testResourceBundles() throws Exception {
     50         Locale eng = new Locale("en", "US");
     51         DateFormatSymbols engSymbols = new DateFormatSymbols(eng);
     52 
     53         Locale deu = new Locale("de", "DE");
     54         DateFormatSymbols deuSymbols = new DateFormatSymbols(deu);
     55 
     56         TimeZone berlin = TimeZone.getTimeZone("Europe/Berlin");
     57 
     58         assertEquals("January", engSymbols.getMonths()[0]);
     59         assertEquals("Januar", deuSymbols.getMonths()[0]);
     60 
     61         assertEquals("Sunday", engSymbols.getWeekdays()[Calendar.SUNDAY]);
     62         assertEquals("Sonntag", deuSymbols.getWeekdays()[Calendar.SUNDAY]);
     63 
     64         assertEquals("Central European Standard Time",
     65                 berlin.getDisplayName(false, TimeZone.LONG, eng));
     66         assertEquals("Central European Summer Time",
     67                 berlin.getDisplayName(true, TimeZone.LONG, eng));
     68 
     69         assertEquals("Mitteleuropische Normalzeit",
     70                 berlin.getDisplayName(false, TimeZone.LONG, deu));
     71         assertEquals("Mitteleuropische Sommerzeit",
     72                 berlin.getDisplayName(true, TimeZone.LONG, deu));
     73 
     74         assertTrue(engSymbols.getZoneStrings().length > 100);
     75     }
     76 
     77     // This one makes sure we have all necessary locales installed.
     78     public void testICULocales() {
     79         // List of locales currently required for Android.
     80         Locale[] locales = new Locale[] {
     81                 new Locale("en", "US"),
     82                 new Locale("es", "US"),
     83                 new Locale("en", "GB"),
     84                 new Locale("fr", "FR"),
     85                 new Locale("de", "DE"),
     86                 new Locale("de", "AT"),
     87                 new Locale("cs", "CZ"),
     88                 new Locale("nl", "NL") };
     89 
     90         String[] mondays = new String[] {
     91                 "Monday", "lunes", "Monday", "lundi", "Montag", "Montag", "pond\u011bl\u00ed", "maandag" };
     92 
     93         String[] currencies = new String[] {
     94                 "USD", "USD", "GBP", "EUR", "EUR", "EUR", "CZK", "EUR"};
     95 
     96         for (int i = 0; i < locales.length; i++) {
     97             final Locale l = locales[i];
     98 
     99             DateFormatSymbols d = new DateFormatSymbols(l);
    100             assertEquals("Monday name for " + locales[i] + " must match",
    101                     mondays[i], d.getWeekdays()[2]);
    102 
    103             Currency c = Currency.getInstance(l);
    104             assertEquals("Currency code for " + locales[i] + " must match",
    105                     currencies[i], c.getCurrencyCode());
    106         }
    107     }
    108 
    109     // Regression test for 1118570: Create test cases for tracking ICU config
    110     // changes. This one makes sure we have the necessary converters installed
    111     // and don't lose the changes to the converter alias table.
    112     public void testICUConverters() {
    113         // List of encodings currently required for Android.
    114         String[] encodings = new String[] {
    115                 // Encoding required by the language specification.
    116                 "US-ASCII",
    117                 "UTF-8",
    118                 "UTF-16",
    119                 "UTF-16BE",
    120                 "UTF-16LE",
    121                 "ISO-8859-1",
    122 
    123                 // Additional encodings included in standard ICU
    124                 "ISO-8859-2",
    125                 "ISO-8859-3",
    126                 "ISO-8859-4",
    127                 "ISO-8859-5",
    128                 "ISO-8859-6",
    129                 "ISO-8859-7",
    130                 "ISO-8859-8",
    131                 "ISO-8859-8-I",
    132                 "ISO-8859-9",
    133                 "ISO-8859-10",
    134                 "ISO-8859-11",
    135                 "ISO-8859-13",
    136                 "ISO-8859-14",
    137                 "ISO-8859-15",
    138                 "ISO-2022-JP",
    139                 "Windows-950",
    140                 "Windows-1250",
    141                 "Windows-1251",
    142                 "Windows-1252",
    143                 "Windows-1253",
    144                 "Windows-1254",
    145                 "Windows-1255",
    146                 "Windows-1256",
    147                 "Windows-1257",
    148                 "Windows-1258",
    149                 "Big5",
    150                 "CP864",
    151                 "CP874",
    152                 "EUC-CN",
    153                 "EUC-JP",
    154                 "KOI8-R",
    155                 "Macintosh",
    156                 "GBK",
    157                 "GB2312",
    158                 "EUC-KR",
    159                 "GSM0338"
    160         };
    161 
    162         for (String encoding : encodings) {
    163             assertTrue("Charset " + encoding + " must be supported", Charset.isSupported(encoding));
    164 
    165             Charset cs = Charset.forName(encoding);
    166             Set<String> aliases = cs.aliases();
    167             System.out.println(cs.name() + ": " + aliases);
    168         }
    169     }
    170 
    171 }
    172