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