1 /*
2  * Copyright (C) 2013 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.icu;
18 
19 import java.util.Arrays;
20 import java.util.Locale;
21 import java.util.HashSet;
22 import java.util.TimeZone;
23 
24 public class TimeZoneNamesTest extends junit.framework.TestCase {
test_forLocale()25   public void test_forLocale() throws Exception {
26     String[] ids = TimeZoneNames.forLocale(Locale.CANADA);
27     // Check that we got some ids.
28     assertTrue(ids.length > 0);
29     HashSet<String> allIds = new HashSet<String>(Arrays.asList(TimeZone.getAvailableIDs()));
30     // Check that they're all real.
31     for (String id : ids) {
32       assertTrue(allIds.contains(id));
33     }
34     // Check that Toronto comes before Atikokan. http://b/8391426.
35     int toronto = linearSearch(ids, "America/Toronto");
36     assertTrue(toronto >= 0);
37     int atikokan = linearSearch(ids, "America/Atikokan");
38     assertTrue(atikokan >= 0);
39     assertTrue(toronto < atikokan);
40   }
41 
42   private int linearSearch(String[] xs, String x) {
43     for (int i = 0; i < xs.length; ++i) {
44       if (xs[i].equals(x)) {
45         return i;
46       }
47     }
48     return -1;
49   }
50 
51   public void testBug9327819() throws Exception {
52     // Check one specific example of a zone.tab line without a comment thoroughly.
53     String[] ids = TimeZoneNames.forLocale(Locale.KOREA);
54     assertEquals("Asia/Seoul", ids[0]);
55     assertEquals(1, ids.length);
56 
57     // Now check we can parse all countries' lines.
58     for (Locale l : Locale.getAvailableLocales()) {
59       assertTrue(TimeZoneNames.forLocale(l) != null);
60     }
61   }
62 
63   public void test_getExemplarLocation() throws Exception {
64     assertEquals("Moscow", TimeZoneNames.getExemplarLocation("en_US", "Europe/Moscow"));
65     assertEquals("Moskau", TimeZoneNames.getExemplarLocation("de_DE", "Europe/Moscow"));
66     assertEquals("Seoul", TimeZoneNames.getExemplarLocation("en_US", "Asia/Seoul"));
67     assertEquals("서울", TimeZoneNames.getExemplarLocation("ko_KR", "Asia/Seoul"));
68   }
69 }
70