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.text.format.DateUtils; 27 28 import androidx.test.InstrumentationRegistry; 29 import androidx.test.filters.SmallTest; 30 import androidx.test.runner.AndroidJUnit4; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 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 // This is just to exercise the wrapper that calls the libcore/icu4c implementation. 192 // Full testing, in multiple locales, is in libcore's CTS tests. 193 @Test testFormatDateRange()194 public void testFormatDateRange() { 195 if (!LocaleUtils.isCurrentLocale(mContext, Locale.US)) { 196 return; 197 } 198 199 final Date date = new Date(109, 0, 19, 3, 30, 15); 200 final long fixedTime = date.getTime(); 201 final long hourDuration = 2 * 60 * 60 * 1000; 202 assertEquals("Monday", DateUtils.formatDateRange(mContext, fixedTime, 203 fixedTime + hourDuration, DateUtils.FORMAT_SHOW_WEEKDAY)); 204 } 205 206 @Test testFormatDateRange_withFormatter()207 public void testFormatDateRange_withFormatter() { 208 if (!LocaleUtils.isCurrentLocale(mContext, Locale.US)) { 209 return; 210 } 211 212 final Date date = new Date(109, 0, 19, 3, 30, 15); 213 final long fixedTime = date.getTime(); 214 final long hourDuration = 2 * 60 * 60 * 1000; 215 final Formatter formatter = new Formatter(); 216 final Formatter result = DateUtils.formatDateRange(mContext, formatter, fixedTime, 217 fixedTime + hourDuration, DateUtils.FORMAT_SHOW_WEEKDAY); 218 assertEquals("Monday", result.toString()); 219 assertSame(result, formatter); 220 } 221 222 @Test testFormatDateRange_withFormatterAndTimezone()223 public void testFormatDateRange_withFormatterAndTimezone() { 224 if (!LocaleUtils.isCurrentLocale(mContext, Locale.US)) { 225 return; 226 } 227 228 final Date date = new Date(109, 0, 19, 3, 30, 15); 229 final long fixedTime = date.getTime(); 230 final long hourDuration = 2 * 60 * 60 * 1000; 231 final Formatter formatter = new Formatter(); 232 final Formatter result = DateUtils.formatDateRange(mContext, formatter, fixedTime, 233 fixedTime + hourDuration, DateUtils.FORMAT_SHOW_WEEKDAY, null /* local */); 234 assertEquals("Monday", result.toString()); 235 assertSame(result, formatter); 236 } 237 238 @Test testFormatDateTime()239 public void testFormatDateTime() { 240 if (!LocaleUtils.isCurrentLocale(mContext, Locale.US)) { 241 return; 242 } 243 244 final Date date = new Date(109, 0, 19, 3, 30, 15); 245 final long fixedTime = date.getTime(); 246 assertEquals("Monday", DateUtils.formatDateTime(mContext, fixedTime, 247 DateUtils.FORMAT_SHOW_WEEKDAY)); 248 } 249 250 @Test testIsToday()251 public void testIsToday() { 252 final long ONE_DAY_IN_MS = 24 * 60 * 60 * 1000; 253 assertTrue(DateUtils.isToday(mBaseTime)); 254 assertFalse(DateUtils.isToday(mBaseTime - ONE_DAY_IN_MS)); 255 } 256 257 @Test test_bug_7548161()258 public void test_bug_7548161() { 259 if (!LocaleUtils.isCurrentLocale(mContext, Locale.US)) { 260 return; 261 } 262 263 long now = System.currentTimeMillis(); 264 long today = now; 265 long tomorrow = now + DateUtils.DAY_IN_MILLIS; 266 long yesterday = now - DateUtils.DAY_IN_MILLIS; 267 assertEquals("Tomorrow", DateUtils.getRelativeTimeSpanString(tomorrow, now, 268 DateUtils.DAY_IN_MILLIS, 0)); 269 assertEquals("Yesterday", DateUtils.getRelativeTimeSpanString(yesterday, now, 270 DateUtils.DAY_IN_MILLIS, 0)); 271 assertEquals("Today", DateUtils.getRelativeTimeSpanString(today, now, 272 DateUtils.DAY_IN_MILLIS, 0)); 273 } 274 } 275