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    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(new Locale("US")));
95        assertEquals("heure normale du Pacifique", 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, 0));
102        assertEquals("Pacific Daylight Time", tz.getDisplayName(true, 1));
103        assertEquals("Pacific Standard Time", tz.getDisplayName(false, 1));
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("PST",                   tz.getDisplayName(false, 0, Locale.US));
110        assertEquals("Pacific Daylight Time", tz.getDisplayName(true,  1, Locale.US));
111        assertEquals("Pacific Standard Time", tz.getDisplayName(false, 1, Locale.UK));
112        // RI always returns short time zone name as "PST"
113        // ICU zone/root.txt patched to allow metazone names.
114        assertEquals("PST",             tz.getDisplayName(false, 0, Locale.FRANCE));
115        assertEquals("heure avanc\u00e9e du Pacifique", tz.getDisplayName(true,  1, Locale.FRANCE));
116        assertEquals("heure normale du Pacifique", tz.getDisplayName(false, 1, Locale.FRANCE));
117    }
118
119    public void test_getID() {
120        TimeZone tz = TimeZone.getTimeZone("GMT-6");
121        assertEquals("GMT-06:00", tz.getID());
122        tz = TimeZone.getTimeZone("America/Denver");
123        assertEquals("America/Denver", tz.getID());
124    }
125
126    public void test_hasSameRulesLjava_util_TimeZone() {
127        TimeZone tz1 = TimeZone.getTimeZone("America/Denver");
128        TimeZone tz2 = TimeZone.getTimeZone("America/Phoenix");
129        assertEquals(tz1.getDisplayName(false, 0), tz2.getDisplayName(false, 0));
130        // Arizona doesn't observe DST. See http://phoenix.about.com/cs/weather/qt/timezone.htm
131        assertFalse(tz1.hasSameRules(tz2));
132        assertFalse(tz1.hasSameRules(null));
133        tz1 = TimeZone.getTimeZone("America/New_York");
134        tz2 = TimeZone.getTimeZone("US/Eastern");
135        assertEquals(tz1.getDisplayName(), tz2.getDisplayName());
136        assertFalse(tz1.getID().equals(tz2.getID()));
137        assertTrue(tz2.hasSameRules(tz1));
138        assertTrue(tz1.hasSameRules(tz1));
139    }
140
141    public void test_setIDLjava_lang_String() {
142        TimeZone tz = TimeZone.getTimeZone("GMT-6");
143        assertEquals("GMT-06:00", tz.getID());
144        tz.setID("New ID for GMT-6");
145        assertEquals("New ID for GMT-6", tz.getID());
146    }
147}
148