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