1package com.xtremelabs.robolectric.shadows;
2
3import android.text.format.Time;
4import android.util.TimeFormatException;
5import com.xtremelabs.robolectric.WithTestDefaultsRunner;
6import org.junit.Test;
7import org.junit.runner.RunWith;
8
9import static org.hamcrest.CoreMatchers.equalTo;
10import static org.hamcrest.CoreMatchers.not;
11import static org.junit.Assert.*;
12
13@RunWith(WithTestDefaultsRunner.class)
14public class TimeTest {
15    @Test
16    public void shouldSetToNow() throws Exception {
17        Time t = new Time();
18        t.setToNow();
19        assertThat(t.toMillis(false), not(equalTo(0l)));
20    }
21
22    @Test
23    public void shouldHaveNoArgsConstructor() throws Exception {
24        Time t = new Time();
25        assertNotNull(t.timezone);
26    }
27
28    @Test
29    public void shouldHaveCopyConstructor() throws Exception {
30        Time t = new Time();
31        t.setToNow();
32        Time t2 = new Time(t);
33        assertEquals(t.timezone, t2.timezone);
34        assertEquals(t.year, t2.year);
35        assertEquals(t.month, t2.month);
36        assertEquals(t.monthDay, t2.monthDay);
37        assertEquals(t.hour, t2.hour);
38        assertEquals(t.minute, t2.minute);
39        assertEquals(t.second, t2.second);
40    }
41
42    @Test
43    public void shouldHaveSetTime() throws Exception {
44        Time t = new Time();
45        t.setToNow();
46        Time t2 = new Time();
47        t2.set(t);
48        assertEquals(t.timezone, t2.timezone);
49        assertEquals(t.year, t2.year);
50        assertEquals(t.month, t2.month);
51        assertEquals(t.monthDay, t2.monthDay);
52        assertEquals(t.hour, t2.hour);
53        assertEquals(t.minute, t2.minute);
54        assertEquals(t.second, t2.second);
55    }
56
57    @Test
58    public void shouldHaveSet3Args() throws Exception {
59        Time t = new Time();
60        t.set(1, 1, 2000);
61        assertEquals(t.year, 2000);
62        assertEquals(t.month, 1);
63        assertEquals(t.monthDay, 1);
64    }
65
66    @Test
67    public void shouldHaveSet6Args() throws Exception {
68        Time t = new Time();
69        t.set(1, 1, 1, 1, 1, 2000);
70        assertEquals(t.year, 2000);
71        assertEquals(t.month, 1);
72        assertEquals(t.monthDay, 1);
73        assertEquals(t.second, 1);
74        assertEquals(t.minute, 1);
75        assertEquals(t.hour, 1);
76    }
77
78    @Test
79    public void shouldHaveTimeZoneConstructor() throws Exception {
80        Time t = new Time("UTC");
81        assertEquals(t.timezone, "UTC");
82    }
83
84    @Test
85    public void shouldClear() throws Exception {
86        Time t = new Time();
87        t.setToNow();
88        t.clear("UTC");
89        assertEquals("UTC", t.timezone);
90        assertEquals(0, t.year);
91        assertEquals(0, t.month);
92        assertEquals(0, t.monthDay);
93        assertEquals(0, t.hour);
94        assertEquals(0, t.minute);
95        assertEquals(0, t.second);
96        assertEquals(0, t.weekDay);
97        assertEquals(0, t.yearDay);
98        assertEquals(0, t.gmtoff);
99        assertEquals(-1, t.isDst);
100    }
101
102    @Test
103    public void shouldHaveToMillis() throws Exception {
104        Time t = new Time();
105        t.set(86400 * 1000);
106        assertEquals(86400 * 1000, t.toMillis(false));
107    }
108
109    @Test
110    public void shouldHaveCurrentTimeZone() throws Exception {
111        assertNotNull(Time.getCurrentTimezone());
112    }
113
114    @Test
115    public void shouldHaveCompareAndBeforeAfter() throws Exception {
116        Time a = new Time();
117        Time b = new Time();
118        assertEquals(0, Time.compare(a, b));
119        assertFalse(a.before(b));
120        assertFalse(a.after(b));
121        a.year = 2000;
122        assertEquals(1, Time.compare(a, b));
123        assertTrue(a.after(b));
124        assertTrue(b.before(a));
125        b.year = 2001;
126        assertEquals(-1, Time.compare(a, b));
127        assertTrue(b.after(a));
128        assertTrue(a.before(b));
129    }
130
131    @Test
132    public void shouldHaveParse() throws Exception {
133        Time t = new Time("Europe/Berlin");
134        assertFalse(t.parse("20081013T160000"));
135        assertEquals(2008, t.year);
136        assertEquals(9, t.month);
137        assertEquals(13, t.monthDay);
138        assertEquals(16, t.hour);
139        assertEquals(0, t.minute);
140        assertEquals(0, t.second);
141
142        assertTrue(t.parse("20081013T160000Z"));
143        assertEquals(2008, t.year);
144        assertEquals(9, t.month);
145        assertEquals(13, t.monthDay);
146        assertEquals(16, t.hour);
147        assertEquals(0, t.minute);
148        assertEquals(0, t.second);
149    }
150
151    @Test(expected = TimeFormatException.class)
152    public void shouldThrowTimeFormatException() throws Exception {
153        Time t = new Time();
154        t.parse("BLARGH");
155    }
156
157    @Test
158    public void shouldHaveParseShort() throws Exception {
159        Time t = new Time();
160        t.parse("20081013");
161        assertEquals(2008, t.year);
162        assertEquals(9, t.month);
163        assertEquals(13, t.monthDay);
164        assertEquals(0, t.hour);
165        assertEquals(0, t.minute);
166        assertEquals(0, t.second);
167    }
168
169    @Test
170    public void shouldFormat() throws Exception {
171        Time t = new Time();
172        assertEquals("Hallo epoch 01 1970 01", t.format("Hallo epoch %d %Y %d"));
173    }
174
175    @Test
176    public void shouldFormat2445() throws Exception {
177        Time t = new Time();
178        assertEquals("19700101T000000", t.format2445());
179    }
180
181    @Test
182    public void shouldFormat3339() throws Exception {
183        Time t = new Time("Europe/Berlin");
184        assertEquals("1970-01-01T00:00:00.000+00:00", t.format3339(false));
185        assertEquals("1970-01-01", t.format3339(true));
186    }
187
188    @Test
189    public void testIsEpoch() throws Exception {
190        Time t = new Time();
191        boolean isEpoch = Time.isEpoch(t);
192        assertEquals(true, isEpoch);
193    }
194
195    @Test
196    public void testGetJulianDay() throws Exception {
197        Time time = new Time();
198
199        time.set(0, 0, 0, 12, 5, 2008);
200        time.timezone = "Australia/Sydney";
201        long millis = time.normalize(true);
202
203        // This is the Julian day for 12am for this day of the year
204        int julianDay = Time.getJulianDay(millis, time.gmtoff);
205
206        // Change the time during the day and check that we get the same
207        // Julian day.
208        for (int hour = 0; hour < 24; hour++) {
209            for (int minute = 0; minute < 60; minute += 15) {
210                time.set(0, minute, hour, 12, 5, 2008);
211                millis = time.normalize(true);
212                int day = Time.getJulianDay(millis, time.gmtoff);
213
214                assertEquals(day, julianDay);
215            }
216        }
217    }
218
219    @Test
220    public void testSetJulianDay() throws Exception {
221        Time time = new Time();
222        time.set(0, 0, 0, 12, 5, 2008);
223        time.timezone = "Australia/Sydney";
224        long millis = time.normalize(true);
225
226        int julianDay = Time.getJulianDay(millis, time.gmtoff);
227        time.setJulianDay(julianDay);
228
229        assertTrue(time.hour == 0 || time.hour == 1);
230        assertEquals(0, time.minute);
231        assertEquals(0, time.second);
232
233        millis = time.toMillis(false);
234        int day = Time.getJulianDay(millis, time.gmtoff);
235
236        assertEquals(day, julianDay);
237    }
238}