1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package libcore.java.util;
19
20import dalvik.annotation.AndroidOnly;
21import java.util.Date;
22import java.util.Locale;
23import java.util.TimeZone;
24import junit.framework.TestCase;
25
26public class OldTimeZoneTest extends TestCase {
27
28    static class Mock_TimeZone extends TimeZone {
29        @Override
30        public int getOffset(int era, int year, int month, int day, int dayOfWeek, int milliseconds) {
31            return 0;
32        }
33
34        @Override
35        public int getRawOffset() {
36            return 0;
37        }
38
39        @Override
40        public boolean inDaylightTime(Date date) {
41            return false;
42        }
43
44        @Override
45        public void setRawOffset(int offsetMillis) {
46
47        }
48
49        @Override
50        public boolean useDaylightTime() {
51            return false;
52        }
53    }
54
55    public void test_constructor() {
56        assertNotNull(new Mock_TimeZone());
57    }
58
59    public void test_clone() {
60        TimeZone tz1 = TimeZone.getDefault();
61        TimeZone tz2 = (TimeZone)tz1.clone();
62
63        assertTrue(tz1.equals(tz2));
64    }
65
66    public void test_getAvailableIDs() {
67        String[] str = TimeZone.getAvailableIDs();
68        assertNotNull(str);
69        assertTrue(str.length != 0);
70        for(int i = 0; i < str.length; i++) {
71            assertNotNull(TimeZone.getTimeZone(str[i]));
72        }
73    }
74
75    public void test_getAvailableIDsI() {
76        String[] str = TimeZone.getAvailableIDs(0);
77        assertNotNull(str);
78        assertTrue(str.length != 0);
79        for(int i = 0; i < str.length; i++) {
80            assertNotNull(TimeZone.getTimeZone(str[i]));
81        }
82    }
83
84    public void test_getDisplayName() {
85        Locale.setDefault(Locale.US);
86        TimeZone tz = TimeZone.getTimeZone("GMT-6");
87        assertEquals("GMT-06:00", tz.getDisplayName());
88        tz = TimeZone.getTimeZone("America/Los_Angeles");
89        assertEquals("Pacific Standard Time", tz.getDisplayName());
90    }
91
92    public void test_getDisplayNameLjava_util_Locale() {
93        TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
94        assertEquals("Pacific Standard Time", tz.getDisplayName(Locale.US));
95        assertEquals("heure normale du Pacifique nord-américain", tz.getDisplayName(Locale.FRANCE));
96    }
97
98    public void test_getDisplayNameZI() {
99        Locale.setDefault(Locale.US);
100        TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
101        assertEquals("PST",                   tz.getDisplayName(false, TimeZone.SHORT));
102        assertEquals("Pacific Daylight Time", tz.getDisplayName(true, TimeZone.LONG));
103        assertEquals("Pacific Standard Time", tz.getDisplayName(false, TimeZone.LONG));
104    }
105
106    @AndroidOnly("fail on RI. See comment below")
107    public void test_getDisplayNameZILjava_util_Locale() {
108        TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
109        assertEquals("Pacific Daylight Time", tz.getDisplayName(true,  TimeZone.LONG, Locale.US));
110        assertEquals("Pacific Standard Time", tz.getDisplayName(false, TimeZone.LONG, Locale.UK));
111        assertEquals("heure avanc\u00e9e du Pacifique",
112                tz.getDisplayName(true,  TimeZone.LONG, Locale.FRANCE));
113        assertEquals("heure normale du Pacifique nord-américain",
114                tz.getDisplayName(false, TimeZone.LONG, Locale.FRANCE));
115
116        assertEquals("PDT", tz.getDisplayName(true, TimeZone.SHORT, Locale.US));
117        assertEquals("PST", tz.getDisplayName(false, TimeZone.SHORT, Locale.US));
118        // RI fails on following lines. RI always returns short time zone name for
119        // "America/Los_Angeles" as "PST", Android only returns a string if ICU has a translation.
120        // There is no short time zone name for America/Los_Angeles in French or British English in
121        // ICU data so an offset is returned instead.
122        assertEquals("GMT-08:00", tz.getDisplayName(false, TimeZone.SHORT, Locale.FRANCE));
123        assertEquals("GMT-07:00", tz.getDisplayName(true, TimeZone.SHORT, Locale.FRANCE));
124        assertEquals("GMT-08:00", tz.getDisplayName(false, TimeZone.SHORT, Locale.UK));
125        assertEquals("GMT-07:00", tz.getDisplayName(true, TimeZone.SHORT, Locale.UK));
126
127        // The RI behavior mentioned above does not appear to be because "PST" is a legacy
128        // three-character timezone supported by the RI: it happens for "Asia/Tehran"/"IRST" too
129        // (IRST is not a legacy code). The RI may just use a different dataset that has "PST" /
130        // "IRST" as valid translations (even for scripts like Chinese).
131        TimeZone iranTz = TimeZone.getTimeZone("Asia/Tehran");
132        assertEquals("Iran Summer Time", iranTz.getDisplayName(true, TimeZone.LONG, Locale.UK));
133        assertEquals("Iran Daylight Time", iranTz.getDisplayName(true, TimeZone.LONG, Locale.US));
134        assertEquals("Iran Standard Time", iranTz.getDisplayName(false, TimeZone.LONG, Locale.UK));
135        assertEquals("Iran Standard Time", iranTz.getDisplayName(false, TimeZone.LONG, Locale.US));
136        assertEquals("GMT+03:30", iranTz.getDisplayName(false, TimeZone.SHORT, Locale.UK));
137        assertEquals("GMT+04:30", iranTz.getDisplayName(true, TimeZone.SHORT, Locale.UK));
138    }
139
140    public void test_getID() {
141        TimeZone tz = TimeZone.getTimeZone("GMT-6");
142        assertEquals("GMT-06:00", tz.getID());
143        tz = TimeZone.getTimeZone("America/Denver");
144        assertEquals("America/Denver", tz.getID());
145    }
146
147    public void test_hasSameRulesLjava_util_TimeZone() {
148        TimeZone tz1 = TimeZone.getTimeZone("America/Denver");
149        TimeZone tz2 = TimeZone.getTimeZone("America/Phoenix");
150        assertEquals(tz1.getDisplayName(false, 0), tz2.getDisplayName(false, 0));
151        // Arizona doesn't observe DST. See http://phoenix.about.com/cs/weather/qt/timezone.htm
152        assertFalse(tz1.hasSameRules(tz2));
153        assertFalse(tz1.hasSameRules(null));
154        tz1 = TimeZone.getTimeZone("America/New_York");
155        tz2 = TimeZone.getTimeZone("US/Eastern");
156        assertEquals(tz1.getDisplayName(), tz2.getDisplayName());
157        assertFalse(tz1.getID().equals(tz2.getID()));
158        assertTrue(tz2.hasSameRules(tz1));
159        assertTrue(tz1.hasSameRules(tz1));
160    }
161
162    public void test_setIDLjava_lang_String() {
163        TimeZone tz = TimeZone.getTimeZone("GMT-6");
164        assertEquals("GMT-06:00", tz.getID());
165        tz.setID("New ID for GMT-6");
166        assertEquals("New ID for GMT-6", tz.getID());
167    }
168}
169