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 android.content.Context;
20 import android.test.AndroidTestCase;
21 import android.text.format.DateUtils;
22 import dalvik.annotation.KnownFailure;
23 import java.util.Calendar;
24 import java.util.Date;
25 import java.util.Formatter;
26 import java.util.Locale;
27 import java.util.TimeZone;
28 
29 public class DateUtilsTest extends AndroidTestCase {
30 
31     private long mBaseTime;
32     private Context mContext;
33 
34     @Override
setUp()35     protected void setUp() throws Exception {
36         super.setUp();
37         mContext = getContext();
38         TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
39         mBaseTime = System.currentTimeMillis();
40     }
41 
testGetDayOfWeekString()42     public void testGetDayOfWeekString() {
43         if (!LocaleUtils.isCurrentLocale(mContext, Locale.US)) {
44             return;
45         }
46 
47         assertEquals("Sunday",
48                 DateUtils.getDayOfWeekString(Calendar.SUNDAY, DateUtils.LENGTH_LONG));
49         assertEquals("Sun",
50                 DateUtils.getDayOfWeekString(Calendar.SUNDAY, DateUtils.LENGTH_MEDIUM));
51         assertEquals("Sun",
52                 DateUtils.getDayOfWeekString(Calendar.SUNDAY, DateUtils.LENGTH_SHORT));
53         assertEquals("Sun",
54                 DateUtils.getDayOfWeekString(Calendar.SUNDAY, DateUtils.LENGTH_SHORTER));
55         assertEquals("S",
56                 DateUtils.getDayOfWeekString(Calendar.SUNDAY, DateUtils.LENGTH_SHORTEST));
57         // Other abbrev
58         assertEquals("Sun",
59                 DateUtils.getDayOfWeekString(Calendar.SUNDAY, 60));
60     }
61 
testGetMonthString()62     public void testGetMonthString() {
63         if (!LocaleUtils.isCurrentLocale(mContext, Locale.US)) {
64             return;
65         }
66         assertEquals("January", DateUtils.getMonthString(Calendar.JANUARY, DateUtils.LENGTH_LONG));
67         assertEquals("Jan",
68                 DateUtils.getMonthString(Calendar.JANUARY, DateUtils.LENGTH_MEDIUM));
69         assertEquals("Jan", DateUtils.getMonthString(Calendar.JANUARY, DateUtils.LENGTH_SHORT));
70         assertEquals("Jan",
71                 DateUtils.getMonthString(Calendar.JANUARY, DateUtils.LENGTH_SHORTER));
72         assertEquals("J",
73                 DateUtils.getMonthString(Calendar.JANUARY, DateUtils.LENGTH_SHORTEST));
74         // Other abbrev
75         assertEquals("Jan", DateUtils.getMonthString(Calendar.JANUARY, 60));
76     }
77 
testGetAMPMString()78     public void testGetAMPMString() {
79         if (!LocaleUtils.isCurrentLocale(mContext, Locale.US)) {
80             return;
81         }
82         assertEquals("AM", DateUtils.getAMPMString(Calendar.AM));
83         assertEquals("PM", DateUtils.getAMPMString(Calendar.PM));
84     }
85 
86     // This is to test the mapping between DateUtils' public API and
87     // libcore/icu4c's implementation. More tests, in different locales, are
88     // in libcore's CTS tests.
test_getRelativeTimeSpanString()89     public void test_getRelativeTimeSpanString() {
90         if (!LocaleUtils.isCurrentLocale(mContext, Locale.US)) {
91             return;
92         }
93 
94         final long ONE_SECOND_IN_MS = 1000;
95         assertEquals("0 minutes ago",
96                 DateUtils.getRelativeTimeSpanString(mBaseTime - ONE_SECOND_IN_MS));
97         assertEquals("in 0 minutes",
98                 DateUtils.getRelativeTimeSpanString(mBaseTime + ONE_SECOND_IN_MS));
99 
100         final long ONE_MINUTE_IN_MS = 60 * ONE_SECOND_IN_MS;
101         assertEquals("1 minute ago", DateUtils.getRelativeTimeSpanString(0, ONE_MINUTE_IN_MS,
102                 DateUtils.MINUTE_IN_MILLIS));
103         assertEquals("in 1 minute", DateUtils.getRelativeTimeSpanString(ONE_MINUTE_IN_MS, 0,
104                 DateUtils.MINUTE_IN_MILLIS));
105 
106         final long ONE_HOUR_IN_MS = 60 * 60 * 1000;
107         final long TWO_HOURS_IN_MS = 2 * ONE_HOUR_IN_MS;
108         assertEquals("2 hours ago", DateUtils.getRelativeTimeSpanString(mBaseTime - TWO_HOURS_IN_MS,
109                 mBaseTime, DateUtils.MINUTE_IN_MILLIS, DateUtils.FORMAT_NUMERIC_DATE));
110         assertEquals("in 2 hours", DateUtils.getRelativeTimeSpanString(mBaseTime + TWO_HOURS_IN_MS,
111                 mBaseTime, DateUtils.MINUTE_IN_MILLIS, DateUtils.FORMAT_NUMERIC_DATE));
112     }
113 
114     // Similar to test_getRelativeTimeSpanString(). The function here is to
115     // test the mapping between DateUtils's public API and libcore/icu4c's
116     // implementation. More tests, in different locales, are in libcore's
117     // CTS tests.
test_getRelativeDateTimeString()118     public void test_getRelativeDateTimeString() {
119         final long DAY_DURATION = 5 * 24 * 60 * 60 * 1000;
120         assertNotNull(DateUtils.getRelativeDateTimeString(mContext, mBaseTime - DAY_DURATION,
121                 DateUtils.MINUTE_IN_MILLIS, DateUtils.DAY_IN_MILLIS, DateUtils.FORMAT_NUMERIC_DATE));
122     }
123 
test_formatElapsedTime()124     public void test_formatElapsedTime() {
125         if (!LocaleUtils.isCurrentLocale(mContext, Locale.US)) {
126             return;
127         }
128 
129         long MINUTES = 60;
130         long HOURS = 60 * MINUTES;
131         test_formatElapsedTime("02:01", 2 * MINUTES + 1);
132         test_formatElapsedTime("3:02:01", 3 * HOURS + 2 * MINUTES + 1);
133         // http://code.google.com/p/android/issues/detail?id=41401
134         test_formatElapsedTime("123:02:01", 123 * HOURS + 2 * MINUTES + 1);
135     }
136 
test_formatElapsedTime(String expected, long elapsedTime)137     private void test_formatElapsedTime(String expected, long elapsedTime) {
138         assertEquals(expected, DateUtils.formatElapsedTime(elapsedTime));
139         StringBuilder sb = new StringBuilder();
140         assertEquals(expected, DateUtils.formatElapsedTime(sb, elapsedTime));
141         assertEquals(expected, sb.toString());
142     }
143 
testFormatSameDayTime()144     public void testFormatSameDayTime() {
145         if (!LocaleUtils.isCurrentLocale(mContext, Locale.US)) {
146             return;
147         }
148 
149         Date date = new Date(109, 0, 19, 3, 30, 15);
150         long fixedTime = date.getTime();
151 
152         int currentYear = Calendar.getInstance().get(Calendar.YEAR);
153         Date dateWithCurrentYear = new Date(currentYear - 1900, 0, 19, 3, 30, 15);
154         long timeWithCurrentYear = dateWithCurrentYear.getTime();
155 
156         final long DAY_DURATION = 5 * 24 * 60 * 60 * 1000;
157         assertEquals("Saturday, January 24, 2009", DateUtils.formatSameDayTime(
158                 fixedTime + DAY_DURATION, fixedTime, java.text.DateFormat.FULL,
159                 java.text.DateFormat.FULL));
160         assertEquals("Jan 24, 2009", DateUtils.formatSameDayTime(fixedTime + DAY_DURATION,
161                 fixedTime, java.text.DateFormat.DEFAULT, java.text.DateFormat.FULL));
162         assertEquals("January 24, 2009", DateUtils.formatSameDayTime(fixedTime + DAY_DURATION,
163                 fixedTime, java.text.DateFormat.LONG, java.text.DateFormat.FULL));
164         assertEquals("Jan 24, 2009", DateUtils.formatSameDayTime(fixedTime + DAY_DURATION,
165                 fixedTime, java.text.DateFormat.MEDIUM, java.text.DateFormat.FULL));
166         assertEquals("1/24/09", DateUtils.formatSameDayTime(fixedTime + DAY_DURATION,
167                 fixedTime, java.text.DateFormat.SHORT, java.text.DateFormat.FULL));
168 
169         final long HOUR_DURATION = 2 * 60 * 60 * 1000;
170         assertEquals("5:30:15 AM GMT+00:00", DateUtils.formatSameDayTime(fixedTime + HOUR_DURATION,
171                 fixedTime, java.text.DateFormat.FULL, java.text.DateFormat.FULL));
172         assertEquals("5:30:15 AM", DateUtils.formatSameDayTime(fixedTime + HOUR_DURATION,
173                 fixedTime, java.text.DateFormat.FULL, java.text.DateFormat.DEFAULT));
174         assertEquals("5:30:15 AM GMT+00:00", DateUtils.formatSameDayTime(fixedTime + HOUR_DURATION,
175                 fixedTime, java.text.DateFormat.FULL, java.text.DateFormat.LONG));
176         assertEquals("5:30:15 AM", DateUtils.formatSameDayTime(fixedTime + HOUR_DURATION,
177                 fixedTime, java.text.DateFormat.FULL, java.text.DateFormat.MEDIUM));
178         assertEquals("5:30 AM", DateUtils.formatSameDayTime(fixedTime + HOUR_DURATION,
179                 fixedTime, java.text.DateFormat.FULL, java.text.DateFormat.SHORT));
180     }
181 
182     // This is just to exercise the wrapper that calls the libcore/icu4c implementation.
183     // Full testing, in multiple locales, is in libcore's CTS tests.
testFormatDateRange()184     public void testFormatDateRange() {
185         if (!LocaleUtils.isCurrentLocale(mContext, Locale.US)) {
186             return;
187         }
188 
189         Date date = new Date(109, 0, 19, 3, 30, 15);
190         long fixedTime = date.getTime();
191         final long HOUR_DURATION = 2 * 60 * 60 * 1000;
192         assertEquals("Monday", DateUtils.formatDateRange(mContext, fixedTime,
193                 fixedTime + HOUR_DURATION, DateUtils.FORMAT_SHOW_WEEKDAY));
194     }
195 
testIsToday()196     public void testIsToday() {
197         final long ONE_DAY_IN_MS = 24 * 60 * 60 * 1000;
198         assertTrue(DateUtils.isToday(mBaseTime));
199         assertFalse(DateUtils.isToday(mBaseTime - ONE_DAY_IN_MS));
200     }
201 
test_bug_7548161()202     public void test_bug_7548161() {
203         if (!LocaleUtils.isCurrentLocale(mContext, Locale.US)) {
204             return;
205         }
206 
207         long now = System.currentTimeMillis();
208         long today = now;
209         long tomorrow = now + DateUtils.DAY_IN_MILLIS;
210         long yesterday = now - DateUtils.DAY_IN_MILLIS;
211         assertEquals("Tomorrow", DateUtils.getRelativeTimeSpanString(tomorrow, now,
212                 DateUtils.DAY_IN_MILLIS, 0));
213         assertEquals("Yesterday", DateUtils.getRelativeTimeSpanString(yesterday, now,
214                 DateUtils.DAY_IN_MILLIS, 0));
215         assertEquals("Today", DateUtils.getRelativeTimeSpanString(today, now,
216                 DateUtils.DAY_IN_MILLIS, 0));
217     }
218 }
219