• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.core;
18 
19 import junit.framework.TestCase;
20 
21 import java.nio.charset.Charset;
22 import java.text.DateFormatSymbols;
23 import java.util.Calendar;
24 import java.util.Currency;
25 import java.util.Locale;
26 import java.util.Set;
27 import java.util.TimeZone;
28 import android.test.suitebuilder.annotation.MediumTest;
29 import android.test.suitebuilder.annotation.SmallTest;
30 import android.test.suitebuilder.annotation.LargeTest;
31 import android.test.suitebuilder.annotation.Suppress;
32 
33 /**
34  * Test some locale-dependent stuff for Android. This test mainly ensures that
35  * our ICU configuration is correct and contains all the needed locales and
36  * resource bundles.
37  */
38 public class LocaleTest extends TestCase {
39 
40     // Test basic Locale infrastructure.
41     @SmallTest
testLocale()42     public void testLocale() throws Exception {
43         Locale locale = new Locale("en");
44         assertEquals("en", locale.toString());
45 
46         locale = new Locale("en", "US");
47         assertEquals("en_US", locale.toString());
48 
49         locale = new Locale("en", "", "POSIX");
50         assertEquals("en__POSIX", locale.toString());
51 
52         locale = new Locale("en", "US", "POSIX");
53         assertEquals("en_US_POSIX", locale.toString());
54     }
55 
56     /*
57      * Tests some must-have locales. TODO: Add back "de". See discussion
58      * immediately below this method.
59      */
60     @LargeTest
testResourceBundles()61     public void testResourceBundles() throws Exception {
62         Locale eng = new Locale("en", "US");
63         DateFormatSymbols engSymbols = new DateFormatSymbols(eng);
64 
65         //Locale deu = new Locale("de", "DE");
66         //DateFormatSymbols deuSymbols = new DateFormatSymbols(deu);
67 
68         TimeZone berlin = TimeZone.getTimeZone("Europe/Berlin");
69 
70         assertEquals("January", engSymbols.getMonths()[0]);
71         //assertEquals("Januar", deuSymbols.getMonths()[0]);
72 
73         assertEquals("Sunday", engSymbols.getWeekdays()[Calendar.SUNDAY]);
74         //assertEquals("Sonntag", deuSymbols.getWeekdays()[Calendar.SUNDAY]);
75 
76         assertEquals("Central European Time",
77                 berlin.getDisplayName(false, TimeZone.LONG, eng));
78         assertEquals("Central European Summer Time",
79                 berlin.getDisplayName(true, TimeZone.LONG, eng));
80 
81         //assertEquals("Mitteleurop\u00E4ische Zeit",
82         //        berlin.getDisplayName(false, TimeZone.LONG, deu));
83         //assertEquals("Mitteleurop\u00E4ische Sommerzeit",
84         //        berlin.getDisplayName(true, TimeZone.LONG, deu));
85 
86         assertTrue(engSymbols.getZoneStrings().length > 100);
87     }
88 
89     /*
90      * Disabled version of the above test. The version above omits
91      * checks for stuff in the "de" locale, because we stripped that
92      * out as part of the flash reduction effort (so that we could
93      * still ship on Dream). We expect to have a baseline target that
94      * includes a large enough system partition to include "de"
95      * immediately after the last official release for Dream (whenever
96      * that may be).
97      *
98     // Test some must-have locales.
99     @LargeTest
100     public void testResourceBundles() throws Exception {
101         Locale eng = new Locale("en", "US");
102         DateFormatSymbols engSymbols = new DateFormatSymbols(eng);
103 
104         Locale deu = new Locale("de", "DE");
105         DateFormatSymbols deuSymbols = new DateFormatSymbols(deu);
106 
107         TimeZone berlin = TimeZone.getTimeZone("Europe/Berlin");
108 
109         assertEquals("January", engSymbols.getMonths()[0]);
110         assertEquals("Januar", deuSymbols.getMonths()[0]);
111 
112         assertEquals("Sunday", engSymbols.getWeekdays()[Calendar.SUNDAY]);
113         assertEquals("Sonntag", deuSymbols.getWeekdays()[Calendar.SUNDAY]);
114 
115         assertEquals("Central European Time",
116                 berlin.getDisplayName(false, TimeZone.LONG, eng));
117         assertEquals("Central European Summer Time",
118                 berlin.getDisplayName(true, TimeZone.LONG, eng));
119 
120         assertEquals("Mitteleurop\u00E4ische Zeit",
121                 berlin.getDisplayName(false, TimeZone.LONG, deu));
122         assertEquals("Mitteleurop\u00E4ische Sommerzeit",
123                 berlin.getDisplayName(true, TimeZone.LONG, deu));
124 
125         assertTrue(engSymbols.getZoneStrings().length > 100);
126     }
127     */
128 
129     // This one makes sure we have all necessary locales installed.
130     // Suppress this flaky test for now.
131     @Suppress
testICULocales()132     public void testICULocales() {
133         String[] locales = new String[] {
134                 // List of locales currently required for Android.
135                 "en_US", "es_US", "en_GB", "fr_FR", "de_DE", "de_AT", "cs_CZ", "nl_NL" };
136 
137         String[] mondays = new String[] {
138                 "Monday", "lunes", "Monday", "lundi", "Montag", "Montag", "pond\u011bl\u00ed", "maandag" };
139 
140         String[] currencies = new String[] {
141                 "USD", "USD", "GBP", "EUR", "EUR", "EUR", "CZK", "EUR"};
142 
143         for (int i = 0; i < locales.length; i++) {
144             Locale l = new Locale(locales[i].substring(0, 2), locales[i].substring(3));
145 
146             // Check language part of locale.
147             DateFormatSymbols d = new DateFormatSymbols(l);
148             assertEquals("Monday name for " + locales[i] + " must match",
149                     mondays[i], d.getWeekdays()[2]);
150 
151             // Check country part of locale.
152             Currency c = Currency.getInstance(l);
153             assertEquals("Currency code for " + locales[i] + " must match",
154                     currencies[i], c.getCurrencyCode());
155         }
156     }
157 
158     // Regression test for 1118570: Create test cases for tracking ICU config
159     // changes. This one makes sure we have the necessary converters installed
160     // and don't lose the changes to the converter alias table.
161     @MediumTest
testICUConverters()162     public void testICUConverters() {
163         // List of encodings currently required for Android.
164         String[] encodings = new String[] {
165                 // Encoding required by the language specification.
166                 "US-ASCII",
167                 "UTF-8",
168                 "UTF-16",
169                 "UTF-16BE",
170                 "UTF-16LE",
171                 "ISO-8859-1",
172 
173                 // Additional encodings included in standard ICU
174                 "ISO-8859-2",
175                 "ISO-8859-3",
176                 "ISO-8859-4",
177                 "ISO-8859-5",
178                 "ISO-8859-6",
179                 "ISO-8859-7",
180                 "ISO-8859-8",
181                 "ISO-8859-8-I",
182                 "ISO-8859-9",
183                 "ISO-8859-10",
184                 "ISO-8859-11",
185                 "ISO-8859-13",
186                 "ISO-8859-14",
187                 "ISO-8859-15",
188                 "ISO-8859-16",
189                 "ISO-2022-JP",
190                 "Windows-950",
191                 "Windows-1250",
192                 "Windows-1251",
193                 "Windows-1252",
194                 "Windows-1253",
195                 "Windows-1254",
196                 "Windows-1255",
197                 "Windows-1256",
198                 "Windows-1257",
199                 "Windows-1258",
200                 "Big5",
201                 "CP864",
202                 "CP874",
203                 "EUC-CN",
204                 "EUC-JP",
205                 "KOI8-R",
206                 "Macintosh",
207                 "GBK",
208                 "GB2312",
209                 "EUC-KR",
210 
211                 // Additional encoding not included in standard ICU.
212                 "GSM0338" };
213 
214         for (int i = 0; i < encodings.length; i++) {
215             assertTrue("Charset " + encodings[i] + " must be supported",
216                     Charset.isSupported(encodings[i]));
217 
218             Charset cs = Charset.forName(encodings[i]);
219             android.util.Log.d("LocaleTest", cs.name());
220 
221             Set<String> aliases = cs.aliases();
222             for (String s: aliases) {
223                 android.util.Log.d("LocaleTest", " - " + s);
224             }
225         }
226 
227         // Test for valid encoding that is not included in Android. IBM-37 is
228         // a perfect candidate for this, as it is being used for mainframes and
229         // thus somewhat out of the scope of Android.
230         assertFalse("Charset IBM-37 must not be supported",
231                 Charset.isSupported("IBM-37"));
232 
233         // Test for a bogus encoding.
234         assertFalse("Charset KLINGON must not be supported",
235                 Charset.isSupported("KLINGON"));
236 
237         // Make sure our local change to the real translation table used for
238         // EUC-JP doesn't get lost.
239         Charset cs = Charset.forName("EUC-JP");
240         assertTrue("EUC-JP must use 'ibm-954_P101-2007'", cs.aliases().contains("ibm-954_P101-2007"));
241     }
242 
243 }
244