LocaleTest.javaAPI DocAndroid 1.5 API9344Wed May 06 22:42:02 BST 2009android.core


public class LocaleTest extends TestCase
Test some locale-dependent stuff for Android. This test mainly ensures that our ICU configuration is correct and contains all the needed locales and resource bundles.

Fields Summary
Constructors Summary
Methods Summary
public voidtestICUConverters()

        // List of encodings currently required for Android.
        String[] encodings = new String[] {
                // Encoding required by the language specification.
                // Additional encodings included in standard ICU
                // Additional encoding not included in standard ICU.
                "GSM0338" };
        for (int i = 0; i < encodings.length; i++) {
            assertTrue("Charset " + encodings[i] + " must be supported",
            Charset cs = Charset.forName(encodings[i]);
            Set<String> aliases = cs.aliases();
            for (String s: aliases) {
                android.util.Log.d("LocaleTest", " - " + s);
        // Test for valid encoding that is not included in Android. IBM-37 is
        // a perfect candidate for this, as it is being used for mainframes and
        // thus somewhat out of the scope of Android.
        assertFalse("Charset IBM-37 must not be supported",

        // Test for a bogus encoding.
        assertFalse("Charset KLINGON must not be supported",
        // Make sure our local change to the real translation table used for
        // EUC-JP doesn't get lost.
        Charset cs = Charset.forName("EUC-JP");
        assertTrue("EUC-JP must use 'ibm-954_P101-2007'", cs.aliases().contains("ibm-954_P101-2007"));
public voidtestICULocales()

        String[] locales = new String[] {
                // List of locales currently required for Android.
                "en_US", "es_US", "en_GB", "fr_FR", "de_DE", "de_AT", "cs_CZ", "nl_NL" };
        String[] mondays = new String[] {
                "Monday", "lunes", "Monday", "lundi", "Montag", "Montag", "pond\u011bl\u00ed", "maandag" };
        String[] currencies = new String[] {
                "USD", "USD", "GBP", "EUR", "EUR", "EUR", "CZK", "EUR"};

        for (int i = 0; i < locales.length; i++) {
            Locale l = new Locale(locales[i].substring(0, 2), locales[i].substring(3));
            // Check language part of locale.
            DateFormatSymbols d = new DateFormatSymbols(l);
            assertEquals("Monday name for " + locales[i] + " must match",
                    mondays[i], d.getWeekdays()[2]);
            // Check country part of locale.
            Currency c = Currency.getInstance(l);
            assertEquals("Currency code for " + locales[i] + " must match",
                    currencies[i], c.getCurrencyCode());
public voidtestLocale()

        Locale locale = new Locale("en");
        assertEquals("en", locale.toString());

        locale = new Locale("en", "US");
        assertEquals("en_US", locale.toString());

        locale = new Locale("en", "", "POSIX");
        assertEquals("en__POSIX", locale.toString());

        locale = new Locale("en", "US", "POSIX");
        assertEquals("en_US_POSIX", locale.toString());
public voidtestResourceBundles()

        Locale eng = new Locale("en", "US");
        DateFormatSymbols engSymbols = new DateFormatSymbols(eng);
        //Locale deu = new Locale("de", "DE");
        //DateFormatSymbols deuSymbols = new DateFormatSymbols(deu);
        TimeZone berlin = TimeZone.getTimeZone("Europe/Berlin");
        assertEquals("January", engSymbols.getMonths()[0]);
        //assertEquals("Januar", deuSymbols.getMonths()[0]);

        assertEquals("Sunday", engSymbols.getWeekdays()[Calendar.SUNDAY]);
        //assertEquals("Sonntag", deuSymbols.getWeekdays()[Calendar.SUNDAY]);
        assertEquals("Central European Time",
                berlin.getDisplayName(false, TimeZone.LONG, eng));
        assertEquals("Central European Summer Time",
                berlin.getDisplayName(true, TimeZone.LONG, eng));

        //assertEquals("Mitteleurop\u00E4ische Zeit",
        //        berlin.getDisplayName(false, TimeZone.LONG, deu));
        //assertEquals("Mitteleurop\u00E4ische Sommerzeit",
        //        berlin.getDisplayName(true, TimeZone.LONG, deu));
        assertTrue(engSymbols.getZoneStrings().length > 100);