1 /*
2  * Copyright (C) 2009 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.text.format.cts;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertSame;
23 import static org.junit.Assert.assertTrue;
24 
25 import android.content.Context;
26 import android.support.test.InstrumentationRegistry;
27 import android.support.test.filters.SmallTest;
28 import android.support.test.runner.AndroidJUnit4;
29 import android.text.format.DateUtils;
30 
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 
35 import java.text.DateFormat;
36 import java.util.Calendar;
37 import java.util.Date;
38 import java.util.Formatter;
39 import java.util.GregorianCalendar;
40 import java.util.Locale;
41 import java.util.TimeZone;
42 
43 @SmallTest
44 @RunWith(AndroidJUnit4.class)
45 public class DateUtilsTest {
46     private long mBaseTime;
47     private Context mContext;
48 
49     @Before
setup()50     public void setup() {
51         mContext = InstrumentationRegistry.getTargetContext();
52         TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
53         mBaseTime = System.currentTimeMillis();
54     }
55 
56     @Test
testGetDayOfWeekString()57     public void testGetDayOfWeekString() {
58         if (!LocaleUtils.isCurrentLocale(mContext, Locale.US)) {
59             return;
60         }
61 
62         assertEquals("Sunday",
63                 DateUtils.getDayOfWeekString(Calendar.SUNDAY, DateUtils.LENGTH_LONG));
64         assertEquals("Sun",
65                 DateUtils.getDayOfWeekString(Calendar.SUNDAY, DateUtils.LENGTH_MEDIUM));
66         assertEquals("Sun",
67                 DateUtils.getDayOfWeekString(Calendar.SUNDAY, DateUtils.LENGTH_SHORT));
68         assertEquals("Sun",
69                 DateUtils.getDayOfWeekString(Calendar.SUNDAY, DateUtils.LENGTH_SHORTER));
70         assertEquals("S",
71                 DateUtils.getDayOfWeekString(Calendar.SUNDAY, DateUtils.LENGTH_SHORTEST));
72         // Other abbrev
73         assertEquals("Sun",
74                 DateUtils.getDayOfWeekString(Calendar.SUNDAY, 60));
75     }
76 
77     @Test
testGetMonthString()78     public void testGetMonthString() {
79         if (!LocaleUtils.isCurrentLocale(mContext, Locale.US)) {
80             return;
81         }
82         assertEquals("January", DateUtils.getMonthString(Calendar.JANUARY, DateUtils.LENGTH_LONG));
83         assertEquals("Jan",
84                 DateUtils.getMonthString(Calendar.JANUARY, DateUtils.LENGTH_MEDIUM));
85         assertEquals("Jan", DateUtils.getMonthString(Calendar.JANUARY, DateUtils.LENGTH_SHORT));
86         assertEquals("Jan",
87                 DateUtils.getMonthString(Calendar.JANUARY, DateUtils.LENGTH_SHORTER));
88         assertEquals("J",
89                 DateUtils.getMonthString(Calendar.JANUARY, DateUtils.LENGTH_SHORTEST));
90         // Other abbrev
91         assertEquals("Jan", DateUtils.getMonthString(Calendar.JANUARY, 60));
92     }
93 
94     @Test
testGetAMPMString()95     public void testGetAMPMString() {
96         if (!LocaleUtils.isCurrentLocale(mContext, Locale.US)) {
97             return;
98         }
99         assertEquals("AM", DateUtils.getAMPMString(Calendar.AM));
100         assertEquals("PM", DateUtils.getAMPMString(Calendar.PM));
101     }
102 
103     // This is to test the mapping between DateUtils' public API and
104     // libcore/icu4c's implementation. More tests, in different locales, are
105     // in libcore's CTS tests.
106     @Test
test_getRelativeTimeSpanString()107     public void test_getRelativeTimeSpanString() {
108         if (!LocaleUtils.isCurrentLocale(mContext, Locale.US)) {
109             return;
110         }
111 
112         final long ONE_SECOND_IN_MS = 1000;
113         assertEquals("0 minutes ago",
114                 DateUtils.getRelativeTimeSpanString(mBaseTime - ONE_SECOND_IN_MS));
115         assertEquals("In 0 minutes",
116                 DateUtils.getRelativeTimeSpanString(mBaseTime + ONE_SECOND_IN_MS));
117 
118         final long ONE_MINUTE_IN_MS = 60 * ONE_SECOND_IN_MS;
119         assertEquals("1 minute ago", DateUtils.getRelativeTimeSpanString(0, ONE_MINUTE_IN_MS,
120                 DateUtils.MINUTE_IN_MILLIS));
121         assertEquals("In 1 minute", DateUtils.getRelativeTimeSpanString(ONE_MINUTE_IN_MS, 0,
122                 DateUtils.MINUTE_IN_MILLIS));
123 
124         final long ONE_HOUR_IN_MS = 60 * 60 * 1000;
125         final long TWO_HOURS_IN_MS = 2 * ONE_HOUR_IN_MS;
126         assertEquals("2 hours ago", DateUtils.getRelativeTimeSpanString(mBaseTime - TWO_HOURS_IN_MS,
127                 mBaseTime, DateUtils.MINUTE_IN_MILLIS, DateUtils.FORMAT_NUMERIC_DATE));
128         assertEquals("In 2 hours", DateUtils.getRelativeTimeSpanString(mBaseTime + TWO_HOURS_IN_MS,
129                 mBaseTime, DateUtils.MINUTE_IN_MILLIS, DateUtils.FORMAT_NUMERIC_DATE));
130     }
131 
132     @Test
test_getRelativeTimeSpanString_withContext()133     public void test_getRelativeTimeSpanString_withContext() {
134         if (!LocaleUtils.isCurrentLocale(mContext, Locale.US)) {
135             return;
136         }
137 
138         final GregorianCalendar cal = new GregorianCalendar();
139         cal.setTimeInMillis(mBaseTime);
140         cal.set(Calendar.HOUR_OF_DAY, 10);
141         cal.set(Calendar.MINUTE, 0);
142         final long today10am = cal.getTimeInMillis();
143 
144         final CharSequence withPrep = DateUtils.getRelativeTimeSpanString(mContext, today10am,
145                 true /* with preposition */);
146         final CharSequence noPrep = DateUtils.getRelativeTimeSpanString(mContext, today10am,
147                 false /* no preposition */);
148         assertEquals(noPrep, DateUtils.getRelativeTimeSpanString(mContext, today10am));
149 
150         if (android.text.format.DateFormat.is24HourFormat(mContext)) {
151             assertEquals("at 10:00", withPrep);
152             assertEquals("10:00", noPrep);
153         } else {
154             assertEquals("at 10:00 AM", withPrep);
155             assertEquals("10:00 AM", noPrep);
156         }
157     }
158 
159     // Similar to test_getRelativeTimeSpanString(). The function here is to
160     // test the mapping between DateUtils's public API and libcore/icu4c's
161     // implementation. More tests, in different locales, are in libcore's
162     // CTS tests.
163     @Test
test_getRelativeDateTimeString()164     public void test_getRelativeDateTimeString() {
165         final long DAY_DURATION = 5 * 24 * 60 * 60 * 1000;
166         assertNotNull(DateUtils.getRelativeDateTimeString(mContext, mBaseTime - DAY_DURATION,
167                 DateUtils.MINUTE_IN_MILLIS, DateUtils.DAY_IN_MILLIS, DateUtils.FORMAT_NUMERIC_DATE));
168     }
169 
170     @Test
test_formatElapsedTime()171     public void test_formatElapsedTime() {
172         if (!LocaleUtils.isCurrentLocale(mContext, Locale.US)) {
173             return;
174         }
175 
176         long MINUTES = 60;
177         long HOURS = 60 * MINUTES;
178         verifyFormatElapsedTime("02:01", 2 * MINUTES + 1);
179         verifyFormatElapsedTime("3:02:01", 3 * HOURS + 2 * MINUTES + 1);
180         // http://code.google.com/p/android/issues/detail?id=41401
181         verifyFormatElapsedTime("123:02:01", 123 * HOURS + 2 * MINUTES + 1);
182     }
183 
verifyFormatElapsedTime(String expected, long elapsedTime)184     private void verifyFormatElapsedTime(String expected, long elapsedTime) {
185         assertEquals(expected, DateUtils.formatElapsedTime(elapsedTime));
186         StringBuilder sb = new StringBuilder();
187         assertEquals(expected, DateUtils.formatElapsedTime(sb, elapsedTime));
188         assertEquals(expected, sb.toString());
189     }
190 
191     @Test
testFormatSameDayTime()192     public void testFormatSameDayTime() {
193         if (!LocaleUtils.isCurrentLocale(mContext, Locale.US)) {
194             return;
195         }
196 
197         // This test assumes a default DateFormat.is24Hour setting.
198         DateFormat.is24Hour = null;
199         Date date = new Date(109, 0, 19, 3, 30, 15);
200         long fixedTime = date.getTime();
201 
202         int currentYear = Calendar.getInstance().get(Calendar.YEAR);
203         Date dateWithCurrentYear = new Date(currentYear - 1900, 0, 19, 3, 30, 15);
204 
205         final long DAY_DURATION = 5 * 24 * 60 * 60 * 1000;
206         assertEquals("Saturday, January 24, 2009", DateUtils.formatSameDayTime(
207                 fixedTime + DAY_DURATION, fixedTime, java.text.DateFormat.FULL,
208                 java.text.DateFormat.FULL));
209         assertEquals("Jan 24, 2009", DateUtils.formatSameDayTime(fixedTime + DAY_DURATION,
210                 fixedTime, java.text.DateFormat.DEFAULT, java.text.DateFormat.FULL));
211         assertEquals("January 24, 2009", DateUtils.formatSameDayTime(fixedTime + DAY_DURATION,
212                 fixedTime, java.text.DateFormat.LONG, java.text.DateFormat.FULL));
213         assertEquals("Jan 24, 2009", DateUtils.formatSameDayTime(fixedTime + DAY_DURATION,
214                 fixedTime, java.text.DateFormat.MEDIUM, java.text.DateFormat.FULL));
215         assertEquals("1/24/09", DateUtils.formatSameDayTime(fixedTime + DAY_DURATION,
216                 fixedTime, java.text.DateFormat.SHORT, java.text.DateFormat.FULL));
217 
218         final long HOUR_DURATION = 2 * 60 * 60 * 1000;
219         assertEquals("5:30:15 AM GMT+00:00", DateUtils.formatSameDayTime(fixedTime + HOUR_DURATION,
220                 fixedTime, java.text.DateFormat.FULL, java.text.DateFormat.FULL));
221         assertEquals("5:30:15 AM", DateUtils.formatSameDayTime(fixedTime + HOUR_DURATION,
222                 fixedTime, java.text.DateFormat.FULL, java.text.DateFormat.DEFAULT));
223         assertEquals("5:30:15 AM GMT+00:00", DateUtils.formatSameDayTime(fixedTime + HOUR_DURATION,
224                 fixedTime, java.text.DateFormat.FULL, java.text.DateFormat.LONG));
225         assertEquals("5:30:15 AM", DateUtils.formatSameDayTime(fixedTime + HOUR_DURATION,
226                 fixedTime, java.text.DateFormat.FULL, java.text.DateFormat.MEDIUM));
227         assertEquals("5:30 AM", DateUtils.formatSameDayTime(fixedTime + HOUR_DURATION,
228                 fixedTime, java.text.DateFormat.FULL, java.text.DateFormat.SHORT));
229     }
230 
231     // This is just to exercise the wrapper that calls the libcore/icu4c implementation.
232     // Full testing, in multiple locales, is in libcore's CTS tests.
233     @Test
testFormatDateRange()234     public void testFormatDateRange() {
235         if (!LocaleUtils.isCurrentLocale(mContext, Locale.US)) {
236             return;
237         }
238 
239         final Date date = new Date(109, 0, 19, 3, 30, 15);
240         final long fixedTime = date.getTime();
241         final long hourDuration = 2 * 60 * 60 * 1000;
242         assertEquals("Monday", DateUtils.formatDateRange(mContext, fixedTime,
243                 fixedTime + hourDuration, DateUtils.FORMAT_SHOW_WEEKDAY));
244     }
245 
246     @Test
testFormatDateRange_withFormatter()247     public void testFormatDateRange_withFormatter() {
248         if (!LocaleUtils.isCurrentLocale(mContext, Locale.US)) {
249             return;
250         }
251 
252         final Date date = new Date(109, 0, 19, 3, 30, 15);
253         final long fixedTime = date.getTime();
254         final long hourDuration = 2 * 60 * 60 * 1000;
255         final Formatter formatter = new Formatter();
256         final Formatter result = DateUtils.formatDateRange(mContext, formatter, fixedTime,
257                 fixedTime + hourDuration, DateUtils.FORMAT_SHOW_WEEKDAY);
258         assertEquals("Monday", result.toString());
259         assertSame(result, formatter);
260     }
261 
262     @Test
testFormatDateRange_withFormatterAndTimezone()263     public void testFormatDateRange_withFormatterAndTimezone() {
264         if (!LocaleUtils.isCurrentLocale(mContext, Locale.US)) {
265             return;
266         }
267 
268         final Date date = new Date(109, 0, 19, 3, 30, 15);
269         final long fixedTime = date.getTime();
270         final long hourDuration = 2 * 60 * 60 * 1000;
271         final Formatter formatter = new Formatter();
272         final Formatter result = DateUtils.formatDateRange(mContext, formatter, fixedTime,
273                 fixedTime + hourDuration, DateUtils.FORMAT_SHOW_WEEKDAY, null /* local */);
274         assertEquals("Monday", result.toString());
275         assertSame(result, formatter);
276     }
277 
278     @Test
testFormatDateTime()279     public void testFormatDateTime() {
280         if (!LocaleUtils.isCurrentLocale(mContext, Locale.US)) {
281             return;
282         }
283 
284         final Date date = new Date(109, 0, 19, 3, 30, 15);
285         final long fixedTime = date.getTime();
286         assertEquals("Monday", DateUtils.formatDateTime(mContext, fixedTime,
287                 DateUtils.FORMAT_SHOW_WEEKDAY));
288     }
289 
290     @Test
testIsToday()291     public void testIsToday() {
292         final long ONE_DAY_IN_MS = 24 * 60 * 60 * 1000;
293         assertTrue(DateUtils.isToday(mBaseTime));
294         assertFalse(DateUtils.isToday(mBaseTime - ONE_DAY_IN_MS));
295     }
296 
297     @Test
test_bug_7548161()298     public void test_bug_7548161() {
299         if (!LocaleUtils.isCurrentLocale(mContext, Locale.US)) {
300             return;
301         }
302 
303         long now = System.currentTimeMillis();
304         long today = now;
305         long tomorrow = now + DateUtils.DAY_IN_MILLIS;
306         long yesterday = now - DateUtils.DAY_IN_MILLIS;
307         assertEquals("Tomorrow", DateUtils.getRelativeTimeSpanString(tomorrow, now,
308                 DateUtils.DAY_IN_MILLIS, 0));
309         assertEquals("Yesterday", DateUtils.getRelativeTimeSpanString(yesterday, now,
310                 DateUtils.DAY_IN_MILLIS, 0));
311         assertEquals("Today", DateUtils.getRelativeTimeSpanString(today, now,
312                 DateUtils.DAY_IN_MILLIS, 0));
313     }
314 }
315