1 /* 2 * Copyright (C) 2007 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 com.android.calendar; 18 19 import android.test.AndroidTestCase; 20 import android.text.format.DateUtils; 21 import android.text.format.Time; 22 import android.util.Log; 23 24 import androidx.test.filters.MediumTest; 25 26 import java.util.Calendar; 27 28 /** 29 * Unit tests for {@link android.text.format.DateUtils#formatDateRange}. 30 */ 31 public class FormatDateRangeTest extends AndroidTestCase { 32 33 static private class DateTest { 34 public Time date1; 35 public Time date2; 36 public int flags; 37 public String expectedOutput; 38 DateTest(int year1, int month1, int day1, int hour1, int minute1, int year2, int month2, int day2, int hour2, int minute2, int flags, String output)39 public DateTest(int year1, int month1, int day1, int hour1, int minute1, 40 int year2, int month2, int day2, int hour2, int minute2, 41 int flags, String output) { 42 if ((flags & DateUtils.FORMAT_UTC) != 0) { 43 date1 = new Time(Time.TIMEZONE_UTC); 44 date2 = new Time(Time.TIMEZONE_UTC); 45 } else { 46 date1 = new Time(); 47 date2 = new Time(); 48 } 49 50 // If the year is zero, then set it to the current year. 51 if (year1 == 0 && year2 == 0) { 52 date1.set(System.currentTimeMillis()); 53 year1 = year2 = date1.year; 54 } 55 56 date1.set(0, minute1, hour1, day1, month1, year1); 57 date1.normalize(true /* ignore isDst */); 58 59 date2.set(0, minute2, hour2, day2, month2, year2); 60 date2.normalize(true /* ignore isDst */); 61 62 this.flags = flags; 63 expectedOutput = output; 64 } 65 66 // Single point in time. (not a range) DateTest(int year1, int month1, int day1, int hour1, int minute1, int flags, String output)67 public DateTest(int year1, int month1, int day1, int hour1, int minute1, 68 int flags, String output) { 69 this(year1, month1, day1, hour1, minute1, 70 year1, month1, day1, hour1, minute1, 71 flags, output); 72 } 73 } 74 75 DateTest[] tests = { 76 new DateTest(0, 10, 9, 8, 0, 0, 10, 9, 11, 0, 77 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_ALL, "8 \u2013 11 AM"), 78 new DateTest(0, 10, 9, 8, 0, 0, 10, 9, 11, 0, 79 DateUtils.FORMAT_SHOW_TIME, "8:00 \u2013 11:00 AM"), 80 new DateTest(0, 10, 9, 8, 0, 0, 10, 9, 17, 0, 81 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_24HOUR, "08:00 \u2013 17:00"), 82 new DateTest(0, 10, 9, 8, 0, 0, 10, 9, 12, 0, 83 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_ALL, "8 AM \u2013 12 PM"), 84 new DateTest(0, 10, 9, 8, 0, 0, 10, 9, 12, 0, 85 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_ABBREV_ALL, 86 "8 AM \u2013 12 PM"), 87 new DateTest(0, 10, 9, 8, 0, 0, 10, 9, 12, 0, 88 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_CAP_NOON | DateUtils.FORMAT_ABBREV_ALL, 89 "8 AM \u2013 12 PM"), 90 new DateTest(0, 10, 9, 10, 30, 0, 10, 9, 13, 0, 91 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_ALL, "10:30 AM \u2013 1:00 PM"), 92 new DateTest(0, 10, 9, 13, 0, 0, 10, 9, 14, 0, 93 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_ALL, "1 \u2013 2 PM"), 94 new DateTest(0, 10, 9, 0, 0, 0, 10, 9, 14, 0, 95 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_ALL, "12 AM \u2013 2 PM"), 96 new DateTest(0, 10, 9, 20, 0, 0, 10, 10, 0, 0, 97 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_ALL, "8 PM \u2013 12 AM"), 98 new DateTest(0, 10, 10, 0, 0, 99 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_ALL, "12 AM"), 100 new DateTest(0, 10, 9, 20, 0, 0, 10, 10, 0, 0, 101 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_24HOUR | DateUtils.FORMAT_ABBREV_ALL, 102 "20:00 \u2013 00:00"), 103 new DateTest(0, 10, 10, 0, 0, 104 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_24HOUR | DateUtils.FORMAT_ABBREV_ALL, 105 "00:00"), 106 new DateTest(0, 10, 9, 20, 0, 0, 10, 10, 0, 0, 107 DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL, "Nov 9"), 108 new DateTest(0, 10, 10, 0, 0, 0, 10, 10, 0, 0, 109 DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL, "Nov 10"), 110 new DateTest(0, 10, 9, 20, 0, 0, 10, 10, 0, 0, 111 DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_24HOUR | DateUtils.FORMAT_ABBREV_ALL, 112 "Nov 9"), 113 new DateTest(0, 10, 10, 0, 0, 114 DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_24HOUR | DateUtils.FORMAT_ABBREV_ALL, 115 "Nov 10"), 116 new DateTest(0, 10, 9, 20, 0, 0, 10, 10, 0, 0, 117 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_NO_MIDNIGHT | DateUtils.FORMAT_ABBREV_ALL, 118 "8 PM \u2013 12 AM"), 119 new DateTest(0, 10, 9, 20, 0, 0, 10, 10, 0, 0, 120 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_CAP_MIDNIGHT | DateUtils.FORMAT_ABBREV_ALL, 121 "8 PM \u2013 12 AM"), 122 new DateTest(0, 10, 9, 0, 0, 0, 10, 10, 0, 0, 123 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_ALL, "Nov 9, 12 AM \u2013 Nov 10, 12 AM"), 124 new DateTest(0, 10, 9, 0, 0, 0, 10, 10, 0, 0, 125 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_24HOUR | DateUtils.FORMAT_ABBREV_ALL, 126 "Nov 9, 00:00 \u2013 Nov 10, 00:00"), 127 new DateTest(0, 10, 9, 0, 0, 0, 10, 10, 0, 0, 128 DateUtils.FORMAT_UTC | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL, "Nov 9"), 129 new DateTest(0, 10, 9, 0, 0, 0, 10, 10, 0, 0, 130 DateUtils.FORMAT_UTC | DateUtils.FORMAT_ABBREV_ALL, "Nov 9"), 131 new DateTest(0, 10, 9, 0, 0, 0, 10, 10, 0, 0, 132 DateUtils.FORMAT_UTC, "November 9"), 133 new DateTest(0, 10, 8, 0, 0, 0, 10, 10, 0, 0, 134 DateUtils.FORMAT_UTC | DateUtils.FORMAT_ABBREV_ALL, "Nov 8 \u2013 9"), 135 new DateTest(0, 10, 9, 0, 0, 0, 10, 11, 0, 0, 136 DateUtils.FORMAT_UTC | DateUtils.FORMAT_ABBREV_ALL, "Nov 9 \u2013 10"), 137 new DateTest(0, 10, 9, 8, 0, 0, 10, 11, 17, 0, 138 DateUtils.FORMAT_UTC | DateUtils.FORMAT_ABBREV_ALL, "Nov 9 \u2013 11"), 139 new DateTest(0, 9, 29, 8, 0, 0, 10, 3, 17, 0, 140 DateUtils.FORMAT_UTC | DateUtils.FORMAT_ABBREV_ALL, "Oct 29 \u2013 Nov 3"), 141 new DateTest(2007, 11, 29, 8, 0, 2008, 0, 2, 17, 0, 142 DateUtils.FORMAT_UTC | DateUtils.FORMAT_ABBREV_ALL, "Dec 29, 2007 \u2013 Jan 2, 2008"), 143 new DateTest(2007, 11, 29, 0, 0, 2008, 0, 2, 0, 0, 144 DateUtils.FORMAT_UTC | DateUtils.FORMAT_ABBREV_ALL, "Dec 29, 2007 \u2013 Jan 1, 2008"), 145 new DateTest(2007, 11, 29, 8, 0, 2008, 0, 2, 17, 0, 146 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_ALL, 147 "Dec 29, 2007, 8 AM \u2013 Jan 2, 2008, 5 PM"), 148 new DateTest(0, 10, 9, 8, 0, 0, 10, 11, 17, 0, 149 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_ALL, 150 "Nov 9, 8 AM \u2013 Nov 11, 5 PM"), 151 new DateTest(2007, 10, 9, 8, 0, 2007, 10, 11, 17, 0, 152 DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_ABBREV_ALL, 153 "Fri, Nov 9 \u2013 Sun, Nov 11, 2007"), 154 new DateTest(2007, 10, 9, 8, 0, 2007, 10, 11, 17, 0, 155 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_ABBREV_ALL, 156 "Fri, Nov 9, 2007, 8 AM \u2013 Sun, Nov 11, 2007, 5 PM"), 157 new DateTest(2007, 11, 3, 13, 0, 2007, 11, 3, 14, 0, 158 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR, 159 "December 3, 2007, 1:00 \u2013 2:00 PM"), 160 // Tests that FORMAT_SHOW_YEAR takes precedence over FORMAT_NO_YEAR: 161 new DateTest(2007, 11, 3, 13, 0, 2007, 11, 3, 13, 0, 162 DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_NO_YEAR, 163 "December 3, 2007"), 164 // Tests that year isn't shown by default with no year flags when time is the current year: 165 new DateTest( 166 Calendar.getInstance().get(Calendar.YEAR), 0, 3, 13, 0, 167 DateUtils.FORMAT_SHOW_DATE, 168 "January 3"), 169 // Tests that the year is shown by default with no year flags when time isn't the current year: 170 new DateTest( 171 Calendar.getInstance().get(Calendar.YEAR) - 1, 0, 3, 13, 0, 172 DateUtils.FORMAT_SHOW_DATE, 173 "January 3, " + (Calendar.getInstance().get(Calendar.YEAR) - 1)), 174 }; 175 176 @Override setUp()177 protected void setUp() throws Exception { 178 super.setUp(); 179 } 180 181 @MediumTest testAll()182 public void testAll() throws Exception { 183 int len = tests.length; 184 for (int index = 0; index < len; index++) { 185 DateTest dateTest = tests[index]; 186 long startMillis = dateTest.date1.toMillis(false /* use isDst */); 187 long endMillis = dateTest.date2.toMillis(false /* use isDst */); 188 int flags = dateTest.flags; 189 String output = DateUtils.formatDateRange(mContext, startMillis, endMillis, flags); 190 if (!dateTest.expectedOutput.equals(output)) { 191 Log.i("FormatDateRangeTest", "index " + index 192 + " expected: " + dateTest.expectedOutput 193 + " actual: " + output); 194 } 195 assertEquals(dateTest.expectedOutput, output); 196 } 197 } 198 } 199