FormatDateRangeTest.java revision b67e98de50a61dcf9bec814cabeb97ff12057d13
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 17package com.android.calendar; 18 19import android.content.res.Resources; 20import android.text.format.DateUtils; 21import android.text.format.Time; 22import android.test.AndroidTestCase; 23import android.test.suitebuilder.annotation.MediumTest; 24import android.util.Log; 25 26import java.util.Calendar; 27 28/** 29 * Unit tests for {@link android.text.format.DateUtils#formatDateRange}. 30 */ 31public 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 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) 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 private Resources mResources; 76 77 DateTest[] tests = { 78 new DateTest(0, 10, 9, 8, 0, 0, 10, 9, 11, 0, 79 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_ALL, "8am \u2013 11am"), 80 new DateTest(0, 10, 9, 8, 0, 0, 10, 9, 11, 0, 81 DateUtils.FORMAT_SHOW_TIME, "8:00am \u2013 11:00am"), 82 new DateTest(0, 10, 9, 8, 0, 0, 10, 9, 17, 0, 83 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_24HOUR, "08:00 \u2013 17:00"), 84 new DateTest(0, 10, 9, 8, 0, 0, 10, 9, 12, 0, 85 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_ALL, "8am \u2013 noon"), 86 new DateTest(0, 10, 9, 8, 0, 0, 10, 9, 12, 0, 87 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_NO_NOON | DateUtils.FORMAT_ABBREV_ALL, 88 "8am \u2013 12pm"), 89 new DateTest(0, 10, 9, 8, 0, 0, 10, 9, 12, 0, 90 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_CAP_NOON | DateUtils.FORMAT_ABBREV_ALL, 91 "8am \u2013 Noon"), 92 new DateTest(0, 10, 9, 10, 30, 0, 10, 9, 13, 0, 93 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_ALL, "10:30am \u2013 1pm"), 94 new DateTest(0, 10, 9, 13, 0, 0, 10, 9, 14, 0, 95 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_ALL, "1pm \u2013 2pm"), 96 new DateTest(0, 10, 9, 0, 0, 0, 10, 9, 14, 0, 97 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_ALL, "12am \u2013 2pm"), 98 new DateTest(0, 10, 9, 20, 0, 0, 10, 10, 0, 0, 99 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_ALL, "8pm \u2013 midnight"), 100 new DateTest(0, 10, 10, 0, 0, 101 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_ALL, "12am"), 102 new DateTest(0, 10, 9, 20, 0, 0, 10, 10, 0, 0, 103 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_24HOUR | DateUtils.FORMAT_ABBREV_ALL, 104 "20:00 \u2013 00:00"), 105 new DateTest(0, 10, 10, 0, 0, 106 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_24HOUR | DateUtils.FORMAT_ABBREV_ALL, 107 "00:00"), 108 new DateTest(0, 10, 9, 20, 0, 0, 10, 10, 0, 0, 109 DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL, "Nov 9"), 110 new DateTest(0, 10, 10, 0, 0, 0, 10, 10, 0, 0, 111 DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL, "Nov 10"), 112 new DateTest(0, 10, 9, 20, 0, 0, 10, 10, 0, 0, 113 DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_24HOUR | DateUtils.FORMAT_ABBREV_ALL, 114 "Nov 9"), 115 new DateTest(0, 10, 10, 0, 0, 116 DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_24HOUR | DateUtils.FORMAT_ABBREV_ALL, 117 "Nov 10"), 118 new DateTest(0, 10, 9, 20, 0, 0, 10, 10, 0, 0, 119 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_NO_MIDNIGHT | DateUtils.FORMAT_ABBREV_ALL, 120 "8pm \u2013 12am"), 121 new DateTest(0, 10, 9, 20, 0, 0, 10, 10, 0, 0, 122 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_CAP_MIDNIGHT | DateUtils.FORMAT_ABBREV_ALL, 123 "8pm \u2013 Midnight"), 124 new DateTest(0, 10, 9, 0, 0, 0, 10, 10, 0, 0, 125 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_ALL, "12am \u2013 midnight"), 126 new DateTest(0, 10, 9, 0, 0, 0, 10, 10, 0, 0, 127 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_24HOUR | DateUtils.FORMAT_ABBREV_ALL, 128 "00:00 \u2013 00:00"), 129 new DateTest(0, 10, 9, 0, 0, 0, 10, 10, 0, 0, 130 DateUtils.FORMAT_UTC | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL, "Nov 9"), 131 new DateTest(0, 10, 9, 0, 0, 0, 10, 10, 0, 0, 132 DateUtils.FORMAT_UTC | DateUtils.FORMAT_ABBREV_ALL, "Nov 9"), 133 new DateTest(0, 10, 9, 0, 0, 0, 10, 10, 0, 0, 134 DateUtils.FORMAT_UTC, "November 9"), 135 new DateTest(0, 10, 8, 0, 0, 0, 10, 10, 0, 0, 136 DateUtils.FORMAT_UTC | DateUtils.FORMAT_ABBREV_ALL, "Nov 8 \u2013 9"), 137 new DateTest(0, 10, 9, 0, 0, 0, 10, 11, 0, 0, 138 DateUtils.FORMAT_UTC | DateUtils.FORMAT_ABBREV_ALL, "Nov 9 \u2013 10"), 139 new DateTest(0, 10, 9, 8, 0, 0, 10, 11, 17, 0, 140 DateUtils.FORMAT_UTC | DateUtils.FORMAT_ABBREV_ALL, "Nov 9 \u2013 11"), 141 new DateTest(0, 9, 29, 8, 0, 0, 10, 3, 17, 0, 142 DateUtils.FORMAT_UTC | DateUtils.FORMAT_ABBREV_ALL, "Oct 29 \u2013 Nov 3"), 143 new DateTest(2007, 11, 29, 8, 0, 2008, 0, 2, 17, 0, 144 DateUtils.FORMAT_UTC | DateUtils.FORMAT_ABBREV_ALL, "Dec 29, 2007 \u2013 Jan 2, 2008"), 145 new DateTest(2007, 11, 29, 0, 0, 2008, 0, 2, 0, 0, 146 DateUtils.FORMAT_UTC | DateUtils.FORMAT_ABBREV_ALL, "Dec 29, 2007 \u2013 Jan 1, 2008"), 147 new DateTest(2007, 11, 29, 8, 0, 2008, 0, 2, 17, 0, 148 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_ALL, 149 "Dec 29, 2007, 8am \u2013 Jan 2, 2008, 5pm"), 150 new DateTest(0, 10, 9, 8, 0, 0, 10, 11, 17, 0, 151 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_ABBREV_ALL, 152 "Nov 9, 8am \u2013 Nov 11, 5pm"), 153 new DateTest(2007, 10, 9, 8, 0, 2007, 10, 11, 17, 0, 154 DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_ABBREV_ALL, 155 "Fri, Nov 9, 2007 \u2013 Sun, Nov 11, 2007"), 156 new DateTest(2007, 10, 9, 8, 0, 2007, 10, 11, 17, 0, 157 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_ABBREV_ALL, 158 "Fri, Nov 9, 2007, 8am \u2013 Sun, Nov 11, 2007, 5pm"), 159 new DateTest(2007, 11, 3, 13, 0, 2007, 11, 3, 14, 0, 160 DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR, 161 "1:00pm \u2013 2:00pm, December 3, 2007"), 162 // Tests that FORMAT_SHOW_YEAR takes precedence over FORMAT_NO_YEAR: 163 new DateTest(2007, 11, 3, 13, 0, 2007, 11, 3, 13, 0, 164 DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_NO_YEAR, 165 "December 3, 2007"), 166 // Tests that year isn't shown by default with no year flags when time is the current year: 167 new DateTest( 168 Calendar.getInstance().get(Calendar.YEAR), 0, 3, 13, 0, 169 DateUtils.FORMAT_SHOW_DATE, 170 "January 3"), 171 // Tests that the year is shown by default with no year flags when time isn't the current year: 172 new DateTest( 173 Calendar.getInstance().get(Calendar.YEAR) - 1, 0, 3, 13, 0, 174 DateUtils.FORMAT_SHOW_DATE, 175 "January 3, " + (Calendar.getInstance().get(Calendar.YEAR) - 1)), 176 }; 177 178 @Override 179 protected void setUp() throws Exception { 180 super.setUp(); 181 182 mResources = mContext.getResources(); 183 } 184 185 @MediumTest 186 public void testAll() throws Exception { 187 int len = tests.length; 188 for (int index = 0; index < len; index++) { 189 DateTest dateTest = tests[index]; 190 long startMillis = dateTest.date1.toMillis(false /* use isDst */); 191 long endMillis = dateTest.date2.toMillis(false /* use isDst */); 192 int flags = dateTest.flags; 193 String output = DateUtils.formatDateRange(mContext, startMillis, endMillis, flags); 194 if (!dateTest.expectedOutput.equals(output)) { 195 Log.i("FormatDateRangeTest", "index " + index 196 + " expected: " + dateTest.expectedOutput 197 + " actual: " + output); 198 } 199 assertEquals(dateTest.expectedOutput, output); 200 } 201 } 202} 203