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