From 504844526f1b7afec048c6d2976ffb332670d5ba Mon Sep 17 00:00:00 2001 From: Michael Chan Date: Tue, 26 Jun 2012 17:49:56 -0700 Subject: Rename calendarcommon to calendarcommon2 to workaround OEM silliness Change-Id: I5bd618866d6484037671376e192ee3217c42c366 --- README | 2 +- src/com/android/calendarcommon/DateException.java | 25 - src/com/android/calendarcommon/Duration.java | 150 -- .../android/calendarcommon/EventRecurrence.java | 907 ------- src/com/android/calendarcommon/ICalendar.java | 660 ----- .../calendarcommon/RecurrenceProcessor.java | 1314 ---------- src/com/android/calendarcommon/RecurrenceSet.java | 526 ---- src/com/android/calendarcommon2/DateException.java | 25 + src/com/android/calendarcommon2/Duration.java | 150 ++ .../android/calendarcommon2/EventRecurrence.java | 907 +++++++ src/com/android/calendarcommon2/ICalendar.java | 660 +++++ .../calendarcommon2/RecurrenceProcessor.java | 1314 ++++++++++ src/com/android/calendarcommon2/RecurrenceSet.java | 526 ++++ tests/AndroidManifest.xml | 4 +- .../com/android/calendarcommon/DurationTest.java | 77 - .../calendarcommon/EventRecurrenceTest.java | 889 ------- .../src/com/android/calendarcommon/RRuleTest.java | 756 ------ .../calendarcommon/RecurrenceProcessorTest.java | 2537 -------------------- .../android/calendarcommon/RecurrenceSetTest.java | 155 -- .../com/android/calendarcommon2/DurationTest.java | 77 + .../calendarcommon2/EventRecurrenceTest.java | 889 +++++++ .../src/com/android/calendarcommon2/RRuleTest.java | 756 ++++++ .../calendarcommon2/RecurrenceProcessorTest.java | 2537 ++++++++++++++++++++ .../android/calendarcommon2/RecurrenceSetTest.java | 155 ++ 24 files changed, 7999 insertions(+), 7999 deletions(-) delete mode 100644 src/com/android/calendarcommon/DateException.java delete mode 100644 src/com/android/calendarcommon/Duration.java delete mode 100644 src/com/android/calendarcommon/EventRecurrence.java delete mode 100644 src/com/android/calendarcommon/ICalendar.java delete mode 100644 src/com/android/calendarcommon/RecurrenceProcessor.java delete mode 100644 src/com/android/calendarcommon/RecurrenceSet.java create mode 100644 src/com/android/calendarcommon2/DateException.java create mode 100644 src/com/android/calendarcommon2/Duration.java create mode 100644 src/com/android/calendarcommon2/EventRecurrence.java create mode 100644 src/com/android/calendarcommon2/ICalendar.java create mode 100644 src/com/android/calendarcommon2/RecurrenceProcessor.java create mode 100644 src/com/android/calendarcommon2/RecurrenceSet.java delete mode 100644 tests/src/com/android/calendarcommon/DurationTest.java delete mode 100644 tests/src/com/android/calendarcommon/EventRecurrenceTest.java delete mode 100644 tests/src/com/android/calendarcommon/RRuleTest.java delete mode 100644 tests/src/com/android/calendarcommon/RecurrenceProcessorTest.java delete mode 100644 tests/src/com/android/calendarcommon/RecurrenceSetTest.java create mode 100644 tests/src/com/android/calendarcommon2/DurationTest.java create mode 100644 tests/src/com/android/calendarcommon2/EventRecurrenceTest.java create mode 100644 tests/src/com/android/calendarcommon2/RRuleTest.java create mode 100644 tests/src/com/android/calendarcommon2/RecurrenceProcessorTest.java create mode 100644 tests/src/com/android/calendarcommon2/RecurrenceSetTest.java diff --git a/README b/README index bfb9a28..62fbf41 100644 --- a/README +++ b/README @@ -2,4 +2,4 @@ To build and run tests: mmm -j20 frameworks/opt/calendar adb install -r $OUT/data/app/CalendarCommonTests.apk -adb shell am instrument -w com.android.calendarcommon.tests/android.test.InstrumentationTestRunner +adb shell am instrument -w com.android.calendarcommon2.tests/android.test.InstrumentationTestRunner diff --git a/src/com/android/calendarcommon/DateException.java b/src/com/android/calendarcommon/DateException.java deleted file mode 100644 index c917885..0000000 --- a/src/com/android/calendarcommon/DateException.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (C) 2006 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.calendarcommon; - -public class DateException extends Exception -{ - public DateException(String message) - { - super(message); - } -} diff --git a/src/com/android/calendarcommon/Duration.java b/src/com/android/calendarcommon/Duration.java deleted file mode 100644 index 70fe89d..0000000 --- a/src/com/android/calendarcommon/Duration.java +++ /dev/null @@ -1,150 +0,0 @@ -/* -** Copyright 2006, The Android Open Source Project -** -** Licensed under the Apache License, Version 2.0 (the "License"); -** you may not use this file except in compliance with the License. -** You may obtain a copy of the License at -** -** http://www.apache.org/licenses/LICENSE-2.0 -** -** Unless required by applicable law or agreed to in writing, software -** distributed under the License is distributed on an "AS IS" BASIS, -** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -** See the License for the specific language governing permissions and -** limitations under the License. -*/ - -package com.android.calendarcommon; - -import java.util.Calendar; - -/** - * According to RFC2445, durations are like this: - * WEEKS - * | DAYS [ HOURS [ MINUTES [ SECONDS ] ] ] - * | HOURS [ MINUTES [ SECONDS ] ] - * it doesn't specifically, say, but this sort of implies that you can't have - * 70 seconds. - */ -public class Duration -{ - public int sign; // 1 or -1 - public int weeks; - public int days; - public int hours; - public int minutes; - public int seconds; - - public Duration() - { - sign = 1; - } - - /** - * Parse according to RFC2445 ss4.3.6. (It's actually a little loose with - * its parsing, for better or for worse) - */ - public void parse(String str) throws DateException - { - sign = 1; - weeks = 0; - days = 0; - hours = 0; - minutes = 0; - seconds = 0; - - int len = str.length(); - int index = 0; - char c; - - if (len < 1) { - return ; - } - - c = str.charAt(0); - if (c == '-') { - sign = -1; - index++; - } - else if (c == '+') { - index++; - } - - if (len < index) { - return ; - } - - c = str.charAt(index); - if (c != 'P') { - throw new DateException ( - "Duration.parse(str='" + str + "') expected 'P' at index=" - + index); - } - index++; - c = str.charAt(index); - if (c == 'T') { - index++; - } - - int n = 0; - for (; index < len; index++) { - c = str.charAt(index); - if (c >= '0' && c <= '9') { - n *= 10; - n += ((int)(c-'0')); - } - else if (c == 'W') { - weeks = n; - n = 0; - } - else if (c == 'H') { - hours = n; - n = 0; - } - else if (c == 'M') { - minutes = n; - n = 0; - } - else if (c == 'S') { - seconds = n; - n = 0; - } - else if (c == 'D') { - days = n; - n = 0; - } - else if (c == 'T') { - } - else { - throw new DateException ( - "Duration.parse(str='" + str + "') unexpected char '" - + c + "' at index=" + index); - } - } - } - - /** - * Add this to the calendar provided, in place, in the calendar. - */ - public void addTo(Calendar cal) - { - cal.add(Calendar.DAY_OF_MONTH, sign*weeks*7); - cal.add(Calendar.DAY_OF_MONTH, sign*days); - cal.add(Calendar.HOUR, sign*hours); - cal.add(Calendar.MINUTE, sign*minutes); - cal.add(Calendar.SECOND, sign*seconds); - } - - public long addTo(long dt) { - return dt + getMillis(); - } - - public long getMillis() { - long factor = 1000 * sign; - return factor * ((7*24*60*60*weeks) - + (24*60*60*days) - + (60*60*hours) - + (60*minutes) - + seconds); - } -} diff --git a/src/com/android/calendarcommon/EventRecurrence.java b/src/com/android/calendarcommon/EventRecurrence.java deleted file mode 100644 index ac03e45..0000000 --- a/src/com/android/calendarcommon/EventRecurrence.java +++ /dev/null @@ -1,907 +0,0 @@ -/* - * Copyright (C) 2006 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.calendarcommon; - -import android.text.TextUtils; -import android.text.format.Time; -import android.util.Log; -import android.util.TimeFormatException; - -import java.util.Calendar; -import java.util.HashMap; - -/** - * Event recurrence utility functions. - */ -public class EventRecurrence { - private static String TAG = "EventRecur"; - - public static final int SECONDLY = 1; - public static final int MINUTELY = 2; - public static final int HOURLY = 3; - public static final int DAILY = 4; - public static final int WEEKLY = 5; - public static final int MONTHLY = 6; - public static final int YEARLY = 7; - - public static final int SU = 0x00010000; - public static final int MO = 0x00020000; - public static final int TU = 0x00040000; - public static final int WE = 0x00080000; - public static final int TH = 0x00100000; - public static final int FR = 0x00200000; - public static final int SA = 0x00400000; - - public Time startDate; // set by setStartDate(), not parse() - - public int freq; // SECONDLY, MINUTELY, etc. - public String until; - public int count; - public int interval; - public int wkst; // SU, MO, TU, etc. - - /* lists with zero entries may be null references */ - public int[] bysecond; - public int bysecondCount; - public int[] byminute; - public int byminuteCount; - public int[] byhour; - public int byhourCount; - public int[] byday; - public int[] bydayNum; - public int bydayCount; - public int[] bymonthday; - public int bymonthdayCount; - public int[] byyearday; - public int byyeardayCount; - public int[] byweekno; - public int byweeknoCount; - public int[] bymonth; - public int bymonthCount; - public int[] bysetpos; - public int bysetposCount; - - /** maps a part string to a parser object */ - private static HashMap sParsePartMap; - static { - sParsePartMap = new HashMap(); - sParsePartMap.put("FREQ", new ParseFreq()); - sParsePartMap.put("UNTIL", new ParseUntil()); - sParsePartMap.put("COUNT", new ParseCount()); - sParsePartMap.put("INTERVAL", new ParseInterval()); - sParsePartMap.put("BYSECOND", new ParseBySecond()); - sParsePartMap.put("BYMINUTE", new ParseByMinute()); - sParsePartMap.put("BYHOUR", new ParseByHour()); - sParsePartMap.put("BYDAY", new ParseByDay()); - sParsePartMap.put("BYMONTHDAY", new ParseByMonthDay()); - sParsePartMap.put("BYYEARDAY", new ParseByYearDay()); - sParsePartMap.put("BYWEEKNO", new ParseByWeekNo()); - sParsePartMap.put("BYMONTH", new ParseByMonth()); - sParsePartMap.put("BYSETPOS", new ParseBySetPos()); - sParsePartMap.put("WKST", new ParseWkst()); - } - - /* values for bit vector that keeps track of what we have already seen */ - private static final int PARSED_FREQ = 1 << 0; - private static final int PARSED_UNTIL = 1 << 1; - private static final int PARSED_COUNT = 1 << 2; - private static final int PARSED_INTERVAL = 1 << 3; - private static final int PARSED_BYSECOND = 1 << 4; - private static final int PARSED_BYMINUTE = 1 << 5; - private static final int PARSED_BYHOUR = 1 << 6; - private static final int PARSED_BYDAY = 1 << 7; - private static final int PARSED_BYMONTHDAY = 1 << 8; - private static final int PARSED_BYYEARDAY = 1 << 9; - private static final int PARSED_BYWEEKNO = 1 << 10; - private static final int PARSED_BYMONTH = 1 << 11; - private static final int PARSED_BYSETPOS = 1 << 12; - private static final int PARSED_WKST = 1 << 13; - - /** maps a FREQ value to an integer constant */ - private static final HashMap sParseFreqMap = new HashMap(); - static { - sParseFreqMap.put("SECONDLY", SECONDLY); - sParseFreqMap.put("MINUTELY", MINUTELY); - sParseFreqMap.put("HOURLY", HOURLY); - sParseFreqMap.put("DAILY", DAILY); - sParseFreqMap.put("WEEKLY", WEEKLY); - sParseFreqMap.put("MONTHLY", MONTHLY); - sParseFreqMap.put("YEARLY", YEARLY); - } - - /** maps a two-character weekday string to an integer constant */ - private static final HashMap sParseWeekdayMap = new HashMap(); - static { - sParseWeekdayMap.put("SU", SU); - sParseWeekdayMap.put("MO", MO); - sParseWeekdayMap.put("TU", TU); - sParseWeekdayMap.put("WE", WE); - sParseWeekdayMap.put("TH", TH); - sParseWeekdayMap.put("FR", FR); - sParseWeekdayMap.put("SA", SA); - } - - /** If set, allow lower-case recurrence rule strings. Minor performance impact. */ - private static final boolean ALLOW_LOWER_CASE = true; - - /** If set, validate the value of UNTIL parts. Minor performance impact. */ - private static final boolean VALIDATE_UNTIL = false; - - /** If set, require that only one of {UNTIL,COUNT} is present. Breaks compat w/ old parser. */ - private static final boolean ONLY_ONE_UNTIL_COUNT = false; - - - /** - * Thrown when a recurrence string provided can not be parsed according - * to RFC2445. - */ - public static class InvalidFormatException extends RuntimeException { - InvalidFormatException(String s) { - super(s); - } - } - - - public void setStartDate(Time date) { - startDate = date; - } - - /** - * Converts one of the Calendar.SUNDAY constants to the SU, MO, etc. - * constants. btw, I think we should switch to those here too, to - * get rid of this function, if possible. - */ - public static int calendarDay2Day(int day) - { - switch (day) - { - case Calendar.SUNDAY: - return SU; - case Calendar.MONDAY: - return MO; - case Calendar.TUESDAY: - return TU; - case Calendar.WEDNESDAY: - return WE; - case Calendar.THURSDAY: - return TH; - case Calendar.FRIDAY: - return FR; - case Calendar.SATURDAY: - return SA; - default: - throw new RuntimeException("bad day of week: " + day); - } - } - - public static int timeDay2Day(int day) - { - switch (day) - { - case Time.SUNDAY: - return SU; - case Time.MONDAY: - return MO; - case Time.TUESDAY: - return TU; - case Time.WEDNESDAY: - return WE; - case Time.THURSDAY: - return TH; - case Time.FRIDAY: - return FR; - case Time.SATURDAY: - return SA; - default: - throw new RuntimeException("bad day of week: " + day); - } - } - public static int day2TimeDay(int day) - { - switch (day) - { - case SU: - return Time.SUNDAY; - case MO: - return Time.MONDAY; - case TU: - return Time.TUESDAY; - case WE: - return Time.WEDNESDAY; - case TH: - return Time.THURSDAY; - case FR: - return Time.FRIDAY; - case SA: - return Time.SATURDAY; - default: - throw new RuntimeException("bad day of week: " + day); - } - } - - /** - * Converts one of the SU, MO, etc. constants to the Calendar.SUNDAY - * constants. btw, I think we should switch to those here too, to - * get rid of this function, if possible. - */ - public static int day2CalendarDay(int day) - { - switch (day) - { - case SU: - return Calendar.SUNDAY; - case MO: - return Calendar.MONDAY; - case TU: - return Calendar.TUESDAY; - case WE: - return Calendar.WEDNESDAY; - case TH: - return Calendar.THURSDAY; - case FR: - return Calendar.FRIDAY; - case SA: - return Calendar.SATURDAY; - default: - throw new RuntimeException("bad day of week: " + day); - } - } - - /** - * Converts one of the internal day constants (SU, MO, etc.) to the - * two-letter string representing that constant. - * - * @param day one the internal constants SU, MO, etc. - * @return the two-letter string for the day ("SU", "MO", etc.) - * - * @throws IllegalArgumentException Thrown if the day argument is not one of - * the defined day constants. - */ - private static String day2String(int day) { - switch (day) { - case SU: - return "SU"; - case MO: - return "MO"; - case TU: - return "TU"; - case WE: - return "WE"; - case TH: - return "TH"; - case FR: - return "FR"; - case SA: - return "SA"; - default: - throw new IllegalArgumentException("bad day argument: " + day); - } - } - - private static void appendNumbers(StringBuilder s, String label, - int count, int[] values) - { - if (count > 0) { - s.append(label); - count--; - for (int i=0; i - * Negative days, e.g. "FREQ=MONTHLY;BYDAY=-1TU" (the last Tuesday of every month), - * will cause "false" to be returned. - *

- * Rules that fire every week, such as "FREQ=MONTHLY;BYDAY=TU" (every Tuesday of every - * month) will cause "false" to be returned. (Note these are usually expressed as - * WEEKLY rules, and hence are uncommon.) - * - * @return true if this rule is of the appropriate form - */ - public boolean repeatsMonthlyOnDayCount() { - if (this.freq != MONTHLY) { - return false; - } - - if (bydayCount != 1 || bymonthdayCount != 0) { - return false; - } - - if (bydayNum[0] <= 0) { - return false; - } - - return true; - } - - /** - * Determines whether two integer arrays contain identical elements. - *

- * The native implementation over-allocated the arrays (and may have stuff left over from - * a previous run), so we can't just check the arrays -- the separately-maintained count - * field also matters. We assume that a null array will have a count of zero, and that the - * array can hold as many elements as the associated count indicates. - *

- * TODO: replace this with Arrays.equals() when the old parser goes away. - */ - private static boolean arraysEqual(int[] array1, int count1, int[] array2, int count2) { - if (count1 != count2) { - return false; - } - - for (int i = 0; i < count1; i++) { - if (array1[i] != array2[i]) - return false; - } - - return true; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (!(obj instanceof EventRecurrence)) { - return false; - } - - EventRecurrence er = (EventRecurrence) obj; - return (startDate == null ? - er.startDate == null : Time.compare(startDate, er.startDate) == 0) && - freq == er.freq && - (until == null ? er.until == null : until.equals(er.until)) && - count == er.count && - interval == er.interval && - wkst == er.wkst && - arraysEqual(bysecond, bysecondCount, er.bysecond, er.bysecondCount) && - arraysEqual(byminute, byminuteCount, er.byminute, er.byminuteCount) && - arraysEqual(byhour, byhourCount, er.byhour, er.byhourCount) && - arraysEqual(byday, bydayCount, er.byday, er.bydayCount) && - arraysEqual(bydayNum, bydayCount, er.bydayNum, er.bydayCount) && - arraysEqual(bymonthday, bymonthdayCount, er.bymonthday, er.bymonthdayCount) && - arraysEqual(byyearday, byyeardayCount, er.byyearday, er.byyeardayCount) && - arraysEqual(byweekno, byweeknoCount, er.byweekno, er.byweeknoCount) && - arraysEqual(bymonth, bymonthCount, er.bymonth, er.bymonthCount) && - arraysEqual(bysetpos, bysetposCount, er.bysetpos, er.bysetposCount); - } - - @Override public int hashCode() { - // We overrode equals, so we must override hashCode(). Nobody seems to need this though. - throw new UnsupportedOperationException(); - } - - /** - * Resets parser-modified fields to their initial state. Does not alter startDate. - *

- * The original parser always set all of the "count" fields, "wkst", and "until", - * essentially allowing the same object to be used multiple times by calling parse(). - * It's unclear whether this behavior was intentional. For now, be paranoid and - * preserve the existing behavior by resetting the fields. - *

- * We don't need to touch the integer arrays; they will either be ignored or - * overwritten. The "startDate" field is not set by the parser, so we ignore it here. - */ - private void resetFields() { - until = null; - freq = count = interval = bysecondCount = byminuteCount = byhourCount = - bydayCount = bymonthdayCount = byyeardayCount = byweeknoCount = bymonthCount = - bysetposCount = 0; - } - - /** - * Parses an rfc2445 recurrence rule string into its component pieces. Attempting to parse - * malformed input will result in an EventRecurrence.InvalidFormatException. - * - * @param recur The recurrence rule to parse (in un-folded form). - */ - public void parse(String recur) { - /* - * From RFC 2445 section 4.3.10: - * - * recur = "FREQ"=freq *( - * ; either UNTIL or COUNT may appear in a 'recur', - * ; but UNTIL and COUNT MUST NOT occur in the same 'recur' - * - * ( ";" "UNTIL" "=" enddate ) / - * ( ";" "COUNT" "=" 1*DIGIT ) / - * - * ; the rest of these keywords are optional, - * ; but MUST NOT occur more than once - * - * ( ";" "INTERVAL" "=" 1*DIGIT ) / - * ( ";" "BYSECOND" "=" byseclist ) / - * ( ";" "BYMINUTE" "=" byminlist ) / - * ( ";" "BYHOUR" "=" byhrlist ) / - * ( ";" "BYDAY" "=" bywdaylist ) / - * ( ";" "BYMONTHDAY" "=" bymodaylist ) / - * ( ";" "BYYEARDAY" "=" byyrdaylist ) / - * ( ";" "BYWEEKNO" "=" bywknolist ) / - * ( ";" "BYMONTH" "=" bymolist ) / - * ( ";" "BYSETPOS" "=" bysplist ) / - * ( ";" "WKST" "=" weekday ) / - * ( ";" x-name "=" text ) - * ) - * - * The rule parts are not ordered in any particular sequence. - * - * Examples: - * FREQ=MONTHLY;INTERVAL=2;COUNT=10;BYDAY=1SU,-1SU - * FREQ=YEARLY;INTERVAL=4;BYMONTH=11;BYDAY=TU;BYMONTHDAY=2,3,4,5,6,7,8 - * - * Strategy: - * (1) Split the string at ';' boundaries to get an array of rule "parts". - * (2) For each part, find substrings for left/right sides of '=' (name/value). - * (3) Call a -specific parsing function to parse the into an - * output field. - * - * By keeping track of which names we've seen in a bit vector, we can verify the - * constraints indicated above (FREQ appears first, none of them appear more than once -- - * though x-[name] would require special treatment), and we have either UNTIL or COUNT - * but not both. - * - * In general, RFC 2445 property names (e.g. "FREQ") and enumerations ("TU") must - * be handled in a case-insensitive fashion, but case may be significant for other - * properties. We don't have any case-sensitive values in RRULE, except possibly - * for the custom "X-" properties, but we ignore those anyway. Thus, we can trivially - * convert the entire string to upper case and then use simple comparisons. - * - * Differences from previous version: - * - allows lower-case property and enumeration values [optional] - * - enforces that FREQ appears first - * - enforces that only one of UNTIL and COUNT may be specified - * - allows (but ignores) X-* parts - * - improved validation on various values (e.g. UNTIL timestamps) - * - error messages are more specific - * - * TODO: enforce additional constraints listed in RFC 5545, notably the "N/A" entries - * in section 3.3.10. For example, if FREQ=WEEKLY, we should reject a rule that - * includes a BYMONTHDAY part. - */ - - /* TODO: replace with "if (freq != 0) throw" if nothing requires this */ - resetFields(); - - int parseFlags = 0; - String[] parts; - if (ALLOW_LOWER_CASE) { - parts = recur.toUpperCase().split(";"); - } else { - parts = recur.split(";"); - } - for (String part : parts) { - // allow empty part (e.g., double semicolon ";;") - if (TextUtils.isEmpty(part)) { - continue; - } - int equalIndex = part.indexOf('='); - if (equalIndex <= 0) { - /* no '=' or no LHS */ - throw new InvalidFormatException("Missing LHS in " + part); - } - - String lhs = part.substring(0, equalIndex); - String rhs = part.substring(equalIndex + 1); - if (rhs.length() == 0) { - throw new InvalidFormatException("Missing RHS in " + part); - } - - /* - * In lieu of a "switch" statement that allows string arguments, we use a - * map from strings to parsing functions. - */ - PartParser parser = sParsePartMap.get(lhs); - if (parser == null) { - if (lhs.startsWith("X-")) { - //Log.d(TAG, "Ignoring custom part " + lhs); - continue; - } - throw new InvalidFormatException("Couldn't find parser for " + lhs); - } else { - int flag = parser.parsePart(rhs, this); - if ((parseFlags & flag) != 0) { - throw new InvalidFormatException("Part " + lhs + " was specified twice"); - } - parseFlags |= flag; - } - } - - // If not specified, week starts on Monday. - if ((parseFlags & PARSED_WKST) == 0) { - wkst = MO; - } - - // FREQ is mandatory. - if ((parseFlags & PARSED_FREQ) == 0) { - throw new InvalidFormatException("Must specify a FREQ value"); - } - - // Can't have both UNTIL and COUNT. - if ((parseFlags & (PARSED_UNTIL | PARSED_COUNT)) == (PARSED_UNTIL | PARSED_COUNT)) { - if (ONLY_ONE_UNTIL_COUNT) { - throw new InvalidFormatException("Must not specify both UNTIL and COUNT: " + recur); - } else { - Log.w(TAG, "Warning: rrule has both UNTIL and COUNT: " + recur); - } - } - } - - /** - * Base class for the RRULE part parsers. - */ - abstract static class PartParser { - /** - * Parses a single part. - * - * @param value The right-hand-side of the part. - * @param er The EventRecurrence into which the result is stored. - * @return A bit value indicating which part was parsed. - */ - public abstract int parsePart(String value, EventRecurrence er); - - /** - * Parses an integer, with range-checking. - * - * @param str The string to parse. - * @param minVal Minimum allowed value. - * @param maxVal Maximum allowed value. - * @param allowZero Is 0 allowed? - * @return The parsed value. - */ - public static int parseIntRange(String str, int minVal, int maxVal, boolean allowZero) { - try { - if (str.charAt(0) == '+') { - // Integer.parseInt does not allow a leading '+', so skip it manually. - str = str.substring(1); - } - int val = Integer.parseInt(str); - if (val < minVal || val > maxVal || (val == 0 && !allowZero)) { - throw new InvalidFormatException("Integer value out of range: " + str); - } - return val; - } catch (NumberFormatException nfe) { - throw new InvalidFormatException("Invalid integer value: " + str); - } - } - - /** - * Parses a comma-separated list of integers, with range-checking. - * - * @param listStr The string to parse. - * @param minVal Minimum allowed value. - * @param maxVal Maximum allowed value. - * @param allowZero Is 0 allowed? - * @return A new array with values, sized to hold the exact number of elements. - */ - public static int[] parseNumberList(String listStr, int minVal, int maxVal, - boolean allowZero) { - int[] values; - - if (listStr.indexOf(",") < 0) { - // Common case: only one entry, skip split() overhead. - values = new int[1]; - values[0] = parseIntRange(listStr, minVal, maxVal, allowZero); - } else { - String[] valueStrs = listStr.split(","); - int len = valueStrs.length; - values = new int[len]; - for (int i = 0; i < len; i++) { - values[i] = parseIntRange(valueStrs[i], minVal, maxVal, allowZero); - } - } - return values; - } - } - - /** parses FREQ={SECONDLY,MINUTELY,...} */ - private static class ParseFreq extends PartParser { - @Override public int parsePart(String value, EventRecurrence er) { - Integer freq = sParseFreqMap.get(value); - if (freq == null) { - throw new InvalidFormatException("Invalid FREQ value: " + value); - } - er.freq = freq; - return PARSED_FREQ; - } - } - /** parses UNTIL=enddate, e.g. "19970829T021400" */ - private static class ParseUntil extends PartParser { - @Override public int parsePart(String value, EventRecurrence er) { - if (VALIDATE_UNTIL) { - try { - // Parse the time to validate it. The result isn't retained. - Time until = new Time(); - until.parse(value); - } catch (TimeFormatException tfe) { - throw new InvalidFormatException("Invalid UNTIL value: " + value); - } - } - er.until = value; - return PARSED_UNTIL; - } - } - /** parses COUNT=[non-negative-integer] */ - private static class ParseCount extends PartParser { - @Override public int parsePart(String value, EventRecurrence er) { - er.count = parseIntRange(value, Integer.MIN_VALUE, Integer.MAX_VALUE, true); - if (er.count < 0) { - Log.d(TAG, "Invalid Count. Forcing COUNT to 1 from " + value); - er.count = 1; // invalid count. assume one time recurrence. - } - return PARSED_COUNT; - } - } - /** parses INTERVAL=[non-negative-integer] */ - private static class ParseInterval extends PartParser { - @Override public int parsePart(String value, EventRecurrence er) { - er.interval = parseIntRange(value, Integer.MIN_VALUE, Integer.MAX_VALUE, true); - if (er.interval < 1) { - Log.d(TAG, "Invalid Interval. Forcing INTERVAL to 1 from " + value); - er.interval = 1; - } - return PARSED_INTERVAL; - } - } - /** parses BYSECOND=byseclist */ - private static class ParseBySecond extends PartParser { - @Override public int parsePart(String value, EventRecurrence er) { - int[] bysecond = parseNumberList(value, 0, 59, true); - er.bysecond = bysecond; - er.bysecondCount = bysecond.length; - return PARSED_BYSECOND; - } - } - /** parses BYMINUTE=byminlist */ - private static class ParseByMinute extends PartParser { - @Override public int parsePart(String value, EventRecurrence er) { - int[] byminute = parseNumberList(value, 0, 59, true); - er.byminute = byminute; - er.byminuteCount = byminute.length; - return PARSED_BYMINUTE; - } - } - /** parses BYHOUR=byhrlist */ - private static class ParseByHour extends PartParser { - @Override public int parsePart(String value, EventRecurrence er) { - int[] byhour = parseNumberList(value, 0, 23, true); - er.byhour = byhour; - er.byhourCount = byhour.length; - return PARSED_BYHOUR; - } - } - /** parses BYDAY=bywdaylist, e.g. "1SU,-1SU" */ - private static class ParseByDay extends PartParser { - @Override public int parsePart(String value, EventRecurrence er) { - int[] byday; - int[] bydayNum; - int bydayCount; - - if (value.indexOf(",") < 0) { - /* only one entry, skip split() overhead */ - bydayCount = 1; - byday = new int[1]; - bydayNum = new int[1]; - parseWday(value, byday, bydayNum, 0); - } else { - String[] wdays = value.split(","); - int len = wdays.length; - bydayCount = len; - byday = new int[len]; - bydayNum = new int[len]; - for (int i = 0; i < len; i++) { - parseWday(wdays[i], byday, bydayNum, i); - } - } - er.byday = byday; - er.bydayNum = bydayNum; - er.bydayCount = bydayCount; - return PARSED_BYDAY; - } - - /** parses [int]weekday, putting the pieces into parallel array entries */ - private static void parseWday(String str, int[] byday, int[] bydayNum, int index) { - int wdayStrStart = str.length() - 2; - String wdayStr; - - if (wdayStrStart > 0) { - /* number is included; parse it out and advance to weekday */ - String numPart = str.substring(0, wdayStrStart); - int num = parseIntRange(numPart, -53, 53, false); - bydayNum[index] = num; - wdayStr = str.substring(wdayStrStart); - } else { - /* just the weekday string */ - wdayStr = str; - } - Integer wday = sParseWeekdayMap.get(wdayStr); - if (wday == null) { - throw new InvalidFormatException("Invalid BYDAY value: " + str); - } - byday[index] = wday; - } - } - /** parses BYMONTHDAY=bymodaylist */ - private static class ParseByMonthDay extends PartParser { - @Override public int parsePart(String value, EventRecurrence er) { - int[] bymonthday = parseNumberList(value, -31, 31, false); - er.bymonthday = bymonthday; - er.bymonthdayCount = bymonthday.length; - return PARSED_BYMONTHDAY; - } - } - /** parses BYYEARDAY=byyrdaylist */ - private static class ParseByYearDay extends PartParser { - @Override public int parsePart(String value, EventRecurrence er) { - int[] byyearday = parseNumberList(value, -366, 366, false); - er.byyearday = byyearday; - er.byyeardayCount = byyearday.length; - return PARSED_BYYEARDAY; - } - } - /** parses BYWEEKNO=bywknolist */ - private static class ParseByWeekNo extends PartParser { - @Override public int parsePart(String value, EventRecurrence er) { - int[] byweekno = parseNumberList(value, -53, 53, false); - er.byweekno = byweekno; - er.byweeknoCount = byweekno.length; - return PARSED_BYWEEKNO; - } - } - /** parses BYMONTH=bymolist */ - private static class ParseByMonth extends PartParser { - @Override public int parsePart(String value, EventRecurrence er) { - int[] bymonth = parseNumberList(value, 1, 12, false); - er.bymonth = bymonth; - er.bymonthCount = bymonth.length; - return PARSED_BYMONTH; - } - } - /** parses BYSETPOS=bysplist */ - private static class ParseBySetPos extends PartParser { - @Override public int parsePart(String value, EventRecurrence er) { - int[] bysetpos = parseNumberList(value, Integer.MIN_VALUE, Integer.MAX_VALUE, true); - er.bysetpos = bysetpos; - er.bysetposCount = bysetpos.length; - return PARSED_BYSETPOS; - } - } - /** parses WKST={SU,MO,...} */ - private static class ParseWkst extends PartParser { - @Override public int parsePart(String value, EventRecurrence er) { - Integer wkst = sParseWeekdayMap.get(value); - if (wkst == null) { - throw new InvalidFormatException("Invalid WKST value: " + value); - } - er.wkst = wkst; - return PARSED_WKST; - } - } -} diff --git a/src/com/android/calendarcommon/ICalendar.java b/src/com/android/calendarcommon/ICalendar.java deleted file mode 100644 index 2374706..0000000 --- a/src/com/android/calendarcommon/ICalendar.java +++ /dev/null @@ -1,660 +0,0 @@ -/* - * Copyright (C) 2007 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.calendarcommon; - -import android.util.Log; - -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; -import java.util.ArrayList; - -/** - * Parses RFC 2445 iCalendar objects. - */ -public class ICalendar { - - private static final String TAG = "Sync"; - - // TODO: keep track of VEVENT, VTODO, VJOURNAL, VFREEBUSY, VTIMEZONE, VALARM - // components, by type field or by subclass? subclass would allow us to - // enforce grammars. - - /** - * Exception thrown when an iCalendar object has invalid syntax. - */ - public static class FormatException extends Exception { - public FormatException() { - super(); - } - - public FormatException(String msg) { - super(msg); - } - - public FormatException(String msg, Throwable cause) { - super(msg, cause); - } - } - - /** - * A component within an iCalendar (VEVENT, VTODO, VJOURNAL, VFEEBUSY, - * VTIMEZONE, VALARM). - */ - public static class Component { - - // components - static final String BEGIN = "BEGIN"; - static final String END = "END"; - private static final String NEWLINE = "\n"; - public static final String VCALENDAR = "VCALENDAR"; - public static final String VEVENT = "VEVENT"; - public static final String VTODO = "VTODO"; - public static final String VJOURNAL = "VJOURNAL"; - public static final String VFREEBUSY = "VFREEBUSY"; - public static final String VTIMEZONE = "VTIMEZONE"; - public static final String VALARM = "VALARM"; - - private final String mName; - private final Component mParent; // see if we can get rid of this - private LinkedList mChildren = null; - private final LinkedHashMap> mPropsMap = - new LinkedHashMap>(); - - /** - * Creates a new component with the provided name. - * @param name The name of the component. - */ - public Component(String name, Component parent) { - mName = name; - mParent = parent; - } - - /** - * Returns the name of the component. - * @return The name of the component. - */ - public String getName() { - return mName; - } - - /** - * Returns the parent of this component. - * @return The parent of this component. - */ - public Component getParent() { - return mParent; - } - - /** - * Helper that lazily gets/creates the list of children. - * @return The list of children. - */ - protected LinkedList getOrCreateChildren() { - if (mChildren == null) { - mChildren = new LinkedList(); - } - return mChildren; - } - - /** - * Adds a child component to this component. - * @param child The child component. - */ - public void addChild(Component child) { - getOrCreateChildren().add(child); - } - - /** - * Returns a list of the Component children of this component. May be - * null, if there are no children. - * - * @return A list of the children. - */ - public List getComponents() { - return mChildren; - } - - /** - * Adds a Property to this component. - * @param prop - */ - public void addProperty(Property prop) { - String name= prop.getName(); - ArrayList props = mPropsMap.get(name); - if (props == null) { - props = new ArrayList(); - mPropsMap.put(name, props); - } - props.add(prop); - } - - /** - * Returns a set of the property names within this component. - * @return A set of property names within this component. - */ - public Set getPropertyNames() { - return mPropsMap.keySet(); - } - - /** - * Returns a list of properties with the specified name. Returns null - * if there are no such properties. - * @param name The name of the property that should be returned. - * @return A list of properties with the requested name. - */ - public List getProperties(String name) { - return mPropsMap.get(name); - } - - /** - * Returns the first property with the specified name. Returns null - * if there is no such property. - * @param name The name of the property that should be returned. - * @return The first property with the specified name. - */ - public Property getFirstProperty(String name) { - List props = mPropsMap.get(name); - if (props == null || props.size() == 0) { - return null; - } - return props.get(0); - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - toString(sb); - sb.append(NEWLINE); - return sb.toString(); - } - - /** - * Helper method that appends this component to a StringBuilder. The - * caller is responsible for appending a newline at the end of the - * component. - */ - public void toString(StringBuilder sb) { - sb.append(BEGIN); - sb.append(":"); - sb.append(mName); - sb.append(NEWLINE); - - // append the properties - for (String propertyName : getPropertyNames()) { - for (Property property : getProperties(propertyName)) { - property.toString(sb); - sb.append(NEWLINE); - } - } - - // append the sub-components - if (mChildren != null) { - for (Component component : mChildren) { - component.toString(sb); - sb.append(NEWLINE); - } - } - - sb.append(END); - sb.append(":"); - sb.append(mName); - } - } - - /** - * A property within an iCalendar component (e.g., DTSTART, DTEND, etc., - * within a VEVENT). - */ - public static class Property { - // properties - // TODO: do we want to list these here? the complete list is long. - public static final String DTSTART = "DTSTART"; - public static final String DTEND = "DTEND"; - public static final String DURATION = "DURATION"; - public static final String RRULE = "RRULE"; - public static final String RDATE = "RDATE"; - public static final String EXRULE = "EXRULE"; - public static final String EXDATE = "EXDATE"; - // ... need to add more. - - private final String mName; - private LinkedHashMap> mParamsMap = - new LinkedHashMap>(); - private String mValue; // TODO: make this final? - - /** - * Creates a new property with the provided name. - * @param name The name of the property. - */ - public Property(String name) { - mName = name; - } - - /** - * Creates a new property with the provided name and value. - * @param name The name of the property. - * @param value The value of the property. - */ - public Property(String name, String value) { - mName = name; - mValue = value; - } - - /** - * Returns the name of the property. - * @return The name of the property. - */ - public String getName() { - return mName; - } - - /** - * Returns the value of this property. - * @return The value of this property. - */ - public String getValue() { - return mValue; - } - - /** - * Sets the value of this property. - * @param value The desired value for this property. - */ - public void setValue(String value) { - mValue = value; - } - - /** - * Adds a {@link Parameter} to this property. - * @param param The parameter that should be added. - */ - public void addParameter(Parameter param) { - ArrayList params = mParamsMap.get(param.name); - if (params == null) { - params = new ArrayList(); - mParamsMap.put(param.name, params); - } - params.add(param); - } - - /** - * Returns the set of parameter names for this property. - * @return The set of parameter names for this property. - */ - public Set getParameterNames() { - return mParamsMap.keySet(); - } - - /** - * Returns the list of parameters with the specified name. May return - * null if there are no such parameters. - * @param name The name of the parameters that should be returned. - * @return The list of parameters with the specified name. - */ - public List getParameters(String name) { - return mParamsMap.get(name); - } - - /** - * Returns the first parameter with the specified name. May return - * nll if there is no such parameter. - * @param name The name of the parameter that should be returned. - * @return The first parameter with the specified name. - */ - public Parameter getFirstParameter(String name) { - ArrayList params = mParamsMap.get(name); - if (params == null || params.size() == 0) { - return null; - } - return params.get(0); - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - toString(sb); - return sb.toString(); - } - - /** - * Helper method that appends this property to a StringBuilder. The - * caller is responsible for appending a newline after this property. - */ - public void toString(StringBuilder sb) { - sb.append(mName); - Set parameterNames = getParameterNames(); - for (String parameterName : parameterNames) { - for (Parameter param : getParameters(parameterName)) { - sb.append(";"); - param.toString(sb); - } - } - sb.append(":"); - sb.append(mValue); - } - } - - /** - * A parameter defined for an iCalendar property. - */ - // TODO: make this a proper class rather than a struct? - public static class Parameter { - public String name; - public String value; - - /** - * Creates a new empty parameter. - */ - public Parameter() { - } - - /** - * Creates a new parameter with the specified name and value. - * @param name The name of the parameter. - * @param value The value of the parameter. - */ - public Parameter(String name, String value) { - this.name = name; - this.value = value; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - toString(sb); - return sb.toString(); - } - - /** - * Helper method that appends this parameter to a StringBuilder. - */ - public void toString(StringBuilder sb) { - sb.append(name); - sb.append("="); - sb.append(value); - } - } - - private static final class ParserState { - // public int lineNumber = 0; - public String line; // TODO: just point to original text - public int index; - } - - // use factory method - private ICalendar() { - } - - // TODO: get rid of this -- handle all of the parsing in one pass through - // the text. - private static String normalizeText(String text) { - // it's supposed to be \r\n, but not everyone does that - text = text.replaceAll("\r\n", "\n"); - text = text.replaceAll("\r", "\n"); - - // we deal with line folding, by replacing all "\n " strings - // with nothing. The RFC specifies "\r\n " to be folded, but - // we handle "\n " and "\r " too because we can get those. - text = text.replaceAll("\n ", ""); - - return text; - } - - /** - * Parses text into an iCalendar component. Parses into the provided - * component, if not null, or parses into a new component. In the latter - * case, expects a BEGIN as the first line. Returns the provided or newly - * created top-level component. - */ - // TODO: use an index into the text, so we can make this a recursive - // function? - private static Component parseComponentImpl(Component component, - String text) - throws FormatException { - Component current = component; - ParserState state = new ParserState(); - state.index = 0; - - // split into lines - String[] lines = text.split("\n"); - - // each line is of the format: - // name *(";" param) ":" value - for (String line : lines) { - try { - current = parseLine(line, state, current); - // if the provided component was null, we will return the root - // NOTE: in this case, if the first line is not a BEGIN, a - // FormatException will get thrown. - if (component == null) { - component = current; - } - } catch (FormatException fe) { - if (false) { - Log.v(TAG, "Cannot parse " + line, fe); - } - // for now, we ignore the parse error. Google Calendar seems - // to be emitting some misformatted iCalendar objects. - } - continue; - } - return component; - } - - /** - * Parses a line into the provided component. Creates a new component if - * the line is a BEGIN, adding the newly created component to the provided - * parent. Returns whatever component is the current one (to which new - * properties will be added) in the parse. - */ - private static Component parseLine(String line, ParserState state, - Component component) - throws FormatException { - state.line = line; - int len = state.line.length(); - - // grab the name - char c = 0; - for (state.index = 0; state.index < len; ++state.index) { - c = line.charAt(state.index); - if (c == ';' || c == ':') { - break; - } - } - String name = line.substring(0, state.index); - - if (component == null) { - if (!Component.BEGIN.equals(name)) { - throw new FormatException("Expected BEGIN"); - } - } - - Property property; - if (Component.BEGIN.equals(name)) { - // start a new component - String componentName = extractValue(state); - Component child = new Component(componentName, component); - if (component != null) { - component.addChild(child); - } - return child; - } else if (Component.END.equals(name)) { - // finish the current component - String componentName = extractValue(state); - if (component == null || - !componentName.equals(component.getName())) { - throw new FormatException("Unexpected END " + componentName); - } - return component.getParent(); - } else { - property = new Property(name); - } - - if (c == ';') { - Parameter parameter = null; - while ((parameter = extractParameter(state)) != null) { - property.addParameter(parameter); - } - } - String value = extractValue(state); - property.setValue(value); - component.addProperty(property); - return component; - } - - /** - * Extracts the value ":..." on the current line. The first character must - * be a ':'. - */ - private static String extractValue(ParserState state) - throws FormatException { - String line = state.line; - if (state.index >= line.length() || line.charAt(state.index) != ':') { - throw new FormatException("Expected ':' before end of line in " - + line); - } - String value = line.substring(state.index + 1); - state.index = line.length() - 1; - return value; - } - - /** - * Extracts the next parameter from the line, if any. If there are no more - * parameters, returns null. - */ - private static Parameter extractParameter(ParserState state) - throws FormatException { - String text = state.line; - int len = text.length(); - Parameter parameter = null; - int startIndex = -1; - int equalIndex = -1; - while (state.index < len) { - char c = text.charAt(state.index); - if (c == ':') { - if (parameter != null) { - if (equalIndex == -1) { - throw new FormatException("Expected '=' within " - + "parameter in " + text); - } - parameter.value = text.substring(equalIndex + 1, - state.index); - } - return parameter; // may be null - } else if (c == ';') { - if (parameter != null) { - if (equalIndex == -1) { - throw new FormatException("Expected '=' within " - + "parameter in " + text); - } - parameter.value = text.substring(equalIndex + 1, - state.index); - return parameter; - } else { - parameter = new Parameter(); - startIndex = state.index; - } - } else if (c == '=') { - equalIndex = state.index; - if ((parameter == null) || (startIndex == -1)) { - throw new FormatException("Expected ';' before '=' in " - + text); - } - parameter.name = text.substring(startIndex + 1, equalIndex); - } else if (c == '"') { - if (parameter == null) { - throw new FormatException("Expected parameter before '\"' in " + text); - } - if (equalIndex == -1) { - throw new FormatException("Expected '=' within parameter in " + text); - } - if (state.index > equalIndex + 1) { - throw new FormatException("Parameter value cannot contain a '\"' in " + text); - } - final int endQuote = text.indexOf('"', state.index + 1); - if (endQuote < 0) { - throw new FormatException("Expected closing '\"' in " + text); - } - parameter.value = text.substring(state.index + 1, endQuote); - state.index = endQuote + 1; - return parameter; - } - ++state.index; - } - throw new FormatException("Expected ':' before end of line in " + text); - } - - /** - * Parses the provided text into an iCalendar object. The top-level - * component must be of type VCALENDAR. - * @param text The text to be parsed. - * @return The top-level VCALENDAR component. - * @throws FormatException Thrown if the text could not be parsed into an - * iCalendar VCALENDAR object. - */ - public static Component parseCalendar(String text) throws FormatException { - Component calendar = parseComponent(null, text); - if (calendar == null || !Component.VCALENDAR.equals(calendar.getName())) { - throw new FormatException("Expected " + Component.VCALENDAR); - } - return calendar; - } - - /** - * Parses the provided text into an iCalendar event. The top-level - * component must be of type VEVENT. - * @param text The text to be parsed. - * @return The top-level VEVENT component. - * @throws FormatException Thrown if the text could not be parsed into an - * iCalendar VEVENT. - */ - public static Component parseEvent(String text) throws FormatException { - Component event = parseComponent(null, text); - if (event == null || !Component.VEVENT.equals(event.getName())) { - throw new FormatException("Expected " + Component.VEVENT); - } - return event; - } - - /** - * Parses the provided text into an iCalendar component. - * @param text The text to be parsed. - * @return The top-level component. - * @throws FormatException Thrown if the text could not be parsed into an - * iCalendar component. - */ - public static Component parseComponent(String text) throws FormatException { - return parseComponent(null, text); - } - - /** - * Parses the provided text, adding to the provided component. - * @param component The component to which the parsed iCalendar data should - * be added. - * @param text The text to be parsed. - * @return The top-level component. - * @throws FormatException Thrown if the text could not be parsed as an - * iCalendar object. - */ - public static Component parseComponent(Component component, String text) - throws FormatException { - text = normalizeText(text); - return parseComponentImpl(component, text); - } -} diff --git a/src/com/android/calendarcommon/RecurrenceProcessor.java b/src/com/android/calendarcommon/RecurrenceProcessor.java deleted file mode 100644 index 82b699b..0000000 --- a/src/com/android/calendarcommon/RecurrenceProcessor.java +++ /dev/null @@ -1,1314 +0,0 @@ -/* //device/content/providers/pim/RecurrenceProcessor.java -** -** Copyright 2006, The Android Open Source Project -** -** Licensed under the Apache License, Version 2.0 (the "License"); -** you may not use this file except in compliance with the License. -** You may obtain a copy of the License at -** -** http://www.apache.org/licenses/LICENSE-2.0 -** -** Unless required by applicable law or agreed to in writing, software -** distributed under the License is distributed on an "AS IS" BASIS, -** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -** See the License for the specific language governing permissions and -** limitations under the License. -*/ - -package com.android.calendarcommon; - -import android.text.format.Time; -import android.util.Log; - -import java.util.TreeSet; - -public class RecurrenceProcessor -{ - // these are created once and reused. - private Time mIterator = new Time(Time.TIMEZONE_UTC); - private Time mUntil = new Time(Time.TIMEZONE_UTC); - private StringBuilder mStringBuilder = new StringBuilder(); - private Time mGenerated = new Time(Time.TIMEZONE_UTC); - private DaySet mDays = new DaySet(false); - // Give up after this many loops. This is roughly 1 second of expansion. - private static final int MAX_ALLOWED_ITERATIONS = 2000; - - public RecurrenceProcessor() - { - } - - private static final String TAG = "RecurrenceProcessor"; - - private static final boolean SPEW = false; - - /** - * Returns the time (millis since epoch) of the last occurrence, - * or -1 if the event repeats forever. If there are no occurrences - * (because the exrule or exdates cancel all the occurrences) and the - * event does not repeat forever, then 0 is returned. - * - * This computes a conservative estimate of the last occurrence. That is, - * the time of the actual last occurrence might be earlier than the time - * returned by this method. - * - * @param dtstart the time of the first occurrence - * @param recur the recurrence - * @return an estimate of the time (in UTC milliseconds) of the last - * occurrence, which may be greater than the actual last occurrence - * @throws DateException - */ - public long getLastOccurence(Time dtstart, - RecurrenceSet recur) throws DateException { - return getLastOccurence(dtstart, null /* no limit */, recur); - } - - /** - * Returns the time (millis since epoch) of the last occurrence, - * or -1 if the event repeats forever. If there are no occurrences - * (because the exrule or exdates cancel all the occurrences) and the - * event does not repeat forever, then 0 is returned. - * - * This computes a conservative estimate of the last occurrence. That is, - * the time of the actual last occurrence might be earlier than the time - * returned by this method. - * - * @param dtstart the time of the first occurrence - * @param maxtime the max possible time of the last occurrence. null means no limit - * @param recur the recurrence - * @return an estimate of the time (in UTC milliseconds) of the last - * occurrence, which may be greater than the actual last occurrence - * @throws DateException - */ - public long getLastOccurence(Time dtstart, Time maxtime, - RecurrenceSet recur) throws DateException { - long lastTime = -1; - boolean hasCount = false; - - // first see if there are any "until"s specified. if so, use the latest - // until / rdate. - if (recur.rrules != null) { - for (EventRecurrence rrule : recur.rrules) { - if (rrule.count != 0) { - hasCount = true; - } else if (rrule.until != null) { - // according to RFC 2445, until must be in UTC. - mIterator.parse(rrule.until); - long untilTime = mIterator.toMillis(false /* use isDst */); - if (untilTime > lastTime) { - lastTime = untilTime; - } - } - } - if (lastTime != -1 && recur.rdates != null) { - for (long dt : recur.rdates) { - if (dt > lastTime) { - lastTime = dt; - } - } - } - - // If there were only "until"s and no "count"s, then return the - // last "until" date or "rdate". - if (lastTime != -1 && !hasCount) { - return lastTime; - } - } else if (recur.rdates != null && - recur.exrules == null && recur.exdates == null) { - // if there are only rdates, we can just pick the last one. - for (long dt : recur.rdates) { - if (dt > lastTime) { - lastTime = dt; - } - } - return lastTime; - } - - // Expand the complete recurrence if there were any counts specified, - // or if there were rdates specified. - if (hasCount || recur.rdates != null || maxtime != null) { - // The expansion might not contain any dates if the exrule or - // exdates cancel all the generated dates. - long[] dates = expand(dtstart, recur, - dtstart.toMillis(false /* use isDst */) /* range start */, - (maxtime != null) ? - maxtime.toMillis(false /* use isDst */) : -1 /* range end */); - - // The expansion might not contain any dates if exrule or exdates - // cancel all the generated dates. - if (dates.length == 0) { - return 0; - } - return dates[dates.length - 1]; - } - return -1; - } - - /** - * a -- list of values - * N -- number of values to use in a - * v -- value to check for - */ - private static boolean listContains(int[] a, int N, int v) - { - for (int i=0; i 0) { - if (w == v) { - return true; - } - } else { - max += w; // w is negative - if (max == v) { - return true; - } - } - } - return false; - } - - /** - * Filter out the ones for events whose BYxxx rule is for - * a period greater than or equal to the period of the FREQ. - * - * Returns 0 if the event should not be filtered out - * Returns something else (a rule number which is useful for debugging) - * if the event should not be returned - */ - private static int filter(EventRecurrence r, Time iterator) - { - boolean found; - int freq = r.freq; - - if (EventRecurrence.MONTHLY >= freq) { - // BYMONTH - if (r.bymonthCount > 0) { - found = listContains(r.bymonth, r.bymonthCount, - iterator.month + 1); - if (!found) { - return 1; - } - } - } - if (EventRecurrence.WEEKLY >= freq) { - // BYWEEK -- this is just a guess. I wonder how many events - // acutally use BYWEEKNO. - if (r.byweeknoCount > 0) { - found = listContains(r.byweekno, r.byweeknoCount, - iterator.getWeekNumber(), - iterator.getActualMaximum(Time.WEEK_NUM)); - if (!found) { - return 2; - } - } - } - if (EventRecurrence.DAILY >= freq) { - // BYYEARDAY - if (r.byyeardayCount > 0) { - found = listContains(r.byyearday, r.byyeardayCount, - iterator.yearDay, iterator.getActualMaximum(Time.YEAR_DAY)); - if (!found) { - return 3; - } - } - // BYMONTHDAY - if (r.bymonthdayCount > 0 ) { - found = listContains(r.bymonthday, r.bymonthdayCount, - iterator.monthDay, - iterator.getActualMaximum(Time.MONTH_DAY)); - if (!found) { - return 4; - } - } - // BYDAY -- when filtering, we ignore the number field, because it - // only is meaningful when creating more events. -byday: - if (r.bydayCount > 0) { - int a[] = r.byday; - int N = r.bydayCount; - int v = EventRecurrence.timeDay2Day(iterator.weekDay); - for (int i=0; i= freq) { - // BYHOUR - found = listContains(r.byhour, r.byhourCount, - iterator.hour, - iterator.getActualMaximum(Time.HOUR)); - if (!found) { - return 6; - } - } - if (EventRecurrence.MINUTELY >= freq) { - // BYMINUTE - found = listContains(r.byminute, r.byminuteCount, - iterator.minute, - iterator.getActualMaximum(Time.MINUTE)); - if (!found) { - return 7; - } - } - if (EventRecurrence.SECONDLY >= freq) { - // BYSECOND - found = listContains(r.bysecond, r.bysecondCount, - iterator.second, - iterator.getActualMaximum(Time.SECOND)); - if (!found) { - return 8; - } - } - - if (r.bysetposCount > 0) { -bysetpos: - // BYSETPOS - we only handle rules like FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1 - if (freq == EventRecurrence.MONTHLY && r.bydayCount > 0) { - // Check for stuff like BYDAY=1TU - for (int i = r.bydayCount - 1; i >= 0; i--) { - if (r.bydayNum[i] != 0) { - if (Log.isLoggable(TAG, Log.VERBOSE)) { - Log.v(TAG, "BYSETPOS not supported with these rules: " + r); - } - break bysetpos; - } - } - if (!filterMonthlySetPos(r, iterator)) { - // Not allowed, filter it out. - return 9; - } - } else { - if (Log.isLoggable(TAG, Log.VERBOSE)) { - Log.v(TAG, "BYSETPOS not supported with these rules: " + r); - } - } - // BYSETPOS was defined but we don't know how to handle it. Do no filtering based - // on it. - } - - // if we got to here, we didn't filter it out - return 0; - } - - /** - * Filters out instances that don't match the BYSETPOS clause of a monthly recurrence rule. - * This is an awkward and inefficient way to go about it. - * - * @returns true if this instance should be kept - */ - private static boolean filterMonthlySetPos(EventRecurrence r, Time instance) { - /* - * Compute the day of the week for the first day of the month. "instance" has a - * day number and a DotW, so we compute the DotW of the 1st from that. Note DotW - * is 0-6, where 0=SUNDAY. - * - * The basic calculation is to take the instance's "day of the week" number, subtract - * (day of the month - 1) mod 7, and then make sure it's positive. We can simplify - * that with some algebra. - */ - int dotw = (instance.weekDay - instance.monthDay + 36) % 7; - - /* - * The byday[] values are specified as bits, so we can just OR them all - * together. - */ - int bydayMask = 0; - for (int i = 0; i < r.bydayCount; i++) { - bydayMask |= r.byday[i]; - } - - /* - * Generate a set according to the BYDAY rules. For each day of the month, determine - * if its day of the week is included. If so, append it to the day set. - */ - int maxDay = instance.getActualMaximum(Time.MONTH_DAY); - int daySet[] = new int[maxDay]; - int daySetLength = 0; - - for (int md = 1; md <= maxDay; md++) { - // For each month day, see if it's part of the set. (This makes some assumptions - // about the exact form of the DotW constants.) - int dayBit = EventRecurrence.SU << dotw; - if ((bydayMask & dayBit) != 0) { - daySet[daySetLength++] = md; - } - - dotw++; - if (dotw == 7) - dotw = 0; - } - - /* - * Now walk through the BYSETPOS list and see if the instance is equal to any of the - * specified daySet entries. - */ - for (int i = r.bysetposCount - 1; i >= 0; i--) { - int index = r.bysetpos[i]; - if (index > 0) { - if (index > daySetLength) { - continue; // out of range - } - if (daySet[index-1] == instance.monthDay) { - return true; - } - } else if (index < 0) { - if (daySetLength + index < 0) { - continue; // out of range - } - if (daySet[daySetLength + index] == instance.monthDay) { - return true; - } - } else { - // should have been caught by parser - throw new RuntimeException("invalid bysetpos value"); - } - } - - return false; - } - - - private static final int USE_ITERATOR = 0; - private static final int USE_BYLIST = 1; - - /** - * Return whether we should make this list from the BYxxx list or - * from the component of the iterator. - */ - int generateByList(int count, int freq, int byFreq) - { - if (byFreq >= freq) { - return USE_ITERATOR; - } else { - if (count == 0) { - return USE_ITERATOR; - } else { - return USE_BYLIST; - } - } - } - - private static boolean useBYX(int freq, int freqConstant, int count) - { - return freq > freqConstant && count > 0; - } - - public static class DaySet - { - public DaySet(boolean zulu) - { - mTime = new Time(Time.TIMEZONE_UTC); - } - - void setRecurrence(EventRecurrence r) - { - mYear = 0; - mMonth = -1; - mR = r; - } - - boolean get(Time iterator, int day) - { - int realYear = iterator.year; - int realMonth = iterator.month; - - Time t = null; - - if (SPEW) { - Log.i(TAG, "get called with iterator=" + iterator - + " " + iterator.month - + "/" + iterator.monthDay - + "/" + iterator.year + " day=" + day); - } - if (day < 1 || day > 28) { - // if might be past the end of the month, we need to normalize it - t = mTime; - t.set(day, realMonth, realYear); - unsafeNormalize(t); - realYear = t.year; - realMonth = t.month; - day = t.monthDay; - if (SPEW) { - Log.i(TAG, "normalized t=" + t + " " + t.month - + "/" + t.monthDay - + "/" + t.year); - } - } - - /* - if (true || SPEW) { - Log.i(TAG, "set t=" + t + " " + realMonth + "/" + day + "/" + realYear); - } - */ - if (realYear != mYear || realMonth != mMonth) { - if (t == null) { - t = mTime; - t.set(day, realMonth, realYear); - unsafeNormalize(t); - if (SPEW) { - Log.i(TAG, "set t=" + t + " " + t.month - + "/" + t.monthDay - + "/" + t.year - + " realMonth=" + realMonth + " mMonth=" + mMonth); - } - } - mYear = realYear; - mMonth = realMonth; - mDays = generateDaysList(t, mR); - if (SPEW) { - Log.i(TAG, "generated days list"); - } - } - return (mDays & (1< DAILY. Otherwise, we should be - * processing the BYDAY, BYMONTHDAY, etc. as filters instead. - * - * monthOffset may be -1, 0 or 1 - */ - private static int generateDaysList(Time generated, EventRecurrence r) - { - int days = 0; - - int i, count, v; - int[] byday, bydayNum, bymonthday; - int j, lastDayThisMonth; - int first; // Time.SUNDAY, etc - int k; - - lastDayThisMonth = generated.getActualMaximum(Time.MONTH_DAY); - - // BYDAY - count = r.bydayCount; - if (count > 0) { - // calculate the day of week for the first of this month (first) - j = generated.monthDay; - while (j >= 8) { - j -= 7; - } - first = generated.weekDay; - if (first >= j) { - first = first - j + 1; - } else { - first = first - j + 8; - } - - // What to do if the event is weekly: - // This isn't ideal, but we'll generate a month's worth of events - // and the code that calls this will only use the ones that matter - // for the current week. - byday = r.byday; - bydayNum = r.bydayNum; - for (i=0; i 0) { - // v is positive, count from the beginning of the month - // -1 b/c the first one should add 0 - j += 7*(v-1); - if (j <= lastDayThisMonth) { - if (SPEW) Log.i(TAG, "setting " + j + " for rule " - + v + "/" + EventRecurrence.day2TimeDay(byday[i])); - // if it's impossible, we drop it - days |= 1 << j; - } - } - else { - // v is negative, count from the end of the month - // find the last one - for (; j<=lastDayThisMonth; j+=7) { - } - // v is negative - // should do +1 b/c the last one should add 0, but we also - // skipped the j -= 7 b/c the loop to find the last one - // overshot by one week - j += 7*v; - if (j >= 1) { - if (SPEW) Log.i(TAG, "setting " + j + " for rule " - + v + "/" + EventRecurrence.day2TimeDay(byday[i])); - days |= 1 << j; - } - } - } - } - - // BYMONTHDAY - // Q: What happens if we have BYMONTHDAY and BYDAY? - // A: I didn't see it in the spec, so in lieu of that, we'll - // intersect the two. That seems reasonable to me. - if (r.freq > EventRecurrence.WEEKLY) { - count = r.bymonthdayCount; - if (count != 0) { - bymonthday = r.bymonthday; - if (r.bydayCount == 0) { - for (i=0; i= 0) { - days |= 1 << v; - } else { - j = lastDayThisMonth + v + 1; // v is negative - if (j >= 1 && j <= lastDayThisMonth) { - days |= 1 << j; - } - } - } - } else { - // This is O(lastDayThisMonth*count), which is really - // O(count) with a decent sized constant. - for (j=1; j<=lastDayThisMonth; j++) { - next_day : { - if ((days&(1< dtSet = new TreeSet(); - - if (recur.rrules != null) { - for (EventRecurrence rrule : recur.rrules) { - expand(dtstart, rrule, rangeStartDateValue, - rangeEndDateValue, true /* add */, dtSet); - } - } - if (recur.rdates != null) { - for (long dt : recur.rdates) { - // The dates are stored as milliseconds. We need to convert - // them to year/month/day values in the local timezone. - mIterator.set(dt); - long dtvalue = normDateTimeComparisonValue(mIterator); - dtSet.add(dtvalue); - } - } - if (recur.exrules != null) { - for (EventRecurrence exrule : recur.exrules) { - expand(dtstart, exrule, rangeStartDateValue, - rangeEndDateValue, false /* remove */, dtSet); - } - } - if (recur.exdates != null) { - for (long dt : recur.exdates) { - // The dates are stored as milliseconds. We need to convert - // them to year/month/day values in the local timezone. - mIterator.set(dt); - long dtvalue = normDateTimeComparisonValue(mIterator); - dtSet.remove(dtvalue); - } - } - if (dtSet.isEmpty()) { - // this can happen if the recurrence does not occur within the - // expansion window. - return new long[0]; - } - - // The values in dtSet are represented in a special form that is useful - // for fast comparisons and that is easy to generate from year/month/day - // values. We need to convert these to UTC milliseconds and also to - // ensure that the dates are valid. - int len = dtSet.size(); - long[] dates = new long[len]; - int i = 0; - for (Long val: dtSet) { - setTimeFromLongValue(mIterator, val); - dates[i++] = mIterator.toMillis(true /* ignore isDst */); - } - return dates; - } - - /** - * Run the recurrence algorithm. Processes events defined in the local - * timezone of the event. Return a list of iCalendar DATETIME - * strings containing the start date/times of the occurrences; the output - * times are defined in the local timezone of the event. - * - * If you want all of the events, pass Long.MAX_VALUE for rangeEndDateValue. If you pass - * Long.MAX_VALUE for rangeEnd, and the event doesn't have a COUNT or UNTIL field, - * you'll get a DateException. - * - * @param dtstart the dtstart date as defined in RFC2445. This - * {@link Time} should be in the timezone of the event. - * @param r the parsed recurrence, as defiend in RFC2445 - * @param rangeStartDateValue the first date-time you care about, inclusive - * @param rangeEndDateValue the last date-time you care about, not inclusive (so - * if you care about everything up through and including - * Dec 22 1995, set last to Dec 23, 1995 00:00:00 - * @param add Whether or not we should add to out, or remove from out. - * @param out the TreeSet you'd like to fill with the events - * @throws DateException - * @throws android.util.TimeFormatException if r cannot be parsed. - */ - public void expand(Time dtstart, - EventRecurrence r, - long rangeStartDateValue, - long rangeEndDateValue, - boolean add, - TreeSet out) throws DateException { - unsafeNormalize(dtstart); - long dtstartDateValue = normDateTimeComparisonValue(dtstart); - int count = 0; - - // add the dtstart instance to the recurrence, if within range. - // For example, if dtstart is Mar 1, 2010 and the range is Jan 1 - Apr 1, - // then add it here and increment count. If the range is earlier or later, - // then don't add it here. In that case, count will be incremented later - // inside the loop. It is important that count gets incremented exactly - // once here or in the loop for dtstart. - // - // NOTE: if DTSTART is not synchronized with the recurrence rule, the first instance - // we return will not fit the RRULE pattern. - if (add && dtstartDateValue >= rangeStartDateValue - && dtstartDateValue < rangeEndDateValue) { - out.add(dtstartDateValue); - ++count; - } - - Time iterator = mIterator; - Time until = mUntil; - StringBuilder sb = mStringBuilder; - Time generated = mGenerated; - DaySet days = mDays; - - try { - - days.setRecurrence(r); - if (rangeEndDateValue == Long.MAX_VALUE && r.until == null && r.count == 0) { - throw new DateException( - "No range end provided for a recurrence that has no UNTIL or COUNT."); - } - - // the top-level frequency - int freqField; - int freqAmount = r.interval; - int freq = r.freq; - switch (freq) - { - case EventRecurrence.SECONDLY: - freqField = Time.SECOND; - break; - case EventRecurrence.MINUTELY: - freqField = Time.MINUTE; - break; - case EventRecurrence.HOURLY: - freqField = Time.HOUR; - break; - case EventRecurrence.DAILY: - freqField = Time.MONTH_DAY; - break; - case EventRecurrence.WEEKLY: - freqField = Time.MONTH_DAY; - freqAmount = 7 * r.interval; - if (freqAmount <= 0) { - freqAmount = 7; - } - break; - case EventRecurrence.MONTHLY: - freqField = Time.MONTH; - break; - case EventRecurrence.YEARLY: - freqField = Time.YEAR; - break; - default: - throw new DateException("bad freq=" + freq); - } - if (freqAmount <= 0) { - freqAmount = 1; - } - - int bymonthCount = r.bymonthCount; - boolean usebymonth = useBYX(freq, EventRecurrence.MONTHLY, bymonthCount); - boolean useDays = freq >= EventRecurrence.WEEKLY && - (r.bydayCount > 0 || r.bymonthdayCount > 0); - int byhourCount = r.byhourCount; - boolean usebyhour = useBYX(freq, EventRecurrence.HOURLY, byhourCount); - int byminuteCount = r.byminuteCount; - boolean usebyminute = useBYX(freq, EventRecurrence.MINUTELY, byminuteCount); - int bysecondCount = r.bysecondCount; - boolean usebysecond = useBYX(freq, EventRecurrence.SECONDLY, bysecondCount); - - // initialize the iterator - iterator.set(dtstart); - if (freqField == Time.MONTH) { - if (useDays) { - // if it's monthly, and we're going to be generating - // days, set the iterator day field to 1 because sometimes - // we'll skip months if it's greater than 28. - // XXX Do we generate days for MONTHLY w/ BYHOUR? If so, - // we need to do this then too. - iterator.monthDay = 1; - } - } - - long untilDateValue; - if (r.until != null) { - // Ensure that the "until" date string is specified in UTC. - String untilStr = r.until; - // 15 is length of date-time without trailing Z e.g. "20090204T075959" - // A string such as 20090204 is a valid UNTIL (see RFC 2445) and the - // Z should not be added. - if (untilStr.length() == 15) { - untilStr = untilStr + 'Z'; - } - // The parse() method will set the timezone to UTC - until.parse(untilStr); - - // We need the "until" year/month/day values to be in the same - // timezone as all the generated dates so that we can compare them - // using the values returned by normDateTimeComparisonValue(). - until.switchTimezone(dtstart.timezone); - untilDateValue = normDateTimeComparisonValue(until); - } else { - untilDateValue = Long.MAX_VALUE; - } - - sb.ensureCapacity(15); - sb.setLength(15); // TODO: pay attention to whether or not the event - // is an all-day one. - - if (SPEW) { - Log.i(TAG, "expand called w/ rangeStart=" + rangeStartDateValue - + " rangeEnd=" + rangeEndDateValue); - } - - // go until the end of the range or we're done with this event - boolean eventEnded = false; - int failsafe = 0; // Avoid infinite loops - events: { - while (true) { - int monthIndex = 0; - if (failsafe++ > MAX_ALLOWED_ITERATIONS) { // Give up after about 1 second of processing - throw new DateException("Recurrence processing stuck: " + r.toString()); - } - - unsafeNormalize(iterator); - - int iteratorYear = iterator.year; - int iteratorMonth = iterator.month + 1; - int iteratorDay = iterator.monthDay; - int iteratorHour = iterator.hour; - int iteratorMinute = iterator.minute; - int iteratorSecond = iterator.second; - - // year is never expanded -- there is no BYYEAR - generated.set(iterator); - - if (SPEW) Log.i(TAG, "year=" + generated.year); - - do { // month - int month = usebymonth - ? r.bymonth[monthIndex] - : iteratorMonth; - month--; - if (SPEW) Log.i(TAG, " month=" + month); - - int dayIndex = 1; - int lastDayToExamine = 0; - - // Use this to handle weeks that overlap the end of the month. - // Keep the year and month that days is for, and generate it - // when needed in the loop - if (useDays) { - // Determine where to start and end, don't worry if this happens - // to be before dtstart or after the end, because that will be - // filtered in the inner loop - if (freq == EventRecurrence.WEEKLY) { - /* - * iterator.weekDay indicates the day of the week (0-6, SU-SA). - * Because dayIndex might start in the middle of a week, and we're - * interested in treating a week as a unit, we want to move - * backward to the start of the week. (This could make the - * dayIndex negative, which will be corrected by normalization - * later on.) - * - * The day that starts the week is determined by WKST, which - * defaults to MO. - * - * Example: dayIndex is Tuesday the 8th, and weeks start on - * Thursdays. Tuesday is day 2, Thursday is day 4, so we - * want to move back (2 - 4 + 7) % 7 = 5 days to the previous - * Thursday. If weeks started on Mondays, we would only - * need to move back (2 - 1 + 7) % 7 = 1 day. - */ - int weekStartAdj = (iterator.weekDay - - EventRecurrence.day2TimeDay(r.wkst) + 7) % 7; - dayIndex = iterator.monthDay - weekStartAdj; - lastDayToExamine = dayIndex + 6; - } else { - lastDayToExamine = generated - .getActualMaximum(Time.MONTH_DAY); - } - if (SPEW) Log.i(TAG, "dayIndex=" + dayIndex - + " lastDayToExamine=" + lastDayToExamine - + " days=" + days); - } - - do { // day - int day; - if (useDays) { - if (!days.get(iterator, dayIndex)) { - dayIndex++; - continue; - } else { - day = dayIndex; - } - } else { - day = iteratorDay; - } - if (SPEW) Log.i(TAG, " day=" + day); - - // hour - int hourIndex = 0; - do { - int hour = usebyhour - ? r.byhour[hourIndex] - : iteratorHour; - if (SPEW) Log.i(TAG, " hour=" + hour + " usebyhour=" + usebyhour); - - // minute - int minuteIndex = 0; - do { - int minute = usebyminute - ? r.byminute[minuteIndex] - : iteratorMinute; - if (SPEW) Log.i(TAG, " minute=" + minute); - - // second - int secondIndex = 0; - do { - int second = usebysecond - ? r.bysecond[secondIndex] - : iteratorSecond; - if (SPEW) Log.i(TAG, " second=" + second); - - // we do this here each time, because if we distribute it, we find the - // month advancing extra times, as we set the month to the 32nd, 33rd, etc. - // days. - generated.set(second, minute, hour, day, month, iteratorYear); - unsafeNormalize(generated); - - long genDateValue = normDateTimeComparisonValue(generated); - // sometimes events get generated (BYDAY, BYHOUR, etc.) that - // are before dtstart. Filter these. I believe this is correct, - // but Google Calendar doesn't seem to always do this. - if (genDateValue >= dtstartDateValue) { - // filter and then add - // TODO: we don't check for stop conditions (like - // passing the "end" date) unless the filter - // allows the event. Could stop sooner. - int filtered = filter(r, generated); - if (0 == filtered) { - - // increase the count as long - // as this isn't the same - // as the first instance - // specified by the DTSTART - // (for RRULEs -- additive). - // This condition must be the complement of the - // condition for incrementing count at the - // beginning of the method, so if we don't - // increment count there, we increment it here. - // For example, if add is set and dtstartDateValue - // is inside the start/end range, then it was added - // and count was incremented at the beginning. - // If dtstartDateValue is outside the range or add - // is not set, then we must increment count here. - if (!(dtstartDateValue == genDateValue - && add - && dtstartDateValue >= rangeStartDateValue - && dtstartDateValue < rangeEndDateValue)) { - ++count; - } - // one reason we can stop is that - // we're past the until date - if (genDateValue > untilDateValue) { - if (SPEW) { - Log.i(TAG, "stopping b/c until=" - + untilDateValue - + " generated=" - + genDateValue); - } - break events; - } - // or we're past rangeEnd - if (genDateValue >= rangeEndDateValue) { - if (SPEW) { - Log.i(TAG, "stopping b/c rangeEnd=" - + rangeEndDateValue - + " generated=" + generated); - } - break events; - } - - if (genDateValue >= rangeStartDateValue) { - if (SPEW) { - Log.i(TAG, "adding date=" + generated + " filtered=" + filtered); - } - if (add) { - out.add(genDateValue); - } else { - out.remove(genDateValue); - } - } - // another is that count is high enough - if (r.count > 0 && r.count == count) { - //Log.i(TAG, "stopping b/c count=" + count); - break events; - } - } - } - secondIndex++; - } while (usebysecond && secondIndex < bysecondCount); - minuteIndex++; - } while (usebyminute && minuteIndex < byminuteCount); - hourIndex++; - } while (usebyhour && hourIndex < byhourCount); - dayIndex++; - } while (useDays && dayIndex <= lastDayToExamine); - monthIndex++; - } while (usebymonth && monthIndex < bymonthCount); - - // Add freqAmount to freqField until we get another date that we want. - // We don't want to "generate" dates with the iterator. - // XXX: We do this for days, because there is a varying number of days - // per month - int oldDay = iterator.monthDay; - generated.set(iterator); // just using generated as a temporary. - int n = 1; - while (true) { - int value = freqAmount * n; - switch (freqField) { - case Time.SECOND: - iterator.second += value; - break; - case Time.MINUTE: - iterator.minute += value; - break; - case Time.HOUR: - iterator.hour += value; - break; - case Time.MONTH_DAY: - iterator.monthDay += value; - break; - case Time.MONTH: - iterator.month += value; - break; - case Time.YEAR: - iterator.year += value; - break; - case Time.WEEK_DAY: - iterator.monthDay += value; - break; - case Time.YEAR_DAY: - iterator.monthDay += value; - break; - default: - throw new RuntimeException("bad field=" + freqField); - } - - unsafeNormalize(iterator); - if (freqField != Time.YEAR && freqField != Time.MONTH) { - break; - } - if (iterator.monthDay == oldDay) { - break; - } - n++; - iterator.set(generated); - } - } - } - } - catch (DateException e) { - Log.w(TAG, "DateException with r=" + r + " rangeStart=" + rangeStartDateValue - + " rangeEnd=" + rangeEndDateValue); - throw e; - } - catch (RuntimeException t) { - Log.w(TAG, "RuntimeException with r=" + r + " rangeStart=" + rangeStartDateValue - + " rangeEnd=" + rangeEndDateValue); - throw t; - } - } - - /** - * Normalizes the date fields to give a valid date, but if the time falls - * in the invalid window during a transition out of Daylight Saving Time - * when time jumps forward an hour, then the "normalized" value will be - * invalid. - *

- * This method also computes the weekDay and yearDay fields. - * - *

- * This method does not modify the fields isDst, or gmtOff. - */ - static void unsafeNormalize(Time date) { - int second = date.second; - int minute = date.minute; - int hour = date.hour; - int monthDay = date.monthDay; - int month = date.month; - int year = date.year; - - int addMinutes = ((second < 0) ? (second - 59) : second) / 60; - second -= addMinutes * 60; - minute += addMinutes; - int addHours = ((minute < 0) ? (minute - 59) : minute) / 60; - minute -= addHours * 60; - hour += addHours; - int addDays = ((hour < 0) ? (hour - 23) : hour) / 24; - hour -= addDays * 24; - monthDay += addDays; - - // We want to make "monthDay" positive. We do this by subtracting one - // from the year and adding a year's worth of days to "monthDay" in - // the following loop while "monthDay" <= 0. - while (monthDay <= 0) { - // If month is after Feb, then add this year's length so that we - // include this year's leap day, if any. - // Otherwise (the month is Feb or earlier), add last year's length. - // Subtract one from the year in either case. This gives the same - // effective date but makes monthDay (the day of the month) much - // larger. Eventually (usually in one iteration) monthDay will - // be positive. - int days = month > 1 ? yearLength(year) : yearLength(year - 1); - monthDay += days; - year -= 1; - } - // At this point, monthDay >= 1. Normalize the month to the range [0,11]. - if (month < 0) { - int years = (month + 1) / 12 - 1; - year += years; - month -= 12 * years; - } else if (month >= 12) { - int years = month / 12; - year += years; - month -= 12 * years; - } - // At this point, month is in the range [0,11] and monthDay >= 1. - // Now loop until the monthDay is in the correct range for the month. - while (true) { - // On January, check if we can jump forward a whole year. - if (month == 0) { - int yearLength = yearLength(year); - if (monthDay > yearLength) { - year++; - monthDay -= yearLength; - } - } - int monthLength = monthLength(year, month); - if (monthDay > monthLength) { - monthDay -= monthLength; - month++; - if (month >= 12) { - month -= 12; - year++; - } - } else break; - } - // At this point, monthDay <= the length of the current month and is - // in the range [1,31]. - - date.second = second; - date.minute = minute; - date.hour = hour; - date.monthDay = monthDay; - date.month = month; - date.year = year; - date.weekDay = weekDay(year, month, monthDay); - date.yearDay = yearDay(year, month, monthDay); - } - - /** - * Returns true if the given year is a leap year. - * - * @param year the given year to test - * @return true if the given year is a leap year. - */ - static boolean isLeapYear(int year) { - return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)); - } - - /** - * Returns the number of days in the given year. - * - * @param year the given year - * @return the number of days in the given year. - */ - static int yearLength(int year) { - return isLeapYear(year) ? 366 : 365; - } - - private static final int[] DAYS_PER_MONTH = { 31, 28, 31, 30, 31, 30, 31, - 31, 30, 31, 30, 31 }; - private static final int[] DAYS_IN_YEAR_PRECEDING_MONTH = { 0, 31, 59, 90, - 120, 151, 180, 212, 243, 273, 304, 334 }; - - /** - * Returns the number of days in the given month of the given year. - * - * @param year the given year. - * @param month the given month in the range [0,11] - * @return the number of days in the given month of the given year. - */ - static int monthLength(int year, int month) { - int n = DAYS_PER_MONTH[month]; - if (n != 28) { - return n; - } - return isLeapYear(year) ? 29 : 28; - } - - /** - * Computes the weekday, a number in the range [0,6] where Sunday=0, from - * the given year, month, and day. - * - * @param year the year - * @param month the 0-based month in the range [0,11] - * @param day the 1-based day of the month in the range [1,31] - * @return the weekday, a number in the range [0,6] where Sunday=0 - */ - static int weekDay(int year, int month, int day) { - if (month <= 1) { - month += 12; - year -= 1; - } - return (day + (13 * month - 14) / 5 + year + year/4 - year/100 + year/400) % 7; - } - - /** - * Computes the 0-based "year day", given the year, month, and day. - * - * @param year the year - * @param month the 0-based month in the range [0,11] - * @param day the 1-based day in the range [1,31] - * @return the 0-based "year day", the number of days into the year - */ - static int yearDay(int year, int month, int day) { - int yearDay = DAYS_IN_YEAR_PRECEDING_MONTH[month] + day - 1; - if (month >= 2 && isLeapYear(year)) { - yearDay += 1; - } - return yearDay; - } - - /** - * Converts a normalized Time value to a 64-bit long. The mapping of Time - * values to longs provides a total ordering on the Time values so that - * two Time values can be compared efficiently by comparing their 64-bit - * long values. This is faster than converting the Time values to UTC - * millliseconds. - * - * @param normalized a Time object whose date and time fields have been - * normalized - * @return a 64-bit long value that can be used for comparing and ordering - * dates and times represented by Time objects - */ - private static final long normDateTimeComparisonValue(Time normalized) { - // 37 bits for the year, 4 bits for the month, 5 bits for the monthDay, - // 5 bits for the hour, 6 bits for the minute, 6 bits for the second. - return ((long)normalized.year << 26) + (normalized.month << 22) - + (normalized.monthDay << 17) + (normalized.hour << 12) - + (normalized.minute << 6) + normalized.second; - } - - private static final void setTimeFromLongValue(Time date, long val) { - date.year = (int) (val >> 26); - date.month = (int) (val >> 22) & 0xf; - date.monthDay = (int) (val >> 17) & 0x1f; - date.hour = (int) (val >> 12) & 0x1f; - date.minute = (int) (val >> 6) & 0x3f; - date.second = (int) (val & 0x3f); - } -} diff --git a/src/com/android/calendarcommon/RecurrenceSet.java b/src/com/android/calendarcommon/RecurrenceSet.java deleted file mode 100644 index d8fb7cf..0000000 --- a/src/com/android/calendarcommon/RecurrenceSet.java +++ /dev/null @@ -1,526 +0,0 @@ -/* - * Copyright (C) 2007 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.calendarcommon; - -import android.content.ContentValues; -import android.database.Cursor; -import android.provider.CalendarContract; -import android.text.TextUtils; -import android.text.format.Time; -import android.util.Log; -import android.util.TimeFormatException; - -import java.util.List; -import java.util.regex.Pattern; - -/** - * Basic information about a recurrence, following RFC 2445 Section 4.8.5. - * Contains the RRULEs, RDATE, EXRULEs, and EXDATE properties. - */ -public class RecurrenceSet { - - private final static String TAG = "RecurrenceSet"; - - private final static String RULE_SEPARATOR = "\n"; - private final static String FOLDING_SEPARATOR = "\n "; - - // TODO: make these final? - public EventRecurrence[] rrules = null; - public long[] rdates = null; - public EventRecurrence[] exrules = null; - public long[] exdates = null; - - /** - * Creates a new RecurrenceSet from information stored in the - * events table in the CalendarProvider. - * @param values The values retrieved from the Events table. - */ - public RecurrenceSet(ContentValues values) - throws EventRecurrence.InvalidFormatException { - String rruleStr = values.getAsString(CalendarContract.Events.RRULE); - String rdateStr = values.getAsString(CalendarContract.Events.RDATE); - String exruleStr = values.getAsString(CalendarContract.Events.EXRULE); - String exdateStr = values.getAsString(CalendarContract.Events.EXDATE); - init(rruleStr, rdateStr, exruleStr, exdateStr); - } - - /** - * Creates a new RecurrenceSet from information stored in a database - * {@link Cursor} pointing to the events table in the - * CalendarProvider. The cursor must contain the RRULE, RDATE, EXRULE, - * and EXDATE columns. - * - * @param cursor The cursor containing the RRULE, RDATE, EXRULE, and EXDATE - * columns. - */ - public RecurrenceSet(Cursor cursor) - throws EventRecurrence.InvalidFormatException { - int rruleColumn = cursor.getColumnIndex(CalendarContract.Events.RRULE); - int rdateColumn = cursor.getColumnIndex(CalendarContract.Events.RDATE); - int exruleColumn = cursor.getColumnIndex(CalendarContract.Events.EXRULE); - int exdateColumn = cursor.getColumnIndex(CalendarContract.Events.EXDATE); - String rruleStr = cursor.getString(rruleColumn); - String rdateStr = cursor.getString(rdateColumn); - String exruleStr = cursor.getString(exruleColumn); - String exdateStr = cursor.getString(exdateColumn); - init(rruleStr, rdateStr, exruleStr, exdateStr); - } - - public RecurrenceSet(String rruleStr, String rdateStr, - String exruleStr, String exdateStr) - throws EventRecurrence.InvalidFormatException { - init(rruleStr, rdateStr, exruleStr, exdateStr); - } - - private void init(String rruleStr, String rdateStr, - String exruleStr, String exdateStr) - throws EventRecurrence.InvalidFormatException { - if (!TextUtils.isEmpty(rruleStr) || !TextUtils.isEmpty(rdateStr)) { - - if (!TextUtils.isEmpty(rruleStr)) { - String[] rruleStrs = rruleStr.split(RULE_SEPARATOR); - rrules = new EventRecurrence[rruleStrs.length]; - for (int i = 0; i < rruleStrs.length; ++i) { - EventRecurrence rrule = new EventRecurrence(); - rrule.parse(rruleStrs[i]); - rrules[i] = rrule; - } - } - - if (!TextUtils.isEmpty(rdateStr)) { - rdates = parseRecurrenceDates(rdateStr); - } - - if (!TextUtils.isEmpty(exruleStr)) { - String[] exruleStrs = exruleStr.split(RULE_SEPARATOR); - exrules = new EventRecurrence[exruleStrs.length]; - for (int i = 0; i < exruleStrs.length; ++i) { - EventRecurrence exrule = new EventRecurrence(); - exrule.parse(exruleStr); - exrules[i] = exrule; - } - } - - if (!TextUtils.isEmpty(exdateStr)) { - exdates = parseRecurrenceDates(exdateStr); - } - } - } - - /** - * Returns whether or not a recurrence is defined in this RecurrenceSet. - * @return Whether or not a recurrence is defined in this RecurrenceSet. - */ - public boolean hasRecurrence() { - return (rrules != null || rdates != null); - } - - /** - * Parses the provided RDATE or EXDATE string into an array of longs - * representing each date/time in the recurrence. - * @param recurrence The recurrence to be parsed. - * @return The list of date/times. - */ - public static long[] parseRecurrenceDates(String recurrence) - throws EventRecurrence.InvalidFormatException{ - // TODO: use "local" time as the default. will need to handle times - // that end in "z" (UTC time) explicitly at that point. - String tz = Time.TIMEZONE_UTC; - int tzidx = recurrence.indexOf(";"); - if (tzidx != -1) { - tz = recurrence.substring(0, tzidx); - recurrence = recurrence.substring(tzidx + 1); - } - Time time = new Time(tz); - String[] rawDates = recurrence.split(","); - int n = rawDates.length; - long[] dates = new long[n]; - for (int i = 0; i4.1 Content Lines - * - *

The iCalendar object is organized into individual lines of text, called - * content lines. Content lines are delimited by a line break, which is a CRLF - * sequence (US-ASCII decimal 13, followed by US-ASCII decimal 10). - * - *

Lines of text SHOULD NOT be longer than 75 octets, excluding the line - * break. Long content lines SHOULD be split into a multiple line - * representations using a line "folding" technique. That is, a long line can - * be split between any two characters by inserting a CRLF immediately - * followed by a single linear white space character (i.e., SPACE, US-ASCII - * decimal 32 or HTAB, US-ASCII decimal 9). Any sequence of CRLF followed - * immediately by a single linear white space character is ignored (i.e., - * removed) when processing the content type. - */ - public static String fold(String unfoldedIcalContent) { - return FOLD_RE.matcher(unfoldedIcalContent).replaceAll("$0\r\n "); - } - - public static String unfold(String foldedIcalContent) { - return IGNORABLE_ICAL_WHITESPACE_RE.matcher( - foldedIcalContent).replaceAll(""); - } - - public static void addPropertyForDateStr(ICalendar.Component component, - String propertyName, - String dateStr) { - if (TextUtils.isEmpty(dateStr)) { - return; - } - - ICalendar.Property prop = new ICalendar.Property(propertyName); - String tz = null; - int tzidx = dateStr.indexOf(";"); - if (tzidx != -1) { - tz = dateStr.substring(0, tzidx); - dateStr = dateStr.substring(tzidx + 1); - } - if (!TextUtils.isEmpty(tz)) { - prop.addParameter(new ICalendar.Parameter("TZID", tz)); - } - prop.setValue(dateStr); - component.addProperty(prop); - } - - private static String computeDuration(Time start, - ICalendar.Component component) { - // see if a duration is defined - ICalendar.Property durationProperty = - component.getFirstProperty("DURATION"); - if (durationProperty != null) { - // just return the duration - return durationProperty.getValue(); - } - - // must compute a duration from the DTEND - ICalendar.Property dtendProperty = - component.getFirstProperty("DTEND"); - if (dtendProperty == null) { - // no DURATION, no DTEND: 0 second duration - return "+P0S"; - } - ICalendar.Parameter endTzidParameter = - dtendProperty.getFirstParameter("TZID"); - String endTzid = (endTzidParameter == null) - ? start.timezone : endTzidParameter.value; - - Time end = new Time(endTzid); - end.parse(dtendProperty.getValue()); - long durationMillis = end.toMillis(false /* use isDst */) - - start.toMillis(false /* use isDst */); - long durationSeconds = (durationMillis / 1000); - if (start.allDay && (durationSeconds % 86400) == 0) { - return "P" + (durationSeconds / 86400) + "D"; // Server wants this instead of P86400S - } else { - return "P" + durationSeconds + "S"; - } - } - - private static String flattenProperties(ICalendar.Component component, - String name) { - List properties = component.getProperties(name); - if (properties == null || properties.isEmpty()) { - return null; - } - - if (properties.size() == 1) { - return properties.get(0).getValue(); - } - - StringBuilder sb = new StringBuilder(); - - boolean first = true; - for (ICalendar.Property property : component.getProperties(name)) { - if (first) { - first = false; - } else { - // TODO: use commas. our RECUR parsing should handle that - // anyway. - sb.append(RULE_SEPARATOR); - } - sb.append(property.getValue()); - } - return sb.toString(); - } - - private static String extractDates(ICalendar.Property recurrence) { - if (recurrence == null) { - return null; - } - ICalendar.Parameter tzidParam = - recurrence.getFirstParameter("TZID"); - if (tzidParam != null) { - return tzidParam.value + ";" + recurrence.getValue(); - } - return recurrence.getValue(); - } -} diff --git a/src/com/android/calendarcommon2/DateException.java b/src/com/android/calendarcommon2/DateException.java new file mode 100644 index 0000000..0221274 --- /dev/null +++ b/src/com/android/calendarcommon2/DateException.java @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2006 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.calendarcommon2; + +public class DateException extends Exception +{ + public DateException(String message) + { + super(message); + } +} diff --git a/src/com/android/calendarcommon2/Duration.java b/src/com/android/calendarcommon2/Duration.java new file mode 100644 index 0000000..dad7f74 --- /dev/null +++ b/src/com/android/calendarcommon2/Duration.java @@ -0,0 +1,150 @@ +/* +** Copyright 2006, The Android Open Source Project +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +*/ + +package com.android.calendarcommon2; + +import java.util.Calendar; + +/** + * According to RFC2445, durations are like this: + * WEEKS + * | DAYS [ HOURS [ MINUTES [ SECONDS ] ] ] + * | HOURS [ MINUTES [ SECONDS ] ] + * it doesn't specifically, say, but this sort of implies that you can't have + * 70 seconds. + */ +public class Duration +{ + public int sign; // 1 or -1 + public int weeks; + public int days; + public int hours; + public int minutes; + public int seconds; + + public Duration() + { + sign = 1; + } + + /** + * Parse according to RFC2445 ss4.3.6. (It's actually a little loose with + * its parsing, for better or for worse) + */ + public void parse(String str) throws DateException + { + sign = 1; + weeks = 0; + days = 0; + hours = 0; + minutes = 0; + seconds = 0; + + int len = str.length(); + int index = 0; + char c; + + if (len < 1) { + return ; + } + + c = str.charAt(0); + if (c == '-') { + sign = -1; + index++; + } + else if (c == '+') { + index++; + } + + if (len < index) { + return ; + } + + c = str.charAt(index); + if (c != 'P') { + throw new DateException ( + "Duration.parse(str='" + str + "') expected 'P' at index=" + + index); + } + index++; + c = str.charAt(index); + if (c == 'T') { + index++; + } + + int n = 0; + for (; index < len; index++) { + c = str.charAt(index); + if (c >= '0' && c <= '9') { + n *= 10; + n += ((int)(c-'0')); + } + else if (c == 'W') { + weeks = n; + n = 0; + } + else if (c == 'H') { + hours = n; + n = 0; + } + else if (c == 'M') { + minutes = n; + n = 0; + } + else if (c == 'S') { + seconds = n; + n = 0; + } + else if (c == 'D') { + days = n; + n = 0; + } + else if (c == 'T') { + } + else { + throw new DateException ( + "Duration.parse(str='" + str + "') unexpected char '" + + c + "' at index=" + index); + } + } + } + + /** + * Add this to the calendar provided, in place, in the calendar. + */ + public void addTo(Calendar cal) + { + cal.add(Calendar.DAY_OF_MONTH, sign*weeks*7); + cal.add(Calendar.DAY_OF_MONTH, sign*days); + cal.add(Calendar.HOUR, sign*hours); + cal.add(Calendar.MINUTE, sign*minutes); + cal.add(Calendar.SECOND, sign*seconds); + } + + public long addTo(long dt) { + return dt + getMillis(); + } + + public long getMillis() { + long factor = 1000 * sign; + return factor * ((7*24*60*60*weeks) + + (24*60*60*days) + + (60*60*hours) + + (60*minutes) + + seconds); + } +} diff --git a/src/com/android/calendarcommon2/EventRecurrence.java b/src/com/android/calendarcommon2/EventRecurrence.java new file mode 100644 index 0000000..45fd12f --- /dev/null +++ b/src/com/android/calendarcommon2/EventRecurrence.java @@ -0,0 +1,907 @@ +/* + * Copyright (C) 2006 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.calendarcommon2; + +import android.text.TextUtils; +import android.text.format.Time; +import android.util.Log; +import android.util.TimeFormatException; + +import java.util.Calendar; +import java.util.HashMap; + +/** + * Event recurrence utility functions. + */ +public class EventRecurrence { + private static String TAG = "EventRecur"; + + public static final int SECONDLY = 1; + public static final int MINUTELY = 2; + public static final int HOURLY = 3; + public static final int DAILY = 4; + public static final int WEEKLY = 5; + public static final int MONTHLY = 6; + public static final int YEARLY = 7; + + public static final int SU = 0x00010000; + public static final int MO = 0x00020000; + public static final int TU = 0x00040000; + public static final int WE = 0x00080000; + public static final int TH = 0x00100000; + public static final int FR = 0x00200000; + public static final int SA = 0x00400000; + + public Time startDate; // set by setStartDate(), not parse() + + public int freq; // SECONDLY, MINUTELY, etc. + public String until; + public int count; + public int interval; + public int wkst; // SU, MO, TU, etc. + + /* lists with zero entries may be null references */ + public int[] bysecond; + public int bysecondCount; + public int[] byminute; + public int byminuteCount; + public int[] byhour; + public int byhourCount; + public int[] byday; + public int[] bydayNum; + public int bydayCount; + public int[] bymonthday; + public int bymonthdayCount; + public int[] byyearday; + public int byyeardayCount; + public int[] byweekno; + public int byweeknoCount; + public int[] bymonth; + public int bymonthCount; + public int[] bysetpos; + public int bysetposCount; + + /** maps a part string to a parser object */ + private static HashMap sParsePartMap; + static { + sParsePartMap = new HashMap(); + sParsePartMap.put("FREQ", new ParseFreq()); + sParsePartMap.put("UNTIL", new ParseUntil()); + sParsePartMap.put("COUNT", new ParseCount()); + sParsePartMap.put("INTERVAL", new ParseInterval()); + sParsePartMap.put("BYSECOND", new ParseBySecond()); + sParsePartMap.put("BYMINUTE", new ParseByMinute()); + sParsePartMap.put("BYHOUR", new ParseByHour()); + sParsePartMap.put("BYDAY", new ParseByDay()); + sParsePartMap.put("BYMONTHDAY", new ParseByMonthDay()); + sParsePartMap.put("BYYEARDAY", new ParseByYearDay()); + sParsePartMap.put("BYWEEKNO", new ParseByWeekNo()); + sParsePartMap.put("BYMONTH", new ParseByMonth()); + sParsePartMap.put("BYSETPOS", new ParseBySetPos()); + sParsePartMap.put("WKST", new ParseWkst()); + } + + /* values for bit vector that keeps track of what we have already seen */ + private static final int PARSED_FREQ = 1 << 0; + private static final int PARSED_UNTIL = 1 << 1; + private static final int PARSED_COUNT = 1 << 2; + private static final int PARSED_INTERVAL = 1 << 3; + private static final int PARSED_BYSECOND = 1 << 4; + private static final int PARSED_BYMINUTE = 1 << 5; + private static final int PARSED_BYHOUR = 1 << 6; + private static final int PARSED_BYDAY = 1 << 7; + private static final int PARSED_BYMONTHDAY = 1 << 8; + private static final int PARSED_BYYEARDAY = 1 << 9; + private static final int PARSED_BYWEEKNO = 1 << 10; + private static final int PARSED_BYMONTH = 1 << 11; + private static final int PARSED_BYSETPOS = 1 << 12; + private static final int PARSED_WKST = 1 << 13; + + /** maps a FREQ value to an integer constant */ + private static final HashMap sParseFreqMap = new HashMap(); + static { + sParseFreqMap.put("SECONDLY", SECONDLY); + sParseFreqMap.put("MINUTELY", MINUTELY); + sParseFreqMap.put("HOURLY", HOURLY); + sParseFreqMap.put("DAILY", DAILY); + sParseFreqMap.put("WEEKLY", WEEKLY); + sParseFreqMap.put("MONTHLY", MONTHLY); + sParseFreqMap.put("YEARLY", YEARLY); + } + + /** maps a two-character weekday string to an integer constant */ + private static final HashMap sParseWeekdayMap = new HashMap(); + static { + sParseWeekdayMap.put("SU", SU); + sParseWeekdayMap.put("MO", MO); + sParseWeekdayMap.put("TU", TU); + sParseWeekdayMap.put("WE", WE); + sParseWeekdayMap.put("TH", TH); + sParseWeekdayMap.put("FR", FR); + sParseWeekdayMap.put("SA", SA); + } + + /** If set, allow lower-case recurrence rule strings. Minor performance impact. */ + private static final boolean ALLOW_LOWER_CASE = true; + + /** If set, validate the value of UNTIL parts. Minor performance impact. */ + private static final boolean VALIDATE_UNTIL = false; + + /** If set, require that only one of {UNTIL,COUNT} is present. Breaks compat w/ old parser. */ + private static final boolean ONLY_ONE_UNTIL_COUNT = false; + + + /** + * Thrown when a recurrence string provided can not be parsed according + * to RFC2445. + */ + public static class InvalidFormatException extends RuntimeException { + InvalidFormatException(String s) { + super(s); + } + } + + + public void setStartDate(Time date) { + startDate = date; + } + + /** + * Converts one of the Calendar.SUNDAY constants to the SU, MO, etc. + * constants. btw, I think we should switch to those here too, to + * get rid of this function, if possible. + */ + public static int calendarDay2Day(int day) + { + switch (day) + { + case Calendar.SUNDAY: + return SU; + case Calendar.MONDAY: + return MO; + case Calendar.TUESDAY: + return TU; + case Calendar.WEDNESDAY: + return WE; + case Calendar.THURSDAY: + return TH; + case Calendar.FRIDAY: + return FR; + case Calendar.SATURDAY: + return SA; + default: + throw new RuntimeException("bad day of week: " + day); + } + } + + public static int timeDay2Day(int day) + { + switch (day) + { + case Time.SUNDAY: + return SU; + case Time.MONDAY: + return MO; + case Time.TUESDAY: + return TU; + case Time.WEDNESDAY: + return WE; + case Time.THURSDAY: + return TH; + case Time.FRIDAY: + return FR; + case Time.SATURDAY: + return SA; + default: + throw new RuntimeException("bad day of week: " + day); + } + } + public static int day2TimeDay(int day) + { + switch (day) + { + case SU: + return Time.SUNDAY; + case MO: + return Time.MONDAY; + case TU: + return Time.TUESDAY; + case WE: + return Time.WEDNESDAY; + case TH: + return Time.THURSDAY; + case FR: + return Time.FRIDAY; + case SA: + return Time.SATURDAY; + default: + throw new RuntimeException("bad day of week: " + day); + } + } + + /** + * Converts one of the SU, MO, etc. constants to the Calendar.SUNDAY + * constants. btw, I think we should switch to those here too, to + * get rid of this function, if possible. + */ + public static int day2CalendarDay(int day) + { + switch (day) + { + case SU: + return Calendar.SUNDAY; + case MO: + return Calendar.MONDAY; + case TU: + return Calendar.TUESDAY; + case WE: + return Calendar.WEDNESDAY; + case TH: + return Calendar.THURSDAY; + case FR: + return Calendar.FRIDAY; + case SA: + return Calendar.SATURDAY; + default: + throw new RuntimeException("bad day of week: " + day); + } + } + + /** + * Converts one of the internal day constants (SU, MO, etc.) to the + * two-letter string representing that constant. + * + * @param day one the internal constants SU, MO, etc. + * @return the two-letter string for the day ("SU", "MO", etc.) + * + * @throws IllegalArgumentException Thrown if the day argument is not one of + * the defined day constants. + */ + private static String day2String(int day) { + switch (day) { + case SU: + return "SU"; + case MO: + return "MO"; + case TU: + return "TU"; + case WE: + return "WE"; + case TH: + return "TH"; + case FR: + return "FR"; + case SA: + return "SA"; + default: + throw new IllegalArgumentException("bad day argument: " + day); + } + } + + private static void appendNumbers(StringBuilder s, String label, + int count, int[] values) + { + if (count > 0) { + s.append(label); + count--; + for (int i=0; i + * Negative days, e.g. "FREQ=MONTHLY;BYDAY=-1TU" (the last Tuesday of every month), + * will cause "false" to be returned. + *

+ * Rules that fire every week, such as "FREQ=MONTHLY;BYDAY=TU" (every Tuesday of every + * month) will cause "false" to be returned. (Note these are usually expressed as + * WEEKLY rules, and hence are uncommon.) + * + * @return true if this rule is of the appropriate form + */ + public boolean repeatsMonthlyOnDayCount() { + if (this.freq != MONTHLY) { + return false; + } + + if (bydayCount != 1 || bymonthdayCount != 0) { + return false; + } + + if (bydayNum[0] <= 0) { + return false; + } + + return true; + } + + /** + * Determines whether two integer arrays contain identical elements. + *

+ * The native implementation over-allocated the arrays (and may have stuff left over from + * a previous run), so we can't just check the arrays -- the separately-maintained count + * field also matters. We assume that a null array will have a count of zero, and that the + * array can hold as many elements as the associated count indicates. + *

+ * TODO: replace this with Arrays.equals() when the old parser goes away. + */ + private static boolean arraysEqual(int[] array1, int count1, int[] array2, int count2) { + if (count1 != count2) { + return false; + } + + for (int i = 0; i < count1; i++) { + if (array1[i] != array2[i]) + return false; + } + + return true; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!(obj instanceof EventRecurrence)) { + return false; + } + + EventRecurrence er = (EventRecurrence) obj; + return (startDate == null ? + er.startDate == null : Time.compare(startDate, er.startDate) == 0) && + freq == er.freq && + (until == null ? er.until == null : until.equals(er.until)) && + count == er.count && + interval == er.interval && + wkst == er.wkst && + arraysEqual(bysecond, bysecondCount, er.bysecond, er.bysecondCount) && + arraysEqual(byminute, byminuteCount, er.byminute, er.byminuteCount) && + arraysEqual(byhour, byhourCount, er.byhour, er.byhourCount) && + arraysEqual(byday, bydayCount, er.byday, er.bydayCount) && + arraysEqual(bydayNum, bydayCount, er.bydayNum, er.bydayCount) && + arraysEqual(bymonthday, bymonthdayCount, er.bymonthday, er.bymonthdayCount) && + arraysEqual(byyearday, byyeardayCount, er.byyearday, er.byyeardayCount) && + arraysEqual(byweekno, byweeknoCount, er.byweekno, er.byweeknoCount) && + arraysEqual(bymonth, bymonthCount, er.bymonth, er.bymonthCount) && + arraysEqual(bysetpos, bysetposCount, er.bysetpos, er.bysetposCount); + } + + @Override public int hashCode() { + // We overrode equals, so we must override hashCode(). Nobody seems to need this though. + throw new UnsupportedOperationException(); + } + + /** + * Resets parser-modified fields to their initial state. Does not alter startDate. + *

+ * The original parser always set all of the "count" fields, "wkst", and "until", + * essentially allowing the same object to be used multiple times by calling parse(). + * It's unclear whether this behavior was intentional. For now, be paranoid and + * preserve the existing behavior by resetting the fields. + *

+ * We don't need to touch the integer arrays; they will either be ignored or + * overwritten. The "startDate" field is not set by the parser, so we ignore it here. + */ + private void resetFields() { + until = null; + freq = count = interval = bysecondCount = byminuteCount = byhourCount = + bydayCount = bymonthdayCount = byyeardayCount = byweeknoCount = bymonthCount = + bysetposCount = 0; + } + + /** + * Parses an rfc2445 recurrence rule string into its component pieces. Attempting to parse + * malformed input will result in an EventRecurrence.InvalidFormatException. + * + * @param recur The recurrence rule to parse (in un-folded form). + */ + public void parse(String recur) { + /* + * From RFC 2445 section 4.3.10: + * + * recur = "FREQ"=freq *( + * ; either UNTIL or COUNT may appear in a 'recur', + * ; but UNTIL and COUNT MUST NOT occur in the same 'recur' + * + * ( ";" "UNTIL" "=" enddate ) / + * ( ";" "COUNT" "=" 1*DIGIT ) / + * + * ; the rest of these keywords are optional, + * ; but MUST NOT occur more than once + * + * ( ";" "INTERVAL" "=" 1*DIGIT ) / + * ( ";" "BYSECOND" "=" byseclist ) / + * ( ";" "BYMINUTE" "=" byminlist ) / + * ( ";" "BYHOUR" "=" byhrlist ) / + * ( ";" "BYDAY" "=" bywdaylist ) / + * ( ";" "BYMONTHDAY" "=" bymodaylist ) / + * ( ";" "BYYEARDAY" "=" byyrdaylist ) / + * ( ";" "BYWEEKNO" "=" bywknolist ) / + * ( ";" "BYMONTH" "=" bymolist ) / + * ( ";" "BYSETPOS" "=" bysplist ) / + * ( ";" "WKST" "=" weekday ) / + * ( ";" x-name "=" text ) + * ) + * + * The rule parts are not ordered in any particular sequence. + * + * Examples: + * FREQ=MONTHLY;INTERVAL=2;COUNT=10;BYDAY=1SU,-1SU + * FREQ=YEARLY;INTERVAL=4;BYMONTH=11;BYDAY=TU;BYMONTHDAY=2,3,4,5,6,7,8 + * + * Strategy: + * (1) Split the string at ';' boundaries to get an array of rule "parts". + * (2) For each part, find substrings for left/right sides of '=' (name/value). + * (3) Call a -specific parsing function to parse the into an + * output field. + * + * By keeping track of which names we've seen in a bit vector, we can verify the + * constraints indicated above (FREQ appears first, none of them appear more than once -- + * though x-[name] would require special treatment), and we have either UNTIL or COUNT + * but not both. + * + * In general, RFC 2445 property names (e.g. "FREQ") and enumerations ("TU") must + * be handled in a case-insensitive fashion, but case may be significant for other + * properties. We don't have any case-sensitive values in RRULE, except possibly + * for the custom "X-" properties, but we ignore those anyway. Thus, we can trivially + * convert the entire string to upper case and then use simple comparisons. + * + * Differences from previous version: + * - allows lower-case property and enumeration values [optional] + * - enforces that FREQ appears first + * - enforces that only one of UNTIL and COUNT may be specified + * - allows (but ignores) X-* parts + * - improved validation on various values (e.g. UNTIL timestamps) + * - error messages are more specific + * + * TODO: enforce additional constraints listed in RFC 5545, notably the "N/A" entries + * in section 3.3.10. For example, if FREQ=WEEKLY, we should reject a rule that + * includes a BYMONTHDAY part. + */ + + /* TODO: replace with "if (freq != 0) throw" if nothing requires this */ + resetFields(); + + int parseFlags = 0; + String[] parts; + if (ALLOW_LOWER_CASE) { + parts = recur.toUpperCase().split(";"); + } else { + parts = recur.split(";"); + } + for (String part : parts) { + // allow empty part (e.g., double semicolon ";;") + if (TextUtils.isEmpty(part)) { + continue; + } + int equalIndex = part.indexOf('='); + if (equalIndex <= 0) { + /* no '=' or no LHS */ + throw new InvalidFormatException("Missing LHS in " + part); + } + + String lhs = part.substring(0, equalIndex); + String rhs = part.substring(equalIndex + 1); + if (rhs.length() == 0) { + throw new InvalidFormatException("Missing RHS in " + part); + } + + /* + * In lieu of a "switch" statement that allows string arguments, we use a + * map from strings to parsing functions. + */ + PartParser parser = sParsePartMap.get(lhs); + if (parser == null) { + if (lhs.startsWith("X-")) { + //Log.d(TAG, "Ignoring custom part " + lhs); + continue; + } + throw new InvalidFormatException("Couldn't find parser for " + lhs); + } else { + int flag = parser.parsePart(rhs, this); + if ((parseFlags & flag) != 0) { + throw new InvalidFormatException("Part " + lhs + " was specified twice"); + } + parseFlags |= flag; + } + } + + // If not specified, week starts on Monday. + if ((parseFlags & PARSED_WKST) == 0) { + wkst = MO; + } + + // FREQ is mandatory. + if ((parseFlags & PARSED_FREQ) == 0) { + throw new InvalidFormatException("Must specify a FREQ value"); + } + + // Can't have both UNTIL and COUNT. + if ((parseFlags & (PARSED_UNTIL | PARSED_COUNT)) == (PARSED_UNTIL | PARSED_COUNT)) { + if (ONLY_ONE_UNTIL_COUNT) { + throw new InvalidFormatException("Must not specify both UNTIL and COUNT: " + recur); + } else { + Log.w(TAG, "Warning: rrule has both UNTIL and COUNT: " + recur); + } + } + } + + /** + * Base class for the RRULE part parsers. + */ + abstract static class PartParser { + /** + * Parses a single part. + * + * @param value The right-hand-side of the part. + * @param er The EventRecurrence into which the result is stored. + * @return A bit value indicating which part was parsed. + */ + public abstract int parsePart(String value, EventRecurrence er); + + /** + * Parses an integer, with range-checking. + * + * @param str The string to parse. + * @param minVal Minimum allowed value. + * @param maxVal Maximum allowed value. + * @param allowZero Is 0 allowed? + * @return The parsed value. + */ + public static int parseIntRange(String str, int minVal, int maxVal, boolean allowZero) { + try { + if (str.charAt(0) == '+') { + // Integer.parseInt does not allow a leading '+', so skip it manually. + str = str.substring(1); + } + int val = Integer.parseInt(str); + if (val < minVal || val > maxVal || (val == 0 && !allowZero)) { + throw new InvalidFormatException("Integer value out of range: " + str); + } + return val; + } catch (NumberFormatException nfe) { + throw new InvalidFormatException("Invalid integer value: " + str); + } + } + + /** + * Parses a comma-separated list of integers, with range-checking. + * + * @param listStr The string to parse. + * @param minVal Minimum allowed value. + * @param maxVal Maximum allowed value. + * @param allowZero Is 0 allowed? + * @return A new array with values, sized to hold the exact number of elements. + */ + public static int[] parseNumberList(String listStr, int minVal, int maxVal, + boolean allowZero) { + int[] values; + + if (listStr.indexOf(",") < 0) { + // Common case: only one entry, skip split() overhead. + values = new int[1]; + values[0] = parseIntRange(listStr, minVal, maxVal, allowZero); + } else { + String[] valueStrs = listStr.split(","); + int len = valueStrs.length; + values = new int[len]; + for (int i = 0; i < len; i++) { + values[i] = parseIntRange(valueStrs[i], minVal, maxVal, allowZero); + } + } + return values; + } + } + + /** parses FREQ={SECONDLY,MINUTELY,...} */ + private static class ParseFreq extends PartParser { + @Override public int parsePart(String value, EventRecurrence er) { + Integer freq = sParseFreqMap.get(value); + if (freq == null) { + throw new InvalidFormatException("Invalid FREQ value: " + value); + } + er.freq = freq; + return PARSED_FREQ; + } + } + /** parses UNTIL=enddate, e.g. "19970829T021400" */ + private static class ParseUntil extends PartParser { + @Override public int parsePart(String value, EventRecurrence er) { + if (VALIDATE_UNTIL) { + try { + // Parse the time to validate it. The result isn't retained. + Time until = new Time(); + until.parse(value); + } catch (TimeFormatException tfe) { + throw new InvalidFormatException("Invalid UNTIL value: " + value); + } + } + er.until = value; + return PARSED_UNTIL; + } + } + /** parses COUNT=[non-negative-integer] */ + private static class ParseCount extends PartParser { + @Override public int parsePart(String value, EventRecurrence er) { + er.count = parseIntRange(value, Integer.MIN_VALUE, Integer.MAX_VALUE, true); + if (er.count < 0) { + Log.d(TAG, "Invalid Count. Forcing COUNT to 1 from " + value); + er.count = 1; // invalid count. assume one time recurrence. + } + return PARSED_COUNT; + } + } + /** parses INTERVAL=[non-negative-integer] */ + private static class ParseInterval extends PartParser { + @Override public int parsePart(String value, EventRecurrence er) { + er.interval = parseIntRange(value, Integer.MIN_VALUE, Integer.MAX_VALUE, true); + if (er.interval < 1) { + Log.d(TAG, "Invalid Interval. Forcing INTERVAL to 1 from " + value); + er.interval = 1; + } + return PARSED_INTERVAL; + } + } + /** parses BYSECOND=byseclist */ + private static class ParseBySecond extends PartParser { + @Override public int parsePart(String value, EventRecurrence er) { + int[] bysecond = parseNumberList(value, 0, 59, true); + er.bysecond = bysecond; + er.bysecondCount = bysecond.length; + return PARSED_BYSECOND; + } + } + /** parses BYMINUTE=byminlist */ + private static class ParseByMinute extends PartParser { + @Override public int parsePart(String value, EventRecurrence er) { + int[] byminute = parseNumberList(value, 0, 59, true); + er.byminute = byminute; + er.byminuteCount = byminute.length; + return PARSED_BYMINUTE; + } + } + /** parses BYHOUR=byhrlist */ + private static class ParseByHour extends PartParser { + @Override public int parsePart(String value, EventRecurrence er) { + int[] byhour = parseNumberList(value, 0, 23, true); + er.byhour = byhour; + er.byhourCount = byhour.length; + return PARSED_BYHOUR; + } + } + /** parses BYDAY=bywdaylist, e.g. "1SU,-1SU" */ + private static class ParseByDay extends PartParser { + @Override public int parsePart(String value, EventRecurrence er) { + int[] byday; + int[] bydayNum; + int bydayCount; + + if (value.indexOf(",") < 0) { + /* only one entry, skip split() overhead */ + bydayCount = 1; + byday = new int[1]; + bydayNum = new int[1]; + parseWday(value, byday, bydayNum, 0); + } else { + String[] wdays = value.split(","); + int len = wdays.length; + bydayCount = len; + byday = new int[len]; + bydayNum = new int[len]; + for (int i = 0; i < len; i++) { + parseWday(wdays[i], byday, bydayNum, i); + } + } + er.byday = byday; + er.bydayNum = bydayNum; + er.bydayCount = bydayCount; + return PARSED_BYDAY; + } + + /** parses [int]weekday, putting the pieces into parallel array entries */ + private static void parseWday(String str, int[] byday, int[] bydayNum, int index) { + int wdayStrStart = str.length() - 2; + String wdayStr; + + if (wdayStrStart > 0) { + /* number is included; parse it out and advance to weekday */ + String numPart = str.substring(0, wdayStrStart); + int num = parseIntRange(numPart, -53, 53, false); + bydayNum[index] = num; + wdayStr = str.substring(wdayStrStart); + } else { + /* just the weekday string */ + wdayStr = str; + } + Integer wday = sParseWeekdayMap.get(wdayStr); + if (wday == null) { + throw new InvalidFormatException("Invalid BYDAY value: " + str); + } + byday[index] = wday; + } + } + /** parses BYMONTHDAY=bymodaylist */ + private static class ParseByMonthDay extends PartParser { + @Override public int parsePart(String value, EventRecurrence er) { + int[] bymonthday = parseNumberList(value, -31, 31, false); + er.bymonthday = bymonthday; + er.bymonthdayCount = bymonthday.length; + return PARSED_BYMONTHDAY; + } + } + /** parses BYYEARDAY=byyrdaylist */ + private static class ParseByYearDay extends PartParser { + @Override public int parsePart(String value, EventRecurrence er) { + int[] byyearday = parseNumberList(value, -366, 366, false); + er.byyearday = byyearday; + er.byyeardayCount = byyearday.length; + return PARSED_BYYEARDAY; + } + } + /** parses BYWEEKNO=bywknolist */ + private static class ParseByWeekNo extends PartParser { + @Override public int parsePart(String value, EventRecurrence er) { + int[] byweekno = parseNumberList(value, -53, 53, false); + er.byweekno = byweekno; + er.byweeknoCount = byweekno.length; + return PARSED_BYWEEKNO; + } + } + /** parses BYMONTH=bymolist */ + private static class ParseByMonth extends PartParser { + @Override public int parsePart(String value, EventRecurrence er) { + int[] bymonth = parseNumberList(value, 1, 12, false); + er.bymonth = bymonth; + er.bymonthCount = bymonth.length; + return PARSED_BYMONTH; + } + } + /** parses BYSETPOS=bysplist */ + private static class ParseBySetPos extends PartParser { + @Override public int parsePart(String value, EventRecurrence er) { + int[] bysetpos = parseNumberList(value, Integer.MIN_VALUE, Integer.MAX_VALUE, true); + er.bysetpos = bysetpos; + er.bysetposCount = bysetpos.length; + return PARSED_BYSETPOS; + } + } + /** parses WKST={SU,MO,...} */ + private static class ParseWkst extends PartParser { + @Override public int parsePart(String value, EventRecurrence er) { + Integer wkst = sParseWeekdayMap.get(value); + if (wkst == null) { + throw new InvalidFormatException("Invalid WKST value: " + value); + } + er.wkst = wkst; + return PARSED_WKST; + } + } +} diff --git a/src/com/android/calendarcommon2/ICalendar.java b/src/com/android/calendarcommon2/ICalendar.java new file mode 100644 index 0000000..1bf33fe --- /dev/null +++ b/src/com/android/calendarcommon2/ICalendar.java @@ -0,0 +1,660 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.calendarcommon2; + +import android.util.Log; + +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; +import java.util.ArrayList; + +/** + * Parses RFC 2445 iCalendar objects. + */ +public class ICalendar { + + private static final String TAG = "Sync"; + + // TODO: keep track of VEVENT, VTODO, VJOURNAL, VFREEBUSY, VTIMEZONE, VALARM + // components, by type field or by subclass? subclass would allow us to + // enforce grammars. + + /** + * Exception thrown when an iCalendar object has invalid syntax. + */ + public static class FormatException extends Exception { + public FormatException() { + super(); + } + + public FormatException(String msg) { + super(msg); + } + + public FormatException(String msg, Throwable cause) { + super(msg, cause); + } + } + + /** + * A component within an iCalendar (VEVENT, VTODO, VJOURNAL, VFEEBUSY, + * VTIMEZONE, VALARM). + */ + public static class Component { + + // components + static final String BEGIN = "BEGIN"; + static final String END = "END"; + private static final String NEWLINE = "\n"; + public static final String VCALENDAR = "VCALENDAR"; + public static final String VEVENT = "VEVENT"; + public static final String VTODO = "VTODO"; + public static final String VJOURNAL = "VJOURNAL"; + public static final String VFREEBUSY = "VFREEBUSY"; + public static final String VTIMEZONE = "VTIMEZONE"; + public static final String VALARM = "VALARM"; + + private final String mName; + private final Component mParent; // see if we can get rid of this + private LinkedList mChildren = null; + private final LinkedHashMap> mPropsMap = + new LinkedHashMap>(); + + /** + * Creates a new component with the provided name. + * @param name The name of the component. + */ + public Component(String name, Component parent) { + mName = name; + mParent = parent; + } + + /** + * Returns the name of the component. + * @return The name of the component. + */ + public String getName() { + return mName; + } + + /** + * Returns the parent of this component. + * @return The parent of this component. + */ + public Component getParent() { + return mParent; + } + + /** + * Helper that lazily gets/creates the list of children. + * @return The list of children. + */ + protected LinkedList getOrCreateChildren() { + if (mChildren == null) { + mChildren = new LinkedList(); + } + return mChildren; + } + + /** + * Adds a child component to this component. + * @param child The child component. + */ + public void addChild(Component child) { + getOrCreateChildren().add(child); + } + + /** + * Returns a list of the Component children of this component. May be + * null, if there are no children. + * + * @return A list of the children. + */ + public List getComponents() { + return mChildren; + } + + /** + * Adds a Property to this component. + * @param prop + */ + public void addProperty(Property prop) { + String name= prop.getName(); + ArrayList props = mPropsMap.get(name); + if (props == null) { + props = new ArrayList(); + mPropsMap.put(name, props); + } + props.add(prop); + } + + /** + * Returns a set of the property names within this component. + * @return A set of property names within this component. + */ + public Set getPropertyNames() { + return mPropsMap.keySet(); + } + + /** + * Returns a list of properties with the specified name. Returns null + * if there are no such properties. + * @param name The name of the property that should be returned. + * @return A list of properties with the requested name. + */ + public List getProperties(String name) { + return mPropsMap.get(name); + } + + /** + * Returns the first property with the specified name. Returns null + * if there is no such property. + * @param name The name of the property that should be returned. + * @return The first property with the specified name. + */ + public Property getFirstProperty(String name) { + List props = mPropsMap.get(name); + if (props == null || props.size() == 0) { + return null; + } + return props.get(0); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + toString(sb); + sb.append(NEWLINE); + return sb.toString(); + } + + /** + * Helper method that appends this component to a StringBuilder. The + * caller is responsible for appending a newline at the end of the + * component. + */ + public void toString(StringBuilder sb) { + sb.append(BEGIN); + sb.append(":"); + sb.append(mName); + sb.append(NEWLINE); + + // append the properties + for (String propertyName : getPropertyNames()) { + for (Property property : getProperties(propertyName)) { + property.toString(sb); + sb.append(NEWLINE); + } + } + + // append the sub-components + if (mChildren != null) { + for (Component component : mChildren) { + component.toString(sb); + sb.append(NEWLINE); + } + } + + sb.append(END); + sb.append(":"); + sb.append(mName); + } + } + + /** + * A property within an iCalendar component (e.g., DTSTART, DTEND, etc., + * within a VEVENT). + */ + public static class Property { + // properties + // TODO: do we want to list these here? the complete list is long. + public static final String DTSTART = "DTSTART"; + public static final String DTEND = "DTEND"; + public static final String DURATION = "DURATION"; + public static final String RRULE = "RRULE"; + public static final String RDATE = "RDATE"; + public static final String EXRULE = "EXRULE"; + public static final String EXDATE = "EXDATE"; + // ... need to add more. + + private final String mName; + private LinkedHashMap> mParamsMap = + new LinkedHashMap>(); + private String mValue; // TODO: make this final? + + /** + * Creates a new property with the provided name. + * @param name The name of the property. + */ + public Property(String name) { + mName = name; + } + + /** + * Creates a new property with the provided name and value. + * @param name The name of the property. + * @param value The value of the property. + */ + public Property(String name, String value) { + mName = name; + mValue = value; + } + + /** + * Returns the name of the property. + * @return The name of the property. + */ + public String getName() { + return mName; + } + + /** + * Returns the value of this property. + * @return The value of this property. + */ + public String getValue() { + return mValue; + } + + /** + * Sets the value of this property. + * @param value The desired value for this property. + */ + public void setValue(String value) { + mValue = value; + } + + /** + * Adds a {@link Parameter} to this property. + * @param param The parameter that should be added. + */ + public void addParameter(Parameter param) { + ArrayList params = mParamsMap.get(param.name); + if (params == null) { + params = new ArrayList(); + mParamsMap.put(param.name, params); + } + params.add(param); + } + + /** + * Returns the set of parameter names for this property. + * @return The set of parameter names for this property. + */ + public Set getParameterNames() { + return mParamsMap.keySet(); + } + + /** + * Returns the list of parameters with the specified name. May return + * null if there are no such parameters. + * @param name The name of the parameters that should be returned. + * @return The list of parameters with the specified name. + */ + public List getParameters(String name) { + return mParamsMap.get(name); + } + + /** + * Returns the first parameter with the specified name. May return + * nll if there is no such parameter. + * @param name The name of the parameter that should be returned. + * @return The first parameter with the specified name. + */ + public Parameter getFirstParameter(String name) { + ArrayList params = mParamsMap.get(name); + if (params == null || params.size() == 0) { + return null; + } + return params.get(0); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + toString(sb); + return sb.toString(); + } + + /** + * Helper method that appends this property to a StringBuilder. The + * caller is responsible for appending a newline after this property. + */ + public void toString(StringBuilder sb) { + sb.append(mName); + Set parameterNames = getParameterNames(); + for (String parameterName : parameterNames) { + for (Parameter param : getParameters(parameterName)) { + sb.append(";"); + param.toString(sb); + } + } + sb.append(":"); + sb.append(mValue); + } + } + + /** + * A parameter defined for an iCalendar property. + */ + // TODO: make this a proper class rather than a struct? + public static class Parameter { + public String name; + public String value; + + /** + * Creates a new empty parameter. + */ + public Parameter() { + } + + /** + * Creates a new parameter with the specified name and value. + * @param name The name of the parameter. + * @param value The value of the parameter. + */ + public Parameter(String name, String value) { + this.name = name; + this.value = value; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + toString(sb); + return sb.toString(); + } + + /** + * Helper method that appends this parameter to a StringBuilder. + */ + public void toString(StringBuilder sb) { + sb.append(name); + sb.append("="); + sb.append(value); + } + } + + private static final class ParserState { + // public int lineNumber = 0; + public String line; // TODO: just point to original text + public int index; + } + + // use factory method + private ICalendar() { + } + + // TODO: get rid of this -- handle all of the parsing in one pass through + // the text. + private static String normalizeText(String text) { + // it's supposed to be \r\n, but not everyone does that + text = text.replaceAll("\r\n", "\n"); + text = text.replaceAll("\r", "\n"); + + // we deal with line folding, by replacing all "\n " strings + // with nothing. The RFC specifies "\r\n " to be folded, but + // we handle "\n " and "\r " too because we can get those. + text = text.replaceAll("\n ", ""); + + return text; + } + + /** + * Parses text into an iCalendar component. Parses into the provided + * component, if not null, or parses into a new component. In the latter + * case, expects a BEGIN as the first line. Returns the provided or newly + * created top-level component. + */ + // TODO: use an index into the text, so we can make this a recursive + // function? + private static Component parseComponentImpl(Component component, + String text) + throws FormatException { + Component current = component; + ParserState state = new ParserState(); + state.index = 0; + + // split into lines + String[] lines = text.split("\n"); + + // each line is of the format: + // name *(";" param) ":" value + for (String line : lines) { + try { + current = parseLine(line, state, current); + // if the provided component was null, we will return the root + // NOTE: in this case, if the first line is not a BEGIN, a + // FormatException will get thrown. + if (component == null) { + component = current; + } + } catch (FormatException fe) { + if (false) { + Log.v(TAG, "Cannot parse " + line, fe); + } + // for now, we ignore the parse error. Google Calendar seems + // to be emitting some misformatted iCalendar objects. + } + continue; + } + return component; + } + + /** + * Parses a line into the provided component. Creates a new component if + * the line is a BEGIN, adding the newly created component to the provided + * parent. Returns whatever component is the current one (to which new + * properties will be added) in the parse. + */ + private static Component parseLine(String line, ParserState state, + Component component) + throws FormatException { + state.line = line; + int len = state.line.length(); + + // grab the name + char c = 0; + for (state.index = 0; state.index < len; ++state.index) { + c = line.charAt(state.index); + if (c == ';' || c == ':') { + break; + } + } + String name = line.substring(0, state.index); + + if (component == null) { + if (!Component.BEGIN.equals(name)) { + throw new FormatException("Expected BEGIN"); + } + } + + Property property; + if (Component.BEGIN.equals(name)) { + // start a new component + String componentName = extractValue(state); + Component child = new Component(componentName, component); + if (component != null) { + component.addChild(child); + } + return child; + } else if (Component.END.equals(name)) { + // finish the current component + String componentName = extractValue(state); + if (component == null || + !componentName.equals(component.getName())) { + throw new FormatException("Unexpected END " + componentName); + } + return component.getParent(); + } else { + property = new Property(name); + } + + if (c == ';') { + Parameter parameter = null; + while ((parameter = extractParameter(state)) != null) { + property.addParameter(parameter); + } + } + String value = extractValue(state); + property.setValue(value); + component.addProperty(property); + return component; + } + + /** + * Extracts the value ":..." on the current line. The first character must + * be a ':'. + */ + private static String extractValue(ParserState state) + throws FormatException { + String line = state.line; + if (state.index >= line.length() || line.charAt(state.index) != ':') { + throw new FormatException("Expected ':' before end of line in " + + line); + } + String value = line.substring(state.index + 1); + state.index = line.length() - 1; + return value; + } + + /** + * Extracts the next parameter from the line, if any. If there are no more + * parameters, returns null. + */ + private static Parameter extractParameter(ParserState state) + throws FormatException { + String text = state.line; + int len = text.length(); + Parameter parameter = null; + int startIndex = -1; + int equalIndex = -1; + while (state.index < len) { + char c = text.charAt(state.index); + if (c == ':') { + if (parameter != null) { + if (equalIndex == -1) { + throw new FormatException("Expected '=' within " + + "parameter in " + text); + } + parameter.value = text.substring(equalIndex + 1, + state.index); + } + return parameter; // may be null + } else if (c == ';') { + if (parameter != null) { + if (equalIndex == -1) { + throw new FormatException("Expected '=' within " + + "parameter in " + text); + } + parameter.value = text.substring(equalIndex + 1, + state.index); + return parameter; + } else { + parameter = new Parameter(); + startIndex = state.index; + } + } else if (c == '=') { + equalIndex = state.index; + if ((parameter == null) || (startIndex == -1)) { + throw new FormatException("Expected ';' before '=' in " + + text); + } + parameter.name = text.substring(startIndex + 1, equalIndex); + } else if (c == '"') { + if (parameter == null) { + throw new FormatException("Expected parameter before '\"' in " + text); + } + if (equalIndex == -1) { + throw new FormatException("Expected '=' within parameter in " + text); + } + if (state.index > equalIndex + 1) { + throw new FormatException("Parameter value cannot contain a '\"' in " + text); + } + final int endQuote = text.indexOf('"', state.index + 1); + if (endQuote < 0) { + throw new FormatException("Expected closing '\"' in " + text); + } + parameter.value = text.substring(state.index + 1, endQuote); + state.index = endQuote + 1; + return parameter; + } + ++state.index; + } + throw new FormatException("Expected ':' before end of line in " + text); + } + + /** + * Parses the provided text into an iCalendar object. The top-level + * component must be of type VCALENDAR. + * @param text The text to be parsed. + * @return The top-level VCALENDAR component. + * @throws FormatException Thrown if the text could not be parsed into an + * iCalendar VCALENDAR object. + */ + public static Component parseCalendar(String text) throws FormatException { + Component calendar = parseComponent(null, text); + if (calendar == null || !Component.VCALENDAR.equals(calendar.getName())) { + throw new FormatException("Expected " + Component.VCALENDAR); + } + return calendar; + } + + /** + * Parses the provided text into an iCalendar event. The top-level + * component must be of type VEVENT. + * @param text The text to be parsed. + * @return The top-level VEVENT component. + * @throws FormatException Thrown if the text could not be parsed into an + * iCalendar VEVENT. + */ + public static Component parseEvent(String text) throws FormatException { + Component event = parseComponent(null, text); + if (event == null || !Component.VEVENT.equals(event.getName())) { + throw new FormatException("Expected " + Component.VEVENT); + } + return event; + } + + /** + * Parses the provided text into an iCalendar component. + * @param text The text to be parsed. + * @return The top-level component. + * @throws FormatException Thrown if the text could not be parsed into an + * iCalendar component. + */ + public static Component parseComponent(String text) throws FormatException { + return parseComponent(null, text); + } + + /** + * Parses the provided text, adding to the provided component. + * @param component The component to which the parsed iCalendar data should + * be added. + * @param text The text to be parsed. + * @return The top-level component. + * @throws FormatException Thrown if the text could not be parsed as an + * iCalendar object. + */ + public static Component parseComponent(Component component, String text) + throws FormatException { + text = normalizeText(text); + return parseComponentImpl(component, text); + } +} diff --git a/src/com/android/calendarcommon2/RecurrenceProcessor.java b/src/com/android/calendarcommon2/RecurrenceProcessor.java new file mode 100644 index 0000000..641a161 --- /dev/null +++ b/src/com/android/calendarcommon2/RecurrenceProcessor.java @@ -0,0 +1,1314 @@ +/* //device/content/providers/pim/RecurrenceProcessor.java +** +** Copyright 2006, The Android Open Source Project +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +*/ + +package com.android.calendarcommon2; + +import android.text.format.Time; +import android.util.Log; + +import java.util.TreeSet; + +public class RecurrenceProcessor +{ + // these are created once and reused. + private Time mIterator = new Time(Time.TIMEZONE_UTC); + private Time mUntil = new Time(Time.TIMEZONE_UTC); + private StringBuilder mStringBuilder = new StringBuilder(); + private Time mGenerated = new Time(Time.TIMEZONE_UTC); + private DaySet mDays = new DaySet(false); + // Give up after this many loops. This is roughly 1 second of expansion. + private static final int MAX_ALLOWED_ITERATIONS = 2000; + + public RecurrenceProcessor() + { + } + + private static final String TAG = "RecurrenceProcessor"; + + private static final boolean SPEW = false; + + /** + * Returns the time (millis since epoch) of the last occurrence, + * or -1 if the event repeats forever. If there are no occurrences + * (because the exrule or exdates cancel all the occurrences) and the + * event does not repeat forever, then 0 is returned. + * + * This computes a conservative estimate of the last occurrence. That is, + * the time of the actual last occurrence might be earlier than the time + * returned by this method. + * + * @param dtstart the time of the first occurrence + * @param recur the recurrence + * @return an estimate of the time (in UTC milliseconds) of the last + * occurrence, which may be greater than the actual last occurrence + * @throws DateException + */ + public long getLastOccurence(Time dtstart, + RecurrenceSet recur) throws DateException { + return getLastOccurence(dtstart, null /* no limit */, recur); + } + + /** + * Returns the time (millis since epoch) of the last occurrence, + * or -1 if the event repeats forever. If there are no occurrences + * (because the exrule or exdates cancel all the occurrences) and the + * event does not repeat forever, then 0 is returned. + * + * This computes a conservative estimate of the last occurrence. That is, + * the time of the actual last occurrence might be earlier than the time + * returned by this method. + * + * @param dtstart the time of the first occurrence + * @param maxtime the max possible time of the last occurrence. null means no limit + * @param recur the recurrence + * @return an estimate of the time (in UTC milliseconds) of the last + * occurrence, which may be greater than the actual last occurrence + * @throws DateException + */ + public long getLastOccurence(Time dtstart, Time maxtime, + RecurrenceSet recur) throws DateException { + long lastTime = -1; + boolean hasCount = false; + + // first see if there are any "until"s specified. if so, use the latest + // until / rdate. + if (recur.rrules != null) { + for (EventRecurrence rrule : recur.rrules) { + if (rrule.count != 0) { + hasCount = true; + } else if (rrule.until != null) { + // according to RFC 2445, until must be in UTC. + mIterator.parse(rrule.until); + long untilTime = mIterator.toMillis(false /* use isDst */); + if (untilTime > lastTime) { + lastTime = untilTime; + } + } + } + if (lastTime != -1 && recur.rdates != null) { + for (long dt : recur.rdates) { + if (dt > lastTime) { + lastTime = dt; + } + } + } + + // If there were only "until"s and no "count"s, then return the + // last "until" date or "rdate". + if (lastTime != -1 && !hasCount) { + return lastTime; + } + } else if (recur.rdates != null && + recur.exrules == null && recur.exdates == null) { + // if there are only rdates, we can just pick the last one. + for (long dt : recur.rdates) { + if (dt > lastTime) { + lastTime = dt; + } + } + return lastTime; + } + + // Expand the complete recurrence if there were any counts specified, + // or if there were rdates specified. + if (hasCount || recur.rdates != null || maxtime != null) { + // The expansion might not contain any dates if the exrule or + // exdates cancel all the generated dates. + long[] dates = expand(dtstart, recur, + dtstart.toMillis(false /* use isDst */) /* range start */, + (maxtime != null) ? + maxtime.toMillis(false /* use isDst */) : -1 /* range end */); + + // The expansion might not contain any dates if exrule or exdates + // cancel all the generated dates. + if (dates.length == 0) { + return 0; + } + return dates[dates.length - 1]; + } + return -1; + } + + /** + * a -- list of values + * N -- number of values to use in a + * v -- value to check for + */ + private static boolean listContains(int[] a, int N, int v) + { + for (int i=0; i 0) { + if (w == v) { + return true; + } + } else { + max += w; // w is negative + if (max == v) { + return true; + } + } + } + return false; + } + + /** + * Filter out the ones for events whose BYxxx rule is for + * a period greater than or equal to the period of the FREQ. + * + * Returns 0 if the event should not be filtered out + * Returns something else (a rule number which is useful for debugging) + * if the event should not be returned + */ + private static int filter(EventRecurrence r, Time iterator) + { + boolean found; + int freq = r.freq; + + if (EventRecurrence.MONTHLY >= freq) { + // BYMONTH + if (r.bymonthCount > 0) { + found = listContains(r.bymonth, r.bymonthCount, + iterator.month + 1); + if (!found) { + return 1; + } + } + } + if (EventRecurrence.WEEKLY >= freq) { + // BYWEEK -- this is just a guess. I wonder how many events + // acutally use BYWEEKNO. + if (r.byweeknoCount > 0) { + found = listContains(r.byweekno, r.byweeknoCount, + iterator.getWeekNumber(), + iterator.getActualMaximum(Time.WEEK_NUM)); + if (!found) { + return 2; + } + } + } + if (EventRecurrence.DAILY >= freq) { + // BYYEARDAY + if (r.byyeardayCount > 0) { + found = listContains(r.byyearday, r.byyeardayCount, + iterator.yearDay, iterator.getActualMaximum(Time.YEAR_DAY)); + if (!found) { + return 3; + } + } + // BYMONTHDAY + if (r.bymonthdayCount > 0 ) { + found = listContains(r.bymonthday, r.bymonthdayCount, + iterator.monthDay, + iterator.getActualMaximum(Time.MONTH_DAY)); + if (!found) { + return 4; + } + } + // BYDAY -- when filtering, we ignore the number field, because it + // only is meaningful when creating more events. +byday: + if (r.bydayCount > 0) { + int a[] = r.byday; + int N = r.bydayCount; + int v = EventRecurrence.timeDay2Day(iterator.weekDay); + for (int i=0; i= freq) { + // BYHOUR + found = listContains(r.byhour, r.byhourCount, + iterator.hour, + iterator.getActualMaximum(Time.HOUR)); + if (!found) { + return 6; + } + } + if (EventRecurrence.MINUTELY >= freq) { + // BYMINUTE + found = listContains(r.byminute, r.byminuteCount, + iterator.minute, + iterator.getActualMaximum(Time.MINUTE)); + if (!found) { + return 7; + } + } + if (EventRecurrence.SECONDLY >= freq) { + // BYSECOND + found = listContains(r.bysecond, r.bysecondCount, + iterator.second, + iterator.getActualMaximum(Time.SECOND)); + if (!found) { + return 8; + } + } + + if (r.bysetposCount > 0) { +bysetpos: + // BYSETPOS - we only handle rules like FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1 + if (freq == EventRecurrence.MONTHLY && r.bydayCount > 0) { + // Check for stuff like BYDAY=1TU + for (int i = r.bydayCount - 1; i >= 0; i--) { + if (r.bydayNum[i] != 0) { + if (Log.isLoggable(TAG, Log.VERBOSE)) { + Log.v(TAG, "BYSETPOS not supported with these rules: " + r); + } + break bysetpos; + } + } + if (!filterMonthlySetPos(r, iterator)) { + // Not allowed, filter it out. + return 9; + } + } else { + if (Log.isLoggable(TAG, Log.VERBOSE)) { + Log.v(TAG, "BYSETPOS not supported with these rules: " + r); + } + } + // BYSETPOS was defined but we don't know how to handle it. Do no filtering based + // on it. + } + + // if we got to here, we didn't filter it out + return 0; + } + + /** + * Filters out instances that don't match the BYSETPOS clause of a monthly recurrence rule. + * This is an awkward and inefficient way to go about it. + * + * @returns true if this instance should be kept + */ + private static boolean filterMonthlySetPos(EventRecurrence r, Time instance) { + /* + * Compute the day of the week for the first day of the month. "instance" has a + * day number and a DotW, so we compute the DotW of the 1st from that. Note DotW + * is 0-6, where 0=SUNDAY. + * + * The basic calculation is to take the instance's "day of the week" number, subtract + * (day of the month - 1) mod 7, and then make sure it's positive. We can simplify + * that with some algebra. + */ + int dotw = (instance.weekDay - instance.monthDay + 36) % 7; + + /* + * The byday[] values are specified as bits, so we can just OR them all + * together. + */ + int bydayMask = 0; + for (int i = 0; i < r.bydayCount; i++) { + bydayMask |= r.byday[i]; + } + + /* + * Generate a set according to the BYDAY rules. For each day of the month, determine + * if its day of the week is included. If so, append it to the day set. + */ + int maxDay = instance.getActualMaximum(Time.MONTH_DAY); + int daySet[] = new int[maxDay]; + int daySetLength = 0; + + for (int md = 1; md <= maxDay; md++) { + // For each month day, see if it's part of the set. (This makes some assumptions + // about the exact form of the DotW constants.) + int dayBit = EventRecurrence.SU << dotw; + if ((bydayMask & dayBit) != 0) { + daySet[daySetLength++] = md; + } + + dotw++; + if (dotw == 7) + dotw = 0; + } + + /* + * Now walk through the BYSETPOS list and see if the instance is equal to any of the + * specified daySet entries. + */ + for (int i = r.bysetposCount - 1; i >= 0; i--) { + int index = r.bysetpos[i]; + if (index > 0) { + if (index > daySetLength) { + continue; // out of range + } + if (daySet[index-1] == instance.monthDay) { + return true; + } + } else if (index < 0) { + if (daySetLength + index < 0) { + continue; // out of range + } + if (daySet[daySetLength + index] == instance.monthDay) { + return true; + } + } else { + // should have been caught by parser + throw new RuntimeException("invalid bysetpos value"); + } + } + + return false; + } + + + private static final int USE_ITERATOR = 0; + private static final int USE_BYLIST = 1; + + /** + * Return whether we should make this list from the BYxxx list or + * from the component of the iterator. + */ + int generateByList(int count, int freq, int byFreq) + { + if (byFreq >= freq) { + return USE_ITERATOR; + } else { + if (count == 0) { + return USE_ITERATOR; + } else { + return USE_BYLIST; + } + } + } + + private static boolean useBYX(int freq, int freqConstant, int count) + { + return freq > freqConstant && count > 0; + } + + public static class DaySet + { + public DaySet(boolean zulu) + { + mTime = new Time(Time.TIMEZONE_UTC); + } + + void setRecurrence(EventRecurrence r) + { + mYear = 0; + mMonth = -1; + mR = r; + } + + boolean get(Time iterator, int day) + { + int realYear = iterator.year; + int realMonth = iterator.month; + + Time t = null; + + if (SPEW) { + Log.i(TAG, "get called with iterator=" + iterator + + " " + iterator.month + + "/" + iterator.monthDay + + "/" + iterator.year + " day=" + day); + } + if (day < 1 || day > 28) { + // if might be past the end of the month, we need to normalize it + t = mTime; + t.set(day, realMonth, realYear); + unsafeNormalize(t); + realYear = t.year; + realMonth = t.month; + day = t.monthDay; + if (SPEW) { + Log.i(TAG, "normalized t=" + t + " " + t.month + + "/" + t.monthDay + + "/" + t.year); + } + } + + /* + if (true || SPEW) { + Log.i(TAG, "set t=" + t + " " + realMonth + "/" + day + "/" + realYear); + } + */ + if (realYear != mYear || realMonth != mMonth) { + if (t == null) { + t = mTime; + t.set(day, realMonth, realYear); + unsafeNormalize(t); + if (SPEW) { + Log.i(TAG, "set t=" + t + " " + t.month + + "/" + t.monthDay + + "/" + t.year + + " realMonth=" + realMonth + " mMonth=" + mMonth); + } + } + mYear = realYear; + mMonth = realMonth; + mDays = generateDaysList(t, mR); + if (SPEW) { + Log.i(TAG, "generated days list"); + } + } + return (mDays & (1< DAILY. Otherwise, we should be + * processing the BYDAY, BYMONTHDAY, etc. as filters instead. + * + * monthOffset may be -1, 0 or 1 + */ + private static int generateDaysList(Time generated, EventRecurrence r) + { + int days = 0; + + int i, count, v; + int[] byday, bydayNum, bymonthday; + int j, lastDayThisMonth; + int first; // Time.SUNDAY, etc + int k; + + lastDayThisMonth = generated.getActualMaximum(Time.MONTH_DAY); + + // BYDAY + count = r.bydayCount; + if (count > 0) { + // calculate the day of week for the first of this month (first) + j = generated.monthDay; + while (j >= 8) { + j -= 7; + } + first = generated.weekDay; + if (first >= j) { + first = first - j + 1; + } else { + first = first - j + 8; + } + + // What to do if the event is weekly: + // This isn't ideal, but we'll generate a month's worth of events + // and the code that calls this will only use the ones that matter + // for the current week. + byday = r.byday; + bydayNum = r.bydayNum; + for (i=0; i 0) { + // v is positive, count from the beginning of the month + // -1 b/c the first one should add 0 + j += 7*(v-1); + if (j <= lastDayThisMonth) { + if (SPEW) Log.i(TAG, "setting " + j + " for rule " + + v + "/" + EventRecurrence.day2TimeDay(byday[i])); + // if it's impossible, we drop it + days |= 1 << j; + } + } + else { + // v is negative, count from the end of the month + // find the last one + for (; j<=lastDayThisMonth; j+=7) { + } + // v is negative + // should do +1 b/c the last one should add 0, but we also + // skipped the j -= 7 b/c the loop to find the last one + // overshot by one week + j += 7*v; + if (j >= 1) { + if (SPEW) Log.i(TAG, "setting " + j + " for rule " + + v + "/" + EventRecurrence.day2TimeDay(byday[i])); + days |= 1 << j; + } + } + } + } + + // BYMONTHDAY + // Q: What happens if we have BYMONTHDAY and BYDAY? + // A: I didn't see it in the spec, so in lieu of that, we'll + // intersect the two. That seems reasonable to me. + if (r.freq > EventRecurrence.WEEKLY) { + count = r.bymonthdayCount; + if (count != 0) { + bymonthday = r.bymonthday; + if (r.bydayCount == 0) { + for (i=0; i= 0) { + days |= 1 << v; + } else { + j = lastDayThisMonth + v + 1; // v is negative + if (j >= 1 && j <= lastDayThisMonth) { + days |= 1 << j; + } + } + } + } else { + // This is O(lastDayThisMonth*count), which is really + // O(count) with a decent sized constant. + for (j=1; j<=lastDayThisMonth; j++) { + next_day : { + if ((days&(1< dtSet = new TreeSet(); + + if (recur.rrules != null) { + for (EventRecurrence rrule : recur.rrules) { + expand(dtstart, rrule, rangeStartDateValue, + rangeEndDateValue, true /* add */, dtSet); + } + } + if (recur.rdates != null) { + for (long dt : recur.rdates) { + // The dates are stored as milliseconds. We need to convert + // them to year/month/day values in the local timezone. + mIterator.set(dt); + long dtvalue = normDateTimeComparisonValue(mIterator); + dtSet.add(dtvalue); + } + } + if (recur.exrules != null) { + for (EventRecurrence exrule : recur.exrules) { + expand(dtstart, exrule, rangeStartDateValue, + rangeEndDateValue, false /* remove */, dtSet); + } + } + if (recur.exdates != null) { + for (long dt : recur.exdates) { + // The dates are stored as milliseconds. We need to convert + // them to year/month/day values in the local timezone. + mIterator.set(dt); + long dtvalue = normDateTimeComparisonValue(mIterator); + dtSet.remove(dtvalue); + } + } + if (dtSet.isEmpty()) { + // this can happen if the recurrence does not occur within the + // expansion window. + return new long[0]; + } + + // The values in dtSet are represented in a special form that is useful + // for fast comparisons and that is easy to generate from year/month/day + // values. We need to convert these to UTC milliseconds and also to + // ensure that the dates are valid. + int len = dtSet.size(); + long[] dates = new long[len]; + int i = 0; + for (Long val: dtSet) { + setTimeFromLongValue(mIterator, val); + dates[i++] = mIterator.toMillis(true /* ignore isDst */); + } + return dates; + } + + /** + * Run the recurrence algorithm. Processes events defined in the local + * timezone of the event. Return a list of iCalendar DATETIME + * strings containing the start date/times of the occurrences; the output + * times are defined in the local timezone of the event. + * + * If you want all of the events, pass Long.MAX_VALUE for rangeEndDateValue. If you pass + * Long.MAX_VALUE for rangeEnd, and the event doesn't have a COUNT or UNTIL field, + * you'll get a DateException. + * + * @param dtstart the dtstart date as defined in RFC2445. This + * {@link Time} should be in the timezone of the event. + * @param r the parsed recurrence, as defiend in RFC2445 + * @param rangeStartDateValue the first date-time you care about, inclusive + * @param rangeEndDateValue the last date-time you care about, not inclusive (so + * if you care about everything up through and including + * Dec 22 1995, set last to Dec 23, 1995 00:00:00 + * @param add Whether or not we should add to out, or remove from out. + * @param out the TreeSet you'd like to fill with the events + * @throws DateException + * @throws android.util.TimeFormatException if r cannot be parsed. + */ + public void expand(Time dtstart, + EventRecurrence r, + long rangeStartDateValue, + long rangeEndDateValue, + boolean add, + TreeSet out) throws DateException { + unsafeNormalize(dtstart); + long dtstartDateValue = normDateTimeComparisonValue(dtstart); + int count = 0; + + // add the dtstart instance to the recurrence, if within range. + // For example, if dtstart is Mar 1, 2010 and the range is Jan 1 - Apr 1, + // then add it here and increment count. If the range is earlier or later, + // then don't add it here. In that case, count will be incremented later + // inside the loop. It is important that count gets incremented exactly + // once here or in the loop for dtstart. + // + // NOTE: if DTSTART is not synchronized with the recurrence rule, the first instance + // we return will not fit the RRULE pattern. + if (add && dtstartDateValue >= rangeStartDateValue + && dtstartDateValue < rangeEndDateValue) { + out.add(dtstartDateValue); + ++count; + } + + Time iterator = mIterator; + Time until = mUntil; + StringBuilder sb = mStringBuilder; + Time generated = mGenerated; + DaySet days = mDays; + + try { + + days.setRecurrence(r); + if (rangeEndDateValue == Long.MAX_VALUE && r.until == null && r.count == 0) { + throw new DateException( + "No range end provided for a recurrence that has no UNTIL or COUNT."); + } + + // the top-level frequency + int freqField; + int freqAmount = r.interval; + int freq = r.freq; + switch (freq) + { + case EventRecurrence.SECONDLY: + freqField = Time.SECOND; + break; + case EventRecurrence.MINUTELY: + freqField = Time.MINUTE; + break; + case EventRecurrence.HOURLY: + freqField = Time.HOUR; + break; + case EventRecurrence.DAILY: + freqField = Time.MONTH_DAY; + break; + case EventRecurrence.WEEKLY: + freqField = Time.MONTH_DAY; + freqAmount = 7 * r.interval; + if (freqAmount <= 0) { + freqAmount = 7; + } + break; + case EventRecurrence.MONTHLY: + freqField = Time.MONTH; + break; + case EventRecurrence.YEARLY: + freqField = Time.YEAR; + break; + default: + throw new DateException("bad freq=" + freq); + } + if (freqAmount <= 0) { + freqAmount = 1; + } + + int bymonthCount = r.bymonthCount; + boolean usebymonth = useBYX(freq, EventRecurrence.MONTHLY, bymonthCount); + boolean useDays = freq >= EventRecurrence.WEEKLY && + (r.bydayCount > 0 || r.bymonthdayCount > 0); + int byhourCount = r.byhourCount; + boolean usebyhour = useBYX(freq, EventRecurrence.HOURLY, byhourCount); + int byminuteCount = r.byminuteCount; + boolean usebyminute = useBYX(freq, EventRecurrence.MINUTELY, byminuteCount); + int bysecondCount = r.bysecondCount; + boolean usebysecond = useBYX(freq, EventRecurrence.SECONDLY, bysecondCount); + + // initialize the iterator + iterator.set(dtstart); + if (freqField == Time.MONTH) { + if (useDays) { + // if it's monthly, and we're going to be generating + // days, set the iterator day field to 1 because sometimes + // we'll skip months if it's greater than 28. + // XXX Do we generate days for MONTHLY w/ BYHOUR? If so, + // we need to do this then too. + iterator.monthDay = 1; + } + } + + long untilDateValue; + if (r.until != null) { + // Ensure that the "until" date string is specified in UTC. + String untilStr = r.until; + // 15 is length of date-time without trailing Z e.g. "20090204T075959" + // A string such as 20090204 is a valid UNTIL (see RFC 2445) and the + // Z should not be added. + if (untilStr.length() == 15) { + untilStr = untilStr + 'Z'; + } + // The parse() method will set the timezone to UTC + until.parse(untilStr); + + // We need the "until" year/month/day values to be in the same + // timezone as all the generated dates so that we can compare them + // using the values returned by normDateTimeComparisonValue(). + until.switchTimezone(dtstart.timezone); + untilDateValue = normDateTimeComparisonValue(until); + } else { + untilDateValue = Long.MAX_VALUE; + } + + sb.ensureCapacity(15); + sb.setLength(15); // TODO: pay attention to whether or not the event + // is an all-day one. + + if (SPEW) { + Log.i(TAG, "expand called w/ rangeStart=" + rangeStartDateValue + + " rangeEnd=" + rangeEndDateValue); + } + + // go until the end of the range or we're done with this event + boolean eventEnded = false; + int failsafe = 0; // Avoid infinite loops + events: { + while (true) { + int monthIndex = 0; + if (failsafe++ > MAX_ALLOWED_ITERATIONS) { // Give up after about 1 second of processing + throw new DateException("Recurrence processing stuck: " + r.toString()); + } + + unsafeNormalize(iterator); + + int iteratorYear = iterator.year; + int iteratorMonth = iterator.month + 1; + int iteratorDay = iterator.monthDay; + int iteratorHour = iterator.hour; + int iteratorMinute = iterator.minute; + int iteratorSecond = iterator.second; + + // year is never expanded -- there is no BYYEAR + generated.set(iterator); + + if (SPEW) Log.i(TAG, "year=" + generated.year); + + do { // month + int month = usebymonth + ? r.bymonth[monthIndex] + : iteratorMonth; + month--; + if (SPEW) Log.i(TAG, " month=" + month); + + int dayIndex = 1; + int lastDayToExamine = 0; + + // Use this to handle weeks that overlap the end of the month. + // Keep the year and month that days is for, and generate it + // when needed in the loop + if (useDays) { + // Determine where to start and end, don't worry if this happens + // to be before dtstart or after the end, because that will be + // filtered in the inner loop + if (freq == EventRecurrence.WEEKLY) { + /* + * iterator.weekDay indicates the day of the week (0-6, SU-SA). + * Because dayIndex might start in the middle of a week, and we're + * interested in treating a week as a unit, we want to move + * backward to the start of the week. (This could make the + * dayIndex negative, which will be corrected by normalization + * later on.) + * + * The day that starts the week is determined by WKST, which + * defaults to MO. + * + * Example: dayIndex is Tuesday the 8th, and weeks start on + * Thursdays. Tuesday is day 2, Thursday is day 4, so we + * want to move back (2 - 4 + 7) % 7 = 5 days to the previous + * Thursday. If weeks started on Mondays, we would only + * need to move back (2 - 1 + 7) % 7 = 1 day. + */ + int weekStartAdj = (iterator.weekDay - + EventRecurrence.day2TimeDay(r.wkst) + 7) % 7; + dayIndex = iterator.monthDay - weekStartAdj; + lastDayToExamine = dayIndex + 6; + } else { + lastDayToExamine = generated + .getActualMaximum(Time.MONTH_DAY); + } + if (SPEW) Log.i(TAG, "dayIndex=" + dayIndex + + " lastDayToExamine=" + lastDayToExamine + + " days=" + days); + } + + do { // day + int day; + if (useDays) { + if (!days.get(iterator, dayIndex)) { + dayIndex++; + continue; + } else { + day = dayIndex; + } + } else { + day = iteratorDay; + } + if (SPEW) Log.i(TAG, " day=" + day); + + // hour + int hourIndex = 0; + do { + int hour = usebyhour + ? r.byhour[hourIndex] + : iteratorHour; + if (SPEW) Log.i(TAG, " hour=" + hour + " usebyhour=" + usebyhour); + + // minute + int minuteIndex = 0; + do { + int minute = usebyminute + ? r.byminute[minuteIndex] + : iteratorMinute; + if (SPEW) Log.i(TAG, " minute=" + minute); + + // second + int secondIndex = 0; + do { + int second = usebysecond + ? r.bysecond[secondIndex] + : iteratorSecond; + if (SPEW) Log.i(TAG, " second=" + second); + + // we do this here each time, because if we distribute it, we find the + // month advancing extra times, as we set the month to the 32nd, 33rd, etc. + // days. + generated.set(second, minute, hour, day, month, iteratorYear); + unsafeNormalize(generated); + + long genDateValue = normDateTimeComparisonValue(generated); + // sometimes events get generated (BYDAY, BYHOUR, etc.) that + // are before dtstart. Filter these. I believe this is correct, + // but Google Calendar doesn't seem to always do this. + if (genDateValue >= dtstartDateValue) { + // filter and then add + // TODO: we don't check for stop conditions (like + // passing the "end" date) unless the filter + // allows the event. Could stop sooner. + int filtered = filter(r, generated); + if (0 == filtered) { + + // increase the count as long + // as this isn't the same + // as the first instance + // specified by the DTSTART + // (for RRULEs -- additive). + // This condition must be the complement of the + // condition for incrementing count at the + // beginning of the method, so if we don't + // increment count there, we increment it here. + // For example, if add is set and dtstartDateValue + // is inside the start/end range, then it was added + // and count was incremented at the beginning. + // If dtstartDateValue is outside the range or add + // is not set, then we must increment count here. + if (!(dtstartDateValue == genDateValue + && add + && dtstartDateValue >= rangeStartDateValue + && dtstartDateValue < rangeEndDateValue)) { + ++count; + } + // one reason we can stop is that + // we're past the until date + if (genDateValue > untilDateValue) { + if (SPEW) { + Log.i(TAG, "stopping b/c until=" + + untilDateValue + + " generated=" + + genDateValue); + } + break events; + } + // or we're past rangeEnd + if (genDateValue >= rangeEndDateValue) { + if (SPEW) { + Log.i(TAG, "stopping b/c rangeEnd=" + + rangeEndDateValue + + " generated=" + generated); + } + break events; + } + + if (genDateValue >= rangeStartDateValue) { + if (SPEW) { + Log.i(TAG, "adding date=" + generated + " filtered=" + filtered); + } + if (add) { + out.add(genDateValue); + } else { + out.remove(genDateValue); + } + } + // another is that count is high enough + if (r.count > 0 && r.count == count) { + //Log.i(TAG, "stopping b/c count=" + count); + break events; + } + } + } + secondIndex++; + } while (usebysecond && secondIndex < bysecondCount); + minuteIndex++; + } while (usebyminute && minuteIndex < byminuteCount); + hourIndex++; + } while (usebyhour && hourIndex < byhourCount); + dayIndex++; + } while (useDays && dayIndex <= lastDayToExamine); + monthIndex++; + } while (usebymonth && monthIndex < bymonthCount); + + // Add freqAmount to freqField until we get another date that we want. + // We don't want to "generate" dates with the iterator. + // XXX: We do this for days, because there is a varying number of days + // per month + int oldDay = iterator.monthDay; + generated.set(iterator); // just using generated as a temporary. + int n = 1; + while (true) { + int value = freqAmount * n; + switch (freqField) { + case Time.SECOND: + iterator.second += value; + break; + case Time.MINUTE: + iterator.minute += value; + break; + case Time.HOUR: + iterator.hour += value; + break; + case Time.MONTH_DAY: + iterator.monthDay += value; + break; + case Time.MONTH: + iterator.month += value; + break; + case Time.YEAR: + iterator.year += value; + break; + case Time.WEEK_DAY: + iterator.monthDay += value; + break; + case Time.YEAR_DAY: + iterator.monthDay += value; + break; + default: + throw new RuntimeException("bad field=" + freqField); + } + + unsafeNormalize(iterator); + if (freqField != Time.YEAR && freqField != Time.MONTH) { + break; + } + if (iterator.monthDay == oldDay) { + break; + } + n++; + iterator.set(generated); + } + } + } + } + catch (DateException e) { + Log.w(TAG, "DateException with r=" + r + " rangeStart=" + rangeStartDateValue + + " rangeEnd=" + rangeEndDateValue); + throw e; + } + catch (RuntimeException t) { + Log.w(TAG, "RuntimeException with r=" + r + " rangeStart=" + rangeStartDateValue + + " rangeEnd=" + rangeEndDateValue); + throw t; + } + } + + /** + * Normalizes the date fields to give a valid date, but if the time falls + * in the invalid window during a transition out of Daylight Saving Time + * when time jumps forward an hour, then the "normalized" value will be + * invalid. + *

+ * This method also computes the weekDay and yearDay fields. + * + *

+ * This method does not modify the fields isDst, or gmtOff. + */ + static void unsafeNormalize(Time date) { + int second = date.second; + int minute = date.minute; + int hour = date.hour; + int monthDay = date.monthDay; + int month = date.month; + int year = date.year; + + int addMinutes = ((second < 0) ? (second - 59) : second) / 60; + second -= addMinutes * 60; + minute += addMinutes; + int addHours = ((minute < 0) ? (minute - 59) : minute) / 60; + minute -= addHours * 60; + hour += addHours; + int addDays = ((hour < 0) ? (hour - 23) : hour) / 24; + hour -= addDays * 24; + monthDay += addDays; + + // We want to make "monthDay" positive. We do this by subtracting one + // from the year and adding a year's worth of days to "monthDay" in + // the following loop while "monthDay" <= 0. + while (monthDay <= 0) { + // If month is after Feb, then add this year's length so that we + // include this year's leap day, if any. + // Otherwise (the month is Feb or earlier), add last year's length. + // Subtract one from the year in either case. This gives the same + // effective date but makes monthDay (the day of the month) much + // larger. Eventually (usually in one iteration) monthDay will + // be positive. + int days = month > 1 ? yearLength(year) : yearLength(year - 1); + monthDay += days; + year -= 1; + } + // At this point, monthDay >= 1. Normalize the month to the range [0,11]. + if (month < 0) { + int years = (month + 1) / 12 - 1; + year += years; + month -= 12 * years; + } else if (month >= 12) { + int years = month / 12; + year += years; + month -= 12 * years; + } + // At this point, month is in the range [0,11] and monthDay >= 1. + // Now loop until the monthDay is in the correct range for the month. + while (true) { + // On January, check if we can jump forward a whole year. + if (month == 0) { + int yearLength = yearLength(year); + if (monthDay > yearLength) { + year++; + monthDay -= yearLength; + } + } + int monthLength = monthLength(year, month); + if (monthDay > monthLength) { + monthDay -= monthLength; + month++; + if (month >= 12) { + month -= 12; + year++; + } + } else break; + } + // At this point, monthDay <= the length of the current month and is + // in the range [1,31]. + + date.second = second; + date.minute = minute; + date.hour = hour; + date.monthDay = monthDay; + date.month = month; + date.year = year; + date.weekDay = weekDay(year, month, monthDay); + date.yearDay = yearDay(year, month, monthDay); + } + + /** + * Returns true if the given year is a leap year. + * + * @param year the given year to test + * @return true if the given year is a leap year. + */ + static boolean isLeapYear(int year) { + return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)); + } + + /** + * Returns the number of days in the given year. + * + * @param year the given year + * @return the number of days in the given year. + */ + static int yearLength(int year) { + return isLeapYear(year) ? 366 : 365; + } + + private static final int[] DAYS_PER_MONTH = { 31, 28, 31, 30, 31, 30, 31, + 31, 30, 31, 30, 31 }; + private static final int[] DAYS_IN_YEAR_PRECEDING_MONTH = { 0, 31, 59, 90, + 120, 151, 180, 212, 243, 273, 304, 334 }; + + /** + * Returns the number of days in the given month of the given year. + * + * @param year the given year. + * @param month the given month in the range [0,11] + * @return the number of days in the given month of the given year. + */ + static int monthLength(int year, int month) { + int n = DAYS_PER_MONTH[month]; + if (n != 28) { + return n; + } + return isLeapYear(year) ? 29 : 28; + } + + /** + * Computes the weekday, a number in the range [0,6] where Sunday=0, from + * the given year, month, and day. + * + * @param year the year + * @param month the 0-based month in the range [0,11] + * @param day the 1-based day of the month in the range [1,31] + * @return the weekday, a number in the range [0,6] where Sunday=0 + */ + static int weekDay(int year, int month, int day) { + if (month <= 1) { + month += 12; + year -= 1; + } + return (day + (13 * month - 14) / 5 + year + year/4 - year/100 + year/400) % 7; + } + + /** + * Computes the 0-based "year day", given the year, month, and day. + * + * @param year the year + * @param month the 0-based month in the range [0,11] + * @param day the 1-based day in the range [1,31] + * @return the 0-based "year day", the number of days into the year + */ + static int yearDay(int year, int month, int day) { + int yearDay = DAYS_IN_YEAR_PRECEDING_MONTH[month] + day - 1; + if (month >= 2 && isLeapYear(year)) { + yearDay += 1; + } + return yearDay; + } + + /** + * Converts a normalized Time value to a 64-bit long. The mapping of Time + * values to longs provides a total ordering on the Time values so that + * two Time values can be compared efficiently by comparing their 64-bit + * long values. This is faster than converting the Time values to UTC + * millliseconds. + * + * @param normalized a Time object whose date and time fields have been + * normalized + * @return a 64-bit long value that can be used for comparing and ordering + * dates and times represented by Time objects + */ + private static final long normDateTimeComparisonValue(Time normalized) { + // 37 bits for the year, 4 bits for the month, 5 bits for the monthDay, + // 5 bits for the hour, 6 bits for the minute, 6 bits for the second. + return ((long)normalized.year << 26) + (normalized.month << 22) + + (normalized.monthDay << 17) + (normalized.hour << 12) + + (normalized.minute << 6) + normalized.second; + } + + private static final void setTimeFromLongValue(Time date, long val) { + date.year = (int) (val >> 26); + date.month = (int) (val >> 22) & 0xf; + date.monthDay = (int) (val >> 17) & 0x1f; + date.hour = (int) (val >> 12) & 0x1f; + date.minute = (int) (val >> 6) & 0x3f; + date.second = (int) (val & 0x3f); + } +} diff --git a/src/com/android/calendarcommon2/RecurrenceSet.java b/src/com/android/calendarcommon2/RecurrenceSet.java new file mode 100644 index 0000000..9ee0ae9 --- /dev/null +++ b/src/com/android/calendarcommon2/RecurrenceSet.java @@ -0,0 +1,526 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.calendarcommon2; + +import android.content.ContentValues; +import android.database.Cursor; +import android.provider.CalendarContract; +import android.text.TextUtils; +import android.text.format.Time; +import android.util.Log; +import android.util.TimeFormatException; + +import java.util.List; +import java.util.regex.Pattern; + +/** + * Basic information about a recurrence, following RFC 2445 Section 4.8.5. + * Contains the RRULEs, RDATE, EXRULEs, and EXDATE properties. + */ +public class RecurrenceSet { + + private final static String TAG = "RecurrenceSet"; + + private final static String RULE_SEPARATOR = "\n"; + private final static String FOLDING_SEPARATOR = "\n "; + + // TODO: make these final? + public EventRecurrence[] rrules = null; + public long[] rdates = null; + public EventRecurrence[] exrules = null; + public long[] exdates = null; + + /** + * Creates a new RecurrenceSet from information stored in the + * events table in the CalendarProvider. + * @param values The values retrieved from the Events table. + */ + public RecurrenceSet(ContentValues values) + throws EventRecurrence.InvalidFormatException { + String rruleStr = values.getAsString(CalendarContract.Events.RRULE); + String rdateStr = values.getAsString(CalendarContract.Events.RDATE); + String exruleStr = values.getAsString(CalendarContract.Events.EXRULE); + String exdateStr = values.getAsString(CalendarContract.Events.EXDATE); + init(rruleStr, rdateStr, exruleStr, exdateStr); + } + + /** + * Creates a new RecurrenceSet from information stored in a database + * {@link Cursor} pointing to the events table in the + * CalendarProvider. The cursor must contain the RRULE, RDATE, EXRULE, + * and EXDATE columns. + * + * @param cursor The cursor containing the RRULE, RDATE, EXRULE, and EXDATE + * columns. + */ + public RecurrenceSet(Cursor cursor) + throws EventRecurrence.InvalidFormatException { + int rruleColumn = cursor.getColumnIndex(CalendarContract.Events.RRULE); + int rdateColumn = cursor.getColumnIndex(CalendarContract.Events.RDATE); + int exruleColumn = cursor.getColumnIndex(CalendarContract.Events.EXRULE); + int exdateColumn = cursor.getColumnIndex(CalendarContract.Events.EXDATE); + String rruleStr = cursor.getString(rruleColumn); + String rdateStr = cursor.getString(rdateColumn); + String exruleStr = cursor.getString(exruleColumn); + String exdateStr = cursor.getString(exdateColumn); + init(rruleStr, rdateStr, exruleStr, exdateStr); + } + + public RecurrenceSet(String rruleStr, String rdateStr, + String exruleStr, String exdateStr) + throws EventRecurrence.InvalidFormatException { + init(rruleStr, rdateStr, exruleStr, exdateStr); + } + + private void init(String rruleStr, String rdateStr, + String exruleStr, String exdateStr) + throws EventRecurrence.InvalidFormatException { + if (!TextUtils.isEmpty(rruleStr) || !TextUtils.isEmpty(rdateStr)) { + + if (!TextUtils.isEmpty(rruleStr)) { + String[] rruleStrs = rruleStr.split(RULE_SEPARATOR); + rrules = new EventRecurrence[rruleStrs.length]; + for (int i = 0; i < rruleStrs.length; ++i) { + EventRecurrence rrule = new EventRecurrence(); + rrule.parse(rruleStrs[i]); + rrules[i] = rrule; + } + } + + if (!TextUtils.isEmpty(rdateStr)) { + rdates = parseRecurrenceDates(rdateStr); + } + + if (!TextUtils.isEmpty(exruleStr)) { + String[] exruleStrs = exruleStr.split(RULE_SEPARATOR); + exrules = new EventRecurrence[exruleStrs.length]; + for (int i = 0; i < exruleStrs.length; ++i) { + EventRecurrence exrule = new EventRecurrence(); + exrule.parse(exruleStr); + exrules[i] = exrule; + } + } + + if (!TextUtils.isEmpty(exdateStr)) { + exdates = parseRecurrenceDates(exdateStr); + } + } + } + + /** + * Returns whether or not a recurrence is defined in this RecurrenceSet. + * @return Whether or not a recurrence is defined in this RecurrenceSet. + */ + public boolean hasRecurrence() { + return (rrules != null || rdates != null); + } + + /** + * Parses the provided RDATE or EXDATE string into an array of longs + * representing each date/time in the recurrence. + * @param recurrence The recurrence to be parsed. + * @return The list of date/times. + */ + public static long[] parseRecurrenceDates(String recurrence) + throws EventRecurrence.InvalidFormatException{ + // TODO: use "local" time as the default. will need to handle times + // that end in "z" (UTC time) explicitly at that point. + String tz = Time.TIMEZONE_UTC; + int tzidx = recurrence.indexOf(";"); + if (tzidx != -1) { + tz = recurrence.substring(0, tzidx); + recurrence = recurrence.substring(tzidx + 1); + } + Time time = new Time(tz); + String[] rawDates = recurrence.split(","); + int n = rawDates.length; + long[] dates = new long[n]; + for (int i = 0; i4.1 Content Lines + * + *

The iCalendar object is organized into individual lines of text, called + * content lines. Content lines are delimited by a line break, which is a CRLF + * sequence (US-ASCII decimal 13, followed by US-ASCII decimal 10). + * + *

Lines of text SHOULD NOT be longer than 75 octets, excluding the line + * break. Long content lines SHOULD be split into a multiple line + * representations using a line "folding" technique. That is, a long line can + * be split between any two characters by inserting a CRLF immediately + * followed by a single linear white space character (i.e., SPACE, US-ASCII + * decimal 32 or HTAB, US-ASCII decimal 9). Any sequence of CRLF followed + * immediately by a single linear white space character is ignored (i.e., + * removed) when processing the content type. + */ + public static String fold(String unfoldedIcalContent) { + return FOLD_RE.matcher(unfoldedIcalContent).replaceAll("$0\r\n "); + } + + public static String unfold(String foldedIcalContent) { + return IGNORABLE_ICAL_WHITESPACE_RE.matcher( + foldedIcalContent).replaceAll(""); + } + + public static void addPropertyForDateStr(ICalendar.Component component, + String propertyName, + String dateStr) { + if (TextUtils.isEmpty(dateStr)) { + return; + } + + ICalendar.Property prop = new ICalendar.Property(propertyName); + String tz = null; + int tzidx = dateStr.indexOf(";"); + if (tzidx != -1) { + tz = dateStr.substring(0, tzidx); + dateStr = dateStr.substring(tzidx + 1); + } + if (!TextUtils.isEmpty(tz)) { + prop.addParameter(new ICalendar.Parameter("TZID", tz)); + } + prop.setValue(dateStr); + component.addProperty(prop); + } + + private static String computeDuration(Time start, + ICalendar.Component component) { + // see if a duration is defined + ICalendar.Property durationProperty = + component.getFirstProperty("DURATION"); + if (durationProperty != null) { + // just return the duration + return durationProperty.getValue(); + } + + // must compute a duration from the DTEND + ICalendar.Property dtendProperty = + component.getFirstProperty("DTEND"); + if (dtendProperty == null) { + // no DURATION, no DTEND: 0 second duration + return "+P0S"; + } + ICalendar.Parameter endTzidParameter = + dtendProperty.getFirstParameter("TZID"); + String endTzid = (endTzidParameter == null) + ? start.timezone : endTzidParameter.value; + + Time end = new Time(endTzid); + end.parse(dtendProperty.getValue()); + long durationMillis = end.toMillis(false /* use isDst */) + - start.toMillis(false /* use isDst */); + long durationSeconds = (durationMillis / 1000); + if (start.allDay && (durationSeconds % 86400) == 0) { + return "P" + (durationSeconds / 86400) + "D"; // Server wants this instead of P86400S + } else { + return "P" + durationSeconds + "S"; + } + } + + private static String flattenProperties(ICalendar.Component component, + String name) { + List properties = component.getProperties(name); + if (properties == null || properties.isEmpty()) { + return null; + } + + if (properties.size() == 1) { + return properties.get(0).getValue(); + } + + StringBuilder sb = new StringBuilder(); + + boolean first = true; + for (ICalendar.Property property : component.getProperties(name)) { + if (first) { + first = false; + } else { + // TODO: use commas. our RECUR parsing should handle that + // anyway. + sb.append(RULE_SEPARATOR); + } + sb.append(property.getValue()); + } + return sb.toString(); + } + + private static String extractDates(ICalendar.Property recurrence) { + if (recurrence == null) { + return null; + } + ICalendar.Parameter tzidParam = + recurrence.getFirstParameter("TZID"); + if (tzidParam != null) { + return tzidParam.value + ";" + recurrence.getValue(); + } + return recurrence.getValue(); + } +} diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml index 779b74a..040e885 100644 --- a/tests/AndroidManifest.xml +++ b/tests/AndroidManifest.xml @@ -15,13 +15,13 @@ --> + package="com.android.calendarcommon2.tests"> diff --git a/tests/src/com/android/calendarcommon/DurationTest.java b/tests/src/com/android/calendarcommon/DurationTest.java deleted file mode 100644 index b264713..0000000 --- a/tests/src/com/android/calendarcommon/DurationTest.java +++ /dev/null @@ -1,77 +0,0 @@ -/* //device/content/providers/pim/DurationTest.java -** -** Copyright 2006, The Android Open Source Project -** -** Licensed under the Apache License, Version 2.0 (the "License"); -** you may not use this file except in compliance with the License. -** You may obtain a copy of the License at -** -** http://www.apache.org/licenses/LICENSE-2.0 -** -** Unless required by applicable law or agreed to in writing, software -** distributed under the License is distributed on an "AS IS" BASIS, -** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -** See the License for the specific language governing permissions and -** limitations under the License. -*/ - -package com.android.calendarcommon; - -import junit.framework.TestCase; - -import android.test.suitebuilder.annotation.SmallTest; - -public class DurationTest extends TestCase { - - private void verifyDuration(String str, - int sign, int weeks, int days, int hours, - int minutes, int seconds) throws DateException { - - Duration duration = new Duration(); - duration.parse(str); - - assertEquals("Duration sign is not equal for " + str, sign, duration.sign); - assertEquals("Duration weeks is not equal for " + str, weeks, duration.weeks); - assertEquals("Duration days is not equal for " + str, days, duration.days); - assertEquals("Duration hours is not equal for " + str, hours, duration.hours); - assertEquals("Duration minutes is not equal for " + str, minutes, duration.minutes); - assertEquals("Duration seconds is not equal for " + str, seconds, duration.seconds); - } - - @SmallTest - public void testParse() throws Exception { - verifyDuration("P7W", 1, 7, 0, 0, 0, 0); - verifyDuration("PT7W", 1, 7, 0, 0, 0, 0); - verifyDuration("-PT7W", -1, 7, 0, 0, 0, 0); - verifyDuration("P15DT5H0M20S", 1, 0, 15, 5, 0, 20); - verifyDuration("-P15DT5H0M20S", -1, 0, 15, 5, 0, 20); - verifyDuration("PT1H2M3S", 1, 0, 0, 1, 2, 3); - - verifyDuration("", 1, 0, 0, 0, 0, 0); - verifyDuration("P", 1, 0, 0, 0, 0, 0); - verifyDuration("P0W", 1, 0, 0, 0, 0, 0); - verifyDuration("P0D", 1, 0, 0, 0, 0, 0); - verifyDuration("PT0H0M0S", 1, 0, 0, 0, 0, 0); - verifyDuration("P0DT0H0M0S", 1, 0, 0, 0, 0, 0); - } - - @SmallTest - public void testParseInvalidStrings() throws Exception { - try { - verifyDuration(" -P15DT5H0M20S", 0, 0, 0, 0, 0, 0); - fail("test didn't throw an exception but we expected it to"); - } catch (DateException e) { - // expected - } - - try { - verifyDuration(" not even close", 0, 0, 0, 0, 0, 0); - fail("test didn't throw an exception but we expected it to"); - } catch (DateException e) { - // expected - } - } -} - - - diff --git a/tests/src/com/android/calendarcommon/EventRecurrenceTest.java b/tests/src/com/android/calendarcommon/EventRecurrenceTest.java deleted file mode 100644 index efd5f1c..0000000 --- a/tests/src/com/android/calendarcommon/EventRecurrenceTest.java +++ /dev/null @@ -1,889 +0,0 @@ -/* - * Copyright (C) 2006 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.calendarcommon; - -import com.android.calendarcommon.EventRecurrence.InvalidFormatException; - -import android.test.suitebuilder.annotation.SmallTest; -import android.test.suitebuilder.annotation.Suppress; - -import junit.framework.TestCase; - -import java.util.Arrays; - -/** - * Test android.pim.EventRecurrence. - * - * adb shell am instrument -w -e class com.android.calendarcommon.EventRecurrenceTest \ - * com.android.calendarcommon.tests/android.test.InstrumentationTestRunner - */ -public class EventRecurrenceTest extends TestCase { - - @SmallTest - public void test0() throws Exception { - verifyRecurType("FREQ=SECONDLY", - /* int freq */ EventRecurrence.SECONDLY, - /* String until */ null, - /* int count */ 0, - /* int interval */ 0, - /* int[] bysecond */ null, - /* int[] byminute */ null, - /* int[] byhour */ null, - /* int[] byday */ null, - /* int[] bydayNum */ null, - /* int[] bymonthday */ null, - /* int[] byyearday */ null, - /* int[] byweekno */ null, - /* int[] bymonth */ null, - /* int[] bysetpos */ null, - /* int wkst */ EventRecurrence.MO - ); - } - - @SmallTest - public void test1() throws Exception { - verifyRecurType("FREQ=MINUTELY", - /* int freq */ EventRecurrence.MINUTELY, - /* String until */ null, - /* int count */ 0, - /* int interval */ 0, - /* int[] bysecond */ null, - /* int[] byminute */ null, - /* int[] byhour */ null, - /* int[] byday */ null, - /* int[] bydayNum */ null, - /* int[] bymonthday */ null, - /* int[] byyearday */ null, - /* int[] byweekno */ null, - /* int[] bymonth */ null, - /* int[] bysetpos */ null, - /* int wkst */ EventRecurrence.MO - ); - } - - @SmallTest - public void test2() throws Exception { - verifyRecurType("FREQ=HOURLY", - /* int freq */ EventRecurrence.HOURLY, - /* String until */ null, - /* int count */ 0, - /* int interval */ 0, - /* int[] bysecond */ null, - /* int[] byminute */ null, - /* int[] byhour */ null, - /* int[] byday */ null, - /* int[] bydayNum */ null, - /* int[] bymonthday */ null, - /* int[] byyearday */ null, - /* int[] byweekno */ null, - /* int[] bymonth */ null, - /* int[] bysetpos */ null, - /* int wkst */ EventRecurrence.MO - ); - } - - @SmallTest - public void test3() throws Exception { - verifyRecurType("FREQ=DAILY", - /* int freq */ EventRecurrence.DAILY, - /* String until */ null, - /* int count */ 0, - /* int interval */ 0, - /* int[] bysecond */ null, - /* int[] byminute */ null, - /* int[] byhour */ null, - /* int[] byday */ null, - /* int[] bydayNum */ null, - /* int[] bymonthday */ null, - /* int[] byyearday */ null, - /* int[] byweekno */ null, - /* int[] bymonth */ null, - /* int[] bysetpos */ null, - /* int wkst */ EventRecurrence.MO - ); - } - - @SmallTest - public void test4() throws Exception { - verifyRecurType("FREQ=WEEKLY", - /* int freq */ EventRecurrence.WEEKLY, - /* String until */ null, - /* int count */ 0, - /* int interval */ 0, - /* int[] bysecond */ null, - /* int[] byminute */ null, - /* int[] byhour */ null, - /* int[] byday */ null, - /* int[] bydayNum */ null, - /* int[] bymonthday */ null, - /* int[] byyearday */ null, - /* int[] byweekno */ null, - /* int[] bymonth */ null, - /* int[] bysetpos */ null, - /* int wkst */ EventRecurrence.MO - ); - } - - @SmallTest - public void test5() throws Exception { - verifyRecurType("FREQ=MONTHLY", - /* int freq */ EventRecurrence.MONTHLY, - /* String until */ null, - /* int count */ 0, - /* int interval */ 0, - /* int[] bysecond */ null, - /* int[] byminute */ null, - /* int[] byhour */ null, - /* int[] byday */ null, - /* int[] bydayNum */ null, - /* int[] bymonthday */ null, - /* int[] byyearday */ null, - /* int[] byweekno */ null, - /* int[] bymonth */ null, - /* int[] bysetpos */ null, - /* int wkst */ EventRecurrence.MO - ); - } - - @SmallTest - public void test6() throws Exception { - verifyRecurType("FREQ=YEARLY", - /* int freq */ EventRecurrence.YEARLY, - /* String until */ null, - /* int count */ 0, - /* int interval */ 0, - /* int[] bysecond */ null, - /* int[] byminute */ null, - /* int[] byhour */ null, - /* int[] byday */ null, - /* int[] bydayNum */ null, - /* int[] bymonthday */ null, - /* int[] byyearday */ null, - /* int[] byweekno */ null, - /* int[] bymonth */ null, - /* int[] bysetpos */ null, - /* int wkst */ EventRecurrence.MO - ); - } - - @SmallTest - public void test7() throws Exception { - // with an until - verifyRecurType("FREQ=DAILY;UNTIL=112233T223344Z", - /* int freq */ EventRecurrence.DAILY, - /* String until */ "112233T223344Z", - /* int count */ 0, - /* int interval */ 0, - /* int[] bysecond */ null, - /* int[] byminute */ null, - /* int[] byhour */ null, - /* int[] byday */ null, - /* int[] bydayNum */ null, - /* int[] bymonthday */ null, - /* int[] byyearday */ null, - /* int[] byweekno */ null, - /* int[] bymonth */ null, - /* int[] bysetpos */ null, - /* int wkst */ EventRecurrence.MO - ); - } - - @SmallTest - public void test8() throws Exception { - // with a count - verifyRecurType("FREQ=DAILY;COUNT=334", - /* int freq */ EventRecurrence.DAILY, - /* String until */ null, - /* int count */ 334, - /* int interval */ 0, - /* int[] bysecond */ null, - /* int[] byminute */ null, - /* int[] byhour */ null, - /* int[] byday */ null, - /* int[] bydayNum */ null, - /* int[] bymonthday */ null, - /* int[] byyearday */ null, - /* int[] byweekno */ null, - /* int[] bymonth */ null, - /* int[] bysetpos */ null, - /* int wkst */ EventRecurrence.MO - ); - } - - @SmallTest - public void test9() throws Exception { - // with a count - verifyRecurType("FREQ=DAILY;INTERVAL=5000", - /* int freq */ EventRecurrence.DAILY, - /* String until */ null, - /* int count */ 0, - /* int interval */ 5000, - /* int[] bysecond */ null, - /* int[] byminute */ null, - /* int[] byhour */ null, - /* int[] byday */ null, - /* int[] bydayNum */ null, - /* int[] bymonthday */ null, - /* int[] byyearday */ null, - /* int[] byweekno */ null, - /* int[] bymonth */ null, - /* int[] bysetpos */ null, - /* int wkst */ EventRecurrence.MO - ); - } - - @SmallTest - public void test10() throws Exception { - // verifyRecurType all of the BY* ones with one element - verifyRecurType("FREQ=DAILY" - + ";BYSECOND=0" - + ";BYMINUTE=1" - + ";BYHOUR=2" - + ";BYMONTHDAY=30" - + ";BYYEARDAY=300" - + ";BYWEEKNO=53" - + ";BYMONTH=12" - + ";BYSETPOS=-15" - + ";WKST=SU", - /* int freq */ EventRecurrence.DAILY, - /* String until */ null, - /* int count */ 0, - /* int interval */ 0, - /* int[] bysecond */ new int[]{0}, - /* int[] byminute */ new int[]{1}, - /* int[] byhour */ new int[]{2}, - /* int[] byday */ null, - /* int[] bydayNum */ null, - /* int[] bymonthday */ new int[]{30}, - /* int[] byyearday */ new int[]{300}, - /* int[] byweekno */ new int[]{53}, - /* int[] bymonth */ new int[]{12}, - /* int[] bysetpos */ new int[]{-15}, - /* int wkst */ EventRecurrence.SU - ); - } - - @SmallTest - public void test11() throws Exception { - // verifyRecurType all of the BY* ones with one element - verifyRecurType("FREQ=DAILY" - + ";BYSECOND=0,30,59" - + ";BYMINUTE=0,41,59" - + ";BYHOUR=0,4,23" - + ";BYMONTHDAY=-31,-1,1,31" - + ";BYYEARDAY=-366,-1,1,366" - + ";BYWEEKNO=-53,-1,1,53" - + ";BYMONTH=1,12" - + ";BYSETPOS=1,2,3,4,500,10000" - + ";WKST=SU", - /* int freq */ EventRecurrence.DAILY, - /* String until */ null, - /* int count */ 0, - /* int interval */ 0, - /* int[] bysecond */ new int[]{0, 30, 59}, - /* int[] byminute */ new int[]{0, 41, 59}, - /* int[] byhour */ new int[]{0, 4, 23}, - /* int[] byday */ null, - /* int[] bydayNum */ null, - /* int[] bymonthday */ new int[]{-31, -1, 1, 31}, - /* int[] byyearday */ new int[]{-366, -1, 1, 366}, - /* int[] byweekno */ new int[]{-53, -1, 1, 53}, - /* int[] bymonth */ new int[]{1, 12}, - /* int[] bysetpos */ new int[]{1, 2, 3, 4, 500, 10000}, - /* int wkst */ EventRecurrence.SU - ); - } - - private static class Check { - Check(String k, int... v) { - key = k; - values = v; - } - - String key; - int[] values; - } - - // this is a negative verifyRecurType case to verifyRecurType the range of the numbers accepted - @SmallTest - public void test12() throws Exception { - Check[] checks = new Check[]{ - new Check("BYSECOND", -100, -1, 60, 100), - new Check("BYMINUTE", -100, -1, 60, 100), - new Check("BYHOUR", -100, -1, 24, 100), - new Check("BYMONTHDAY", -100, -32, 0, 32, 100), - new Check("BYYEARDAY", -400, -367, 0, 367, 400), - new Check("BYWEEKNO", -100, -54, 0, 54, 100), - new Check("BYMONTH", -100, -5, 0, 13, 100) - }; - - for (Check ck : checks) { - for (int n : ck.values) { - String recur = "FREQ=DAILY;" + ck.key + "=" + n; - try { - EventRecurrence er = new EventRecurrence(); - er.parse(recur); - fail("Negative verifyRecurType failed. " - + " parse failed to throw an exception for '" - + recur + "'"); - } catch (EventRecurrence.InvalidFormatException e) { - // expected - } - } - } - } - - // verifyRecurType BYDAY - @SmallTest - public void test13() throws Exception { - verifyRecurType("FREQ=DAILY;BYDAY=1SU,-2MO,+33TU,WE,TH,FR,SA", - /* int freq */ EventRecurrence.DAILY, - /* String until */ null, - /* int count */ 0, - /* int interval */ 0, - /* int[] bysecond */ null, - /* int[] byminute */ null, - /* int[] byhour */ null, - /* int[] byday */ new int[] { - EventRecurrence.SU, - EventRecurrence.MO, - EventRecurrence.TU, - EventRecurrence.WE, - EventRecurrence.TH, - EventRecurrence.FR, - EventRecurrence.SA - }, - /* int[] bydayNum */ new int[]{1, -2, 33, 0, 0, 0, 0}, - /* int[] bymonthday */ null, - /* int[] byyearday */ null, - /* int[] byweekno */ null, - /* int[] bymonth */ null, - /* int[] bysetpos */ null, - /* int wkst */ EventRecurrence.MO - ); - } - - @Suppress - // Repro bug #2331761 - this should fail because of the last comma into BYDAY - public void test14() throws Exception { - verifyRecurType("FREQ=WEEKLY;WKST=MO;UNTIL=20100129T130000Z;INTERVAL=1;BYDAY=MO,TU,WE,", - /* int freq */ EventRecurrence.WEEKLY, - /* String until */ "20100129T130000Z", - /* int count */ 0, - /* int interval */ 1, - /* int[] bysecond */ null, - /* int[] byminute */ null, - /* int[] byhour */ null, - /* int[] byday */ new int[] { - EventRecurrence.MO, - EventRecurrence.TU, - EventRecurrence.WE, - }, - /* int[] bydayNum */ new int[]{0, 0, 0}, - /* int[] bymonthday */ null, - /* int[] byyearday */ null, - /* int[] byweekno */ null, - /* int[] bymonth */ null, - /* int[] bysetpos */ null, - /* int wkst */ EventRecurrence.MO - ); - } - - // This test should pass - public void test15() throws Exception { - verifyRecurType("FREQ=WEEKLY;WKST=MO;UNTIL=20100129T130000Z;INTERVAL=1;" - + "BYDAY=MO,TU,WE,TH,FR,SA,SU", - /* int freq */ EventRecurrence.WEEKLY, - /* String until */ "20100129T130000Z", - /* int count */ 0, - /* int interval */ 1, - /* int[] bysecond */ null, - /* int[] byminute */ null, - /* int[] byhour */ null, - /* int[] byday */ new int[] { - EventRecurrence.MO, - EventRecurrence.TU, - EventRecurrence.WE, - EventRecurrence.TH, - EventRecurrence.FR, - EventRecurrence.SA, - EventRecurrence.SU - }, - /* int[] bydayNum */ new int[]{0, 0, 0, 0, 0, 0, 0}, - /* int[] bymonthday */ null, - /* int[] byyearday */ null, - /* int[] byweekno */ null, - /* int[] bymonth */ null, - /* int[] bysetpos */ null, - /* int wkst */ EventRecurrence.MO - ); - } - - // Sample coming from RFC2445 - public void test16() throws Exception { - verifyRecurType("FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1", - /* int freq */ EventRecurrence.MONTHLY, - /* String until */ null, - /* int count */ 0, - /* int interval */ 0, - /* int[] bysecond */ null, - /* int[] byminute */ null, - /* int[] byhour */ null, - /* int[] byday */ new int[] { - EventRecurrence.MO, - EventRecurrence.TU, - EventRecurrence.WE, - EventRecurrence.TH, - EventRecurrence.FR - }, - /* int[] bydayNum */ new int[] {0, 0, 0, 0, 0}, - /* int[] bymonthday */ null, - /* int[] byyearday */ null, - /* int[] byweekno */ null, - /* int[] bymonth */ null, - /* int[] bysetpos */ new int[] { -1 }, - /* int wkst */ EventRecurrence.MO - ); - } - - // Sample coming from RFC2445 - public void test17() throws Exception { - verifyRecurType("FREQ=DAILY;COUNT=10;INTERVAL=2", - /* int freq */ EventRecurrence.DAILY, - /* String until */ null, - /* int count */ 10, - /* int interval */ 2, - /* int[] bysecond */ null, - /* int[] byminute */ null, - /* int[] byhour */ null, - /* int[] byday */ null, - /* int[] bydayNum */ null, - /* int[] bymonthday */ null, - /* int[] byyearday */ null, - /* int[] byweekno */ null, - /* int[] bymonth */ null, - /* int[] bysetpos */ null, - /* int wkst */ EventRecurrence.MO - ); - } - - // Sample coming from RFC2445 - public void test18() throws Exception { - verifyRecurType("FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10", - /* int freq */ EventRecurrence.YEARLY, - /* String until */ null, - /* int count */ 0, - /* int interval */ 0, - /* int[] bysecond */ null, - /* int[] byminute */ null, - /* int[] byhour */ null, - /* int[] byday */ new int[] { - EventRecurrence.SU - }, - /* int[] bydayNum */ new int[] { -1 }, - /* int[] bymonthday */ null, - /* int[] byyearday */ null, - /* int[] byweekno */ null, - /* int[] bymonth */ new int[] { 10 }, - /* int[] bysetpos */ null, - /* int wkst */ EventRecurrence.MO - ); - } - - // Sample coming from bug #1640517 - public void test19() throws Exception { - verifyRecurType("FREQ=YEARLY;BYMONTH=3;BYDAY=TH", - /* int freq */ EventRecurrence.YEARLY, - /* String until */ null, - /* int count */ 0, - /* int interval */ 0, - /* int[] bysecond */ null, - /* int[] byminute */ null, - /* int[] byhour */ null, - /* int[] byday */ new int[] { - EventRecurrence.TH - }, - /* int[] bydayNum */ new int[] { 0 }, - /* int[] bymonthday */ null, - /* int[] byyearday */ null, - /* int[] byweekno */ null, - /* int[] bymonth */ new int[] { 3 }, - /* int[] bysetpos */ null, - /* int wkst */ EventRecurrence.MO - ); - } - - // INTERVAL = 0 -> Interval = 1 bug #5676414 - public void test20() throws Exception { - verifyRecurType("FREQ=YEARLY;BYMONTHDAY=18;BYMONTH=10;INTERVAL=0;", - /* int freq */ EventRecurrence.YEARLY, - /* String until */ null, - /* int count */ 0, - /* int interval */ 1, - /* int[] bysecond */ null, - /* int[] byminute */ null, - /* int[] byhour */ null, - /* int[] byday */ null, - /* int[] bydayNum */ null, - /* int[] bymonthday */ new int[]{18}, - /* int[] byyearday */ null, - /* int[] byweekno */ null, - /* int[] bymonth */ new int[]{10}, - /* int[] bysetpos */ null, - /* int wkst */ EventRecurrence.MO - ); - } - - // Working case: INTERVAL=1 -> Interval = 1 bug #5676414 - public void test21() throws Exception { - verifyRecurType("FREQ=WEEKLY;WKST=SU;INTERVAL=1;BYDAY=MO,TU,WE,TH,FR", - /* int freq */ EventRecurrence.WEEKLY, - /* String until */ null, - /* int count */ 0, - /* int interval */ 1, - /* int[] bysecond */ null, - /* int[] byminute */ null, - /* int[] byhour */ null, - /* int[] byday */ new int[] { - EventRecurrence.MO, - EventRecurrence.TU, - EventRecurrence.WE, - EventRecurrence.TH, - EventRecurrence.FR, - }, - /* int[] bydayNum */ new int[]{0, 0, 0, 0, 0}, - /* int[] bymonthday */ null, - /* int[] byyearday */ null, - /* int[] byweekno */ null, - /* int[] bymonth */ null, - /* int[] bysetpos */ null, - /* int wkst */ EventRecurrence.SU - ); - } - - // Working case: INTERVAL=2 -> Interval = 2 bug #5676414 - public void test22() throws Exception { - verifyRecurType("FREQ=WEEKLY;WKST=SU;INTERVAL=2;BYDAY=MO,TU,WE,TH,FR", - /* int freq */ EventRecurrence.WEEKLY, - /* String until */ null, - /* int count */ 0, - /* int interval */ 2, - /* int[] bysecond */ null, - /* int[] byminute */ null, - /* int[] byhour */ null, - /* int[] byday */ new int[] { - EventRecurrence.MO, - EventRecurrence.TU, - EventRecurrence.WE, - EventRecurrence.TH, - EventRecurrence.FR, - }, - /* int[] bydayNum */ new int[]{0, 0, 0, 0, 0}, - /* int[] bymonthday */ null, - /* int[] byyearday */ null, - /* int[] byweekno */ null, - /* int[] bymonth */ null, - /* int[] bysetpos */ null, - /* int wkst */ EventRecurrence.SU - ); - } - - // COUNT < 0 -> Count = 1 bug #5676414 - public void test23() throws Exception { - verifyRecurType("FREQ=WEEKLY;COUNT=-20;BYDAY=MO,TU,WE,TH,FR;", - /* int freq */ EventRecurrence.WEEKLY, - /* String until */ null, - /* int count */ 1, - /* int interval */ 0, - /* int[] bysecond */ null, - /* int[] byminute */ null, - /* int[] byhour */ null, - /* int[] byday */ new int[] { - EventRecurrence.MO, - EventRecurrence.TU, - EventRecurrence.WE, - EventRecurrence.TH, - EventRecurrence.FR, - }, - /* int[] bydayNum */ new int[]{0, 0, 0, 0, 0}, - /* int[] bymonthday */ null, - /* int[] byyearday */ null, - /* int[] byweekno */ null, - /* int[] bymonth */ null, - /* int[] bysetpos */ null, - /* int wkst */ EventRecurrence.MO - ); - } - - // Working case: COUNT=2 -> Count=2 bug #5676414 - public void test24() throws Exception { - verifyRecurType("FREQ=WEEKLY;COUNT=2;BYDAY=MO,TU,WE,TH,FR;", - /* int freq */ EventRecurrence.WEEKLY, - /* String until */ null, - /* int count */ 2, - /* int interval */ 0, - /* int[] bysecond */ null, - /* int[] byminute */ null, - /* int[] byhour */ null, - /* int[] byday */ new int[] { - EventRecurrence.MO, - EventRecurrence.TU, - EventRecurrence.WE, - EventRecurrence.TH, - EventRecurrence.FR, - }, - /* int[] bydayNum */ new int[]{0, 0, 0, 0, 0}, - /* int[] bymonthday */ null, - /* int[] byyearday */ null, - /* int[] byweekno */ null, - /* int[] bymonth */ null, - /* int[] bysetpos */ null, - /* int wkst */ EventRecurrence.MO - ); - } - - // for your copying pleasure - public void fakeTestXX() throws Exception { - verifyRecurType("FREQ=DAILY;", - /* int freq */ EventRecurrence.DAILY, - /* String until */ null, - /* int count */ 0, - /* int interval */ 0, - /* int[] bysecond */ null, - /* int[] byminute */ null, - /* int[] byhour */ null, - /* int[] byday */ null, - /* int[] bydayNum */ null, - /* int[] bymonthday */ null, - /* int[] byyearday */ null, - /* int[] byweekno */ null, - /* int[] bymonth */ null, - /* int[] bysetpos */ null, - /* int wkst */ EventRecurrence.MO - ); - } - - private static void cmp(int vlen, int[] v, int[] correct, String name) { - if ((correct == null && v != null) - || (correct != null && v == null)) { - throw new RuntimeException("One is null, one isn't for " + name - + ": correct=" + Arrays.toString(correct) - + " actual=" + Arrays.toString(v)); - } - if ((correct == null && vlen != 0) - || (vlen != (correct == null ? 0 : correct.length))) { - throw new RuntimeException("Reported length mismatch for " + name - + ": correct=" + ((correct == null) ? "null" : correct.length) - + " actual=" + vlen); - } - if (correct == null) { - return; - } - if (v.length < correct.length) { - throw new RuntimeException("Array length mismatch for " + name - + ": correct=" + Arrays.toString(correct) - + " actual=" + Arrays.toString(v)); - } - for (int i = 0; i < correct.length; i++) { - if (v[i] != correct[i]) { - throw new RuntimeException("Array value mismatch for " + name - + ": correct=" + Arrays.toString(correct) - + " actual=" + Arrays.toString(v)); - } - } - } - - private static boolean eq(String a, String b) { - if ((a == null && b != null) || (a != null && b == null)) { - return false; - } else { - return a == b || a.equals(b); - } - } - - private static void verifyRecurType(String recur, - int freq, String until, int count, int interval, - int[] bysecond, int[] byminute, int[] byhour, - int[] byday, int[] bydayNum, int[] bymonthday, - int[] byyearday, int[] byweekno, int[] bymonth, - int[] bysetpos, int wkst) { - EventRecurrence eventRecurrence = new EventRecurrence(); - eventRecurrence.parse(recur); - if (eventRecurrence.freq != freq - || !eq(eventRecurrence.until, until) - || eventRecurrence.count != count - || eventRecurrence.interval != interval - || eventRecurrence.wkst != wkst) { - System.out.println("Error... got:"); - print(eventRecurrence); - System.out.println("expected:"); - System.out.println("{"); - System.out.println(" freq=" + freq); - System.out.println(" until=" + until); - System.out.println(" count=" + count); - System.out.println(" interval=" + interval); - System.out.println(" wkst=" + wkst); - System.out.println(" bysecond=" + Arrays.toString(bysecond)); - System.out.println(" byminute=" + Arrays.toString(byminute)); - System.out.println(" byhour=" + Arrays.toString(byhour)); - System.out.println(" byday=" + Arrays.toString(byday)); - System.out.println(" bydayNum=" + Arrays.toString(bydayNum)); - System.out.println(" bymonthday=" + Arrays.toString(bymonthday)); - System.out.println(" byyearday=" + Arrays.toString(byyearday)); - System.out.println(" byweekno=" + Arrays.toString(byweekno)); - System.out.println(" bymonth=" + Arrays.toString(bymonth)); - System.out.println(" bysetpos=" + Arrays.toString(bysetpos)); - System.out.println("}"); - throw new RuntimeException("Mismatch in fields"); - } - cmp(eventRecurrence.bysecondCount, eventRecurrence.bysecond, bysecond, "bysecond"); - cmp(eventRecurrence.byminuteCount, eventRecurrence.byminute, byminute, "byminute"); - cmp(eventRecurrence.byhourCount, eventRecurrence.byhour, byhour, "byhour"); - cmp(eventRecurrence.bydayCount, eventRecurrence.byday, byday, "byday"); - cmp(eventRecurrence.bydayCount, eventRecurrence.bydayNum, bydayNum, "bydayNum"); - cmp(eventRecurrence.bymonthdayCount, eventRecurrence.bymonthday, bymonthday, "bymonthday"); - cmp(eventRecurrence.byyeardayCount, eventRecurrence.byyearday, byyearday, "byyearday"); - cmp(eventRecurrence.byweeknoCount, eventRecurrence.byweekno, byweekno, "byweekno"); - cmp(eventRecurrence.bymonthCount, eventRecurrence.bymonth, bymonth, "bymonth"); - cmp(eventRecurrence.bysetposCount, eventRecurrence.bysetpos, bysetpos, "bysetpos"); - } - - private static void print(EventRecurrence er) { - System.out.println("{"); - System.out.println(" freq=" + er.freq); - System.out.println(" until=" + er.until); - System.out.println(" count=" + er.count); - System.out.println(" interval=" + er.interval); - System.out.println(" wkst=" + er.wkst); - System.out.println(" bysecond=" + Arrays.toString(er.bysecond)); - System.out.println(" bysecondCount=" + er.bysecondCount); - System.out.println(" byminute=" + Arrays.toString(er.byminute)); - System.out.println(" byminuteCount=" + er.byminuteCount); - System.out.println(" byhour=" + Arrays.toString(er.byhour)); - System.out.println(" byhourCount=" + er.byhourCount); - System.out.println(" byday=" + Arrays.toString(er.byday)); - System.out.println(" bydayNum=" + Arrays.toString(er.bydayNum)); - System.out.println(" bydayCount=" + er.bydayCount); - System.out.println(" bymonthday=" + Arrays.toString(er.bymonthday)); - System.out.println(" bymonthdayCount=" + er.bymonthdayCount); - System.out.println(" byyearday=" + Arrays.toString(er.byyearday)); - System.out.println(" byyeardayCount=" + er.byyeardayCount); - System.out.println(" byweekno=" + Arrays.toString(er.byweekno)); - System.out.println(" byweeknoCount=" + er.byweeknoCount); - System.out.println(" bymonth=" + Arrays.toString(er.bymonth)); - System.out.println(" bymonthCount=" + er.bymonthCount); - System.out.println(" bysetpos=" + Arrays.toString(er.bysetpos)); - System.out.println(" bysetposCount=" + er.bysetposCount); - System.out.println("}"); - } - - - /** A list of valid rules. The parser must accept these. */ - private static final String[] GOOD_RRULES = { - /* extracted wholesale from from RFC 2445 section 4.8.5.4 */ - "FREQ=DAILY;COUNT=10", - "FREQ=DAILY;UNTIL=19971224T000000Z", - "FREQ=DAILY;INTERVAL=2", - "FREQ=DAILY;INTERVAL=10;COUNT=5", - "FREQ=YEARLY;UNTIL=20000131T090000Z;BYMONTH=1;BYDAY=SU,MO,TU,WE,TH,FR,SA", - "FREQ=DAILY;UNTIL=20000131T090000Z;BYMONTH=1", - "FREQ=WEEKLY;COUNT=10", - "FREQ=WEEKLY;UNTIL=19971224T000000Z", - "FREQ=WEEKLY;INTERVAL=2;WKST=SU", - "FREQ=WEEKLY;UNTIL=19971007T000000Z;WKST=SU;BYDAY=TU,TH", - "FREQ=WEEKLY;COUNT=10;WKST=SU;BYDAY=TU,TH", - "FREQ=WEEKLY;INTERVAL=2;UNTIL=19971224T000000Z;WKST=SU;BYDAY=MO,WE,FR", - "FREQ=WEEKLY;INTERVAL=2;COUNT=8;WKST=SU;BYDAY=TU,TH", - "FREQ=MONTHLY;COUNT=10;BYDAY=1FR", - "FREQ=MONTHLY;UNTIL=19971224T000000Z;BYDAY=1FR", - "FREQ=MONTHLY;INTERVAL=2;COUNT=10;BYDAY=1SU,-1SU", - "FREQ=MONTHLY;COUNT=6;BYDAY=-2MO", - "FREQ=MONTHLY;BYMONTHDAY=-3", - "FREQ=MONTHLY;COUNT=10;BYMONTHDAY=2,15", - "FREQ=MONTHLY;COUNT=10;BYMONTHDAY=1,-1", - "FREQ=MONTHLY;INTERVAL=18;COUNT=10;BYMONTHDAY=10,11,12,13,14,15", - "FREQ=MONTHLY;INTERVAL=2;BYDAY=TU", - "FREQ=YEARLY;COUNT=10;BYMONTH=6,7", - "FREQ=YEARLY;INTERVAL=2;COUNT=10;BYMONTH=1,2,3", - "FREQ=YEARLY;INTERVAL=3;COUNT=10;BYYEARDAY=1,100,200", - "FREQ=YEARLY;BYDAY=20MO", - "FREQ=YEARLY;BYWEEKNO=20;BYDAY=MO", - "FREQ=YEARLY;BYMONTH=3;BYDAY=TH", - "FREQ=YEARLY;BYDAY=TH;BYMONTH=6,7,8", - "FREQ=MONTHLY;BYDAY=FR;BYMONTHDAY=13", - "FREQ=MONTHLY;BYDAY=SA;BYMONTHDAY=7,8,9,10,11,12,13", - "FREQ=YEARLY;INTERVAL=4;BYMONTH=11;BYDAY=TU;BYMONTHDAY=2,3,4,5,6,7,8", - "FREQ=MONTHLY;COUNT=3;BYDAY=TU,WE,TH;BYSETPOS=3", - "FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-2", - "FREQ=HOURLY;INTERVAL=3;UNTIL=19970902T170000Z", - "FREQ=MINUTELY;INTERVAL=15;COUNT=6", - "FREQ=MINUTELY;INTERVAL=90;COUNT=4", - "FREQ=DAILY;BYHOUR=9,10,11,12,13,14,15,16;BYMINUTE=0,20,40", - "FREQ=MINUTELY;INTERVAL=20;BYHOUR=9,10,11,12,13,14,15,16", - "FREQ=WEEKLY;INTERVAL=2;COUNT=4;BYDAY=TU,SU;WKST=MO", - "FREQ=WEEKLY;INTERVAL=2;COUNT=4;BYDAY=TU,SU;WKST=SU", - /* a few more */ - "FREQ=SECONDLY;BYSECOND=0,15,59", - "FREQ=MINUTELY;BYMINUTE=0,15,59", - "FREQ=HOURLY;BYHOUR=+0,+15,+23", - "INTERVAL=4;FREQ=YEARLY", - "FREQ=DAILY;X-WHATEVER=blah", - //"freq=daily;wkst=su", // mixed case currently not allowed - "FREQ=WEEKLY;INTERVAL=2;BYDAY=Mo;;UNTIL=20120327T000000Z", // double simicolon should be allowed - "FREQ=MONTHLY;BYDAY=1Mo", - "FREQ=MONTHLY;BYDAY=2Mo,2We,4Mo,4We", - "FREQ=MONTHLY;WKST=SU;BYMONTHDAY=25;UNTIL=20110524", - "FREQ=WEEKLY;BYDAY=MO;WKST=SU;UNTIL=20111218T010000Z" - }; - - /** The parser must reject these. */ - private static final String[] BAD_RRULES = { - "FREQ=MONTHLY;FREQ=MONTHLY", // can't specify twice - "FREQ=MONTHLY;COUNT=1;COUNT=1", // can't specify twice - "FREQ=SECONDLY;BYSECOND=60", // range - "FREQ=MINUTELY;BYMINUTE=-1", // range - "FREQ=HOURLY;BYHOUR=24", // range - "FREQ=YEARLY;BYMONTHDAY=0", // zero not valid - "BYMONTHDAY=1", // must specify FREQ - //"FREQ=YEARLY;COUNT=1;UNTIL=12345", // can't have both COUNT and UNTIL - //"FREQ=DAILY;UNTIL=19970829T021400e", // invalid date - }; - - /** - * Simple test of good/bad rules. - */ - @SmallTest - public void testBasicParse() { - for (String rule : GOOD_RRULES) { - EventRecurrence recur = new EventRecurrence(); - recur.parse(rule); - } - - for (String rule : BAD_RRULES) { - EventRecurrence recur = new EventRecurrence(); - boolean didThrow = false; - - try { - recur.parse(rule); - } catch (InvalidFormatException ife) { - didThrow = true; - } - - assertTrue("Expected throw on " + rule, didThrow); - } - } -} diff --git a/tests/src/com/android/calendarcommon/RRuleTest.java b/tests/src/com/android/calendarcommon/RRuleTest.java deleted file mode 100644 index 743e236..0000000 --- a/tests/src/com/android/calendarcommon/RRuleTest.java +++ /dev/null @@ -1,756 +0,0 @@ -/* -** -** Copyright 2009, The Android Open Source Project -** -** Licensed under the Apache License, Version 2.0 (the "License"); -** you may not use this file except in compliance with the License. -** You may obtain a copy of the License at -** -** http://www.apache.org/licenses/LICENSE-2.0 -** -** Unless required by applicable law or agreed to in writing, software -** distributed under the License is distributed on an "AS IS" BASIS, -** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -** See the License for the specific language governing permissions and -** limitations under the License. -*/ - -/* Based on http://code.google.com/p/google-rfc-2445/source/browse/trunk/test/com/google/ical/iter/RRuleIteratorImplTest.java */ - -package com.android.calendarcommon; - -import com.android.calendarcommon.RecurrenceSet; - -import android.os.Debug; -import android.test.suitebuilder.annotation.MediumTest; -import android.test.suitebuilder.annotation.Suppress; -import android.text.format.Time; -import junit.framework.TestCase; - -/** - * You can run those tests with: - * - * adb shell am instrument - * -e debug false - * -w - * -e - * class com.android.providers.calendar.RRuleTest - * com.android.providers.calendar.tests/android.test.InstrumentationTestRunner - */ - -public class RRuleTest extends TestCase { - private static final String TAG = "RRuleTest"; - private static final boolean METHOD_TRACE = false; - - private static String[] getFormattedDates(long[] dates, Time time, boolean truncate) { - String[] out = new String[dates.length]; - int i = 0; - for (long date : dates) { - time.set(date); - if (truncate) { - out[i] = time.format2445().substring(0, 8); // Just YYMMDD - } else { - out[i] = time.format2445().substring(0, 15); // YYMMDDThhmmss - } - ++i; - } - return out; - } - - static final String PST = "America/Los_Angeles"; - static final String UTC = "UTC"; - // Use this date as end of recurrence unlessotherwise specified. - static final String DEFAULT_END = "20091212"; - - private void runRecurrenceIteratorTest(String rruleText, String dtStart, int limit, - String golden) throws Exception { - runRecurrenceIteratorTest(rruleText, dtStart, limit, golden, null, null, UTC); - } - - private void runRecurrenceIteratorTest(String rrule, String dtstartStr, int limit, - String golden, String advanceTo, String tz) throws Exception { - runRecurrenceIteratorTest(rrule, dtstartStr, limit, golden, advanceTo, null, tz); - } - - /** - * Tests a recurrence rule - * @param rrule The rule to expand - * @param dtstartStr The dtstart to use - * @param limit Maximum number of entries to expand. if there are more, "..." is appended to - * the result. Note that Android's recurrence expansion doesn't support expanding n results, - * so this is faked by expanding until the endAt date, and then taking limit results. - * @param golden The desired results - * @param advanceTo The starting date for expansion. dtstartStr is used if null is passed in. - * @param endAt The ending date. DEFAULT_END is used if null is passed in. - * @param tz The time zone. UTC is used if null is passed in. - * @throws Exception if anything goes wrong. - */ - private void runRecurrenceIteratorTest(String rrule, String dtstartStr, int limit, - String golden, String advanceTo, String endAt, String tz) throws Exception { - - String rdate = ""; - String exrule = ""; - String exdate = ""; - rrule = rrule.replace("RRULE:", ""); - // RecurrenceSet does not support folding of lines, so fold here - rrule = rrule.replace("\n ", ""); - - Time dtstart = new Time(tz); - Time rangeStart = new Time(tz); - Time rangeEnd = new Time(tz); - Time outCal = new Time(tz); - - dtstart.parse(dtstartStr); - if (advanceTo == null) { - advanceTo = dtstartStr; - } - if (endAt == null) { - endAt = DEFAULT_END; - } - - rangeStart.parse(advanceTo); - rangeEnd.parse(endAt); - - - RecurrenceProcessor rp = new RecurrenceProcessor(); - RecurrenceSet recur = new RecurrenceSet(rrule, rdate, exrule, exdate); - - long[] out = rp.expand(dtstart, recur, rangeStart.toMillis(false /* use isDst */), - rangeEnd.toMillis(false /* use isDst */)); - - if (METHOD_TRACE) { - Debug.stopMethodTracing(); - } - - boolean truncate = dtstartStr.length() <= 8; // Just date, not date-time - String[] actual = getFormattedDates(out, outCal, truncate); - - StringBuilder sb = new StringBuilder(); - int k = 0; - while (k < actual.length && --limit >= 0) { - if (k != 0) { - sb.append(','); - } - sb.append(actual[k]); - k++; - } - if (limit < 0) { - sb.append(",..."); - } - assertEquals(golden, sb.toString()); - } - - // Infinite loop, bug 1662110 - @MediumTest - public void testFrequencyLimits() throws Exception { - try { - runRecurrenceIteratorTest("RRULE:FREQ=SECONDLY;BYSECOND=0,1,2,3,4,5,6,7,8,9,10,11,12,13,14," + "15,16,17,18,19,20,21,22,23,24,25,26,27,28,29," + "30,31,32,33,34,35,36,37,38,39,40,41,42,43,44," + "45,46,47,48,49,50,51,52,53,54,55,56,57,58,59", "20000101", 1, ""); - fail("Don't do that"); - } catch (DateException ex) { - // pass - } - } - - @MediumTest - public void testSimpleDaily() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=DAILY", "20060120", 5, "20060120,20060121,20060122,20060123,20060124,..."); - } - - @MediumTest - public void testSimpleWeekly() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY", "20060120", 5, "20060120,20060127,20060203,20060210,20060217,..."); - } - - @MediumTest - public void testSimpleMonthly() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY", "20060120", 5, "20060120,20060220,20060320,20060420,20060520,..."); - } - - @MediumTest - public void testSimpleYearly() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY", "20060120", 5, "20060120,20070120,20080120,20090120,20100120,...", null, "20120101", UTC); - } - - // from section 4.3.10 - @MediumTest - public void testMultipleByParts() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=2;BYMONTH=1;BYDAY=SU", "19970105", 8, "19970105,19970112,19970119,19970126," + "19990103,19990110,19990117,19990124,..."); - } - - @MediumTest - public void testCountWithInterval() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=DAILY;COUNT=10;INTERVAL=2", "19970105", 11, "19970105,19970107,19970109,19970111,19970113," + "19970115,19970117,19970119,19970121,19970123"); - } - - // from section 4.6.5 - // Fails: wrong dates - @MediumTest - @Suppress - public void testNegativeOffsetsA() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10", "19970105", 5, "19971026,19981025,19991031,20001029,20011028,..."); - } - - // Fails: wrong dates - @MediumTest - @Suppress - public void testNegativeOffsetsB() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=4", "19970105", 5, "19970406,19980405,19990404,20000402,20010401,..."); - } - - // Fails: wrong dates - @MediumTest - @Suppress - public void testNegativeOffsetsC() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=4;UNTIL=19980404T150000Z", "19970105", 5, "19970406"); - } - - // feom section 4.8.5.4 - @MediumTest - public void testDailyFor10Occ() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=DAILY;COUNT=10", "19970902T090000", 11, "19970902T090000,19970903T090000,19970904T090000,19970905T090000," + "19970906T090000,19970907T090000,19970908T090000,19970909T090000," + "19970910T090000,19970911T090000"); - - } - - @MediumTest - public void testDailyUntilDec4() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=DAILY;UNTIL=19971204", "19971128", 11, "19971128,19971129,19971130,19971201,19971202,19971203,19971204"); - } - - // Fails: infinite loop - @MediumTest - @Suppress - public void testEveryOtherDayForever() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=DAILY;INTERVAL=2", "19971128", 5, "19971128,19971130,19971202,19971204,19971206,..."); - } - - @MediumTest - public void testEvery10Days5Occ() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=DAILY;INTERVAL=10;COUNT=5", "19970902", 5, "19970902,19970912,19970922,19971002,19971012"); - } - - @MediumTest - public void testWeeklyFor10Occ() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;COUNT=10", "19970902", 10, "19970902,19970909,19970916,19970923,19970930," + "19971007,19971014,19971021,19971028,19971104"); - } - - @MediumTest - public void testWeeklyUntilDec24() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;UNTIL=19971224", "19970902", 25, "19970902,19970909,19970916,19970923,19970930," + "19971007,19971014,19971021,19971028,19971104," + "19971111,19971118,19971125,19971202,19971209," + "19971216,19971223"); - } - - @MediumTest - public void testEveryOtherWeekForever() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;INTERVAL=2;WKST=SU", "19970902", 11, "19970902,19970916,19970930,19971014,19971028," + "19971111,19971125,19971209,19971223,19980106," + "19980120,..."); - } - - @MediumTest - public void testWeeklyOnTuesdayAndThursdayFor5Weeks() throws Exception { - // if UNTIL date does not match start date, then until date treated as - // occurring on midnight. - runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;UNTIL=19971007;WKST=SU;BYDAY=TU,TH", "19970902T090000", 11, "19970902T090000,19970904T090000,19970909T090000,19970911T090000," + "19970916T090000,19970918T090000,19970923T090000,19970925T090000," + "19970930T090000,19971002T090000"); - runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;UNTIL=19971007T000000Z;WKST=SU;BYDAY=TU,TH", "19970902T090000", 11, "19970902T090000,19970904T090000,19970909T090000,19970911T090000," + "19970916T090000,19970918T090000,19970923T090000,19970925T090000," + "19970930T090000,19971002T090000"); - runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;COUNT=10;WKST=SU;BYDAY=TU,TH", "19970902", 11, "19970902,19970904,19970909,19970911,19970916," + "19970918,19970923,19970925,19970930,19971002"); - } - - @MediumTest - public void testEveryOtherWeekOnMWFUntilDec24() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;INTERVAL=2;UNTIL=19971224T000000Z;WKST=SU;\n" + " BYDAY=MO,WE,FR", "19970903T090000", 25, "19970903T090000,19970905T090000,19970915T090000,19970917T090000," + "19970919T090000,19970929T090000,19971001T090000,19971003T090000," + "19971013T090000,19971015T090000,19971017T090000,19971027T090000," + "19971029T090000,19971031T090000,19971110T090000,19971112T090000," + "19971114T090000,19971124T090000,19971126T090000,19971128T090000," + "19971208T090000,19971210T090000,19971212T090000,19971222T090000"); - } - - @MediumTest - public void testEveryOtherWeekOnMWFUntilDec24a() throws Exception { - // if the UNTIL date is timed, when the start is not, the time should be - // ignored, so we get one more instance - runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;INTERVAL=2;UNTIL=19971224T000000Z;WKST=SU;\n" + " BYDAY=MO,WE,FR", "19970903", 25, "19970903,19970905,19970915,19970917," + "19970919,19970929,19971001,19971003," + "19971013,19971015,19971017,19971027," + "19971029,19971031,19971110,19971112," + "19971114,19971124,19971126,19971128," + "19971208,19971210,19971212,19971222," + "19971224"); - } - - // Fails with wrong times - @MediumTest - @Suppress - public void testEveryOtherWeekOnMWFUntilDec24b() throws Exception { - // test with an alternate timezone - runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;INTERVAL=2;UNTIL=19971224T090000Z;WKST=SU;\n" + " BYDAY=MO,WE,FR", "19970903T090000", 25, "19970903T160000,19970905T160000,19970915T160000,19970917T160000," + "19970919T160000,19970929T160000,19971001T160000,19971003T160000," + "19971013T160000,19971015T160000,19971017T160000,19971027T170000," + "19971029T170000,19971031T170000,19971110T170000,19971112T170000," + "19971114T170000,19971124T170000,19971126T170000,19971128T170000," + "19971208T170000,19971210T170000,19971212T170000,19971222T170000", null, PST); - } - - @MediumTest - public void testEveryOtherWeekOnTuThFor8Occ() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;INTERVAL=2;COUNT=8;WKST=SU;BYDAY=TU,TH", "19970902", 8, "19970902,19970904,19970916,19970918,19970930," + "19971002,19971014,19971016"); - } - - @MediumTest - public void testMonthlyOnThe1stFridayFor10Occ() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;COUNT=10;BYDAY=1FR", "19970905", 10, "19970905,19971003,19971107,19971205,19980102," + "19980206,19980306,19980403,19980501,19980605"); - } - - @MediumTest - public void testMonthlyOnThe1stFridayUntilDec24() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;UNTIL=19971224T000000Z;BYDAY=1FR", "19970905", 4, "19970905,19971003,19971107,19971205"); - } - - @MediumTest - public void testEveryOtherMonthOnThe1stAndLastSundayFor10Occ() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;INTERVAL=2;COUNT=10;BYDAY=1SU,-1SU", "19970907", 10, "19970907,19970928,19971102,19971130,19980104," + "19980125,19980301,19980329,19980503,19980531"); - } - - @MediumTest - public void testMonthlyOnTheSecondToLastMondayOfTheMonthFor6Months() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;COUNT=6;BYDAY=-2MO", "19970922", 6, "19970922,19971020,19971117,19971222,19980119," + "19980216"); - } - - @MediumTest - public void testMonthlyOnTheThirdToLastDay() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;BYMONTHDAY=-3", "19970928", 6, "19970928,19971029,19971128,19971229,19980129,19980226,..."); - } - - @MediumTest - public void testMonthlyOnThe2ndAnd15thFor10Occ() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;COUNT=10;BYMONTHDAY=2,15", "19970902", 10, "19970902,19970915,19971002,19971015,19971102," + "19971115,19971202,19971215,19980102,19980115"); - } - - @MediumTest - public void testMonthlyOnTheFirstAndLastFor10Occ() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;COUNT=10;BYMONTHDAY=1,-1", "19970930", 10, "19970930,19971001,19971031,19971101,19971130," + "19971201,19971231,19980101,19980131,19980201"); - } - - @MediumTest - public void testEvery18MonthsOnThe10thThru15thFor10Occ() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;INTERVAL=18;COUNT=10;BYMONTHDAY=10,11,12,13,14,\n" + " 15", "19970910", 10, "19970910,19970911,19970912,19970913,19970914," + "19970915,19990310,19990311,19990312,19990313"); - } - - @MediumTest - public void testEveryTuesdayEveryOtherMonth() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;INTERVAL=2;BYDAY=TU", "19970902", 18, "19970902,19970909,19970916,19970923,19970930," + "19971104,19971111,19971118,19971125,19980106," + "19980113,19980120,19980127,19980303,19980310," + "19980317,19980324,19980331,..."); - } - - @MediumTest - public void testYearlyInJuneAndJulyFor10Occurrences() throws Exception { - // Note: Since none of the BYDAY, BYMONTHDAY or BYYEARDAY components - // are specified, the day is gotten from DTSTART - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;COUNT=10;BYMONTH=6,7", "19970610", 10, "19970610,19970710,19980610,19980710,19990610," + "19990710,20000610,20000710,20010610,20010710"); - } - - @MediumTest - public void testEveryOtherYearOnJanuaryFebruaryAndMarchFor10Occurrences() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=2;COUNT=10;BYMONTH=1,2,3", "19970310", 10, "19970310,19990110,19990210,19990310,20010110," + "20010210,20010310,20030110,20030210,20030310"); - } - - //Fails: wrong dates - @MediumTest - @Suppress - public void testEvery3rdYearOnThe1st100thAnd200thDayFor10Occurrences() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=3;COUNT=10;BYYEARDAY=1,100,200", "19970101", 10, "19970101,19970410,19970719,20000101,20000409," + "20000718,20030101,20030410,20030719,20060101"); - } - - // Fails: infinite loop - @MediumTest - @Suppress - public void testEvery20thMondayOfTheYearForever() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYDAY=20MO", "19970519", 3, "19970519,19980518,19990517,..."); - } - - // Fails: generates wrong dates - @MediumTest - @Suppress - public void testMondayOfWeekNumber20WhereTheDefaultStartOfTheWeekIsMonday() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYWEEKNO=20;BYDAY=MO", "19970512", 3, "19970512,19980511,19990517,..."); - } - - @MediumTest - public void testEveryThursdayInMarchForever() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=TH", "19970313", 11, "19970313,19970320,19970327,19980305,19980312," + "19980319,19980326,19990304,19990311,19990318," + "19990325,..."); - } - - //Fails: wrong dates - @MediumTest - @Suppress - public void testEveryThursdayButOnlyDuringJuneJulyAndAugustForever() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYDAY=TH;BYMONTH=6,7,8", "19970605", 39, "19970605,19970612,19970619,19970626,19970703," + "19970710,19970717,19970724,19970731,19970807," + "19970814,19970821,19970828,19980604,19980611," + "19980618,19980625,19980702,19980709,19980716," + "19980723,19980730,19980806,19980813,19980820," + "19980827,19990603,19990610,19990617,19990624," + "19990701,19990708,19990715,19990722,19990729," + "19990805,19990812,19990819,19990826,..."); - } - - //Fails: infinite loop - @MediumTest - @Suppress - public void testEveryFridayThe13thForever() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;BYDAY=FR;BYMONTHDAY=13", "19970902", 5, "19980213,19980313,19981113,19990813,20001013," + "..."); - } - - @MediumTest - public void testTheFirstSaturdayThatFollowsTheFirstSundayOfTheMonthForever() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;BYDAY=SA;BYMONTHDAY=7,8,9,10,11,12,13", "19970913", 10, "19970913,19971011,19971108,19971213,19980110," + "19980207,19980307,19980411,19980509,19980613," + "..."); - } - - @MediumTest - public void testEvery4YearsThe1stTuesAfterAMonInNovForever() throws Exception { - // US Presidential Election Day - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=4;BYMONTH=11;BYDAY=TU;BYMONTHDAY=2,3,4,\n" + " 5,6,7,8", "19961105", 3, "19961105,20001107,20041102,..."); - } - - // Fails: wrong dates - @MediumTest - @Suppress - public void testThe3rdInstanceIntoTheMonthOfOneOfTuesWedThursForNext3Months() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;COUNT=3;BYDAY=TU,WE,TH;BYSETPOS=3", "19970904", 3, "19970904,19971007,19971106"); - } - - // Fails: wrong dates - @MediumTest - @Suppress - public void testThe2ndToLastWeekdayOfTheMonth() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-2", "19970929", 7, "19970929,19971030,19971127,19971230,19980129," + "19980226,19980330,..."); - } - - // Fails: infinite loop - @MediumTest - @Suppress - public void testEvery3HoursFrom900AmTo500PmOnASpecificDay() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=HOURLY;INTERVAL=3;UNTIL=19970903T010000Z", "19970902", 7, "00000902,19970909,19970900,19970912,19970900," + "19970915,19970900"); - } - - // Fails: infinite loop - @MediumTest - @Suppress - public void testEvery15MinutesFor6Occurrences() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=MINUTELY;INTERVAL=15;COUNT=6", "19970902", 13, "00000902,19970909,19970900,19970909,19970915," + "19970909,19970930,19970909,19970945,19970910," + "19970900,19970910,19970915"); - } - - @MediumTest - @Suppress - public void testEveryHourAndAHalfFor4Occurrences() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=MINUTELY;INTERVAL=90;COUNT=4", "19970902", 9, "00000902,19970909,19970900,19970910,19970930," + "19970912,19970900,19970913,19970930"); - } - - // Fails: wrong dates - @MediumTest - @Suppress - public void testAnExampleWhereTheDaysGeneratedMakesADifferenceBecauseOfWkst() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;INTERVAL=2;COUNT=4;BYDAY=TU,SU;WKST=MO", "19970805", 4, "19970805,19970810,19970819,19970824"); - } - - // Fails: wrong dates - @MediumTest - @Suppress - public void testAnExampleWhereTheDaysGeneratedMakesADifferenceBecauseOfWkst2() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;INTERVAL=2;COUNT=4;BYDAY=TU,SU;WKST=SU", "19970805", 8, "19970805,19970817,19970819,19970831"); - } - - // Fails: wrong dates - @MediumTest - @Suppress - public void testWithByDayAndByMonthDayFilter() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;COUNT=4;BYDAY=TU,SU;" + "BYMONTHDAY=13,14,15,16,17,18,19,20", "19970805", 8, "19970817,19970819,19970914,19970916"); - } - - // Failed: wrong dates - @MediumTest - @Suppress - public void testAnnuallyInAugustOnTuesAndSunBetween13thAnd20th() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;COUNT=4;BYDAY=TU,SU;" + "BYMONTHDAY=13,14,15,16,17,18,19,20;BYMONTH=8", "19970605", 8, "19970817,19970819,19980816,19980818"); - } - - // Failed: wrong dates - @MediumTest - @Suppress - public void testLastDayOfTheYearIsASundayOrTuesday() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;COUNT=4;BYDAY=TU,SU;BYYEARDAY=-1", "19940605", 8, "19951231,19961231,20001231,20021231"); - } - - // Fails: wrong dates - @MediumTest - @Suppress - public void testLastWeekdayOfMonth() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;BYSETPOS=-1;BYDAY=-1MO,-1TU,-1WE,-1TH,-1FR", "19940605", 8, "19940630,19940729,19940831,19940930," + "19941031,19941130,19941230,19950131,..."); - } - - // Fails: generates wrong dates - @MediumTest - @Suppress - public void testMonthsThatStartOrEndOnFriday() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;BYMONTHDAY=1,-1;BYDAY=FR;COUNT=6", "19940605", 8, "19940701,19940930,19950331,19950630,19950901,19951201"); - } - - // Fails: can't go that far into future - @MediumTest - @Suppress - public void testCenturiesThatAreNotLeapYears() throws Exception { - // I can't think of a good reason anyone would want to specify both a - // month day and a year day, so here's a really contrived example - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=100;BYYEARDAY=60;BYMONTHDAY=1", "19000101", 4, "19000301,21000301,22000301,23000301,...", null, "25000101", UTC); - } - - // Fails: generates instances when it shouldn't - @MediumTest - @Suppress - public void testNoInstancesGenerated() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=DAILY;UNTIL=19990101", "20000101", 4, ""); - } - - // Fails: generates instances when it shouldn't - @MediumTest - @Suppress - public void testNoInstancesGenerated2() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYMONTH=2;BYMONTHDAY=30", "20000101", 4, ""); - } - - // Fails: generates instances when it shouldn't - @MediumTest - @Suppress - public void testNoInstancesGenerated3() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=4;BYYEARDAY=366", "20000101", 4, ""); - } - - //Fails: wrong dates - @MediumTest - @Suppress - public void testLastWeekdayOfMarch() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;BYMONTH=3;BYDAY=SA,SU;BYSETPOS=-1", "20000101", 4, "20000326,20010331,20020331,20030330,..."); - } - - // Fails: wrong dates - @MediumTest - @Suppress - public void testFirstWeekdayOfMarch() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;BYMONTH=3;BYDAY=SA,SU;BYSETPOS=1", "20000101", 4, "20000304,20010303,20020302,20030301,..."); - } - - // January 1999 - // Mo Tu We Th Fr Sa Su - // 1 2 3 // < 4 days, so not a week - // 4 5 6 7 8 9 10 - - // January 2000 - // Mo Tu We Th Fr Sa Su - // 1 2 // < 4 days, so not a week - // 3 4 5 6 7 8 9 - - // January 2001 - // Mo Tu We Th Fr Sa Su - // 1 2 3 4 5 6 7 - // 8 9 10 11 12 13 14 - - // January 2002 - // Mo Tu We Th Fr Sa Su - // 1 2 3 4 5 6 - // 7 8 9 10 11 12 13 - - /** - * Find the first weekday of the first week of the year. - * The first week of the year may be partial, and the first week is considered - * to be the first one with at least four days. - */ - // Fails: wrong dates - @MediumTest - @Suppress - public void testFirstWeekdayOfFirstWeekOfYear() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYWEEKNO=1;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=1", "19990101", 4, "19990104,20000103,20010101,20020101,..."); - } - - // Fails: wrong dates - @MediumTest - @Suppress - public void testFirstSundayOfTheYear1() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYWEEKNO=1;BYDAY=SU", "19990101", 4, "19990110,20000109,20010107,20020106,..."); - } - - // Fails: wrong dates - @MediumTest - @Suppress - public void testFirstSundayOfTheYear2() throws Exception { - // TODO(msamuel): is this right? - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYDAY=1SU", "19990101", 4, "19990103,20000102,20010107,20020106,..."); - } - - // Fails: wrong dates - @MediumTest - @Suppress - public void testFirstSundayOfTheYear3() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYDAY=SU;BYYEARDAY=1,2,3,4,5,6,7,8,9,10,11,12,13" + ";BYSETPOS=1", "19990101", 4, "19990103,20000102,20010107,20020106,..."); - } - - // Fails: wrong dates - @MediumTest - @Suppress - public void testFirstWeekdayOfYear() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=1", "19990101", 4, "19990101,20000103,20010101,20020101,..."); - } - - // Fails: wrong dates - @MediumTest - @Suppress - public void testLastWeekdayOfFirstWeekOfYear() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYWEEKNO=1;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1", "19990101", 4, "19990108,20000107,20010105,20020104,..."); - } - - // January 1999 - // Mo Tu We Th Fr Sa Su - // 1 2 3 - // 4 5 6 7 8 9 10 - // 11 12 13 14 15 16 17 - // 18 19 20 21 22 23 24 - // 25 26 27 28 29 30 31 - - // Fails: wrong dates - @MediumTest - @Suppress - public void testSecondWeekday1() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=2", "19990101", 4, "19990105,19990112,19990119,19990126,..."); - } - - // January 1997 - // Mo Tu We Th Fr Sa Su - // 1 2 3 4 5 - // 6 7 8 9 10 11 12 - // 13 14 15 16 17 18 19 - // 20 21 22 23 24 25 26 - // 27 28 29 30 31 - - // Fails: wrong dates - @MediumTest - @Suppress - public void testSecondWeekday2() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=2", "19970101", 4, "19970102,19970107,19970114,19970121,..."); - } - - // Fails: wrong dates - @MediumTest - @Suppress - public void testByYearDayAndByDayFilterInteraction() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYYEARDAY=15;BYDAY=3MO", "19990101", 4, "20010115,20070115,20180115,20240115,..."); - } - - // Fails: wrong dates - @MediumTest - @Suppress - public void testByDayWithNegWeekNoAsFilter() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;BYMONTHDAY=26;BYDAY=-1FR", "19990101", 4, "19990226,19990326,19991126,20000526,..."); - } - - // Fails: wrong dates - @MediumTest - @Suppress - public void testLastWeekOfTheYear() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYWEEKNO=-1", "19990101", 6, "19991227,19991228,19991229,19991230,19991231,20001225,..."); - } - - // Fails: not enough dates generated - @MediumTest - @Suppress - public void testUserSubmittedTest1() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;INTERVAL=2;WKST=WE;BYDAY=SU,TU,TH,SA" + ";UNTIL=20000215T113000Z", "20000127T033000", 20, "20000127T033000,20000129T033000,20000130T033000,20000201T033000," + "20000210T033000,20000212T033000,20000213T033000,20000215T033000"); - } - - @MediumTest - public void testAdvanceTo1() throws Exception { - // a bunch of tests grabbed from above with an advance-to date tacked on - - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=TH", "19970313", 11, - /*"19970313,19970320,19970327,"*/"19980305,19980312," + "19980319,19980326,19990304,19990311,19990318," + "19990325,20000302,20000309,20000316,...", "19970601", UTC); - } - - // Fails: infinite loop - @MediumTest - @Suppress - public void testAdvanceTo2() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYDAY=20MO", "19970519", 3, - /*"19970519,"*/"19980518,19990517,20000515,...", "19980515", UTC); - } - - // Fails: wrong dates - @MediumTest - @Suppress - public void testAdvanceTo3() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=3;UNTIL=20090101;BYYEARDAY=1,100,200", "19970101", 10, - /*"19970101,19970410,19970719,20000101,"*/"20000409," + "20000718,20030101,20030410,20030719,20060101,20060410,20060719," + "20090101", "20000228", UTC); - } - - //Fails: wrong dates - @MediumTest - @Suppress - public void testAdvanceTo4() throws Exception { - // make sure that count preserved - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=3;COUNT=10;BYYEARDAY=1,100,200", "19970101", 10, - /*"19970101,19970410,19970719,20000101,"*/"20000409," + "20000718,20030101,20030410,20030719,20060101", "20000228", UTC); - } - - // Fails: too many dates - @MediumTest - @Suppress - public void testAdvanceTo5() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=2;COUNT=10;BYMONTH=1,2,3", "19970310", 10, - /*"19970310,"*/"19990110,19990210,19990310,20010110," + "20010210,20010310,20030110,20030210,20030310", "19980401", UTC); - } - - @MediumTest - public void testAdvanceTo6() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;UNTIL=19971224", "19970902", 25, - /*"19970902,19970909,19970916,19970923,"*/"19970930," + "19971007,19971014,19971021,19971028,19971104," + "19971111,19971118,19971125,19971202,19971209," + "19971216,19971223", "19970930", UTC); - } - - @MediumTest - public void testAdvanceTo7() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;INTERVAL=18;BYMONTHDAY=10,11,12,13,14,\n" + " 15", "19970910", 5, - /*"19970910,19970911,19970912,19970913,19970914," + - "19970915,"*/"19990310,19990311,19990312,19990313,19990314,...", "19990101", UTC); - } - - @MediumTest - public void testAdvanceTo8() throws Exception { - // advancing into the past - runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;INTERVAL=18;BYMONTHDAY=10,11,12,13,14,\n" + " 15", "19970910", 11, "19970910,19970911,19970912,19970913,19970914," + "19970915,19990310,19990311,19990312,19990313,19990314,...", "19970901", UTC); - } - - // Fails: wrong dates - @MediumTest - @Suppress - public void testAdvanceTo9() throws Exception { - // skips first instance - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=100;BYMONTH=2;BYMONTHDAY=29", "19000101", 5, - // would return 2000 - "24000229,28000229,32000229,36000229,40000229,...", "20040101", UTC); - } - - // Infinite loop in native code (bug 1686327) - @MediumTest - @Suppress - public void testAdvanceTo10() throws Exception { - // filter hits until date before first instnace - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=100;BYMONTH=2;BYMONTHDAY=29;UNTIL=21000101", "19000101", 5, "", "20040101", UTC); - } - - // Fails: generates wrong dates - @MediumTest - @Suppress - public void testAdvanceTo11() throws Exception { - // advancing something that returns no instances - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYMONTH=2;BYMONTHDAY=30", "20000101", 10, "", "19970901", UTC); - } - - // Fails: generates wrong dates - @MediumTest - @Suppress - public void testAdvanceTo12() throws Exception { - // advancing something that returns no instances and has a BYSETPOS rule - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYMONTH=2;BYMONTHDAY=30,31;BYSETPOS=1", "20000101", 10, "", "19970901", UTC); - } - - // Fails: generates wrong dates - @MediumTest - @Suppress - public void testAdvanceTo13() throws Exception { - // advancing way past year generator timeout - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYMONTH=2;BYMONTHDAY=28", "20000101", 10, "", "25000101", UTC); - } - - /** - * a testcase that yielded dupes due to bysetPos evilness - */ - @MediumTest - @Suppress - public void testCaseThatYieldedDupes() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;WKST=SU;INTERVAL=1;BYMONTH=9,1,12,8" + ";BYMONTHDAY=-9,-29,24;BYSETPOS=-1,-4,10,-6,-1,-10,-10,-9,-8", "20060528", 200, "20060924,20061203,20061224,20070902,20071223,20080803,20080824," + "20090823,20100103,20100124,20110123,20120902,20121223,20130922," + "20140803,20140824,20150823,20160103,20160124,20170924,20171203," + "20171224,20180902,20181223,20190922,20200823,20210103,20210124," + "20220123,20230924,20231203,20231224,20240922,20250803,20250824," + "20260823,20270103,20270124,20280123,20280924,20281203,20281224," + "20290902,20291223,20300922,20310803,20310824,20330123,20340924," + "20341203,20341224,20350902,20351223,20360803,20360824,20370823," + "20380103,20380124,20390123,20400902,20401223,20410922,20420803," + "20420824,20430823,20440103,20440124,20450924,20451203,20451224," + "20460902,20461223,20470922,20480823,20490103,20490124,20500123," + "20510924,20511203,20511224,20520922,20530803,20530824,20540823," + "20550103,20550124,20560123,20560924,20561203,20561224,20570902," + "20571223,20580922,20590803,20590824,20610123,20620924,20621203," + "20621224,20630902,20631223,20640803,20640824,20650823,20660103," + "20660124,20670123,20680902,20681223,20690922,20700803,20700824," + "20710823,20720103,20720124,20730924,20731203,20731224,20740902," + "20741223,20750922,20760823,20770103,20770124,20780123,20790924," + "20791203,20791224,20800922,20810803,20810824,20820823,20830103," + "20830124,20840123,20840924,20841203,20841224,20850902,20851223," + "20860922,20870803,20870824,20890123,20900924,20901203,20901224," + "20910902,20911223,20920803,20920824,20930823,20940103,20940124," + "20950123,20960902,20961223,20970922,20980803,20980824,20990823," + "21000103,21000124,21010123,21020924,21021203,21021224,21030902," + "21031223,21040803,21040824,21050823,21060103,21060124,21070123," + "21080902,21081223,21090922,21100803,21100824,21110823,21120103," + "21120124,21130924,21131203,21131224,21140902,21141223,21150922," + "21160823,21170103,21170124,21180123,21190924,21191203,21191224," + "21200922,21210803,21210824,21220823,..."); - } - - @MediumTest - public void testEveryThursdayinMarchEachYear() throws Exception { - runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=TH", "20100304", 9, "20100304,20100311,20100318,20100325,20110303,20110310,20110317,20110324,20110331", null, "20111231", UTC); - } -} diff --git a/tests/src/com/android/calendarcommon/RecurrenceProcessorTest.java b/tests/src/com/android/calendarcommon/RecurrenceProcessorTest.java deleted file mode 100644 index ba5ec0c..0000000 --- a/tests/src/com/android/calendarcommon/RecurrenceProcessorTest.java +++ /dev/null @@ -1,2537 +0,0 @@ -/* //device/content/providers/pim/RecurrenceProcessorTest.java -** -** Copyright 2006, The Android Open Source Project -** -** Licensed under the Apache License, Version 2.0 (the "License"); -** you may not use this file except in compliance with the License. -** You may obtain a copy of the License at -** -** http://www.apache.org/licenses/LICENSE-2.0 -** -** Unless required by applicable law or agreed to in writing, software -** distributed under the License is distributed on an "AS IS" BASIS, -** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -** See the License for the specific language governing permissions and -** limitations under the License. -*/ - -package com.android.calendarcommon; - -import android.os.Debug; -import android.test.suitebuilder.annotation.LargeTest; -import android.test.suitebuilder.annotation.MediumTest; -import android.test.suitebuilder.annotation.SmallTest; -import android.text.TextUtils; -import android.text.format.Time; -import android.util.Log; -import android.util.TimeFormatException; -import junit.framework.TestCase; - -import java.util.TreeSet; - -public class RecurrenceProcessorTest extends TestCase { - private static final String TAG = "RecurrenceProcessorTest"; - private static final boolean SPEW = true; - private static final boolean METHOD_TRACE = false; - - private static String[] getFormattedDates(long[] dates, Time time) { - String[] out = new String[dates.length]; - int i = 0; - for (long date : dates) { - time.set(date); - out[i] = time.format2445(); - ++i; - } - return out; - } - - private static void printLists(String[] expected, String[] out) { - Log.i(TAG, " expected out"); - int i; - for (i = 0; i < expected.length && i < out.length; i++) { - Log.i(TAG, " [" + i + "] " + expected[i] - + " " + out[i]); - } - for (; i < expected.length; i++) { - Log.i(TAG, " [" + i + "] " + expected[i]); - } - for (; i < out.length; i++) { - Log.i(TAG, " [" + i + "] " + out[i]); - } - } - - public void verifyRecurrence(String dtstartStr, String rrule, String rdate, String exrule, - String exdate, String rangeStartStr, String rangeEndStr, String[] expected) - throws Exception { - verifyRecurrence(dtstartStr, rrule, rdate, exrule, exdate, rangeStartStr, - rangeEndStr, expected, expected[expected.length - 1]); - } - - public void verifyRecurrence(String dtstartStr, String rrule, String rdate, String exrule, - String exdate, String rangeStartStr, String rangeEndStr, String[] expected, - String last) throws Exception { - - // note that the zulu of all parameters here must be the same, expand - // doesn't work otherwise - - if (SPEW) { - Log.i(TAG, "DTSTART:" + dtstartStr - + " RRULE:" + rrule - + " RDATE:" + rdate - + " EXRULE:" + exrule - + " EXDATE:" + exdate); - } - - // we could use any timezone, incl. UTC, but we use a non-UTC - // timezone to make sure there are no UTC assumptions in the - // recurrence processing code. - String tz = "America/Los_Angeles"; - Time dtstart = new Time(tz); - Time rangeStart = new Time(tz); - Time rangeEnd = new Time(tz); - Time outCal = new Time(tz); - - dtstart.parse(dtstartStr); - - rangeStart.parse(rangeStartStr); - rangeEnd.parse(rangeEndStr); - - if (METHOD_TRACE) { - String fn = "/tmp/trace/" + this.getClass().getName().replace('$', '_'); - String df = fn + ".data"; - String kf = fn + ".key"; - Debug.startMethodTracing(fn, 8 * 1024 * 1024); - } - - RecurrenceProcessor rp = new RecurrenceProcessor(); - RecurrenceSet recur = new RecurrenceSet(rrule, rdate, exrule, exdate); - - long[] out = rp.expand(dtstart, recur, rangeStart.toMillis(false /* use isDst */), - rangeEnd.toMillis(false /* use isDst */)); - - if (METHOD_TRACE) { - Debug.stopMethodTracing(); - } - - int count = out.length; - String[] actual = getFormattedDates(out, outCal); - - if (count != expected.length) { - if (SPEW) { - Log.i(TAG, "DTSTART:" + dtstartStr + " RRULE:" + rrule); - printLists(expected, actual); - } - throw new RuntimeException("result lengths don't match. " - + " expected=" + expected.length - + " actual=" + count); - } - - for (int i = 0; i < count; i++) { - String s = actual[i]; - if (!s.equals(expected[i])) { - if (SPEW) { - Log.i(TAG, "DTSTART:" + dtstartStr + " RRULE:" + rrule); - printLists(expected, actual); - Log.i(TAG, "i=" + i); - } - throw new RuntimeException("expected[" + i + "]=" - + expected[i] + " actual=" + actual[i]); - } - } - - long lastOccur = rp.getLastOccurence(dtstart, rangeEnd, recur); - if (lastOccur == 0 && out.length == 0) { - // No occurrence found and 0 returned for lastOccur, this is ok. - return; - } - long lastMillis = -1; - long expectedMillis = -1; - String lastStr = ""; - if (lastOccur != -1) { - outCal.set(lastOccur); - lastStr = outCal.format2445(); - lastMillis = outCal.toMillis(true /* ignore isDst */); - } - if (last != null && last.length() > 0) { - Time expectedLast = new Time(tz); - expectedLast.parse(last); - expectedMillis = expectedLast.toMillis(true /* ignore isDst */); - } - if (lastMillis != expectedMillis) { - if (SPEW) { - Log.i(TAG, "DTSTART:" + dtstartStr + " RRULE:" + rrule); - Log.i(TAG, "Expected: " + last + "; Actual: " + lastStr); - printLists(expected, actual); - } - - throw new RuntimeException("expected last occurrence date does not match." - + " expected=" + last - + " actual=" + lastStr); - } - } - - @SmallTest - public void testMonthly0() throws Exception { - verifyRecurrence("20060205T100000", "FREQ=MONTHLY;COUNT=3", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060205T100000", - "20060305T100000", - "20060405T100000" - }); - } - - @SmallTest - public void testMonthly1() throws Exception { - verifyRecurrence("20060205T100000", "FREQ=MONTHLY;INTERVAL=2;COUNT=3", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060205T100000", - "20060405T100000", - "20060605T100000" - }); - } - - @SmallTest - public void testMonthly2() throws Exception { - // this tests wrapping the year when the interval isn't divisible - // by 12 - verifyRecurrence("20060205T100000", "FREQ=MONTHLY;INTERVAL=5;COUNT=5", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060205T100000", - "20060705T100000", - "20061205T100000", - "20070505T100000", - "20071005T100000" - }); - } - - @SmallTest - public void testMonthly3() throws Exception { - // with a simple BYDAY, spanning two months - verifyRecurrence("20060104T123456", - "FREQ=MONTHLY;UNTIL=20060201T200000Z;BYDAY=TU,WE", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060104T123456", - "20060110T123456", - "20060111T123456", - "20060117T123456", - "20060118T123456", - "20060124T123456", - "20060125T123456", - "20060131T123456" - }, - "20060201T120000"); - } - - @SmallTest - public void testMonthly4() throws Exception { - // with a BYDAY with +1 / etc., spanning two months and - // one day which isn't in the result - verifyRecurrence("20060101T123456", - "FREQ=MONTHLY;UNTIL=20060301T200000Z;BYDAY=+1SU,+2MO,+3TU,+4WE,+5MO,+5TU,+5WE,+6TH", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060101T123456", - "20060109T123456", - "20060117T123456", - "20060125T123456", - "20060130T123456", - "20060131T123456", - "20060205T123456", - "20060213T123456", - "20060221T123456", - "20060222T123456" - }, - "20060301T120000"); - } - - @SmallTest - public void testMonthly5() throws Exception { - // with a BYDAY with -1 / etc. - verifyRecurrence("20060201T123456", - "FREQ=MONTHLY;UNTIL=20060301T200000Z;BYDAY=-1SU,-2MO,-3TU,-4TU,-4WE,-5MO,-5TU,-5WE,-6TH", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060201T123456", - "20060207T123456", - "20060214T123456", - "20060220T123456", - "20060226T123456" - }, - "20060301T120000"); - } - - @SmallTest - public void testMonthly6() throws Exception { - // With positive BYMONTHDAYs - verifyRecurrence("20060201T123456", - "FREQ=MONTHLY;UNTIL=20060301T200000Z;BYMONTHDAY=1,2,5,28,31", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060201T123456", - "20060202T123456", - "20060205T123456", - "20060228T123456" - }, - "20060301T120000"); - } - - @SmallTest - public void testMonthly7() throws Exception { - // With negative BYMONTHDAYs - verifyRecurrence("20060201T123456", - "FREQ=MONTHLY;UNTIL=20060301T200000Z;BYMONTHDAY=-1,-5,-27,-28,-31", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060201T123456", - "20060202T123456", - "20060224T123456", - "20060228T123456" - }, - "20060301T120000"); - } - - @SmallTest - public void testMonthly8() throws Exception { - verifyRecurrence("20060205T100000", "FREQ=MONTHLY;COUNT=3", - "America/Los_Angeles;20060207T140000,20060307T160000,20060407T180000", - null /* exrule */, null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060205T100000", - "20060207T140000", - "20060305T100000", - "20060307T160000", - "20060405T100000", - "20060407T180000", - }); - } - - @SmallTest - public void testMonthly9() throws Exception { - verifyRecurrence("20060205T100000", null /* rrule */, - "America/Los_Angeles;20060207T140000,20060307T160000,20060407T180000", - null /* exrule */, null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060207T140000", - "20060307T160000", - "20060407T180000", - }); - } - - @SmallTest - public void testMonthly10() throws Exception { - verifyRecurrence("20060205T100000", "FREQ=MONTHLY;COUNT=3\nFREQ=WEEKLY;COUNT=2", - "America/Los_Angeles;20060207T140000,20060307T160000,20060407T180000", - null /* exrule */, null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060205T100000", - "20060207T140000", - "20060212T100000", - "20060305T100000", - "20060307T160000", - "20060405T100000", - "20060407T180000", - }); - } - - @SmallTest - public void testMonthly11() throws Exception { - verifyRecurrence("20060205T100000", "FREQ=MONTHLY;COUNT=3\nFREQ=WEEKLY;COUNT=2", - null /* rdate */, - "FREQ=MONTHLY;COUNT=2", null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060212T100000", - "20060405T100000", - }); - } - - @SmallTest - public void testMonthly12() throws Exception { - verifyRecurrence("20060205T100000", "FREQ=MONTHLY;COUNT=3\nFREQ=WEEKLY;COUNT=2", - null /* rdate */, null /* exrule */, - "20060212T180000Z,20060405T170000Z" /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060205T100000", - "20060305T100000", - }); - } - - @SmallTest - public void testMonthly13() throws Exception { - verifyRecurrence("20060101T100000", "FREQ=MONTHLY;BYDAY=FR;BYMONTHDAY=13;COUNT=10", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20101231T000000", - new String[]{ - "20060101T100000", - "20060113T100000", - "20061013T100000", - "20070413T100000", - "20070713T100000", - "20080613T100000", - "20090213T100000", - "20090313T100000", - "20091113T100000", - "20100813T100000", - }); - } - - @SmallTest - public void testMonthly14() throws Exception { - verifyRecurrence("20110103T100000", "FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=1,-1", - null /* rdate */, null /* exrule */, null /* exdate */, - "20110101T000000", "20110331T235959", - new String[]{ - "20110103T100000", - "20110131T100000", - "20110201T100000", - "20110228T100000", - "20110301T100000", - "20110331T100000", - }); - } - - @SmallTest - public void testMonthly15() throws Exception { - verifyRecurrence("20110703T100000", "FREQ=MONTHLY;BYDAY=SA,SU;BYSETPOS=2,-2", - null /* rdate */, null /* exrule */, null /* exdate */, - "20110701T000000", "20110931T235959", - new String[]{ - "20110703T100000", - "20110730T100000", - "20110807T100000", - "20110827T100000", - "20110904T100000", - "20110924T100000", - }); - } - - @SmallTest - public void testWeekly0() throws Exception { - verifyRecurrence("20060215T100000", "FREQ=WEEKLY;COUNT=3", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060215T100000", - "20060222T100000", - "20060301T100000" - }); - } - - @SmallTest - public void testWeekly1() throws Exception { - verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=20060301T100000Z", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060215T100000", - "20060222T100000" - }, "20060301T020000"); - } - - @SmallTest - public void testWeekly2() throws Exception { - verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=20060301T100001Z", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060215T100000", - "20060222T100000" - }, "20060301T020001"); - } - - @SmallTest - public void testWeekly3() throws Exception { - verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=20060301T090000Z", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060215T100000", - "20060222T100000" - }, "20060301T010000"); - } - - @SmallTest - public void testWeekly4() throws Exception { - verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=20060311T100001Z;BYDAY=TU", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060215T100000", - "20060221T100000", - "20060228T100000", - "20060307T100000" - }, "20060311T020001"); - } - - // until without "Z" - @SmallTest - public void testWeekly4a() throws Exception { - verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=20060311T100001;BYDAY=TU", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060215T100000", - "20060221T100000", - "20060228T100000", - "20060307T100000" - }, "20060311T100001"); - } - - @SmallTest - public void testWeekly5() throws Exception { - verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=20060311T100001Z;BYDAY=TH", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060215T100000", - "20060216T100000", - "20060223T100000", - "20060302T100000", - "20060309T100000" - }, "20060311T020001"); - } - - @SmallTest - public void testWeekly6() throws Exception { - verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=20060309T100001Z;BYDAY=WE,TH", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060215T100000", - "20060216T100000", - "20060222T100000", - "20060223T100000", - "20060301T100000", - "20060302T100000", - "20060308T100000" - }, "20060309T020001"); - } - - @SmallTest - public void testWeekly7() throws Exception { - verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=20060220T100001Z;BYDAY=SU", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060215T100000", - "20060219T100000" - }, "20060220T020001"); - } - - @SmallTest - public void testWeekly8() throws Exception { - verifyRecurrence("20060215T100000", "FREQ=WEEKLY;COUNT=4;WKST=SU;BYDAY=TU,TH", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060215T100000", - "20060216T100000", - "20060221T100000", - "20060223T100000" - }); - } - - @SmallTest - public void testWeekly9() throws Exception { - verifyRecurrence("19970805T100000", - "FREQ=WEEKLY;INTERVAL=2;COUNT=4;BYDAY=TU,SU", // uses default WKST=MO - null /* rdate */, null /* exrule */, null /* exdate */, - "19970101T000000", "19980101T000000", - new String[]{ - "19970805T100000", - "19970810T100000", - "19970819T100000", - "19970824T100000", - }); - } - - @SmallTest - public void testWeekly10() throws Exception { - verifyRecurrence("19970805T100000", - "FREQ=WEEKLY;INTERVAL=2;COUNT=4;BYDAY=TU,SU;WKST=SU", - null /* rdate */, null /* exrule */, null /* exdate */, - "19970101T000000", "19980101T000000", - new String[]{ - "19970805T100000", - "19970817T100000", - "19970819T100000", - "19970831T100000", - }); - } - - // BUG 1658567: UNTIL=date - @SmallTest - public void testWeekly11() throws Exception { - verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=20060220;BYDAY=SU", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060215T100000", - "20060219T100000" - }, "20060220T000000"); - } - - @SmallTest - public void testWeekly12() throws Exception { - try { - verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=junk;BYDAY=SU", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060215T100000", - "20060219T100000" - }, "20060220T020001"); - fail("Bad UNTIL string failed to throw exception"); - } catch (TimeFormatException e) { - // expected - } - } - - /** - * Test repeating weekly event with dtstart and dtend (only one occurrence) - * See bug #3267616 - * @throws Exception - */ - @SmallTest - public void testWeekly13() throws Exception { - verifyRecurrence("20101117T150000", - "FREQ=WEEKLY;BYDAY=WE", - null /* rdate */, null /* exrule */, null /* exdate */, - "20101117T150000", "20101117T160000", - new String[]{ "20101117T150000" }); - } - - @SmallTest - public void testWeekly14() throws Exception { - verifyRecurrence("19970805T100000", - "FREQ=WEEKLY;INTERVAL=2;COUNT=4;BYDAY=TU,SU;WKST=TH", - null /* rdate */, null /* exrule */, null /* exdate */, - "19970101T000000", "19980101T000000", - new String[]{ - "19970805T100000", - "19970817T100000", - "19970819T100000", - "19970831T100000", - }); - } - - @SmallTest - public void testDaily0() throws Exception { - verifyRecurrence("20060215T100000", "FREQ=DAILY;COUNT=3", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060215T100000", - "20060216T100000", - "20060217T100000" - }); - } - - @SmallTest - public void testDaily1() throws Exception { - verifyRecurrence("20060215T100000", "FREQ=DAILY;UNTIL=20060302T100001Z", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060215T100000", - "20060216T100000", - "20060217T100000", - "20060218T100000", - "20060219T100000", - "20060220T100000", - "20060221T100000", - "20060222T100000", - "20060223T100000", - "20060224T100000", - "20060225T100000", - "20060226T100000", - "20060227T100000", - "20060228T100000", - "20060301T100000" - }, "20060302T100001Z"); - } - - @SmallTest - public void testDaily2() throws Exception { - verifyRecurrence("20060215T100000", "FREQ=DAILY;UNTIL=20060220T100001Z;BYDAY=SU", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060215T100000", - "20060219T100000" - }, "20060220T020001"); - } - - @SmallTest - public void testDaily3() throws Exception { - verifyRecurrence("20060219T100000", - "FREQ=DAILY;UNTIL=20060225T180304Z;BYDAY=SU,MO,SA;BYHOUR=5,10,22;BYMINUTE=3,59;BYSECOND=2,5", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20080101T000000", - new String[]{ - "20060219T100000", - "20060219T100302", - "20060219T100305", - "20060219T105902", - "20060219T105905", - "20060219T220302", - "20060219T220305", - "20060219T225902", - "20060219T225905", - "20060220T050302", - "20060220T050305", - "20060220T055902", - "20060220T055905", - "20060220T100302", - "20060220T100305", - "20060220T105902", - "20060220T105905", - "20060220T220302", - "20060220T220305", - "20060220T225902", - "20060220T225905", - "20060225T050302", - "20060225T050305", - "20060225T055902", - "20060225T055905", - "20060225T100302" - }, "20060225T100304"); - } - - @MediumTest - public void testFromGoogleCalendar0() throws Exception { - // Tuesday, Thursday (10/2) - verifyRecurrence("20061002T050000", - "FREQ=WEEKLY;UNTIL=20071031T200000Z;INTERVAL=1;BYDAY=TU,TH;WKST=SU", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20090101T000000", - new String[]{ - "20061002T050000", - "20061003T050000", - "20061005T050000", - "20061010T050000", - "20061012T050000", - "20061017T050000", - "20061019T050000", - "20061024T050000", - "20061026T050000", - "20061031T050000", - "20061102T050000", - "20061107T050000", - "20061109T050000", - "20061114T050000", - "20061116T050000", - "20061121T050000", - "20061123T050000", - "20061128T050000", - "20061130T050000", - "20061205T050000", - "20061207T050000", - "20061212T050000", - "20061214T050000", - "20061219T050000", - "20061221T050000", - "20061226T050000", - "20061228T050000", - "20070102T050000", - "20070104T050000", - "20070109T050000", - "20070111T050000", - "20070116T050000", - "20070118T050000", - "20070123T050000", - "20070125T050000", - "20070130T050000", - "20070201T050000", - "20070206T050000", - "20070208T050000", - "20070213T050000", - "20070215T050000", - "20070220T050000", - "20070222T050000", - "20070227T050000", - "20070301T050000", - "20070306T050000", - "20070308T050000", - "20070313T050000", - "20070315T050000", - "20070320T050000", - "20070322T050000", - "20070327T050000", - "20070329T050000", - "20070403T050000", - "20070405T050000", - "20070410T050000", - "20070412T050000", - "20070417T050000", - "20070419T050000", - "20070424T050000", - "20070426T050000", - "20070501T050000", - "20070503T050000", - "20070508T050000", - "20070510T050000", - "20070515T050000", - "20070517T050000", - "20070522T050000", - "20070524T050000", - "20070529T050000", - "20070531T050000", - "20070605T050000", - "20070607T050000", - "20070612T050000", - "20070614T050000", - "20070619T050000", - "20070621T050000", - "20070626T050000", - "20070628T050000", - "20070703T050000", - "20070705T050000", - "20070710T050000", - "20070712T050000", - "20070717T050000", - "20070719T050000", - "20070724T050000", - "20070726T050000", - "20070731T050000", - "20070802T050000", - "20070807T050000", - "20070809T050000", - "20070814T050000", - "20070816T050000", - "20070821T050000", - "20070823T050000", - "20070828T050000", - "20070830T050000", - "20070904T050000", - "20070906T050000", - "20070911T050000", - "20070913T050000", - "20070918T050000", - "20070920T050000", - "20070925T050000", - "20070927T050000", - "20071002T050000", - "20071004T050000", - "20071009T050000", - "20071011T050000", - "20071016T050000", - "20071018T050000", - "20071023T050000", - "20071025T050000", - "20071030T050000", - }, "20071031T130000"); - } - - @MediumTest - public void testFromGoogleCalendar1() throws Exception { - // Mon Wed Fri - verifyRecurrence("20061002T030000", - "FREQ=WEEKLY;UNTIL=20071025T180000Z;INTERVAL=1;BYDAY=MO,WE,FR;", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20090101T000000", - new String[]{ - "20061002T030000", - "20061004T030000", - "20061006T030000", - "20061009T030000", - "20061011T030000", - "20061013T030000", - "20061016T030000", - "20061018T030000", - "20061020T030000", - "20061023T030000", - "20061025T030000", - "20061027T030000", - "20061030T030000", - "20061101T030000", - "20061103T030000", - "20061106T030000", - "20061108T030000", - "20061110T030000", - "20061113T030000", - "20061115T030000", - "20061117T030000", - "20061120T030000", - "20061122T030000", - "20061124T030000", - "20061127T030000", - "20061129T030000", - "20061201T030000", - "20061204T030000", - "20061206T030000", - "20061208T030000", - "20061211T030000", - "20061213T030000", - "20061215T030000", - "20061218T030000", - "20061220T030000", - "20061222T030000", - "20061225T030000", - "20061227T030000", - "20061229T030000", - "20070101T030000", - "20070103T030000", - "20070105T030000", - "20070108T030000", - "20070110T030000", - "20070112T030000", - "20070115T030000", - "20070117T030000", - "20070119T030000", - "20070122T030000", - "20070124T030000", - "20070126T030000", - "20070129T030000", - "20070131T030000", - "20070202T030000", - "20070205T030000", - "20070207T030000", - "20070209T030000", - "20070212T030000", - "20070214T030000", - "20070216T030000", - "20070219T030000", - "20070221T030000", - "20070223T030000", - "20070226T030000", - "20070228T030000", - "20070302T030000", - "20070305T030000", - "20070307T030000", - "20070309T030000", - "20070312T030000", - "20070314T030000", - "20070316T030000", - "20070319T030000", - "20070321T030000", - "20070323T030000", - "20070326T030000", - "20070328T030000", - "20070330T030000", - "20070402T030000", - "20070404T030000", - "20070406T030000", - "20070409T030000", - "20070411T030000", - "20070413T030000", - "20070416T030000", - "20070418T030000", - "20070420T030000", - "20070423T030000", - "20070425T030000", - "20070427T030000", - "20070430T030000", - "20070502T030000", - "20070504T030000", - "20070507T030000", - "20070509T030000", - "20070511T030000", - "20070514T030000", - "20070516T030000", - "20070518T030000", - "20070521T030000", - "20070523T030000", - "20070525T030000", - "20070528T030000", - "20070530T030000", - "20070601T030000", - "20070604T030000", - "20070606T030000", - "20070608T030000", - "20070611T030000", - "20070613T030000", - "20070615T030000", - "20070618T030000", - "20070620T030000", - "20070622T030000", - "20070625T030000", - "20070627T030000", - "20070629T030000", - "20070702T030000", - "20070704T030000", - "20070706T030000", - "20070709T030000", - "20070711T030000", - "20070713T030000", - "20070716T030000", - "20070718T030000", - "20070720T030000", - "20070723T030000", - "20070725T030000", - "20070727T030000", - "20070730T030000", - "20070801T030000", - "20070803T030000", - "20070806T030000", - "20070808T030000", - "20070810T030000", - "20070813T030000", - "20070815T030000", - "20070817T030000", - "20070820T030000", - "20070822T030000", - "20070824T030000", - "20070827T030000", - "20070829T030000", - "20070831T030000", - "20070903T030000", - "20070905T030000", - "20070907T030000", - "20070910T030000", - "20070912T030000", - "20070914T030000", - "20070917T030000", - "20070919T030000", - "20070921T030000", - "20070924T030000", - "20070926T030000", - "20070928T030000", - "20071001T030000", - "20071003T030000", - "20071005T030000", - "20071008T030000", - "20071010T030000", - "20071012T030000", - "20071015T030000", - "20071017T030000", - "20071019T030000", - "20071022T030000", - "20071024T030000", - }, "20071025T110000"); - } - - @SmallTest - public void testFromGoogleCalendar2() throws Exception { - // Monthly on day 2 - verifyRecurrence("20061002T070000", - "FREQ=MONTHLY;INTERVAL=1;WKST=SU", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20090101T000000", - new String[]{ - "20061002T070000", - "20061102T070000", - "20061202T070000", - "20070102T070000", - "20070202T070000", - "20070302T070000", - "20070402T070000", - "20070502T070000", - "20070602T070000", - "20070702T070000", - "20070802T070000", - "20070902T070000", - "20071002T070000", - "20071102T070000", - "20071202T070000", - "20080102T070000", - "20080202T070000", - "20080302T070000", - "20080402T070000", - "20080502T070000", - "20080602T070000", - "20080702T070000", - "20080802T070000", - "20080902T070000", - "20081002T070000", - "20081102T070000", - "20081202T070000", - }, "20081202T070000"); - } - - @MediumTest - public void testFromGoogleCalendar3() throws Exception { - // Every Weekday - verifyRecurrence("20061002T100000", - "FREQ=WEEKLY;UNTIL=20070215T100000Z;INTERVAL=1;BYDAY=MO,TU,WE,TH,FR;", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20090101T000000", - new String[]{ - "20061002T100000", - "20061003T100000", - "20061004T100000", - "20061005T100000", - "20061006T100000", - "20061009T100000", - "20061010T100000", - "20061011T100000", - "20061012T100000", - "20061013T100000", - "20061016T100000", - "20061017T100000", - "20061018T100000", - "20061019T100000", - "20061020T100000", - "20061023T100000", - "20061024T100000", - "20061025T100000", - "20061026T100000", - "20061027T100000", - "20061030T100000", - "20061031T100000", - "20061101T100000", - "20061102T100000", - "20061103T100000", - "20061106T100000", - "20061107T100000", - "20061108T100000", - "20061109T100000", - "20061110T100000", - "20061113T100000", - "20061114T100000", - "20061115T100000", - "20061116T100000", - "20061117T100000", - "20061120T100000", - "20061121T100000", - "20061122T100000", - "20061123T100000", - "20061124T100000", - "20061127T100000", - "20061128T100000", - "20061129T100000", - "20061130T100000", - "20061201T100000", - "20061204T100000", - "20061205T100000", - "20061206T100000", - "20061207T100000", - "20061208T100000", - "20061211T100000", - "20061212T100000", - "20061213T100000", - "20061214T100000", - "20061215T100000", - "20061218T100000", - "20061219T100000", - "20061220T100000", - "20061221T100000", - "20061222T100000", - "20061225T100000", - "20061226T100000", - "20061227T100000", - "20061228T100000", - "20061229T100000", - "20070101T100000", - "20070102T100000", - "20070103T100000", - "20070104T100000", - "20070105T100000", - "20070108T100000", - "20070109T100000", - "20070110T100000", - "20070111T100000", - "20070112T100000", - "20070115T100000", - "20070116T100000", - "20070117T100000", - "20070118T100000", - "20070119T100000", - "20070122T100000", - "20070123T100000", - "20070124T100000", - "20070125T100000", - "20070126T100000", - "20070129T100000", - "20070130T100000", - "20070131T100000", - "20070201T100000", - "20070202T100000", - "20070205T100000", - "20070206T100000", - "20070207T100000", - "20070208T100000", - "20070209T100000", - "20070212T100000", - "20070213T100000", - "20070214T100000", - }, "20070215T020000"); - } - - @SmallTest - public void testFromGoogleCalendar4() throws Exception { - // Every 5 months on day 2 - verifyRecurrence("20061003T100000", - "FREQ=MONTHLY;INTERVAL=5;WKST=SU", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20090101T000000", - new String[]{ - "20061003T100000", - "20070303T100000", - "20070803T100000", - "20080103T100000", - "20080603T100000", - "20081103T100000", - }, "20081103T100000"); - } - - @MediumTest - public void testFromGoogleCalendar5() throws Exception { - // Tuesday, Thursday (10/3) - verifyRecurrence("20061003T040000", - "FREQ=WEEKLY;INTERVAL=1;BYDAY=TU,TH;WKST=SU", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20090101T000000", - new String[]{ - "20061003T040000", - "20061005T040000", - "20061010T040000", - "20061012T040000", - "20061017T040000", - "20061019T040000", - "20061024T040000", - "20061026T040000", - "20061031T040000", - "20061102T040000", - "20061107T040000", - "20061109T040000", - "20061114T040000", - "20061116T040000", - "20061121T040000", - "20061123T040000", - "20061128T040000", - "20061130T040000", - "20061205T040000", - "20061207T040000", - "20061212T040000", - "20061214T040000", - "20061219T040000", - "20061221T040000", - "20061226T040000", - "20061228T040000", - "20070102T040000", - "20070104T040000", - "20070109T040000", - "20070111T040000", - "20070116T040000", - "20070118T040000", - "20070123T040000", - "20070125T040000", - "20070130T040000", - "20070201T040000", - "20070206T040000", - "20070208T040000", - "20070213T040000", - "20070215T040000", - "20070220T040000", - "20070222T040000", - "20070227T040000", - "20070301T040000", - "20070306T040000", - "20070308T040000", - "20070313T040000", - "20070315T040000", - "20070320T040000", - "20070322T040000", - "20070327T040000", - "20070329T040000", - "20070403T040000", - "20070405T040000", - "20070410T040000", - "20070412T040000", - "20070417T040000", - "20070419T040000", - "20070424T040000", - "20070426T040000", - "20070501T040000", - "20070503T040000", - "20070508T040000", - "20070510T040000", - "20070515T040000", - "20070517T040000", - "20070522T040000", - "20070524T040000", - "20070529T040000", - "20070531T040000", - "20070605T040000", - "20070607T040000", - "20070612T040000", - "20070614T040000", - "20070619T040000", - "20070621T040000", - "20070626T040000", - "20070628T040000", - "20070703T040000", - "20070705T040000", - "20070710T040000", - "20070712T040000", - "20070717T040000", - "20070719T040000", - "20070724T040000", - "20070726T040000", - "20070731T040000", - "20070802T040000", - "20070807T040000", - "20070809T040000", - "20070814T040000", - "20070816T040000", - "20070821T040000", - "20070823T040000", - "20070828T040000", - "20070830T040000", - "20070904T040000", - "20070906T040000", - "20070911T040000", - "20070913T040000", - "20070918T040000", - "20070920T040000", - "20070925T040000", - "20070927T040000", - "20071002T040000", - "20071004T040000", - "20071009T040000", - "20071011T040000", - "20071016T040000", - "20071018T040000", - "20071023T040000", - "20071025T040000", - "20071030T040000", - "20071101T040000", - "20071106T040000", - "20071108T040000", - "20071113T040000", - "20071115T040000", - "20071120T040000", - "20071122T040000", - "20071127T040000", - "20071129T040000", - "20071204T040000", - "20071206T040000", - "20071211T040000", - "20071213T040000", - "20071218T040000", - "20071220T040000", - "20071225T040000", - "20071227T040000", - "20080101T040000", - "20080103T040000", - "20080108T040000", - "20080110T040000", - "20080115T040000", - "20080117T040000", - "20080122T040000", - "20080124T040000", - "20080129T040000", - "20080131T040000", - "20080205T040000", - "20080207T040000", - "20080212T040000", - "20080214T040000", - "20080219T040000", - "20080221T040000", - "20080226T040000", - "20080228T040000", - "20080304T040000", - "20080306T040000", - "20080311T040000", - "20080313T040000", - "20080318T040000", - "20080320T040000", - "20080325T040000", - "20080327T040000", - "20080401T040000", - "20080403T040000", - "20080408T040000", - "20080410T040000", - "20080415T040000", - "20080417T040000", - "20080422T040000", - "20080424T040000", - "20080429T040000", - "20080501T040000", - "20080506T040000", - "20080508T040000", - "20080513T040000", - "20080515T040000", - "20080520T040000", - "20080522T040000", - "20080527T040000", - "20080529T040000", - "20080603T040000", - "20080605T040000", - "20080610T040000", - "20080612T040000", - "20080617T040000", - "20080619T040000", - "20080624T040000", - "20080626T040000", - "20080701T040000", - "20080703T040000", - "20080708T040000", - "20080710T040000", - "20080715T040000", - "20080717T040000", - "20080722T040000", - "20080724T040000", - "20080729T040000", - "20080731T040000", - "20080805T040000", - "20080807T040000", - "20080812T040000", - "20080814T040000", - "20080819T040000", - "20080821T040000", - "20080826T040000", - "20080828T040000", - "20080902T040000", - "20080904T040000", - "20080909T040000", - "20080911T040000", - "20080916T040000", - "20080918T040000", - "20080923T040000", - "20080925T040000", - "20080930T040000", - "20081002T040000", - "20081007T040000", - "20081009T040000", - "20081014T040000", - "20081016T040000", - "20081021T040000", - "20081023T040000", - "20081028T040000", - "20081030T040000", - "20081104T040000", - "20081106T040000", - "20081111T040000", - "20081113T040000", - "20081118T040000", - "20081120T040000", - "20081125T040000", - "20081127T040000", - "20081202T040000", - "20081204T040000", - "20081209T040000", - "20081211T040000", - "20081216T040000", - "20081218T040000", - "20081223T040000", - "20081225T040000", - "20081230T040000", - }, "20081230T040000"); - } - - @MediumTest - public void testFromGoogleCalendar6() throws Exception { - // Weekly on all days - verifyRecurrence("20061003T060000", - "FREQ=WEEKLY;INTERVAL=1;BYDAY=SU,MO,TU,WE,TH,FR,SA;WKST=SU", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20071003T060000", - new String[]{ - "20061003T060000", - "20061004T060000", - "20061005T060000", - "20061006T060000", - "20061007T060000", - "20061008T060000", - "20061009T060000", - "20061010T060000", - "20061011T060000", - "20061012T060000", - "20061013T060000", - "20061014T060000", - "20061015T060000", - "20061016T060000", - "20061017T060000", - "20061018T060000", - "20061019T060000", - "20061020T060000", - "20061021T060000", - "20061022T060000", - "20061023T060000", - "20061024T060000", - "20061025T060000", - "20061026T060000", - "20061027T060000", - "20061028T060000", - "20061029T060000", - "20061030T060000", - "20061031T060000", - "20061101T060000", - "20061102T060000", - "20061103T060000", - "20061104T060000", - "20061105T060000", - "20061106T060000", - "20061107T060000", - "20061108T060000", - "20061109T060000", - "20061110T060000", - "20061111T060000", - "20061112T060000", - "20061113T060000", - "20061114T060000", - "20061115T060000", - "20061116T060000", - "20061117T060000", - "20061118T060000", - "20061119T060000", - "20061120T060000", - "20061121T060000", - "20061122T060000", - "20061123T060000", - "20061124T060000", - "20061125T060000", - "20061126T060000", - "20061127T060000", - "20061128T060000", - "20061129T060000", - "20061130T060000", - "20061201T060000", - "20061202T060000", - "20061203T060000", - "20061204T060000", - "20061205T060000", - "20061206T060000", - "20061207T060000", - "20061208T060000", - "20061209T060000", - "20061210T060000", - "20061211T060000", - "20061212T060000", - "20061213T060000", - "20061214T060000", - "20061215T060000", - "20061216T060000", - "20061217T060000", - "20061218T060000", - "20061219T060000", - "20061220T060000", - "20061221T060000", - "20061222T060000", - "20061223T060000", - "20061224T060000", - "20061225T060000", - "20061226T060000", - "20061227T060000", - "20061228T060000", - "20061229T060000", - "20061230T060000", - "20061231T060000", - "20070101T060000", - "20070102T060000", - "20070103T060000", - "20070104T060000", - "20070105T060000", - "20070106T060000", - "20070107T060000", - "20070108T060000", - "20070109T060000", - "20070110T060000", - "20070111T060000", - "20070112T060000", - "20070113T060000", - "20070114T060000", - "20070115T060000", - "20070116T060000", - "20070117T060000", - "20070118T060000", - "20070119T060000", - "20070120T060000", - "20070121T060000", - "20070122T060000", - "20070123T060000", - "20070124T060000", - "20070125T060000", - "20070126T060000", - "20070127T060000", - "20070128T060000", - "20070129T060000", - "20070130T060000", - "20070131T060000", - "20070201T060000", - "20070202T060000", - "20070203T060000", - "20070204T060000", - "20070205T060000", - "20070206T060000", - "20070207T060000", - "20070208T060000", - "20070209T060000", - "20070210T060000", - "20070211T060000", - "20070212T060000", - "20070213T060000", - "20070214T060000", - "20070215T060000", - "20070216T060000", - "20070217T060000", - "20070218T060000", - "20070219T060000", - "20070220T060000", - "20070221T060000", - "20070222T060000", - "20070223T060000", - "20070224T060000", - "20070225T060000", - "20070226T060000", - "20070227T060000", - "20070228T060000", - "20070301T060000", - "20070302T060000", - "20070303T060000", - "20070304T060000", - "20070305T060000", - "20070306T060000", - "20070307T060000", - "20070308T060000", - "20070309T060000", - "20070310T060000", - "20070311T060000", - "20070312T060000", - "20070313T060000", - "20070314T060000", - "20070315T060000", - "20070316T060000", - "20070317T060000", - "20070318T060000", - "20070319T060000", - "20070320T060000", - "20070321T060000", - "20070322T060000", - "20070323T060000", - "20070324T060000", - "20070325T060000", - "20070326T060000", - "20070327T060000", - "20070328T060000", - "20070329T060000", - "20070330T060000", - "20070331T060000", - "20070401T060000", - "20070402T060000", - "20070403T060000", - "20070404T060000", - "20070405T060000", - "20070406T060000", - "20070407T060000", - "20070408T060000", - "20070409T060000", - "20070410T060000", - "20070411T060000", - "20070412T060000", - "20070413T060000", - "20070414T060000", - "20070415T060000", - "20070416T060000", - "20070417T060000", - "20070418T060000", - "20070419T060000", - "20070420T060000", - "20070421T060000", - "20070422T060000", - "20070423T060000", - "20070424T060000", - "20070425T060000", - "20070426T060000", - "20070427T060000", - "20070428T060000", - "20070429T060000", - "20070430T060000", - "20070501T060000", - "20070502T060000", - "20070503T060000", - "20070504T060000", - "20070505T060000", - "20070506T060000", - "20070507T060000", - "20070508T060000", - "20070509T060000", - "20070510T060000", - "20070511T060000", - "20070512T060000", - "20070513T060000", - "20070514T060000", - "20070515T060000", - "20070516T060000", - "20070517T060000", - "20070518T060000", - "20070519T060000", - "20070520T060000", - "20070521T060000", - "20070522T060000", - "20070523T060000", - "20070524T060000", - "20070525T060000", - "20070526T060000", - "20070527T060000", - "20070528T060000", - "20070529T060000", - "20070530T060000", - "20070531T060000", - "20070601T060000", - "20070602T060000", - "20070603T060000", - "20070604T060000", - "20070605T060000", - "20070606T060000", - "20070607T060000", - "20070608T060000", - "20070609T060000", - "20070610T060000", - "20070611T060000", - "20070612T060000", - "20070613T060000", - "20070614T060000", - "20070615T060000", - "20070616T060000", - "20070617T060000", - "20070618T060000", - "20070619T060000", - "20070620T060000", - "20070621T060000", - "20070622T060000", - "20070623T060000", - "20070624T060000", - "20070625T060000", - "20070626T060000", - "20070627T060000", - "20070628T060000", - "20070629T060000", - "20070630T060000", - "20070701T060000", - "20070702T060000", - "20070703T060000", - "20070704T060000", - "20070705T060000", - "20070706T060000", - "20070707T060000", - "20070708T060000", - "20070709T060000", - "20070710T060000", - "20070711T060000", - "20070712T060000", - "20070713T060000", - "20070714T060000", - "20070715T060000", - "20070716T060000", - "20070717T060000", - "20070718T060000", - "20070719T060000", - "20070720T060000", - "20070721T060000", - "20070722T060000", - "20070723T060000", - "20070724T060000", - "20070725T060000", - "20070726T060000", - "20070727T060000", - "20070728T060000", - "20070729T060000", - "20070730T060000", - "20070731T060000", - "20070801T060000", - "20070802T060000", - "20070803T060000", - "20070804T060000", - "20070805T060000", - "20070806T060000", - "20070807T060000", - "20070808T060000", - "20070809T060000", - "20070810T060000", - "20070811T060000", - "20070812T060000", - "20070813T060000", - "20070814T060000", - "20070815T060000", - "20070816T060000", - "20070817T060000", - "20070818T060000", - "20070819T060000", - "20070820T060000", - "20070821T060000", - "20070822T060000", - "20070823T060000", - "20070824T060000", - "20070825T060000", - "20070826T060000", - "20070827T060000", - "20070828T060000", - "20070829T060000", - "20070830T060000", - "20070831T060000", - "20070901T060000", - "20070902T060000", - "20070903T060000", - "20070904T060000", - "20070905T060000", - "20070906T060000", - "20070907T060000", - "20070908T060000", - "20070909T060000", - "20070910T060000", - "20070911T060000", - "20070912T060000", - "20070913T060000", - "20070914T060000", - "20070915T060000", - "20070916T060000", - "20070917T060000", - "20070918T060000", - "20070919T060000", - "20070920T060000", - "20070921T060000", - "20070922T060000", - "20070923T060000", - "20070924T060000", - "20070925T060000", - "20070926T060000", - "20070927T060000", - "20070928T060000", - "20070929T060000", - "20070930T060000", - "20071001T060000", - "20071002T060000", - }, "20071002T060000"); - } - - @MediumTest - public void testFromGoogleCalendar7() throws Exception { - // Every 3 days - verifyRecurrence("20061003T080000", - "FREQ=DAILY;INTERVAL=3;WKST=SU", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20090101T000000", - new String[]{ - "20061003T080000", - "20061006T080000", - "20061009T080000", - "20061012T080000", - "20061015T080000", - "20061018T080000", - "20061021T080000", - "20061024T080000", - "20061027T080000", - "20061030T080000", - "20061102T080000", - "20061105T080000", - "20061108T080000", - "20061111T080000", - "20061114T080000", - "20061117T080000", - "20061120T080000", - "20061123T080000", - "20061126T080000", - "20061129T080000", - "20061202T080000", - "20061205T080000", - "20061208T080000", - "20061211T080000", - "20061214T080000", - "20061217T080000", - "20061220T080000", - "20061223T080000", - "20061226T080000", - "20061229T080000", - "20070101T080000", - "20070104T080000", - "20070107T080000", - "20070110T080000", - "20070113T080000", - "20070116T080000", - "20070119T080000", - "20070122T080000", - "20070125T080000", - "20070128T080000", - "20070131T080000", - "20070203T080000", - "20070206T080000", - "20070209T080000", - "20070212T080000", - "20070215T080000", - "20070218T080000", - "20070221T080000", - "20070224T080000", - "20070227T080000", - "20070302T080000", - "20070305T080000", - "20070308T080000", - "20070311T080000", - "20070314T080000", - "20070317T080000", - "20070320T080000", - "20070323T080000", - "20070326T080000", - "20070329T080000", - "20070401T080000", - "20070404T080000", - "20070407T080000", - "20070410T080000", - "20070413T080000", - "20070416T080000", - "20070419T080000", - "20070422T080000", - "20070425T080000", - "20070428T080000", - "20070501T080000", - "20070504T080000", - "20070507T080000", - "20070510T080000", - "20070513T080000", - "20070516T080000", - "20070519T080000", - "20070522T080000", - "20070525T080000", - "20070528T080000", - "20070531T080000", - "20070603T080000", - "20070606T080000", - "20070609T080000", - "20070612T080000", - "20070615T080000", - "20070618T080000", - "20070621T080000", - "20070624T080000", - "20070627T080000", - "20070630T080000", - "20070703T080000", - "20070706T080000", - "20070709T080000", - "20070712T080000", - "20070715T080000", - "20070718T080000", - "20070721T080000", - "20070724T080000", - "20070727T080000", - "20070730T080000", - "20070802T080000", - "20070805T080000", - "20070808T080000", - "20070811T080000", - "20070814T080000", - "20070817T080000", - "20070820T080000", - "20070823T080000", - "20070826T080000", - "20070829T080000", - "20070901T080000", - "20070904T080000", - "20070907T080000", - "20070910T080000", - "20070913T080000", - "20070916T080000", - "20070919T080000", - "20070922T080000", - "20070925T080000", - "20070928T080000", - "20071001T080000", - "20071004T080000", - "20071007T080000", - "20071010T080000", - "20071013T080000", - "20071016T080000", - "20071019T080000", - "20071022T080000", - "20071025T080000", - "20071028T080000", - "20071031T080000", - "20071103T080000", - "20071106T080000", - "20071109T080000", - "20071112T080000", - "20071115T080000", - "20071118T080000", - "20071121T080000", - "20071124T080000", - "20071127T080000", - "20071130T080000", - "20071203T080000", - "20071206T080000", - "20071209T080000", - "20071212T080000", - "20071215T080000", - "20071218T080000", - "20071221T080000", - "20071224T080000", - "20071227T080000", - "20071230T080000", - "20080102T080000", - "20080105T080000", - "20080108T080000", - "20080111T080000", - "20080114T080000", - "20080117T080000", - "20080120T080000", - "20080123T080000", - "20080126T080000", - "20080129T080000", - "20080201T080000", - "20080204T080000", - "20080207T080000", - "20080210T080000", - "20080213T080000", - "20080216T080000", - "20080219T080000", - "20080222T080000", - "20080225T080000", - "20080228T080000", - "20080302T080000", - "20080305T080000", - "20080308T080000", - "20080311T080000", - "20080314T080000", - "20080317T080000", - "20080320T080000", - "20080323T080000", - "20080326T080000", - "20080329T080000", - "20080401T080000", - "20080404T080000", - "20080407T080000", - "20080410T080000", - "20080413T080000", - "20080416T080000", - "20080419T080000", - "20080422T080000", - "20080425T080000", - "20080428T080000", - "20080501T080000", - "20080504T080000", - "20080507T080000", - "20080510T080000", - "20080513T080000", - "20080516T080000", - "20080519T080000", - "20080522T080000", - "20080525T080000", - "20080528T080000", - "20080531T080000", - "20080603T080000", - "20080606T080000", - "20080609T080000", - "20080612T080000", - "20080615T080000", - "20080618T080000", - "20080621T080000", - "20080624T080000", - "20080627T080000", - "20080630T080000", - "20080703T080000", - "20080706T080000", - "20080709T080000", - "20080712T080000", - "20080715T080000", - "20080718T080000", - "20080721T080000", - "20080724T080000", - "20080727T080000", - "20080730T080000", - "20080802T080000", - "20080805T080000", - "20080808T080000", - "20080811T080000", - "20080814T080000", - "20080817T080000", - "20080820T080000", - "20080823T080000", - "20080826T080000", - "20080829T080000", - "20080901T080000", - "20080904T080000", - "20080907T080000", - "20080910T080000", - "20080913T080000", - "20080916T080000", - "20080919T080000", - "20080922T080000", - "20080925T080000", - "20080928T080000", - "20081001T080000", - "20081004T080000", - "20081007T080000", - "20081010T080000", - "20081013T080000", - "20081016T080000", - "20081019T080000", - "20081022T080000", - "20081025T080000", - "20081028T080000", - "20081031T080000", - "20081103T080000", - "20081106T080000", - "20081109T080000", - "20081112T080000", - "20081115T080000", - "20081118T080000", - "20081121T080000", - "20081124T080000", - "20081127T080000", - "20081130T080000", - "20081203T080000", - "20081206T080000", - "20081209T080000", - "20081212T080000", - "20081215T080000", - "20081218T080000", - "20081221T080000", - "20081224T080000", - "20081227T080000", - "20081230T080000", - }, "20081230T080000"); - } - - @SmallTest - public void testFromGoogleCalendar8() throws Exception { - // Annually on October 4 - verifyRecurrence("20061004T130000", - "FREQ=YEARLY;INTERVAL=1;WKST=SU", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20090101T000000", - new String[]{ - "20061004T130000", - "20071004T130000", - "20081004T130000", - }, "20081004T130000"); - } - - @MediumTest - public void testFromGoogleCalendar9() throws Exception { - // Monthly on the last Monday - verifyRecurrence("20061030T170000", - "FREQ=MONTHLY;INTERVAL=1;BYDAY=-1MO;WKST=SU", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20090101T000000", - new String[]{ - "20061030T170000", - "20061127T170000", - "20061225T170000", - "20070129T170000", - "20070226T170000", - "20070326T170000", - "20070430T170000", - "20070528T170000", - "20070625T170000", - "20070730T170000", - "20070827T170000", - "20070924T170000", - "20071029T170000", - "20071126T170000", - "20071231T170000", - "20080128T170000", - "20080225T170000", - "20080331T170000", - "20080428T170000", - "20080526T170000", - "20080630T170000", - "20080728T170000", - "20080825T170000", - "20080929T170000", - "20081027T170000", - "20081124T170000", - "20081229T170000", - }, "20081229T170000"); - } - - @SmallTest - public void testFromGoogleCalendar10() throws Exception { - // Every 7 weeks on Tuesday, Wednesday - verifyRecurrence("20061004T090000", - "FREQ=WEEKLY;UNTIL=20070223T010000Z;INTERVAL=7;BYDAY=TU,WE;WKST=SU", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20090101T000000", - new String[]{ - "20061004T090000", - "20061121T090000", - "20061122T090000", - "20070109T090000", - "20070110T090000", - }, "20070222T170000"); - } - - @SmallTest - public void testFromGoogleCalendar11() throws Exception { - // Monthly on day 31 - verifyRecurrence("20061031T160000", - "FREQ=MONTHLY;INTERVAL=1;WKST=SU", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20090101T000000", - new String[]{ - "20061031T160000", - "20061231T160000", - "20070131T160000", - "20070331T160000", - "20070531T160000", - "20070731T160000", - "20070831T160000", - "20071031T160000", - "20071231T160000", - "20080131T160000", - "20080331T160000", - "20080531T160000", - "20080731T160000", - "20080831T160000", - "20081031T160000", - "20081231T160000", - }, - "20081231T160000"); - } - - @SmallTest - public void testFromGoogleCalendar12() throws Exception { - // Every 2 months on the first Tuesday - verifyRecurrence("20061004T110000", - "FREQ=MONTHLY;INTERVAL=2;BYDAY=1TU;WKST=SU", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20090101T000000", - new String[]{ - "20061004T110000", - "20061205T110000", - "20070206T110000", - "20070403T110000", - "20070605T110000", - "20070807T110000", - "20071002T110000", - "20071204T110000", - "20080205T110000", - "20080401T110000", - "20080603T110000", - "20080805T110000", - "20081007T110000", - "20081202T110000", - }, - "20081202T110000"); - } - - @SmallTest - public void testYearly0() throws Exception { - verifyRecurrence("20080101T100000", - "FREQ=YEARLY;UNTIL=20090131T090000Z;BYMONTH=1;BYDAY=SU,MO", - null /* rdate */, null /* exrule */, null /* exdate */, - "20080101T000000", "20090130T000000", - new String[]{ - "20080101T100000", - "20080106T100000", - "20080107T100000", - "20080113T100000", - "20080114T100000", - "20080120T100000", - "20080121T100000", - "20080127T100000", - "20080128T100000", - "20090104T100000", - "20090105T100000", - "20090111T100000", - "20090112T100000", - "20090118T100000", - "20090119T100000", - "20090125T100000", - "20090126T100000", - }, "20090131T010000"); - } - - /** - * This test fails because of a bug in RecurrenceProcessor.expand(). We - * don't have time to fix the bug yet but we don't want to lose track of - * this test either. The "failing" prefix on the method name prevents this - * test from being run. Remove the "failing" prefix when the bug is fixed. - * - * @throws Exception - */ - @SmallTest - public void failingTestYearly1() throws Exception { - verifyRecurrence("20060101T100000", - "FREQ=YEARLY;COUNT=10;BYYEARDAY=1,100,200", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20090131T000000", - new String[]{ - "20060101T100000", - "20060409T100000", - "20060718T100000", - "20070101T100000", - "20070409T100000", - "20070718T100000", - "20080101T100000", - "20080410T100000", - "20080719T100000", - "20090101T100000", - }); - } - - /** - * This test fails because of a bug in RecurrenceProcessor.expand(). We - * don't have time to fix the bug yet but we don't want to lose track of - * this test either. The "failing" prefix on the method name prevents this - * test from being run. Remove the "failing" prefix when the bug is fixed. - * - * @throws Exception - */ - @SmallTest - public void failingTestYearly2() throws Exception { - verifyRecurrence("20060101T100000", - "FREQ=YEARLY;COUNT=5;BYWEEKNO=6;BYDAY=MO;WKST=MO", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20090131T000000", - new String[]{ - "20060101T100000", - "20060206T100000", - "20070205T100000", - "20080204T100000", - "20090209T100000", - }); - } - - /** - * This test fails because of a bug in RecurrenceProcessor.expand(). We - * don't have time to fix the bug yet but we don't want to lose track of - * this test either. The "failing" prefix on the method name prevents this - * test from being run. Remove the "failing" prefix when the bug is fixed. - * - * @throws Exception - */ - @SmallTest - public void failingTestYearly3() throws Exception { - verifyRecurrence("20060101T100000", - "FREQ=YEARLY;BYMONTH=3;BYDAY=TH", - null /* rdate */, null /* exrule */, null /* exdate */, - "20060101T000000", "20090131T000000", - new String[]{ - "20060101T100000", - "20060302T100000", - "20060309T100000", - "20060316T100000", - "20060323T100000", - "20060330T100000", - "20070301T100000", - "20070308T100000", - "20070315T100000", - "20070322T100000", - "20070329T100000", - "20080306T100000", - "20080313T100000", - "20080320T100000", - "20080327T100000", - }); - } - - /** - * This test fails because of a bug in RecurrenceProcessor.expand(). We - * don't have time to fix the bug yet but we don't want to lose track of - * this test either. The "failing" prefix on the method name prevents this - * test from being run. Remove the "failing" prefix when the bug is fixed. - * - * @throws Exception - */ - @SmallTest - public void failingTestYearly4() throws Exception { - verifyRecurrence("19920101T100000", - "FREQ=YEARLY;INTERVAL=4;BYMONTH=11;BYDAY=TU;BYMONTHDAY=2,3,4,5,6,7,8", - null /* rdate */, null /* exrule */, null /* exdate */, - "19920101T000000", "20121231T000000", - new String[]{ - "19920101T100000", - "19921103T100000", - "19961105T100000", - "20001107T100000", - "20041102T100000", - "20081104T100000", - "20121106T100000", - }); - } - - /** - * Test repeating event from Exchange with count field. - * Time range covers the whole repetition. - * - * @throws Exception - */ - public void testCount1() throws Exception { - verifyRecurrence("20100324T153000", - "FREQ=WEEKLY;INTERVAL=1;COUNT=10;BYDAY=WE", - null /* rdate */, null /* exrule */, null /* exdate */, - "20100301T000000", "20100630T000000", - new String[]{ - "20100324T153000", - "20100331T153000", - "20100407T153000", - "20100414T153000", - "20100421T153000", - "20100428T153000", - "20100505T153000", - "20100512T153000", - "20100519T153000", - "20100526T153000", - }); - } - - /** - * Test repeating event from Exchange with count field. - * Time range covers the first part of the repetition. - * @throws Exception - */ - public void testCount2() throws Exception { - verifyRecurrence("20100324T153000", - "FREQ=WEEKLY;INTERVAL=1;COUNT=10;BYDAY=WE", - null /* rdate */, null /* exrule */, null /* exdate */, - "20100501T000000", "20100630T000000", - new String[]{ - "20100505T153000", - "20100512T153000", - "20100519T153000", - "20100526T153000", - }); - } - - /** - * Test repeating event from Exchange with count field. - * Time range is beyond the repetition. - * @throws Exception - */ - public void testCount3() throws Exception { - verifyRecurrence("20100324T153000", - "FREQ=WEEKLY;INTERVAL=1;COUNT=10;BYDAY=WE", - null /* rdate */, null /* exrule */, null /* exdate */, - "20100601T000000", "20100630T000000", - new String[]{}, - "20100526T153000" /* last */); - } - - /** - * Test repeating event from Exchange with count field. - * Time range is before the repetition - * @throws Exception - */ - public void testCount4() throws Exception { - verifyRecurrence("20100324T153000", - "FREQ=WEEKLY;INTERVAL=1;COUNT=10;BYDAY=WE", - null /* rdate */, null /* exrule */, null /* exdate */, - "20100101T000000", "20100301T000000", - new String[]{}, - null /* last */); - } - - - // These recurrence rules are used in the loop that measures the performance - // of recurrence expansion. - private static final String[] performanceRrules = new String[] { - "FREQ=DAILY;COUNT=100", - "FREQ=DAILY;INTERVAL=2;UNTIL=20080101T000000Z", - "FREQ=YEARLY;UNTIL=20090131T090000Z;BYMONTH=1;BYDAY=SU,MO,TU,WE,TH,FR,SA", - "FREQ=WEEKLY;INTERVAL=2;WKST=SU", - "FREQ=WEEKLY;COUNT=100;WKST=SU;BYDAY=MO,TU,WE,TH,FR", - "FREQ=MONTHLY;COUNT=100;BYDAY=1FR", - "FREQ=MONTHLY;INTERVAL=2;COUNT=100;BYDAY=1SU,-1SU", - "FREQ=MONTHLY;BYMONTHDAY=1,15", - "FREQ=MONTHLY;INTERVAL=3;COUNT=100;BYMONTHDAY=10,11,12,13,14", - "FREQ=YEARLY;COUNT=100;BYMONTH=6,7,8", - "FREQ=YEARLY;INTERVAL=2;BYMONTH=1,2,3,6,7,8", - "FREQ=YEARLY;COUNT=100;BYYEARDAY=1,100,200", - "FREQ=YEARLY;BYDAY=2MO", - "FREQ=YEARLY;BYWEEKNO=2,3,4;BYDAY=MO", - "FREQ=YEARLY;BYMONTH=3,4,5;BYDAY=TH", - "FREQ=MONTHLY;BYDAY=FR;BYMONTHDAY=13", - "FREQ=MONTHLY;BYDAY=SA;BYMONTHDAY=7,8,9,10,11,12,13", - "FREQ=YEARLY;INTERVAL=2;BYMONTH=11;BYDAY=TU;BYMONTHDAY=2,3,4,5,6,7,8", - "FREQ=WEEKLY;INTERVAL=2;COUNT=100;BYDAY=TU,SU;WKST=MO", - "FREQ=WEEKLY;INTERVAL=2;COUNT=100;BYDAY=TU,SU;WKST=SU", - }; - - /** - * This test never fails. It just runs for a while (about 10 seconds) - * in order to measure the performance of recurrence expansion. - * - * @throws Exception - */ - @LargeTest - public void performanceTextExpand() throws Exception { - String tz = "America/Los_Angeles"; - Time dtstart = new Time(tz); - Time rangeStart = new Time(tz); - Time rangeEnd = new Time(tz); - TreeSet out = new TreeSet(); - - dtstart.parse("20010101T000000"); - rangeStart.parse("20010101T000000"); - rangeEnd.parse("20090101T000000"); - long rangeStartMillis = rangeStart.toMillis(false /* use isDst */); - long rangeEndMillis = rangeEnd.toMillis(false /* use isDst */); - - long startTime = System.currentTimeMillis(); - for (int iterations = 0; iterations < 5; iterations++) { - RecurrenceProcessor rp = new RecurrenceProcessor(); - - int len = performanceRrules.length; - for (int i = 0; i < len; i++) { - String rrule = performanceRrules[i]; - //Log.i(TAG, "expanding rule: " + rrule); - RecurrenceSet recur = new RecurrenceSet(rrule, null, null, null); - - long [] dates = rp.expand(dtstart, recur, rangeStartMillis, rangeEndMillis); - //Log.i(TAG, "num instances: " + out.size()); - - // Also include the time to iterate through the expanded values - for (long date : dates) { - // Use the date so that this loop is not optimized away. - if (date == -1) { - Log.e(TAG, "unexpected date"); - break; - } - } - out.clear(); - } - } - - long endTime = System.currentTimeMillis(); - long elapsed = endTime - startTime; - Log.i(TAG, "testPerformanceExpand() expand() elapsed millis: " + elapsed); - } - - @LargeTest - public void performanceTestNormalize() throws Exception { - final int ITERATIONS = 50000; - - String tz = "America/Los_Angeles"; - Time date = new Time(tz); - date.parse("20090404T100000"); - - long startTime = System.currentTimeMillis(); - - for (int i = 0; i < ITERATIONS; i++) { - date.month += 1; - date.monthDay += 100; - date.normalize(true); - date.month -= 1; - date.monthDay -= 100; - date.normalize(true); - } - - long endTime = System.currentTimeMillis(); - long elapsed = endTime - startTime; - Log.i(TAG, "date: " + date.format2445()); - Log.i(TAG, "testPerformanceNormalize() normalize() elapsed millis: " + elapsed); - - // Time the unsafe version - date.parse("20090404T100000"); - startTime = System.currentTimeMillis(); - for (int i = 0; i < ITERATIONS; i++) { - date.month += 1; - date.monthDay += 100; - RecurrenceProcessor.unsafeNormalize(date); - date.month -= 1; - date.monthDay -= 100; - RecurrenceProcessor.unsafeNormalize(date); - } - - endTime = System.currentTimeMillis(); - elapsed = endTime - startTime; - Log.i(TAG, "date: " + date.format2445()); - Log.i(TAG, "testPerformanceNormalize() unsafeNormalize() elapsed millis: " + elapsed); - } - } diff --git a/tests/src/com/android/calendarcommon/RecurrenceSetTest.java b/tests/src/com/android/calendarcommon/RecurrenceSetTest.java deleted file mode 100644 index 3b348b0..0000000 --- a/tests/src/com/android/calendarcommon/RecurrenceSetTest.java +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Copyright (C) 2009 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.calendarcommon; - -import com.android.calendarcommon.ICalendar; -import com.android.calendarcommon.RecurrenceSet; - -import android.content.ContentValues; -import android.test.suitebuilder.annotation.SmallTest; -import android.util.Log; -import android.provider.CalendarContract; -import junit.framework.TestCase; - -/** - * Test some pim.RecurrenceSet functionality. - */ -public class RecurrenceSetTest extends TestCase { - - // Test a recurrence - @SmallTest - public void testRecurrenceSet0() throws Exception { - String recurrence = "DTSTART;TZID=America/New_York:20080221T070000\n" - + "DTEND;TZID=America/New_York:20080221T190000\n" - + "RRULE:FREQ=DAILY;UNTIL=20080222T000000Z\n" - + "EXDATE:20080222T120000Z"; - verifyPopulateContentValues(recurrence, "FREQ=DAILY;UNTIL=20080222T000000Z", null, - null, "20080222T120000Z", 1203595200000L, "America/New_York", "P43200S", 0, false); - } - - // Test 1 day all-day event - @SmallTest - public void testRecurrenceSet1() throws Exception { - String recurrence = "DTSTART;VALUE=DATE:20090821\nDTEND;VALUE=DATE:20090822\n" - + "RRULE:FREQ=YEARLY;WKST=SU"; - verifyPopulateContentValues(recurrence, "FREQ=YEARLY;WKST=SU", null, - null, null, 1250812800000L, "UTC", "P1D", 1, false); - } - - // Test 2 day all-day event - @SmallTest - public void testRecurrenceSet2() throws Exception { - String recurrence = "DTSTART;VALUE=DATE:20090821\nDTEND;VALUE=DATE:20090823\n" - + "RRULE:FREQ=YEARLY;WKST=SU"; - verifyPopulateContentValues(recurrence, "FREQ=YEARLY;WKST=SU", null, - null, null, 1250812800000L, "UTC", "P2D", 1, false); - } - - // Test multi-rule RRULE. - @SmallTest - public void testRecurrenceSet3() throws Exception { - String recurrence = "DTSTART;VALUE=DATE:20090821\n" - + "RRULE:FREQ=YEARLY;WKST=SU\n" - + "RRULE:FREQ=MONTHLY;COUNT=3\n" - + "DURATION:P2H"; - verifyPopulateContentValues(recurrence, "FREQ=YEARLY;WKST=SU\nFREQ=MONTHLY;COUNT=3", null, - null, null, 1250812800000L, "UTC", "P2H", 1 /*allDay*/, false); - // allDay=1 just means the start time is 00:00:00 UTC. - } - - // Test RDATE with VALUE=DATE. - @SmallTest - public void testRecurrenceSet4() throws Exception { - String recurrence = "DTSTART;TZID=America/Los_Angeles:20090821T010203\n" - + "RDATE;TZID=America/Los_Angeles;VALUE=DATE:20110601,20110602,20110603\n" - + "DURATION:P2H"; - verifyPopulateContentValues(recurrence, null, - //"TZID=America/Los_Angeles;VALUE=DATE:20110601,20110602,20110603", - "America/Los_Angeles;20110601,20110602,20110603", // incorrect - null, null, 1250841723000L, "America/Los_Angeles", "P2H", 0 /*allDay*/, false); - // allDay=1 just means the start time is 00:00:00 UTC. - } - - // Check generation of duration from events in different time zones. - @SmallTest - public void testRecurrenceSet5() throws Exception { - String recurrence = "DTSTART;TZID=America/Los_Angeles:20090821T070000\n" - + "DTEND;TZID=America/New_York:20090821T110000\n" - + "RRULE:FREQ=YEARLY\n"; - verifyPopulateContentValues(recurrence, "FREQ=YEARLY", null, - null, null, 1250863200000L, "America/Los_Angeles", "P3600S" /*P1H*/, 0 /*allDay*/, - false); - // TODO: would like to use P1H for duration - - String recurrence2 = "DTSTART;TZID=America/New_York:20090821T100000\n" - + "DTEND;TZID=America/Los_Angeles:20090821T080000\n" - + "RRULE:FREQ=YEARLY\n"; - verifyPopulateContentValues(recurrence, "FREQ=YEARLY", null, - null, null, 1250863200000L, "America/Los_Angeles", "P3600S" /*P1H*/, 0 /*allDay*/, - false); - // TODO: should we rigorously define which tzid becomes the "event timezone"? - } - - // Test a failure to parse the recurrence data - @SmallTest - public void testRecurrenceSetBadDstart() throws Exception { - String recurrence = "DTSTART;TZID=GMT+05:30:20080221T070000\n" - + "DTEND;TZID=GMT+05:30:20080221T190000\n" - + "RRULE:FREQ=DAILY;UNTIL=20080222T000000Z\n" - + "EXDATE:20080222T120000Z"; - verifyPopulateContentValues(recurrence, "FREQ=DAILY;UNTIL=20080222T000000Z", null, - null, "20080222T120000Z", 1203595200000L, "America/New_York", "P43200S", 0, true); - } - - @SmallTest - public void testRecurrenceSetBadRrule() throws Exception { - String recurrence = "DTSTART;TZID=America/New_York:20080221T070000\n" - + "DTEND;TZID=GMT+05:30:20080221T190000\n" - + "RRULE:FREQ=NEVER;UNTIL=20080222T000000Z\n" - + "EXDATE:20080222T120000Z"; - verifyPopulateContentValues(recurrence, "FREQ=DAILY;UNTIL=20080222T000000Z", null, - null, "20080222T120000Z", 1203595200000L, "America/New_York", "P43200S", 0, true); - } - - // run populateContentValues and verify the results - private void verifyPopulateContentValues(String recurrence, String rrule, String rdate, - String exrule, String exdate, long dtstart, String tzid, String duration, int allDay, - boolean badFormat) - throws ICalendar.FormatException { - ICalendar.Component recurrenceComponent = - new ICalendar.Component("DUMMY", null /* parent */); - ICalendar.parseComponent(recurrenceComponent, recurrence); - ContentValues values = new ContentValues(); - boolean result = RecurrenceSet.populateContentValues(recurrenceComponent, values); - Log.d("KS", "values " + values); - - if (badFormat) { - assertEquals(result, !badFormat); - return; - } - assertEquals(rrule, values.get(android.provider.CalendarContract.Events.RRULE)); - assertEquals(rdate, values.get(android.provider.CalendarContract.Events.RDATE)); - assertEquals(exrule, values.get(android.provider.CalendarContract.Events.EXRULE)); - assertEquals(exdate, values.get(android.provider.CalendarContract.Events.EXDATE)); - assertEquals(dtstart, (long) values.getAsLong(CalendarContract.Events.DTSTART)); - assertEquals(tzid, values.get(android.provider.CalendarContract.Events.EVENT_TIMEZONE)); - assertEquals(duration, values.get(android.provider.CalendarContract.Events.DURATION)); - assertEquals(allDay, - (int) values.getAsInteger(android.provider.CalendarContract.Events.ALL_DAY)); - } - -} diff --git a/tests/src/com/android/calendarcommon2/DurationTest.java b/tests/src/com/android/calendarcommon2/DurationTest.java new file mode 100644 index 0000000..39320a3 --- /dev/null +++ b/tests/src/com/android/calendarcommon2/DurationTest.java @@ -0,0 +1,77 @@ +/* //device/content/providers/pim/DurationTest.java +** +** Copyright 2006, The Android Open Source Project +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +*/ + +package com.android.calendarcommon2; + +import junit.framework.TestCase; + +import android.test.suitebuilder.annotation.SmallTest; + +public class DurationTest extends TestCase { + + private void verifyDuration(String str, + int sign, int weeks, int days, int hours, + int minutes, int seconds) throws DateException { + + Duration duration = new Duration(); + duration.parse(str); + + assertEquals("Duration sign is not equal for " + str, sign, duration.sign); + assertEquals("Duration weeks is not equal for " + str, weeks, duration.weeks); + assertEquals("Duration days is not equal for " + str, days, duration.days); + assertEquals("Duration hours is not equal for " + str, hours, duration.hours); + assertEquals("Duration minutes is not equal for " + str, minutes, duration.minutes); + assertEquals("Duration seconds is not equal for " + str, seconds, duration.seconds); + } + + @SmallTest + public void testParse() throws Exception { + verifyDuration("P7W", 1, 7, 0, 0, 0, 0); + verifyDuration("PT7W", 1, 7, 0, 0, 0, 0); + verifyDuration("-PT7W", -1, 7, 0, 0, 0, 0); + verifyDuration("P15DT5H0M20S", 1, 0, 15, 5, 0, 20); + verifyDuration("-P15DT5H0M20S", -1, 0, 15, 5, 0, 20); + verifyDuration("PT1H2M3S", 1, 0, 0, 1, 2, 3); + + verifyDuration("", 1, 0, 0, 0, 0, 0); + verifyDuration("P", 1, 0, 0, 0, 0, 0); + verifyDuration("P0W", 1, 0, 0, 0, 0, 0); + verifyDuration("P0D", 1, 0, 0, 0, 0, 0); + verifyDuration("PT0H0M0S", 1, 0, 0, 0, 0, 0); + verifyDuration("P0DT0H0M0S", 1, 0, 0, 0, 0, 0); + } + + @SmallTest + public void testParseInvalidStrings() throws Exception { + try { + verifyDuration(" -P15DT5H0M20S", 0, 0, 0, 0, 0, 0); + fail("test didn't throw an exception but we expected it to"); + } catch (DateException e) { + // expected + } + + try { + verifyDuration(" not even close", 0, 0, 0, 0, 0, 0); + fail("test didn't throw an exception but we expected it to"); + } catch (DateException e) { + // expected + } + } +} + + + diff --git a/tests/src/com/android/calendarcommon2/EventRecurrenceTest.java b/tests/src/com/android/calendarcommon2/EventRecurrenceTest.java new file mode 100644 index 0000000..c25e3ec --- /dev/null +++ b/tests/src/com/android/calendarcommon2/EventRecurrenceTest.java @@ -0,0 +1,889 @@ +/* + * Copyright (C) 2006 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.calendarcommon2; + +import com.android.calendarcommon2.EventRecurrence.InvalidFormatException; + +import android.test.suitebuilder.annotation.SmallTest; +import android.test.suitebuilder.annotation.Suppress; + +import junit.framework.TestCase; + +import java.util.Arrays; + +/** + * Test android.pim.EventRecurrence. + * + * adb shell am instrument -w -e class com.android.calendarcommon2.EventRecurrenceTest \ + * com.android.calendarcommon2.tests/android.test.InstrumentationTestRunner + */ +public class EventRecurrenceTest extends TestCase { + + @SmallTest + public void test0() throws Exception { + verifyRecurType("FREQ=SECONDLY", + /* int freq */ EventRecurrence.SECONDLY, + /* String until */ null, + /* int count */ 0, + /* int interval */ 0, + /* int[] bysecond */ null, + /* int[] byminute */ null, + /* int[] byhour */ null, + /* int[] byday */ null, + /* int[] bydayNum */ null, + /* int[] bymonthday */ null, + /* int[] byyearday */ null, + /* int[] byweekno */ null, + /* int[] bymonth */ null, + /* int[] bysetpos */ null, + /* int wkst */ EventRecurrence.MO + ); + } + + @SmallTest + public void test1() throws Exception { + verifyRecurType("FREQ=MINUTELY", + /* int freq */ EventRecurrence.MINUTELY, + /* String until */ null, + /* int count */ 0, + /* int interval */ 0, + /* int[] bysecond */ null, + /* int[] byminute */ null, + /* int[] byhour */ null, + /* int[] byday */ null, + /* int[] bydayNum */ null, + /* int[] bymonthday */ null, + /* int[] byyearday */ null, + /* int[] byweekno */ null, + /* int[] bymonth */ null, + /* int[] bysetpos */ null, + /* int wkst */ EventRecurrence.MO + ); + } + + @SmallTest + public void test2() throws Exception { + verifyRecurType("FREQ=HOURLY", + /* int freq */ EventRecurrence.HOURLY, + /* String until */ null, + /* int count */ 0, + /* int interval */ 0, + /* int[] bysecond */ null, + /* int[] byminute */ null, + /* int[] byhour */ null, + /* int[] byday */ null, + /* int[] bydayNum */ null, + /* int[] bymonthday */ null, + /* int[] byyearday */ null, + /* int[] byweekno */ null, + /* int[] bymonth */ null, + /* int[] bysetpos */ null, + /* int wkst */ EventRecurrence.MO + ); + } + + @SmallTest + public void test3() throws Exception { + verifyRecurType("FREQ=DAILY", + /* int freq */ EventRecurrence.DAILY, + /* String until */ null, + /* int count */ 0, + /* int interval */ 0, + /* int[] bysecond */ null, + /* int[] byminute */ null, + /* int[] byhour */ null, + /* int[] byday */ null, + /* int[] bydayNum */ null, + /* int[] bymonthday */ null, + /* int[] byyearday */ null, + /* int[] byweekno */ null, + /* int[] bymonth */ null, + /* int[] bysetpos */ null, + /* int wkst */ EventRecurrence.MO + ); + } + + @SmallTest + public void test4() throws Exception { + verifyRecurType("FREQ=WEEKLY", + /* int freq */ EventRecurrence.WEEKLY, + /* String until */ null, + /* int count */ 0, + /* int interval */ 0, + /* int[] bysecond */ null, + /* int[] byminute */ null, + /* int[] byhour */ null, + /* int[] byday */ null, + /* int[] bydayNum */ null, + /* int[] bymonthday */ null, + /* int[] byyearday */ null, + /* int[] byweekno */ null, + /* int[] bymonth */ null, + /* int[] bysetpos */ null, + /* int wkst */ EventRecurrence.MO + ); + } + + @SmallTest + public void test5() throws Exception { + verifyRecurType("FREQ=MONTHLY", + /* int freq */ EventRecurrence.MONTHLY, + /* String until */ null, + /* int count */ 0, + /* int interval */ 0, + /* int[] bysecond */ null, + /* int[] byminute */ null, + /* int[] byhour */ null, + /* int[] byday */ null, + /* int[] bydayNum */ null, + /* int[] bymonthday */ null, + /* int[] byyearday */ null, + /* int[] byweekno */ null, + /* int[] bymonth */ null, + /* int[] bysetpos */ null, + /* int wkst */ EventRecurrence.MO + ); + } + + @SmallTest + public void test6() throws Exception { + verifyRecurType("FREQ=YEARLY", + /* int freq */ EventRecurrence.YEARLY, + /* String until */ null, + /* int count */ 0, + /* int interval */ 0, + /* int[] bysecond */ null, + /* int[] byminute */ null, + /* int[] byhour */ null, + /* int[] byday */ null, + /* int[] bydayNum */ null, + /* int[] bymonthday */ null, + /* int[] byyearday */ null, + /* int[] byweekno */ null, + /* int[] bymonth */ null, + /* int[] bysetpos */ null, + /* int wkst */ EventRecurrence.MO + ); + } + + @SmallTest + public void test7() throws Exception { + // with an until + verifyRecurType("FREQ=DAILY;UNTIL=112233T223344Z", + /* int freq */ EventRecurrence.DAILY, + /* String until */ "112233T223344Z", + /* int count */ 0, + /* int interval */ 0, + /* int[] bysecond */ null, + /* int[] byminute */ null, + /* int[] byhour */ null, + /* int[] byday */ null, + /* int[] bydayNum */ null, + /* int[] bymonthday */ null, + /* int[] byyearday */ null, + /* int[] byweekno */ null, + /* int[] bymonth */ null, + /* int[] bysetpos */ null, + /* int wkst */ EventRecurrence.MO + ); + } + + @SmallTest + public void test8() throws Exception { + // with a count + verifyRecurType("FREQ=DAILY;COUNT=334", + /* int freq */ EventRecurrence.DAILY, + /* String until */ null, + /* int count */ 334, + /* int interval */ 0, + /* int[] bysecond */ null, + /* int[] byminute */ null, + /* int[] byhour */ null, + /* int[] byday */ null, + /* int[] bydayNum */ null, + /* int[] bymonthday */ null, + /* int[] byyearday */ null, + /* int[] byweekno */ null, + /* int[] bymonth */ null, + /* int[] bysetpos */ null, + /* int wkst */ EventRecurrence.MO + ); + } + + @SmallTest + public void test9() throws Exception { + // with a count + verifyRecurType("FREQ=DAILY;INTERVAL=5000", + /* int freq */ EventRecurrence.DAILY, + /* String until */ null, + /* int count */ 0, + /* int interval */ 5000, + /* int[] bysecond */ null, + /* int[] byminute */ null, + /* int[] byhour */ null, + /* int[] byday */ null, + /* int[] bydayNum */ null, + /* int[] bymonthday */ null, + /* int[] byyearday */ null, + /* int[] byweekno */ null, + /* int[] bymonth */ null, + /* int[] bysetpos */ null, + /* int wkst */ EventRecurrence.MO + ); + } + + @SmallTest + public void test10() throws Exception { + // verifyRecurType all of the BY* ones with one element + verifyRecurType("FREQ=DAILY" + + ";BYSECOND=0" + + ";BYMINUTE=1" + + ";BYHOUR=2" + + ";BYMONTHDAY=30" + + ";BYYEARDAY=300" + + ";BYWEEKNO=53" + + ";BYMONTH=12" + + ";BYSETPOS=-15" + + ";WKST=SU", + /* int freq */ EventRecurrence.DAILY, + /* String until */ null, + /* int count */ 0, + /* int interval */ 0, + /* int[] bysecond */ new int[]{0}, + /* int[] byminute */ new int[]{1}, + /* int[] byhour */ new int[]{2}, + /* int[] byday */ null, + /* int[] bydayNum */ null, + /* int[] bymonthday */ new int[]{30}, + /* int[] byyearday */ new int[]{300}, + /* int[] byweekno */ new int[]{53}, + /* int[] bymonth */ new int[]{12}, + /* int[] bysetpos */ new int[]{-15}, + /* int wkst */ EventRecurrence.SU + ); + } + + @SmallTest + public void test11() throws Exception { + // verifyRecurType all of the BY* ones with one element + verifyRecurType("FREQ=DAILY" + + ";BYSECOND=0,30,59" + + ";BYMINUTE=0,41,59" + + ";BYHOUR=0,4,23" + + ";BYMONTHDAY=-31,-1,1,31" + + ";BYYEARDAY=-366,-1,1,366" + + ";BYWEEKNO=-53,-1,1,53" + + ";BYMONTH=1,12" + + ";BYSETPOS=1,2,3,4,500,10000" + + ";WKST=SU", + /* int freq */ EventRecurrence.DAILY, + /* String until */ null, + /* int count */ 0, + /* int interval */ 0, + /* int[] bysecond */ new int[]{0, 30, 59}, + /* int[] byminute */ new int[]{0, 41, 59}, + /* int[] byhour */ new int[]{0, 4, 23}, + /* int[] byday */ null, + /* int[] bydayNum */ null, + /* int[] bymonthday */ new int[]{-31, -1, 1, 31}, + /* int[] byyearday */ new int[]{-366, -1, 1, 366}, + /* int[] byweekno */ new int[]{-53, -1, 1, 53}, + /* int[] bymonth */ new int[]{1, 12}, + /* int[] bysetpos */ new int[]{1, 2, 3, 4, 500, 10000}, + /* int wkst */ EventRecurrence.SU + ); + } + + private static class Check { + Check(String k, int... v) { + key = k; + values = v; + } + + String key; + int[] values; + } + + // this is a negative verifyRecurType case to verifyRecurType the range of the numbers accepted + @SmallTest + public void test12() throws Exception { + Check[] checks = new Check[]{ + new Check("BYSECOND", -100, -1, 60, 100), + new Check("BYMINUTE", -100, -1, 60, 100), + new Check("BYHOUR", -100, -1, 24, 100), + new Check("BYMONTHDAY", -100, -32, 0, 32, 100), + new Check("BYYEARDAY", -400, -367, 0, 367, 400), + new Check("BYWEEKNO", -100, -54, 0, 54, 100), + new Check("BYMONTH", -100, -5, 0, 13, 100) + }; + + for (Check ck : checks) { + for (int n : ck.values) { + String recur = "FREQ=DAILY;" + ck.key + "=" + n; + try { + EventRecurrence er = new EventRecurrence(); + er.parse(recur); + fail("Negative verifyRecurType failed. " + + " parse failed to throw an exception for '" + + recur + "'"); + } catch (EventRecurrence.InvalidFormatException e) { + // expected + } + } + } + } + + // verifyRecurType BYDAY + @SmallTest + public void test13() throws Exception { + verifyRecurType("FREQ=DAILY;BYDAY=1SU,-2MO,+33TU,WE,TH,FR,SA", + /* int freq */ EventRecurrence.DAILY, + /* String until */ null, + /* int count */ 0, + /* int interval */ 0, + /* int[] bysecond */ null, + /* int[] byminute */ null, + /* int[] byhour */ null, + /* int[] byday */ new int[] { + EventRecurrence.SU, + EventRecurrence.MO, + EventRecurrence.TU, + EventRecurrence.WE, + EventRecurrence.TH, + EventRecurrence.FR, + EventRecurrence.SA + }, + /* int[] bydayNum */ new int[]{1, -2, 33, 0, 0, 0, 0}, + /* int[] bymonthday */ null, + /* int[] byyearday */ null, + /* int[] byweekno */ null, + /* int[] bymonth */ null, + /* int[] bysetpos */ null, + /* int wkst */ EventRecurrence.MO + ); + } + + @Suppress + // Repro bug #2331761 - this should fail because of the last comma into BYDAY + public void test14() throws Exception { + verifyRecurType("FREQ=WEEKLY;WKST=MO;UNTIL=20100129T130000Z;INTERVAL=1;BYDAY=MO,TU,WE,", + /* int freq */ EventRecurrence.WEEKLY, + /* String until */ "20100129T130000Z", + /* int count */ 0, + /* int interval */ 1, + /* int[] bysecond */ null, + /* int[] byminute */ null, + /* int[] byhour */ null, + /* int[] byday */ new int[] { + EventRecurrence.MO, + EventRecurrence.TU, + EventRecurrence.WE, + }, + /* int[] bydayNum */ new int[]{0, 0, 0}, + /* int[] bymonthday */ null, + /* int[] byyearday */ null, + /* int[] byweekno */ null, + /* int[] bymonth */ null, + /* int[] bysetpos */ null, + /* int wkst */ EventRecurrence.MO + ); + } + + // This test should pass + public void test15() throws Exception { + verifyRecurType("FREQ=WEEKLY;WKST=MO;UNTIL=20100129T130000Z;INTERVAL=1;" + + "BYDAY=MO,TU,WE,TH,FR,SA,SU", + /* int freq */ EventRecurrence.WEEKLY, + /* String until */ "20100129T130000Z", + /* int count */ 0, + /* int interval */ 1, + /* int[] bysecond */ null, + /* int[] byminute */ null, + /* int[] byhour */ null, + /* int[] byday */ new int[] { + EventRecurrence.MO, + EventRecurrence.TU, + EventRecurrence.WE, + EventRecurrence.TH, + EventRecurrence.FR, + EventRecurrence.SA, + EventRecurrence.SU + }, + /* int[] bydayNum */ new int[]{0, 0, 0, 0, 0, 0, 0}, + /* int[] bymonthday */ null, + /* int[] byyearday */ null, + /* int[] byweekno */ null, + /* int[] bymonth */ null, + /* int[] bysetpos */ null, + /* int wkst */ EventRecurrence.MO + ); + } + + // Sample coming from RFC2445 + public void test16() throws Exception { + verifyRecurType("FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1", + /* int freq */ EventRecurrence.MONTHLY, + /* String until */ null, + /* int count */ 0, + /* int interval */ 0, + /* int[] bysecond */ null, + /* int[] byminute */ null, + /* int[] byhour */ null, + /* int[] byday */ new int[] { + EventRecurrence.MO, + EventRecurrence.TU, + EventRecurrence.WE, + EventRecurrence.TH, + EventRecurrence.FR + }, + /* int[] bydayNum */ new int[] {0, 0, 0, 0, 0}, + /* int[] bymonthday */ null, + /* int[] byyearday */ null, + /* int[] byweekno */ null, + /* int[] bymonth */ null, + /* int[] bysetpos */ new int[] { -1 }, + /* int wkst */ EventRecurrence.MO + ); + } + + // Sample coming from RFC2445 + public void test17() throws Exception { + verifyRecurType("FREQ=DAILY;COUNT=10;INTERVAL=2", + /* int freq */ EventRecurrence.DAILY, + /* String until */ null, + /* int count */ 10, + /* int interval */ 2, + /* int[] bysecond */ null, + /* int[] byminute */ null, + /* int[] byhour */ null, + /* int[] byday */ null, + /* int[] bydayNum */ null, + /* int[] bymonthday */ null, + /* int[] byyearday */ null, + /* int[] byweekno */ null, + /* int[] bymonth */ null, + /* int[] bysetpos */ null, + /* int wkst */ EventRecurrence.MO + ); + } + + // Sample coming from RFC2445 + public void test18() throws Exception { + verifyRecurType("FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10", + /* int freq */ EventRecurrence.YEARLY, + /* String until */ null, + /* int count */ 0, + /* int interval */ 0, + /* int[] bysecond */ null, + /* int[] byminute */ null, + /* int[] byhour */ null, + /* int[] byday */ new int[] { + EventRecurrence.SU + }, + /* int[] bydayNum */ new int[] { -1 }, + /* int[] bymonthday */ null, + /* int[] byyearday */ null, + /* int[] byweekno */ null, + /* int[] bymonth */ new int[] { 10 }, + /* int[] bysetpos */ null, + /* int wkst */ EventRecurrence.MO + ); + } + + // Sample coming from bug #1640517 + public void test19() throws Exception { + verifyRecurType("FREQ=YEARLY;BYMONTH=3;BYDAY=TH", + /* int freq */ EventRecurrence.YEARLY, + /* String until */ null, + /* int count */ 0, + /* int interval */ 0, + /* int[] bysecond */ null, + /* int[] byminute */ null, + /* int[] byhour */ null, + /* int[] byday */ new int[] { + EventRecurrence.TH + }, + /* int[] bydayNum */ new int[] { 0 }, + /* int[] bymonthday */ null, + /* int[] byyearday */ null, + /* int[] byweekno */ null, + /* int[] bymonth */ new int[] { 3 }, + /* int[] bysetpos */ null, + /* int wkst */ EventRecurrence.MO + ); + } + + // INTERVAL = 0 -> Interval = 1 bug #5676414 + public void test20() throws Exception { + verifyRecurType("FREQ=YEARLY;BYMONTHDAY=18;BYMONTH=10;INTERVAL=0;", + /* int freq */ EventRecurrence.YEARLY, + /* String until */ null, + /* int count */ 0, + /* int interval */ 1, + /* int[] bysecond */ null, + /* int[] byminute */ null, + /* int[] byhour */ null, + /* int[] byday */ null, + /* int[] bydayNum */ null, + /* int[] bymonthday */ new int[]{18}, + /* int[] byyearday */ null, + /* int[] byweekno */ null, + /* int[] bymonth */ new int[]{10}, + /* int[] bysetpos */ null, + /* int wkst */ EventRecurrence.MO + ); + } + + // Working case: INTERVAL=1 -> Interval = 1 bug #5676414 + public void test21() throws Exception { + verifyRecurType("FREQ=WEEKLY;WKST=SU;INTERVAL=1;BYDAY=MO,TU,WE,TH,FR", + /* int freq */ EventRecurrence.WEEKLY, + /* String until */ null, + /* int count */ 0, + /* int interval */ 1, + /* int[] bysecond */ null, + /* int[] byminute */ null, + /* int[] byhour */ null, + /* int[] byday */ new int[] { + EventRecurrence.MO, + EventRecurrence.TU, + EventRecurrence.WE, + EventRecurrence.TH, + EventRecurrence.FR, + }, + /* int[] bydayNum */ new int[]{0, 0, 0, 0, 0}, + /* int[] bymonthday */ null, + /* int[] byyearday */ null, + /* int[] byweekno */ null, + /* int[] bymonth */ null, + /* int[] bysetpos */ null, + /* int wkst */ EventRecurrence.SU + ); + } + + // Working case: INTERVAL=2 -> Interval = 2 bug #5676414 + public void test22() throws Exception { + verifyRecurType("FREQ=WEEKLY;WKST=SU;INTERVAL=2;BYDAY=MO,TU,WE,TH,FR", + /* int freq */ EventRecurrence.WEEKLY, + /* String until */ null, + /* int count */ 0, + /* int interval */ 2, + /* int[] bysecond */ null, + /* int[] byminute */ null, + /* int[] byhour */ null, + /* int[] byday */ new int[] { + EventRecurrence.MO, + EventRecurrence.TU, + EventRecurrence.WE, + EventRecurrence.TH, + EventRecurrence.FR, + }, + /* int[] bydayNum */ new int[]{0, 0, 0, 0, 0}, + /* int[] bymonthday */ null, + /* int[] byyearday */ null, + /* int[] byweekno */ null, + /* int[] bymonth */ null, + /* int[] bysetpos */ null, + /* int wkst */ EventRecurrence.SU + ); + } + + // COUNT < 0 -> Count = 1 bug #5676414 + public void test23() throws Exception { + verifyRecurType("FREQ=WEEKLY;COUNT=-20;BYDAY=MO,TU,WE,TH,FR;", + /* int freq */ EventRecurrence.WEEKLY, + /* String until */ null, + /* int count */ 1, + /* int interval */ 0, + /* int[] bysecond */ null, + /* int[] byminute */ null, + /* int[] byhour */ null, + /* int[] byday */ new int[] { + EventRecurrence.MO, + EventRecurrence.TU, + EventRecurrence.WE, + EventRecurrence.TH, + EventRecurrence.FR, + }, + /* int[] bydayNum */ new int[]{0, 0, 0, 0, 0}, + /* int[] bymonthday */ null, + /* int[] byyearday */ null, + /* int[] byweekno */ null, + /* int[] bymonth */ null, + /* int[] bysetpos */ null, + /* int wkst */ EventRecurrence.MO + ); + } + + // Working case: COUNT=2 -> Count=2 bug #5676414 + public void test24() throws Exception { + verifyRecurType("FREQ=WEEKLY;COUNT=2;BYDAY=MO,TU,WE,TH,FR;", + /* int freq */ EventRecurrence.WEEKLY, + /* String until */ null, + /* int count */ 2, + /* int interval */ 0, + /* int[] bysecond */ null, + /* int[] byminute */ null, + /* int[] byhour */ null, + /* int[] byday */ new int[] { + EventRecurrence.MO, + EventRecurrence.TU, + EventRecurrence.WE, + EventRecurrence.TH, + EventRecurrence.FR, + }, + /* int[] bydayNum */ new int[]{0, 0, 0, 0, 0}, + /* int[] bymonthday */ null, + /* int[] byyearday */ null, + /* int[] byweekno */ null, + /* int[] bymonth */ null, + /* int[] bysetpos */ null, + /* int wkst */ EventRecurrence.MO + ); + } + + // for your copying pleasure + public void fakeTestXX() throws Exception { + verifyRecurType("FREQ=DAILY;", + /* int freq */ EventRecurrence.DAILY, + /* String until */ null, + /* int count */ 0, + /* int interval */ 0, + /* int[] bysecond */ null, + /* int[] byminute */ null, + /* int[] byhour */ null, + /* int[] byday */ null, + /* int[] bydayNum */ null, + /* int[] bymonthday */ null, + /* int[] byyearday */ null, + /* int[] byweekno */ null, + /* int[] bymonth */ null, + /* int[] bysetpos */ null, + /* int wkst */ EventRecurrence.MO + ); + } + + private static void cmp(int vlen, int[] v, int[] correct, String name) { + if ((correct == null && v != null) + || (correct != null && v == null)) { + throw new RuntimeException("One is null, one isn't for " + name + + ": correct=" + Arrays.toString(correct) + + " actual=" + Arrays.toString(v)); + } + if ((correct == null && vlen != 0) + || (vlen != (correct == null ? 0 : correct.length))) { + throw new RuntimeException("Reported length mismatch for " + name + + ": correct=" + ((correct == null) ? "null" : correct.length) + + " actual=" + vlen); + } + if (correct == null) { + return; + } + if (v.length < correct.length) { + throw new RuntimeException("Array length mismatch for " + name + + ": correct=" + Arrays.toString(correct) + + " actual=" + Arrays.toString(v)); + } + for (int i = 0; i < correct.length; i++) { + if (v[i] != correct[i]) { + throw new RuntimeException("Array value mismatch for " + name + + ": correct=" + Arrays.toString(correct) + + " actual=" + Arrays.toString(v)); + } + } + } + + private static boolean eq(String a, String b) { + if ((a == null && b != null) || (a != null && b == null)) { + return false; + } else { + return a == b || a.equals(b); + } + } + + private static void verifyRecurType(String recur, + int freq, String until, int count, int interval, + int[] bysecond, int[] byminute, int[] byhour, + int[] byday, int[] bydayNum, int[] bymonthday, + int[] byyearday, int[] byweekno, int[] bymonth, + int[] bysetpos, int wkst) { + EventRecurrence eventRecurrence = new EventRecurrence(); + eventRecurrence.parse(recur); + if (eventRecurrence.freq != freq + || !eq(eventRecurrence.until, until) + || eventRecurrence.count != count + || eventRecurrence.interval != interval + || eventRecurrence.wkst != wkst) { + System.out.println("Error... got:"); + print(eventRecurrence); + System.out.println("expected:"); + System.out.println("{"); + System.out.println(" freq=" + freq); + System.out.println(" until=" + until); + System.out.println(" count=" + count); + System.out.println(" interval=" + interval); + System.out.println(" wkst=" + wkst); + System.out.println(" bysecond=" + Arrays.toString(bysecond)); + System.out.println(" byminute=" + Arrays.toString(byminute)); + System.out.println(" byhour=" + Arrays.toString(byhour)); + System.out.println(" byday=" + Arrays.toString(byday)); + System.out.println(" bydayNum=" + Arrays.toString(bydayNum)); + System.out.println(" bymonthday=" + Arrays.toString(bymonthday)); + System.out.println(" byyearday=" + Arrays.toString(byyearday)); + System.out.println(" byweekno=" + Arrays.toString(byweekno)); + System.out.println(" bymonth=" + Arrays.toString(bymonth)); + System.out.println(" bysetpos=" + Arrays.toString(bysetpos)); + System.out.println("}"); + throw new RuntimeException("Mismatch in fields"); + } + cmp(eventRecurrence.bysecondCount, eventRecurrence.bysecond, bysecond, "bysecond"); + cmp(eventRecurrence.byminuteCount, eventRecurrence.byminute, byminute, "byminute"); + cmp(eventRecurrence.byhourCount, eventRecurrence.byhour, byhour, "byhour"); + cmp(eventRecurrence.bydayCount, eventRecurrence.byday, byday, "byday"); + cmp(eventRecurrence.bydayCount, eventRecurrence.bydayNum, bydayNum, "bydayNum"); + cmp(eventRecurrence.bymonthdayCount, eventRecurrence.bymonthday, bymonthday, "bymonthday"); + cmp(eventRecurrence.byyeardayCount, eventRecurrence.byyearday, byyearday, "byyearday"); + cmp(eventRecurrence.byweeknoCount, eventRecurrence.byweekno, byweekno, "byweekno"); + cmp(eventRecurrence.bymonthCount, eventRecurrence.bymonth, bymonth, "bymonth"); + cmp(eventRecurrence.bysetposCount, eventRecurrence.bysetpos, bysetpos, "bysetpos"); + } + + private static void print(EventRecurrence er) { + System.out.println("{"); + System.out.println(" freq=" + er.freq); + System.out.println(" until=" + er.until); + System.out.println(" count=" + er.count); + System.out.println(" interval=" + er.interval); + System.out.println(" wkst=" + er.wkst); + System.out.println(" bysecond=" + Arrays.toString(er.bysecond)); + System.out.println(" bysecondCount=" + er.bysecondCount); + System.out.println(" byminute=" + Arrays.toString(er.byminute)); + System.out.println(" byminuteCount=" + er.byminuteCount); + System.out.println(" byhour=" + Arrays.toString(er.byhour)); + System.out.println(" byhourCount=" + er.byhourCount); + System.out.println(" byday=" + Arrays.toString(er.byday)); + System.out.println(" bydayNum=" + Arrays.toString(er.bydayNum)); + System.out.println(" bydayCount=" + er.bydayCount); + System.out.println(" bymonthday=" + Arrays.toString(er.bymonthday)); + System.out.println(" bymonthdayCount=" + er.bymonthdayCount); + System.out.println(" byyearday=" + Arrays.toString(er.byyearday)); + System.out.println(" byyeardayCount=" + er.byyeardayCount); + System.out.println(" byweekno=" + Arrays.toString(er.byweekno)); + System.out.println(" byweeknoCount=" + er.byweeknoCount); + System.out.println(" bymonth=" + Arrays.toString(er.bymonth)); + System.out.println(" bymonthCount=" + er.bymonthCount); + System.out.println(" bysetpos=" + Arrays.toString(er.bysetpos)); + System.out.println(" bysetposCount=" + er.bysetposCount); + System.out.println("}"); + } + + + /** A list of valid rules. The parser must accept these. */ + private static final String[] GOOD_RRULES = { + /* extracted wholesale from from RFC 2445 section 4.8.5.4 */ + "FREQ=DAILY;COUNT=10", + "FREQ=DAILY;UNTIL=19971224T000000Z", + "FREQ=DAILY;INTERVAL=2", + "FREQ=DAILY;INTERVAL=10;COUNT=5", + "FREQ=YEARLY;UNTIL=20000131T090000Z;BYMONTH=1;BYDAY=SU,MO,TU,WE,TH,FR,SA", + "FREQ=DAILY;UNTIL=20000131T090000Z;BYMONTH=1", + "FREQ=WEEKLY;COUNT=10", + "FREQ=WEEKLY;UNTIL=19971224T000000Z", + "FREQ=WEEKLY;INTERVAL=2;WKST=SU", + "FREQ=WEEKLY;UNTIL=19971007T000000Z;WKST=SU;BYDAY=TU,TH", + "FREQ=WEEKLY;COUNT=10;WKST=SU;BYDAY=TU,TH", + "FREQ=WEEKLY;INTERVAL=2;UNTIL=19971224T000000Z;WKST=SU;BYDAY=MO,WE,FR", + "FREQ=WEEKLY;INTERVAL=2;COUNT=8;WKST=SU;BYDAY=TU,TH", + "FREQ=MONTHLY;COUNT=10;BYDAY=1FR", + "FREQ=MONTHLY;UNTIL=19971224T000000Z;BYDAY=1FR", + "FREQ=MONTHLY;INTERVAL=2;COUNT=10;BYDAY=1SU,-1SU", + "FREQ=MONTHLY;COUNT=6;BYDAY=-2MO", + "FREQ=MONTHLY;BYMONTHDAY=-3", + "FREQ=MONTHLY;COUNT=10;BYMONTHDAY=2,15", + "FREQ=MONTHLY;COUNT=10;BYMONTHDAY=1,-1", + "FREQ=MONTHLY;INTERVAL=18;COUNT=10;BYMONTHDAY=10,11,12,13,14,15", + "FREQ=MONTHLY;INTERVAL=2;BYDAY=TU", + "FREQ=YEARLY;COUNT=10;BYMONTH=6,7", + "FREQ=YEARLY;INTERVAL=2;COUNT=10;BYMONTH=1,2,3", + "FREQ=YEARLY;INTERVAL=3;COUNT=10;BYYEARDAY=1,100,200", + "FREQ=YEARLY;BYDAY=20MO", + "FREQ=YEARLY;BYWEEKNO=20;BYDAY=MO", + "FREQ=YEARLY;BYMONTH=3;BYDAY=TH", + "FREQ=YEARLY;BYDAY=TH;BYMONTH=6,7,8", + "FREQ=MONTHLY;BYDAY=FR;BYMONTHDAY=13", + "FREQ=MONTHLY;BYDAY=SA;BYMONTHDAY=7,8,9,10,11,12,13", + "FREQ=YEARLY;INTERVAL=4;BYMONTH=11;BYDAY=TU;BYMONTHDAY=2,3,4,5,6,7,8", + "FREQ=MONTHLY;COUNT=3;BYDAY=TU,WE,TH;BYSETPOS=3", + "FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-2", + "FREQ=HOURLY;INTERVAL=3;UNTIL=19970902T170000Z", + "FREQ=MINUTELY;INTERVAL=15;COUNT=6", + "FREQ=MINUTELY;INTERVAL=90;COUNT=4", + "FREQ=DAILY;BYHOUR=9,10,11,12,13,14,15,16;BYMINUTE=0,20,40", + "FREQ=MINUTELY;INTERVAL=20;BYHOUR=9,10,11,12,13,14,15,16", + "FREQ=WEEKLY;INTERVAL=2;COUNT=4;BYDAY=TU,SU;WKST=MO", + "FREQ=WEEKLY;INTERVAL=2;COUNT=4;BYDAY=TU,SU;WKST=SU", + /* a few more */ + "FREQ=SECONDLY;BYSECOND=0,15,59", + "FREQ=MINUTELY;BYMINUTE=0,15,59", + "FREQ=HOURLY;BYHOUR=+0,+15,+23", + "INTERVAL=4;FREQ=YEARLY", + "FREQ=DAILY;X-WHATEVER=blah", + //"freq=daily;wkst=su", // mixed case currently not allowed + "FREQ=WEEKLY;INTERVAL=2;BYDAY=Mo;;UNTIL=20120327T000000Z", // double simicolon should be allowed + "FREQ=MONTHLY;BYDAY=1Mo", + "FREQ=MONTHLY;BYDAY=2Mo,2We,4Mo,4We", + "FREQ=MONTHLY;WKST=SU;BYMONTHDAY=25;UNTIL=20110524", + "FREQ=WEEKLY;BYDAY=MO;WKST=SU;UNTIL=20111218T010000Z" + }; + + /** The parser must reject these. */ + private static final String[] BAD_RRULES = { + "FREQ=MONTHLY;FREQ=MONTHLY", // can't specify twice + "FREQ=MONTHLY;COUNT=1;COUNT=1", // can't specify twice + "FREQ=SECONDLY;BYSECOND=60", // range + "FREQ=MINUTELY;BYMINUTE=-1", // range + "FREQ=HOURLY;BYHOUR=24", // range + "FREQ=YEARLY;BYMONTHDAY=0", // zero not valid + "BYMONTHDAY=1", // must specify FREQ + //"FREQ=YEARLY;COUNT=1;UNTIL=12345", // can't have both COUNT and UNTIL + //"FREQ=DAILY;UNTIL=19970829T021400e", // invalid date + }; + + /** + * Simple test of good/bad rules. + */ + @SmallTest + public void testBasicParse() { + for (String rule : GOOD_RRULES) { + EventRecurrence recur = new EventRecurrence(); + recur.parse(rule); + } + + for (String rule : BAD_RRULES) { + EventRecurrence recur = new EventRecurrence(); + boolean didThrow = false; + + try { + recur.parse(rule); + } catch (InvalidFormatException ife) { + didThrow = true; + } + + assertTrue("Expected throw on " + rule, didThrow); + } + } +} diff --git a/tests/src/com/android/calendarcommon2/RRuleTest.java b/tests/src/com/android/calendarcommon2/RRuleTest.java new file mode 100644 index 0000000..53988e2 --- /dev/null +++ b/tests/src/com/android/calendarcommon2/RRuleTest.java @@ -0,0 +1,756 @@ +/* +** +** Copyright 2009, The Android Open Source Project +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +*/ + +/* Based on http://code.google.com/p/google-rfc-2445/source/browse/trunk/test/com/google/ical/iter/RRuleIteratorImplTest.java */ + +package com.android.calendarcommon2; + +import com.android.calendarcommon2.RecurrenceSet; + +import android.os.Debug; +import android.test.suitebuilder.annotation.MediumTest; +import android.test.suitebuilder.annotation.Suppress; +import android.text.format.Time; +import junit.framework.TestCase; + +/** + * You can run those tests with: + * + * adb shell am instrument + * -e debug false + * -w + * -e + * class com.android.providers.calendar.RRuleTest + * com.android.providers.calendar.tests/android.test.InstrumentationTestRunner + */ + +public class RRuleTest extends TestCase { + private static final String TAG = "RRuleTest"; + private static final boolean METHOD_TRACE = false; + + private static String[] getFormattedDates(long[] dates, Time time, boolean truncate) { + String[] out = new String[dates.length]; + int i = 0; + for (long date : dates) { + time.set(date); + if (truncate) { + out[i] = time.format2445().substring(0, 8); // Just YYMMDD + } else { + out[i] = time.format2445().substring(0, 15); // YYMMDDThhmmss + } + ++i; + } + return out; + } + + static final String PST = "America/Los_Angeles"; + static final String UTC = "UTC"; + // Use this date as end of recurrence unlessotherwise specified. + static final String DEFAULT_END = "20091212"; + + private void runRecurrenceIteratorTest(String rruleText, String dtStart, int limit, + String golden) throws Exception { + runRecurrenceIteratorTest(rruleText, dtStart, limit, golden, null, null, UTC); + } + + private void runRecurrenceIteratorTest(String rrule, String dtstartStr, int limit, + String golden, String advanceTo, String tz) throws Exception { + runRecurrenceIteratorTest(rrule, dtstartStr, limit, golden, advanceTo, null, tz); + } + + /** + * Tests a recurrence rule + * @param rrule The rule to expand + * @param dtstartStr The dtstart to use + * @param limit Maximum number of entries to expand. if there are more, "..." is appended to + * the result. Note that Android's recurrence expansion doesn't support expanding n results, + * so this is faked by expanding until the endAt date, and then taking limit results. + * @param golden The desired results + * @param advanceTo The starting date for expansion. dtstartStr is used if null is passed in. + * @param endAt The ending date. DEFAULT_END is used if null is passed in. + * @param tz The time zone. UTC is used if null is passed in. + * @throws Exception if anything goes wrong. + */ + private void runRecurrenceIteratorTest(String rrule, String dtstartStr, int limit, + String golden, String advanceTo, String endAt, String tz) throws Exception { + + String rdate = ""; + String exrule = ""; + String exdate = ""; + rrule = rrule.replace("RRULE:", ""); + // RecurrenceSet does not support folding of lines, so fold here + rrule = rrule.replace("\n ", ""); + + Time dtstart = new Time(tz); + Time rangeStart = new Time(tz); + Time rangeEnd = new Time(tz); + Time outCal = new Time(tz); + + dtstart.parse(dtstartStr); + if (advanceTo == null) { + advanceTo = dtstartStr; + } + if (endAt == null) { + endAt = DEFAULT_END; + } + + rangeStart.parse(advanceTo); + rangeEnd.parse(endAt); + + + RecurrenceProcessor rp = new RecurrenceProcessor(); + RecurrenceSet recur = new RecurrenceSet(rrule, rdate, exrule, exdate); + + long[] out = rp.expand(dtstart, recur, rangeStart.toMillis(false /* use isDst */), + rangeEnd.toMillis(false /* use isDst */)); + + if (METHOD_TRACE) { + Debug.stopMethodTracing(); + } + + boolean truncate = dtstartStr.length() <= 8; // Just date, not date-time + String[] actual = getFormattedDates(out, outCal, truncate); + + StringBuilder sb = new StringBuilder(); + int k = 0; + while (k < actual.length && --limit >= 0) { + if (k != 0) { + sb.append(','); + } + sb.append(actual[k]); + k++; + } + if (limit < 0) { + sb.append(",..."); + } + assertEquals(golden, sb.toString()); + } + + // Infinite loop, bug 1662110 + @MediumTest + public void testFrequencyLimits() throws Exception { + try { + runRecurrenceIteratorTest("RRULE:FREQ=SECONDLY;BYSECOND=0,1,2,3,4,5,6,7,8,9,10,11,12,13,14," + "15,16,17,18,19,20,21,22,23,24,25,26,27,28,29," + "30,31,32,33,34,35,36,37,38,39,40,41,42,43,44," + "45,46,47,48,49,50,51,52,53,54,55,56,57,58,59", "20000101", 1, ""); + fail("Don't do that"); + } catch (DateException ex) { + // pass + } + } + + @MediumTest + public void testSimpleDaily() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=DAILY", "20060120", 5, "20060120,20060121,20060122,20060123,20060124,..."); + } + + @MediumTest + public void testSimpleWeekly() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY", "20060120", 5, "20060120,20060127,20060203,20060210,20060217,..."); + } + + @MediumTest + public void testSimpleMonthly() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY", "20060120", 5, "20060120,20060220,20060320,20060420,20060520,..."); + } + + @MediumTest + public void testSimpleYearly() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY", "20060120", 5, "20060120,20070120,20080120,20090120,20100120,...", null, "20120101", UTC); + } + + // from section 4.3.10 + @MediumTest + public void testMultipleByParts() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=2;BYMONTH=1;BYDAY=SU", "19970105", 8, "19970105,19970112,19970119,19970126," + "19990103,19990110,19990117,19990124,..."); + } + + @MediumTest + public void testCountWithInterval() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=DAILY;COUNT=10;INTERVAL=2", "19970105", 11, "19970105,19970107,19970109,19970111,19970113," + "19970115,19970117,19970119,19970121,19970123"); + } + + // from section 4.6.5 + // Fails: wrong dates + @MediumTest + @Suppress + public void testNegativeOffsetsA() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10", "19970105", 5, "19971026,19981025,19991031,20001029,20011028,..."); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testNegativeOffsetsB() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=4", "19970105", 5, "19970406,19980405,19990404,20000402,20010401,..."); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testNegativeOffsetsC() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=4;UNTIL=19980404T150000Z", "19970105", 5, "19970406"); + } + + // feom section 4.8.5.4 + @MediumTest + public void testDailyFor10Occ() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=DAILY;COUNT=10", "19970902T090000", 11, "19970902T090000,19970903T090000,19970904T090000,19970905T090000," + "19970906T090000,19970907T090000,19970908T090000,19970909T090000," + "19970910T090000,19970911T090000"); + + } + + @MediumTest + public void testDailyUntilDec4() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=DAILY;UNTIL=19971204", "19971128", 11, "19971128,19971129,19971130,19971201,19971202,19971203,19971204"); + } + + // Fails: infinite loop + @MediumTest + @Suppress + public void testEveryOtherDayForever() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=DAILY;INTERVAL=2", "19971128", 5, "19971128,19971130,19971202,19971204,19971206,..."); + } + + @MediumTest + public void testEvery10Days5Occ() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=DAILY;INTERVAL=10;COUNT=5", "19970902", 5, "19970902,19970912,19970922,19971002,19971012"); + } + + @MediumTest + public void testWeeklyFor10Occ() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;COUNT=10", "19970902", 10, "19970902,19970909,19970916,19970923,19970930," + "19971007,19971014,19971021,19971028,19971104"); + } + + @MediumTest + public void testWeeklyUntilDec24() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;UNTIL=19971224", "19970902", 25, "19970902,19970909,19970916,19970923,19970930," + "19971007,19971014,19971021,19971028,19971104," + "19971111,19971118,19971125,19971202,19971209," + "19971216,19971223"); + } + + @MediumTest + public void testEveryOtherWeekForever() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;INTERVAL=2;WKST=SU", "19970902", 11, "19970902,19970916,19970930,19971014,19971028," + "19971111,19971125,19971209,19971223,19980106," + "19980120,..."); + } + + @MediumTest + public void testWeeklyOnTuesdayAndThursdayFor5Weeks() throws Exception { + // if UNTIL date does not match start date, then until date treated as + // occurring on midnight. + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;UNTIL=19971007;WKST=SU;BYDAY=TU,TH", "19970902T090000", 11, "19970902T090000,19970904T090000,19970909T090000,19970911T090000," + "19970916T090000,19970918T090000,19970923T090000,19970925T090000," + "19970930T090000,19971002T090000"); + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;UNTIL=19971007T000000Z;WKST=SU;BYDAY=TU,TH", "19970902T090000", 11, "19970902T090000,19970904T090000,19970909T090000,19970911T090000," + "19970916T090000,19970918T090000,19970923T090000,19970925T090000," + "19970930T090000,19971002T090000"); + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;COUNT=10;WKST=SU;BYDAY=TU,TH", "19970902", 11, "19970902,19970904,19970909,19970911,19970916," + "19970918,19970923,19970925,19970930,19971002"); + } + + @MediumTest + public void testEveryOtherWeekOnMWFUntilDec24() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;INTERVAL=2;UNTIL=19971224T000000Z;WKST=SU;\n" + " BYDAY=MO,WE,FR", "19970903T090000", 25, "19970903T090000,19970905T090000,19970915T090000,19970917T090000," + "19970919T090000,19970929T090000,19971001T090000,19971003T090000," + "19971013T090000,19971015T090000,19971017T090000,19971027T090000," + "19971029T090000,19971031T090000,19971110T090000,19971112T090000," + "19971114T090000,19971124T090000,19971126T090000,19971128T090000," + "19971208T090000,19971210T090000,19971212T090000,19971222T090000"); + } + + @MediumTest + public void testEveryOtherWeekOnMWFUntilDec24a() throws Exception { + // if the UNTIL date is timed, when the start is not, the time should be + // ignored, so we get one more instance + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;INTERVAL=2;UNTIL=19971224T000000Z;WKST=SU;\n" + " BYDAY=MO,WE,FR", "19970903", 25, "19970903,19970905,19970915,19970917," + "19970919,19970929,19971001,19971003," + "19971013,19971015,19971017,19971027," + "19971029,19971031,19971110,19971112," + "19971114,19971124,19971126,19971128," + "19971208,19971210,19971212,19971222," + "19971224"); + } + + // Fails with wrong times + @MediumTest + @Suppress + public void testEveryOtherWeekOnMWFUntilDec24b() throws Exception { + // test with an alternate timezone + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;INTERVAL=2;UNTIL=19971224T090000Z;WKST=SU;\n" + " BYDAY=MO,WE,FR", "19970903T090000", 25, "19970903T160000,19970905T160000,19970915T160000,19970917T160000," + "19970919T160000,19970929T160000,19971001T160000,19971003T160000," + "19971013T160000,19971015T160000,19971017T160000,19971027T170000," + "19971029T170000,19971031T170000,19971110T170000,19971112T170000," + "19971114T170000,19971124T170000,19971126T170000,19971128T170000," + "19971208T170000,19971210T170000,19971212T170000,19971222T170000", null, PST); + } + + @MediumTest + public void testEveryOtherWeekOnTuThFor8Occ() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;INTERVAL=2;COUNT=8;WKST=SU;BYDAY=TU,TH", "19970902", 8, "19970902,19970904,19970916,19970918,19970930," + "19971002,19971014,19971016"); + } + + @MediumTest + public void testMonthlyOnThe1stFridayFor10Occ() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;COUNT=10;BYDAY=1FR", "19970905", 10, "19970905,19971003,19971107,19971205,19980102," + "19980206,19980306,19980403,19980501,19980605"); + } + + @MediumTest + public void testMonthlyOnThe1stFridayUntilDec24() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;UNTIL=19971224T000000Z;BYDAY=1FR", "19970905", 4, "19970905,19971003,19971107,19971205"); + } + + @MediumTest + public void testEveryOtherMonthOnThe1stAndLastSundayFor10Occ() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;INTERVAL=2;COUNT=10;BYDAY=1SU,-1SU", "19970907", 10, "19970907,19970928,19971102,19971130,19980104," + "19980125,19980301,19980329,19980503,19980531"); + } + + @MediumTest + public void testMonthlyOnTheSecondToLastMondayOfTheMonthFor6Months() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;COUNT=6;BYDAY=-2MO", "19970922", 6, "19970922,19971020,19971117,19971222,19980119," + "19980216"); + } + + @MediumTest + public void testMonthlyOnTheThirdToLastDay() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;BYMONTHDAY=-3", "19970928", 6, "19970928,19971029,19971128,19971229,19980129,19980226,..."); + } + + @MediumTest + public void testMonthlyOnThe2ndAnd15thFor10Occ() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;COUNT=10;BYMONTHDAY=2,15", "19970902", 10, "19970902,19970915,19971002,19971015,19971102," + "19971115,19971202,19971215,19980102,19980115"); + } + + @MediumTest + public void testMonthlyOnTheFirstAndLastFor10Occ() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;COUNT=10;BYMONTHDAY=1,-1", "19970930", 10, "19970930,19971001,19971031,19971101,19971130," + "19971201,19971231,19980101,19980131,19980201"); + } + + @MediumTest + public void testEvery18MonthsOnThe10thThru15thFor10Occ() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;INTERVAL=18;COUNT=10;BYMONTHDAY=10,11,12,13,14,\n" + " 15", "19970910", 10, "19970910,19970911,19970912,19970913,19970914," + "19970915,19990310,19990311,19990312,19990313"); + } + + @MediumTest + public void testEveryTuesdayEveryOtherMonth() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;INTERVAL=2;BYDAY=TU", "19970902", 18, "19970902,19970909,19970916,19970923,19970930," + "19971104,19971111,19971118,19971125,19980106," + "19980113,19980120,19980127,19980303,19980310," + "19980317,19980324,19980331,..."); + } + + @MediumTest + public void testYearlyInJuneAndJulyFor10Occurrences() throws Exception { + // Note: Since none of the BYDAY, BYMONTHDAY or BYYEARDAY components + // are specified, the day is gotten from DTSTART + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;COUNT=10;BYMONTH=6,7", "19970610", 10, "19970610,19970710,19980610,19980710,19990610," + "19990710,20000610,20000710,20010610,20010710"); + } + + @MediumTest + public void testEveryOtherYearOnJanuaryFebruaryAndMarchFor10Occurrences() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=2;COUNT=10;BYMONTH=1,2,3", "19970310", 10, "19970310,19990110,19990210,19990310,20010110," + "20010210,20010310,20030110,20030210,20030310"); + } + + //Fails: wrong dates + @MediumTest + @Suppress + public void testEvery3rdYearOnThe1st100thAnd200thDayFor10Occurrences() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=3;COUNT=10;BYYEARDAY=1,100,200", "19970101", 10, "19970101,19970410,19970719,20000101,20000409," + "20000718,20030101,20030410,20030719,20060101"); + } + + // Fails: infinite loop + @MediumTest + @Suppress + public void testEvery20thMondayOfTheYearForever() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYDAY=20MO", "19970519", 3, "19970519,19980518,19990517,..."); + } + + // Fails: generates wrong dates + @MediumTest + @Suppress + public void testMondayOfWeekNumber20WhereTheDefaultStartOfTheWeekIsMonday() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYWEEKNO=20;BYDAY=MO", "19970512", 3, "19970512,19980511,19990517,..."); + } + + @MediumTest + public void testEveryThursdayInMarchForever() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=TH", "19970313", 11, "19970313,19970320,19970327,19980305,19980312," + "19980319,19980326,19990304,19990311,19990318," + "19990325,..."); + } + + //Fails: wrong dates + @MediumTest + @Suppress + public void testEveryThursdayButOnlyDuringJuneJulyAndAugustForever() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYDAY=TH;BYMONTH=6,7,8", "19970605", 39, "19970605,19970612,19970619,19970626,19970703," + "19970710,19970717,19970724,19970731,19970807," + "19970814,19970821,19970828,19980604,19980611," + "19980618,19980625,19980702,19980709,19980716," + "19980723,19980730,19980806,19980813,19980820," + "19980827,19990603,19990610,19990617,19990624," + "19990701,19990708,19990715,19990722,19990729," + "19990805,19990812,19990819,19990826,..."); + } + + //Fails: infinite loop + @MediumTest + @Suppress + public void testEveryFridayThe13thForever() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;BYDAY=FR;BYMONTHDAY=13", "19970902", 5, "19980213,19980313,19981113,19990813,20001013," + "..."); + } + + @MediumTest + public void testTheFirstSaturdayThatFollowsTheFirstSundayOfTheMonthForever() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;BYDAY=SA;BYMONTHDAY=7,8,9,10,11,12,13", "19970913", 10, "19970913,19971011,19971108,19971213,19980110," + "19980207,19980307,19980411,19980509,19980613," + "..."); + } + + @MediumTest + public void testEvery4YearsThe1stTuesAfterAMonInNovForever() throws Exception { + // US Presidential Election Day + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=4;BYMONTH=11;BYDAY=TU;BYMONTHDAY=2,3,4,\n" + " 5,6,7,8", "19961105", 3, "19961105,20001107,20041102,..."); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testThe3rdInstanceIntoTheMonthOfOneOfTuesWedThursForNext3Months() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;COUNT=3;BYDAY=TU,WE,TH;BYSETPOS=3", "19970904", 3, "19970904,19971007,19971106"); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testThe2ndToLastWeekdayOfTheMonth() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-2", "19970929", 7, "19970929,19971030,19971127,19971230,19980129," + "19980226,19980330,..."); + } + + // Fails: infinite loop + @MediumTest + @Suppress + public void testEvery3HoursFrom900AmTo500PmOnASpecificDay() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=HOURLY;INTERVAL=3;UNTIL=19970903T010000Z", "19970902", 7, "00000902,19970909,19970900,19970912,19970900," + "19970915,19970900"); + } + + // Fails: infinite loop + @MediumTest + @Suppress + public void testEvery15MinutesFor6Occurrences() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MINUTELY;INTERVAL=15;COUNT=6", "19970902", 13, "00000902,19970909,19970900,19970909,19970915," + "19970909,19970930,19970909,19970945,19970910," + "19970900,19970910,19970915"); + } + + @MediumTest + @Suppress + public void testEveryHourAndAHalfFor4Occurrences() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MINUTELY;INTERVAL=90;COUNT=4", "19970902", 9, "00000902,19970909,19970900,19970910,19970930," + "19970912,19970900,19970913,19970930"); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testAnExampleWhereTheDaysGeneratedMakesADifferenceBecauseOfWkst() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;INTERVAL=2;COUNT=4;BYDAY=TU,SU;WKST=MO", "19970805", 4, "19970805,19970810,19970819,19970824"); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testAnExampleWhereTheDaysGeneratedMakesADifferenceBecauseOfWkst2() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;INTERVAL=2;COUNT=4;BYDAY=TU,SU;WKST=SU", "19970805", 8, "19970805,19970817,19970819,19970831"); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testWithByDayAndByMonthDayFilter() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;COUNT=4;BYDAY=TU,SU;" + "BYMONTHDAY=13,14,15,16,17,18,19,20", "19970805", 8, "19970817,19970819,19970914,19970916"); + } + + // Failed: wrong dates + @MediumTest + @Suppress + public void testAnnuallyInAugustOnTuesAndSunBetween13thAnd20th() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;COUNT=4;BYDAY=TU,SU;" + "BYMONTHDAY=13,14,15,16,17,18,19,20;BYMONTH=8", "19970605", 8, "19970817,19970819,19980816,19980818"); + } + + // Failed: wrong dates + @MediumTest + @Suppress + public void testLastDayOfTheYearIsASundayOrTuesday() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;COUNT=4;BYDAY=TU,SU;BYYEARDAY=-1", "19940605", 8, "19951231,19961231,20001231,20021231"); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testLastWeekdayOfMonth() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;BYSETPOS=-1;BYDAY=-1MO,-1TU,-1WE,-1TH,-1FR", "19940605", 8, "19940630,19940729,19940831,19940930," + "19941031,19941130,19941230,19950131,..."); + } + + // Fails: generates wrong dates + @MediumTest + @Suppress + public void testMonthsThatStartOrEndOnFriday() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;BYMONTHDAY=1,-1;BYDAY=FR;COUNT=6", "19940605", 8, "19940701,19940930,19950331,19950630,19950901,19951201"); + } + + // Fails: can't go that far into future + @MediumTest + @Suppress + public void testCenturiesThatAreNotLeapYears() throws Exception { + // I can't think of a good reason anyone would want to specify both a + // month day and a year day, so here's a really contrived example + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=100;BYYEARDAY=60;BYMONTHDAY=1", "19000101", 4, "19000301,21000301,22000301,23000301,...", null, "25000101", UTC); + } + + // Fails: generates instances when it shouldn't + @MediumTest + @Suppress + public void testNoInstancesGenerated() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=DAILY;UNTIL=19990101", "20000101", 4, ""); + } + + // Fails: generates instances when it shouldn't + @MediumTest + @Suppress + public void testNoInstancesGenerated2() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYMONTH=2;BYMONTHDAY=30", "20000101", 4, ""); + } + + // Fails: generates instances when it shouldn't + @MediumTest + @Suppress + public void testNoInstancesGenerated3() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=4;BYYEARDAY=366", "20000101", 4, ""); + } + + //Fails: wrong dates + @MediumTest + @Suppress + public void testLastWeekdayOfMarch() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;BYMONTH=3;BYDAY=SA,SU;BYSETPOS=-1", "20000101", 4, "20000326,20010331,20020331,20030330,..."); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testFirstWeekdayOfMarch() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;BYMONTH=3;BYDAY=SA,SU;BYSETPOS=1", "20000101", 4, "20000304,20010303,20020302,20030301,..."); + } + + // January 1999 + // Mo Tu We Th Fr Sa Su + // 1 2 3 // < 4 days, so not a week + // 4 5 6 7 8 9 10 + + // January 2000 + // Mo Tu We Th Fr Sa Su + // 1 2 // < 4 days, so not a week + // 3 4 5 6 7 8 9 + + // January 2001 + // Mo Tu We Th Fr Sa Su + // 1 2 3 4 5 6 7 + // 8 9 10 11 12 13 14 + + // January 2002 + // Mo Tu We Th Fr Sa Su + // 1 2 3 4 5 6 + // 7 8 9 10 11 12 13 + + /** + * Find the first weekday of the first week of the year. + * The first week of the year may be partial, and the first week is considered + * to be the first one with at least four days. + */ + // Fails: wrong dates + @MediumTest + @Suppress + public void testFirstWeekdayOfFirstWeekOfYear() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYWEEKNO=1;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=1", "19990101", 4, "19990104,20000103,20010101,20020101,..."); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testFirstSundayOfTheYear1() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYWEEKNO=1;BYDAY=SU", "19990101", 4, "19990110,20000109,20010107,20020106,..."); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testFirstSundayOfTheYear2() throws Exception { + // TODO(msamuel): is this right? + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYDAY=1SU", "19990101", 4, "19990103,20000102,20010107,20020106,..."); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testFirstSundayOfTheYear3() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYDAY=SU;BYYEARDAY=1,2,3,4,5,6,7,8,9,10,11,12,13" + ";BYSETPOS=1", "19990101", 4, "19990103,20000102,20010107,20020106,..."); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testFirstWeekdayOfYear() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=1", "19990101", 4, "19990101,20000103,20010101,20020101,..."); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testLastWeekdayOfFirstWeekOfYear() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYWEEKNO=1;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1", "19990101", 4, "19990108,20000107,20010105,20020104,..."); + } + + // January 1999 + // Mo Tu We Th Fr Sa Su + // 1 2 3 + // 4 5 6 7 8 9 10 + // 11 12 13 14 15 16 17 + // 18 19 20 21 22 23 24 + // 25 26 27 28 29 30 31 + + // Fails: wrong dates + @MediumTest + @Suppress + public void testSecondWeekday1() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=2", "19990101", 4, "19990105,19990112,19990119,19990126,..."); + } + + // January 1997 + // Mo Tu We Th Fr Sa Su + // 1 2 3 4 5 + // 6 7 8 9 10 11 12 + // 13 14 15 16 17 18 19 + // 20 21 22 23 24 25 26 + // 27 28 29 30 31 + + // Fails: wrong dates + @MediumTest + @Suppress + public void testSecondWeekday2() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=2", "19970101", 4, "19970102,19970107,19970114,19970121,..."); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testByYearDayAndByDayFilterInteraction() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYYEARDAY=15;BYDAY=3MO", "19990101", 4, "20010115,20070115,20180115,20240115,..."); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testByDayWithNegWeekNoAsFilter() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;BYMONTHDAY=26;BYDAY=-1FR", "19990101", 4, "19990226,19990326,19991126,20000526,..."); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testLastWeekOfTheYear() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYWEEKNO=-1", "19990101", 6, "19991227,19991228,19991229,19991230,19991231,20001225,..."); + } + + // Fails: not enough dates generated + @MediumTest + @Suppress + public void testUserSubmittedTest1() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;INTERVAL=2;WKST=WE;BYDAY=SU,TU,TH,SA" + ";UNTIL=20000215T113000Z", "20000127T033000", 20, "20000127T033000,20000129T033000,20000130T033000,20000201T033000," + "20000210T033000,20000212T033000,20000213T033000,20000215T033000"); + } + + @MediumTest + public void testAdvanceTo1() throws Exception { + // a bunch of tests grabbed from above with an advance-to date tacked on + + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=TH", "19970313", 11, + /*"19970313,19970320,19970327,"*/"19980305,19980312," + "19980319,19980326,19990304,19990311,19990318," + "19990325,20000302,20000309,20000316,...", "19970601", UTC); + } + + // Fails: infinite loop + @MediumTest + @Suppress + public void testAdvanceTo2() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYDAY=20MO", "19970519", 3, + /*"19970519,"*/"19980518,19990517,20000515,...", "19980515", UTC); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testAdvanceTo3() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=3;UNTIL=20090101;BYYEARDAY=1,100,200", "19970101", 10, + /*"19970101,19970410,19970719,20000101,"*/"20000409," + "20000718,20030101,20030410,20030719,20060101,20060410,20060719," + "20090101", "20000228", UTC); + } + + //Fails: wrong dates + @MediumTest + @Suppress + public void testAdvanceTo4() throws Exception { + // make sure that count preserved + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=3;COUNT=10;BYYEARDAY=1,100,200", "19970101", 10, + /*"19970101,19970410,19970719,20000101,"*/"20000409," + "20000718,20030101,20030410,20030719,20060101", "20000228", UTC); + } + + // Fails: too many dates + @MediumTest + @Suppress + public void testAdvanceTo5() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=2;COUNT=10;BYMONTH=1,2,3", "19970310", 10, + /*"19970310,"*/"19990110,19990210,19990310,20010110," + "20010210,20010310,20030110,20030210,20030310", "19980401", UTC); + } + + @MediumTest + public void testAdvanceTo6() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;UNTIL=19971224", "19970902", 25, + /*"19970902,19970909,19970916,19970923,"*/"19970930," + "19971007,19971014,19971021,19971028,19971104," + "19971111,19971118,19971125,19971202,19971209," + "19971216,19971223", "19970930", UTC); + } + + @MediumTest + public void testAdvanceTo7() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;INTERVAL=18;BYMONTHDAY=10,11,12,13,14,\n" + " 15", "19970910", 5, + /*"19970910,19970911,19970912,19970913,19970914," + + "19970915,"*/"19990310,19990311,19990312,19990313,19990314,...", "19990101", UTC); + } + + @MediumTest + public void testAdvanceTo8() throws Exception { + // advancing into the past + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;INTERVAL=18;BYMONTHDAY=10,11,12,13,14,\n" + " 15", "19970910", 11, "19970910,19970911,19970912,19970913,19970914," + "19970915,19990310,19990311,19990312,19990313,19990314,...", "19970901", UTC); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testAdvanceTo9() throws Exception { + // skips first instance + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=100;BYMONTH=2;BYMONTHDAY=29", "19000101", 5, + // would return 2000 + "24000229,28000229,32000229,36000229,40000229,...", "20040101", UTC); + } + + // Infinite loop in native code (bug 1686327) + @MediumTest + @Suppress + public void testAdvanceTo10() throws Exception { + // filter hits until date before first instnace + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=100;BYMONTH=2;BYMONTHDAY=29;UNTIL=21000101", "19000101", 5, "", "20040101", UTC); + } + + // Fails: generates wrong dates + @MediumTest + @Suppress + public void testAdvanceTo11() throws Exception { + // advancing something that returns no instances + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYMONTH=2;BYMONTHDAY=30", "20000101", 10, "", "19970901", UTC); + } + + // Fails: generates wrong dates + @MediumTest + @Suppress + public void testAdvanceTo12() throws Exception { + // advancing something that returns no instances and has a BYSETPOS rule + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYMONTH=2;BYMONTHDAY=30,31;BYSETPOS=1", "20000101", 10, "", "19970901", UTC); + } + + // Fails: generates wrong dates + @MediumTest + @Suppress + public void testAdvanceTo13() throws Exception { + // advancing way past year generator timeout + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYMONTH=2;BYMONTHDAY=28", "20000101", 10, "", "25000101", UTC); + } + + /** + * a testcase that yielded dupes due to bysetPos evilness + */ + @MediumTest + @Suppress + public void testCaseThatYieldedDupes() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;WKST=SU;INTERVAL=1;BYMONTH=9,1,12,8" + ";BYMONTHDAY=-9,-29,24;BYSETPOS=-1,-4,10,-6,-1,-10,-10,-9,-8", "20060528", 200, "20060924,20061203,20061224,20070902,20071223,20080803,20080824," + "20090823,20100103,20100124,20110123,20120902,20121223,20130922," + "20140803,20140824,20150823,20160103,20160124,20170924,20171203," + "20171224,20180902,20181223,20190922,20200823,20210103,20210124," + "20220123,20230924,20231203,20231224,20240922,20250803,20250824," + "20260823,20270103,20270124,20280123,20280924,20281203,20281224," + "20290902,20291223,20300922,20310803,20310824,20330123,20340924," + "20341203,20341224,20350902,20351223,20360803,20360824,20370823," + "20380103,20380124,20390123,20400902,20401223,20410922,20420803," + "20420824,20430823,20440103,20440124,20450924,20451203,20451224," + "20460902,20461223,20470922,20480823,20490103,20490124,20500123," + "20510924,20511203,20511224,20520922,20530803,20530824,20540823," + "20550103,20550124,20560123,20560924,20561203,20561224,20570902," + "20571223,20580922,20590803,20590824,20610123,20620924,20621203," + "20621224,20630902,20631223,20640803,20640824,20650823,20660103," + "20660124,20670123,20680902,20681223,20690922,20700803,20700824," + "20710823,20720103,20720124,20730924,20731203,20731224,20740902," + "20741223,20750922,20760823,20770103,20770124,20780123,20790924," + "20791203,20791224,20800922,20810803,20810824,20820823,20830103," + "20830124,20840123,20840924,20841203,20841224,20850902,20851223," + "20860922,20870803,20870824,20890123,20900924,20901203,20901224," + "20910902,20911223,20920803,20920824,20930823,20940103,20940124," + "20950123,20960902,20961223,20970922,20980803,20980824,20990823," + "21000103,21000124,21010123,21020924,21021203,21021224,21030902," + "21031223,21040803,21040824,21050823,21060103,21060124,21070123," + "21080902,21081223,21090922,21100803,21100824,21110823,21120103," + "21120124,21130924,21131203,21131224,21140902,21141223,21150922," + "21160823,21170103,21170124,21180123,21190924,21191203,21191224," + "21200922,21210803,21210824,21220823,..."); + } + + @MediumTest + public void testEveryThursdayinMarchEachYear() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=TH", "20100304", 9, "20100304,20100311,20100318,20100325,20110303,20110310,20110317,20110324,20110331", null, "20111231", UTC); + } +} diff --git a/tests/src/com/android/calendarcommon2/RecurrenceProcessorTest.java b/tests/src/com/android/calendarcommon2/RecurrenceProcessorTest.java new file mode 100644 index 0000000..af850f8 --- /dev/null +++ b/tests/src/com/android/calendarcommon2/RecurrenceProcessorTest.java @@ -0,0 +1,2537 @@ +/* //device/content/providers/pim/RecurrenceProcessorTest.java +** +** Copyright 2006, The Android Open Source Project +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +*/ + +package com.android.calendarcommon2; + +import android.os.Debug; +import android.test.suitebuilder.annotation.LargeTest; +import android.test.suitebuilder.annotation.MediumTest; +import android.test.suitebuilder.annotation.SmallTest; +import android.text.TextUtils; +import android.text.format.Time; +import android.util.Log; +import android.util.TimeFormatException; +import junit.framework.TestCase; + +import java.util.TreeSet; + +public class RecurrenceProcessorTest extends TestCase { + private static final String TAG = "RecurrenceProcessorTest"; + private static final boolean SPEW = true; + private static final boolean METHOD_TRACE = false; + + private static String[] getFormattedDates(long[] dates, Time time) { + String[] out = new String[dates.length]; + int i = 0; + for (long date : dates) { + time.set(date); + out[i] = time.format2445(); + ++i; + } + return out; + } + + private static void printLists(String[] expected, String[] out) { + Log.i(TAG, " expected out"); + int i; + for (i = 0; i < expected.length && i < out.length; i++) { + Log.i(TAG, " [" + i + "] " + expected[i] + + " " + out[i]); + } + for (; i < expected.length; i++) { + Log.i(TAG, " [" + i + "] " + expected[i]); + } + for (; i < out.length; i++) { + Log.i(TAG, " [" + i + "] " + out[i]); + } + } + + public void verifyRecurrence(String dtstartStr, String rrule, String rdate, String exrule, + String exdate, String rangeStartStr, String rangeEndStr, String[] expected) + throws Exception { + verifyRecurrence(dtstartStr, rrule, rdate, exrule, exdate, rangeStartStr, + rangeEndStr, expected, expected[expected.length - 1]); + } + + public void verifyRecurrence(String dtstartStr, String rrule, String rdate, String exrule, + String exdate, String rangeStartStr, String rangeEndStr, String[] expected, + String last) throws Exception { + + // note that the zulu of all parameters here must be the same, expand + // doesn't work otherwise + + if (SPEW) { + Log.i(TAG, "DTSTART:" + dtstartStr + + " RRULE:" + rrule + + " RDATE:" + rdate + + " EXRULE:" + exrule + + " EXDATE:" + exdate); + } + + // we could use any timezone, incl. UTC, but we use a non-UTC + // timezone to make sure there are no UTC assumptions in the + // recurrence processing code. + String tz = "America/Los_Angeles"; + Time dtstart = new Time(tz); + Time rangeStart = new Time(tz); + Time rangeEnd = new Time(tz); + Time outCal = new Time(tz); + + dtstart.parse(dtstartStr); + + rangeStart.parse(rangeStartStr); + rangeEnd.parse(rangeEndStr); + + if (METHOD_TRACE) { + String fn = "/tmp/trace/" + this.getClass().getName().replace('$', '_'); + String df = fn + ".data"; + String kf = fn + ".key"; + Debug.startMethodTracing(fn, 8 * 1024 * 1024); + } + + RecurrenceProcessor rp = new RecurrenceProcessor(); + RecurrenceSet recur = new RecurrenceSet(rrule, rdate, exrule, exdate); + + long[] out = rp.expand(dtstart, recur, rangeStart.toMillis(false /* use isDst */), + rangeEnd.toMillis(false /* use isDst */)); + + if (METHOD_TRACE) { + Debug.stopMethodTracing(); + } + + int count = out.length; + String[] actual = getFormattedDates(out, outCal); + + if (count != expected.length) { + if (SPEW) { + Log.i(TAG, "DTSTART:" + dtstartStr + " RRULE:" + rrule); + printLists(expected, actual); + } + throw new RuntimeException("result lengths don't match. " + + " expected=" + expected.length + + " actual=" + count); + } + + for (int i = 0; i < count; i++) { + String s = actual[i]; + if (!s.equals(expected[i])) { + if (SPEW) { + Log.i(TAG, "DTSTART:" + dtstartStr + " RRULE:" + rrule); + printLists(expected, actual); + Log.i(TAG, "i=" + i); + } + throw new RuntimeException("expected[" + i + "]=" + + expected[i] + " actual=" + actual[i]); + } + } + + long lastOccur = rp.getLastOccurence(dtstart, rangeEnd, recur); + if (lastOccur == 0 && out.length == 0) { + // No occurrence found and 0 returned for lastOccur, this is ok. + return; + } + long lastMillis = -1; + long expectedMillis = -1; + String lastStr = ""; + if (lastOccur != -1) { + outCal.set(lastOccur); + lastStr = outCal.format2445(); + lastMillis = outCal.toMillis(true /* ignore isDst */); + } + if (last != null && last.length() > 0) { + Time expectedLast = new Time(tz); + expectedLast.parse(last); + expectedMillis = expectedLast.toMillis(true /* ignore isDst */); + } + if (lastMillis != expectedMillis) { + if (SPEW) { + Log.i(TAG, "DTSTART:" + dtstartStr + " RRULE:" + rrule); + Log.i(TAG, "Expected: " + last + "; Actual: " + lastStr); + printLists(expected, actual); + } + + throw new RuntimeException("expected last occurrence date does not match." + + " expected=" + last + + " actual=" + lastStr); + } + } + + @SmallTest + public void testMonthly0() throws Exception { + verifyRecurrence("20060205T100000", "FREQ=MONTHLY;COUNT=3", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060205T100000", + "20060305T100000", + "20060405T100000" + }); + } + + @SmallTest + public void testMonthly1() throws Exception { + verifyRecurrence("20060205T100000", "FREQ=MONTHLY;INTERVAL=2;COUNT=3", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060205T100000", + "20060405T100000", + "20060605T100000" + }); + } + + @SmallTest + public void testMonthly2() throws Exception { + // this tests wrapping the year when the interval isn't divisible + // by 12 + verifyRecurrence("20060205T100000", "FREQ=MONTHLY;INTERVAL=5;COUNT=5", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060205T100000", + "20060705T100000", + "20061205T100000", + "20070505T100000", + "20071005T100000" + }); + } + + @SmallTest + public void testMonthly3() throws Exception { + // with a simple BYDAY, spanning two months + verifyRecurrence("20060104T123456", + "FREQ=MONTHLY;UNTIL=20060201T200000Z;BYDAY=TU,WE", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060104T123456", + "20060110T123456", + "20060111T123456", + "20060117T123456", + "20060118T123456", + "20060124T123456", + "20060125T123456", + "20060131T123456" + }, + "20060201T120000"); + } + + @SmallTest + public void testMonthly4() throws Exception { + // with a BYDAY with +1 / etc., spanning two months and + // one day which isn't in the result + verifyRecurrence("20060101T123456", + "FREQ=MONTHLY;UNTIL=20060301T200000Z;BYDAY=+1SU,+2MO,+3TU,+4WE,+5MO,+5TU,+5WE,+6TH", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060101T123456", + "20060109T123456", + "20060117T123456", + "20060125T123456", + "20060130T123456", + "20060131T123456", + "20060205T123456", + "20060213T123456", + "20060221T123456", + "20060222T123456" + }, + "20060301T120000"); + } + + @SmallTest + public void testMonthly5() throws Exception { + // with a BYDAY with -1 / etc. + verifyRecurrence("20060201T123456", + "FREQ=MONTHLY;UNTIL=20060301T200000Z;BYDAY=-1SU,-2MO,-3TU,-4TU,-4WE,-5MO,-5TU,-5WE,-6TH", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060201T123456", + "20060207T123456", + "20060214T123456", + "20060220T123456", + "20060226T123456" + }, + "20060301T120000"); + } + + @SmallTest + public void testMonthly6() throws Exception { + // With positive BYMONTHDAYs + verifyRecurrence("20060201T123456", + "FREQ=MONTHLY;UNTIL=20060301T200000Z;BYMONTHDAY=1,2,5,28,31", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060201T123456", + "20060202T123456", + "20060205T123456", + "20060228T123456" + }, + "20060301T120000"); + } + + @SmallTest + public void testMonthly7() throws Exception { + // With negative BYMONTHDAYs + verifyRecurrence("20060201T123456", + "FREQ=MONTHLY;UNTIL=20060301T200000Z;BYMONTHDAY=-1,-5,-27,-28,-31", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060201T123456", + "20060202T123456", + "20060224T123456", + "20060228T123456" + }, + "20060301T120000"); + } + + @SmallTest + public void testMonthly8() throws Exception { + verifyRecurrence("20060205T100000", "FREQ=MONTHLY;COUNT=3", + "America/Los_Angeles;20060207T140000,20060307T160000,20060407T180000", + null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060205T100000", + "20060207T140000", + "20060305T100000", + "20060307T160000", + "20060405T100000", + "20060407T180000", + }); + } + + @SmallTest + public void testMonthly9() throws Exception { + verifyRecurrence("20060205T100000", null /* rrule */, + "America/Los_Angeles;20060207T140000,20060307T160000,20060407T180000", + null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060207T140000", + "20060307T160000", + "20060407T180000", + }); + } + + @SmallTest + public void testMonthly10() throws Exception { + verifyRecurrence("20060205T100000", "FREQ=MONTHLY;COUNT=3\nFREQ=WEEKLY;COUNT=2", + "America/Los_Angeles;20060207T140000,20060307T160000,20060407T180000", + null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060205T100000", + "20060207T140000", + "20060212T100000", + "20060305T100000", + "20060307T160000", + "20060405T100000", + "20060407T180000", + }); + } + + @SmallTest + public void testMonthly11() throws Exception { + verifyRecurrence("20060205T100000", "FREQ=MONTHLY;COUNT=3\nFREQ=WEEKLY;COUNT=2", + null /* rdate */, + "FREQ=MONTHLY;COUNT=2", null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060212T100000", + "20060405T100000", + }); + } + + @SmallTest + public void testMonthly12() throws Exception { + verifyRecurrence("20060205T100000", "FREQ=MONTHLY;COUNT=3\nFREQ=WEEKLY;COUNT=2", + null /* rdate */, null /* exrule */, + "20060212T180000Z,20060405T170000Z" /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060205T100000", + "20060305T100000", + }); + } + + @SmallTest + public void testMonthly13() throws Exception { + verifyRecurrence("20060101T100000", "FREQ=MONTHLY;BYDAY=FR;BYMONTHDAY=13;COUNT=10", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20101231T000000", + new String[]{ + "20060101T100000", + "20060113T100000", + "20061013T100000", + "20070413T100000", + "20070713T100000", + "20080613T100000", + "20090213T100000", + "20090313T100000", + "20091113T100000", + "20100813T100000", + }); + } + + @SmallTest + public void testMonthly14() throws Exception { + verifyRecurrence("20110103T100000", "FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=1,-1", + null /* rdate */, null /* exrule */, null /* exdate */, + "20110101T000000", "20110331T235959", + new String[]{ + "20110103T100000", + "20110131T100000", + "20110201T100000", + "20110228T100000", + "20110301T100000", + "20110331T100000", + }); + } + + @SmallTest + public void testMonthly15() throws Exception { + verifyRecurrence("20110703T100000", "FREQ=MONTHLY;BYDAY=SA,SU;BYSETPOS=2,-2", + null /* rdate */, null /* exrule */, null /* exdate */, + "20110701T000000", "20110931T235959", + new String[]{ + "20110703T100000", + "20110730T100000", + "20110807T100000", + "20110827T100000", + "20110904T100000", + "20110924T100000", + }); + } + + @SmallTest + public void testWeekly0() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=WEEKLY;COUNT=3", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060222T100000", + "20060301T100000" + }); + } + + @SmallTest + public void testWeekly1() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=20060301T100000Z", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060222T100000" + }, "20060301T020000"); + } + + @SmallTest + public void testWeekly2() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=20060301T100001Z", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060222T100000" + }, "20060301T020001"); + } + + @SmallTest + public void testWeekly3() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=20060301T090000Z", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060222T100000" + }, "20060301T010000"); + } + + @SmallTest + public void testWeekly4() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=20060311T100001Z;BYDAY=TU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060221T100000", + "20060228T100000", + "20060307T100000" + }, "20060311T020001"); + } + + // until without "Z" + @SmallTest + public void testWeekly4a() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=20060311T100001;BYDAY=TU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060221T100000", + "20060228T100000", + "20060307T100000" + }, "20060311T100001"); + } + + @SmallTest + public void testWeekly5() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=20060311T100001Z;BYDAY=TH", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060216T100000", + "20060223T100000", + "20060302T100000", + "20060309T100000" + }, "20060311T020001"); + } + + @SmallTest + public void testWeekly6() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=20060309T100001Z;BYDAY=WE,TH", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060216T100000", + "20060222T100000", + "20060223T100000", + "20060301T100000", + "20060302T100000", + "20060308T100000" + }, "20060309T020001"); + } + + @SmallTest + public void testWeekly7() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=20060220T100001Z;BYDAY=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060219T100000" + }, "20060220T020001"); + } + + @SmallTest + public void testWeekly8() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=WEEKLY;COUNT=4;WKST=SU;BYDAY=TU,TH", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060216T100000", + "20060221T100000", + "20060223T100000" + }); + } + + @SmallTest + public void testWeekly9() throws Exception { + verifyRecurrence("19970805T100000", + "FREQ=WEEKLY;INTERVAL=2;COUNT=4;BYDAY=TU,SU", // uses default WKST=MO + null /* rdate */, null /* exrule */, null /* exdate */, + "19970101T000000", "19980101T000000", + new String[]{ + "19970805T100000", + "19970810T100000", + "19970819T100000", + "19970824T100000", + }); + } + + @SmallTest + public void testWeekly10() throws Exception { + verifyRecurrence("19970805T100000", + "FREQ=WEEKLY;INTERVAL=2;COUNT=4;BYDAY=TU,SU;WKST=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "19970101T000000", "19980101T000000", + new String[]{ + "19970805T100000", + "19970817T100000", + "19970819T100000", + "19970831T100000", + }); + } + + // BUG 1658567: UNTIL=date + @SmallTest + public void testWeekly11() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=20060220;BYDAY=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060219T100000" + }, "20060220T000000"); + } + + @SmallTest + public void testWeekly12() throws Exception { + try { + verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=junk;BYDAY=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060219T100000" + }, "20060220T020001"); + fail("Bad UNTIL string failed to throw exception"); + } catch (TimeFormatException e) { + // expected + } + } + + /** + * Test repeating weekly event with dtstart and dtend (only one occurrence) + * See bug #3267616 + * @throws Exception + */ + @SmallTest + public void testWeekly13() throws Exception { + verifyRecurrence("20101117T150000", + "FREQ=WEEKLY;BYDAY=WE", + null /* rdate */, null /* exrule */, null /* exdate */, + "20101117T150000", "20101117T160000", + new String[]{ "20101117T150000" }); + } + + @SmallTest + public void testWeekly14() throws Exception { + verifyRecurrence("19970805T100000", + "FREQ=WEEKLY;INTERVAL=2;COUNT=4;BYDAY=TU,SU;WKST=TH", + null /* rdate */, null /* exrule */, null /* exdate */, + "19970101T000000", "19980101T000000", + new String[]{ + "19970805T100000", + "19970817T100000", + "19970819T100000", + "19970831T100000", + }); + } + + @SmallTest + public void testDaily0() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=DAILY;COUNT=3", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060216T100000", + "20060217T100000" + }); + } + + @SmallTest + public void testDaily1() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=DAILY;UNTIL=20060302T100001Z", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060216T100000", + "20060217T100000", + "20060218T100000", + "20060219T100000", + "20060220T100000", + "20060221T100000", + "20060222T100000", + "20060223T100000", + "20060224T100000", + "20060225T100000", + "20060226T100000", + "20060227T100000", + "20060228T100000", + "20060301T100000" + }, "20060302T100001Z"); + } + + @SmallTest + public void testDaily2() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=DAILY;UNTIL=20060220T100001Z;BYDAY=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060219T100000" + }, "20060220T020001"); + } + + @SmallTest + public void testDaily3() throws Exception { + verifyRecurrence("20060219T100000", + "FREQ=DAILY;UNTIL=20060225T180304Z;BYDAY=SU,MO,SA;BYHOUR=5,10,22;BYMINUTE=3,59;BYSECOND=2,5", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060219T100000", + "20060219T100302", + "20060219T100305", + "20060219T105902", + "20060219T105905", + "20060219T220302", + "20060219T220305", + "20060219T225902", + "20060219T225905", + "20060220T050302", + "20060220T050305", + "20060220T055902", + "20060220T055905", + "20060220T100302", + "20060220T100305", + "20060220T105902", + "20060220T105905", + "20060220T220302", + "20060220T220305", + "20060220T225902", + "20060220T225905", + "20060225T050302", + "20060225T050305", + "20060225T055902", + "20060225T055905", + "20060225T100302" + }, "20060225T100304"); + } + + @MediumTest + public void testFromGoogleCalendar0() throws Exception { + // Tuesday, Thursday (10/2) + verifyRecurrence("20061002T050000", + "FREQ=WEEKLY;UNTIL=20071031T200000Z;INTERVAL=1;BYDAY=TU,TH;WKST=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090101T000000", + new String[]{ + "20061002T050000", + "20061003T050000", + "20061005T050000", + "20061010T050000", + "20061012T050000", + "20061017T050000", + "20061019T050000", + "20061024T050000", + "20061026T050000", + "20061031T050000", + "20061102T050000", + "20061107T050000", + "20061109T050000", + "20061114T050000", + "20061116T050000", + "20061121T050000", + "20061123T050000", + "20061128T050000", + "20061130T050000", + "20061205T050000", + "20061207T050000", + "20061212T050000", + "20061214T050000", + "20061219T050000", + "20061221T050000", + "20061226T050000", + "20061228T050000", + "20070102T050000", + "20070104T050000", + "20070109T050000", + "20070111T050000", + "20070116T050000", + "20070118T050000", + "20070123T050000", + "20070125T050000", + "20070130T050000", + "20070201T050000", + "20070206T050000", + "20070208T050000", + "20070213T050000", + "20070215T050000", + "20070220T050000", + "20070222T050000", + "20070227T050000", + "20070301T050000", + "20070306T050000", + "20070308T050000", + "20070313T050000", + "20070315T050000", + "20070320T050000", + "20070322T050000", + "20070327T050000", + "20070329T050000", + "20070403T050000", + "20070405T050000", + "20070410T050000", + "20070412T050000", + "20070417T050000", + "20070419T050000", + "20070424T050000", + "20070426T050000", + "20070501T050000", + "20070503T050000", + "20070508T050000", + "20070510T050000", + "20070515T050000", + "20070517T050000", + "20070522T050000", + "20070524T050000", + "20070529T050000", + "20070531T050000", + "20070605T050000", + "20070607T050000", + "20070612T050000", + "20070614T050000", + "20070619T050000", + "20070621T050000", + "20070626T050000", + "20070628T050000", + "20070703T050000", + "20070705T050000", + "20070710T050000", + "20070712T050000", + "20070717T050000", + "20070719T050000", + "20070724T050000", + "20070726T050000", + "20070731T050000", + "20070802T050000", + "20070807T050000", + "20070809T050000", + "20070814T050000", + "20070816T050000", + "20070821T050000", + "20070823T050000", + "20070828T050000", + "20070830T050000", + "20070904T050000", + "20070906T050000", + "20070911T050000", + "20070913T050000", + "20070918T050000", + "20070920T050000", + "20070925T050000", + "20070927T050000", + "20071002T050000", + "20071004T050000", + "20071009T050000", + "20071011T050000", + "20071016T050000", + "20071018T050000", + "20071023T050000", + "20071025T050000", + "20071030T050000", + }, "20071031T130000"); + } + + @MediumTest + public void testFromGoogleCalendar1() throws Exception { + // Mon Wed Fri + verifyRecurrence("20061002T030000", + "FREQ=WEEKLY;UNTIL=20071025T180000Z;INTERVAL=1;BYDAY=MO,WE,FR;", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090101T000000", + new String[]{ + "20061002T030000", + "20061004T030000", + "20061006T030000", + "20061009T030000", + "20061011T030000", + "20061013T030000", + "20061016T030000", + "20061018T030000", + "20061020T030000", + "20061023T030000", + "20061025T030000", + "20061027T030000", + "20061030T030000", + "20061101T030000", + "20061103T030000", + "20061106T030000", + "20061108T030000", + "20061110T030000", + "20061113T030000", + "20061115T030000", + "20061117T030000", + "20061120T030000", + "20061122T030000", + "20061124T030000", + "20061127T030000", + "20061129T030000", + "20061201T030000", + "20061204T030000", + "20061206T030000", + "20061208T030000", + "20061211T030000", + "20061213T030000", + "20061215T030000", + "20061218T030000", + "20061220T030000", + "20061222T030000", + "20061225T030000", + "20061227T030000", + "20061229T030000", + "20070101T030000", + "20070103T030000", + "20070105T030000", + "20070108T030000", + "20070110T030000", + "20070112T030000", + "20070115T030000", + "20070117T030000", + "20070119T030000", + "20070122T030000", + "20070124T030000", + "20070126T030000", + "20070129T030000", + "20070131T030000", + "20070202T030000", + "20070205T030000", + "20070207T030000", + "20070209T030000", + "20070212T030000", + "20070214T030000", + "20070216T030000", + "20070219T030000", + "20070221T030000", + "20070223T030000", + "20070226T030000", + "20070228T030000", + "20070302T030000", + "20070305T030000", + "20070307T030000", + "20070309T030000", + "20070312T030000", + "20070314T030000", + "20070316T030000", + "20070319T030000", + "20070321T030000", + "20070323T030000", + "20070326T030000", + "20070328T030000", + "20070330T030000", + "20070402T030000", + "20070404T030000", + "20070406T030000", + "20070409T030000", + "20070411T030000", + "20070413T030000", + "20070416T030000", + "20070418T030000", + "20070420T030000", + "20070423T030000", + "20070425T030000", + "20070427T030000", + "20070430T030000", + "20070502T030000", + "20070504T030000", + "20070507T030000", + "20070509T030000", + "20070511T030000", + "20070514T030000", + "20070516T030000", + "20070518T030000", + "20070521T030000", + "20070523T030000", + "20070525T030000", + "20070528T030000", + "20070530T030000", + "20070601T030000", + "20070604T030000", + "20070606T030000", + "20070608T030000", + "20070611T030000", + "20070613T030000", + "20070615T030000", + "20070618T030000", + "20070620T030000", + "20070622T030000", + "20070625T030000", + "20070627T030000", + "20070629T030000", + "20070702T030000", + "20070704T030000", + "20070706T030000", + "20070709T030000", + "20070711T030000", + "20070713T030000", + "20070716T030000", + "20070718T030000", + "20070720T030000", + "20070723T030000", + "20070725T030000", + "20070727T030000", + "20070730T030000", + "20070801T030000", + "20070803T030000", + "20070806T030000", + "20070808T030000", + "20070810T030000", + "20070813T030000", + "20070815T030000", + "20070817T030000", + "20070820T030000", + "20070822T030000", + "20070824T030000", + "20070827T030000", + "20070829T030000", + "20070831T030000", + "20070903T030000", + "20070905T030000", + "20070907T030000", + "20070910T030000", + "20070912T030000", + "20070914T030000", + "20070917T030000", + "20070919T030000", + "20070921T030000", + "20070924T030000", + "20070926T030000", + "20070928T030000", + "20071001T030000", + "20071003T030000", + "20071005T030000", + "20071008T030000", + "20071010T030000", + "20071012T030000", + "20071015T030000", + "20071017T030000", + "20071019T030000", + "20071022T030000", + "20071024T030000", + }, "20071025T110000"); + } + + @SmallTest + public void testFromGoogleCalendar2() throws Exception { + // Monthly on day 2 + verifyRecurrence("20061002T070000", + "FREQ=MONTHLY;INTERVAL=1;WKST=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090101T000000", + new String[]{ + "20061002T070000", + "20061102T070000", + "20061202T070000", + "20070102T070000", + "20070202T070000", + "20070302T070000", + "20070402T070000", + "20070502T070000", + "20070602T070000", + "20070702T070000", + "20070802T070000", + "20070902T070000", + "20071002T070000", + "20071102T070000", + "20071202T070000", + "20080102T070000", + "20080202T070000", + "20080302T070000", + "20080402T070000", + "20080502T070000", + "20080602T070000", + "20080702T070000", + "20080802T070000", + "20080902T070000", + "20081002T070000", + "20081102T070000", + "20081202T070000", + }, "20081202T070000"); + } + + @MediumTest + public void testFromGoogleCalendar3() throws Exception { + // Every Weekday + verifyRecurrence("20061002T100000", + "FREQ=WEEKLY;UNTIL=20070215T100000Z;INTERVAL=1;BYDAY=MO,TU,WE,TH,FR;", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090101T000000", + new String[]{ + "20061002T100000", + "20061003T100000", + "20061004T100000", + "20061005T100000", + "20061006T100000", + "20061009T100000", + "20061010T100000", + "20061011T100000", + "20061012T100000", + "20061013T100000", + "20061016T100000", + "20061017T100000", + "20061018T100000", + "20061019T100000", + "20061020T100000", + "20061023T100000", + "20061024T100000", + "20061025T100000", + "20061026T100000", + "20061027T100000", + "20061030T100000", + "20061031T100000", + "20061101T100000", + "20061102T100000", + "20061103T100000", + "20061106T100000", + "20061107T100000", + "20061108T100000", + "20061109T100000", + "20061110T100000", + "20061113T100000", + "20061114T100000", + "20061115T100000", + "20061116T100000", + "20061117T100000", + "20061120T100000", + "20061121T100000", + "20061122T100000", + "20061123T100000", + "20061124T100000", + "20061127T100000", + "20061128T100000", + "20061129T100000", + "20061130T100000", + "20061201T100000", + "20061204T100000", + "20061205T100000", + "20061206T100000", + "20061207T100000", + "20061208T100000", + "20061211T100000", + "20061212T100000", + "20061213T100000", + "20061214T100000", + "20061215T100000", + "20061218T100000", + "20061219T100000", + "20061220T100000", + "20061221T100000", + "20061222T100000", + "20061225T100000", + "20061226T100000", + "20061227T100000", + "20061228T100000", + "20061229T100000", + "20070101T100000", + "20070102T100000", + "20070103T100000", + "20070104T100000", + "20070105T100000", + "20070108T100000", + "20070109T100000", + "20070110T100000", + "20070111T100000", + "20070112T100000", + "20070115T100000", + "20070116T100000", + "20070117T100000", + "20070118T100000", + "20070119T100000", + "20070122T100000", + "20070123T100000", + "20070124T100000", + "20070125T100000", + "20070126T100000", + "20070129T100000", + "20070130T100000", + "20070131T100000", + "20070201T100000", + "20070202T100000", + "20070205T100000", + "20070206T100000", + "20070207T100000", + "20070208T100000", + "20070209T100000", + "20070212T100000", + "20070213T100000", + "20070214T100000", + }, "20070215T020000"); + } + + @SmallTest + public void testFromGoogleCalendar4() throws Exception { + // Every 5 months on day 2 + verifyRecurrence("20061003T100000", + "FREQ=MONTHLY;INTERVAL=5;WKST=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090101T000000", + new String[]{ + "20061003T100000", + "20070303T100000", + "20070803T100000", + "20080103T100000", + "20080603T100000", + "20081103T100000", + }, "20081103T100000"); + } + + @MediumTest + public void testFromGoogleCalendar5() throws Exception { + // Tuesday, Thursday (10/3) + verifyRecurrence("20061003T040000", + "FREQ=WEEKLY;INTERVAL=1;BYDAY=TU,TH;WKST=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090101T000000", + new String[]{ + "20061003T040000", + "20061005T040000", + "20061010T040000", + "20061012T040000", + "20061017T040000", + "20061019T040000", + "20061024T040000", + "20061026T040000", + "20061031T040000", + "20061102T040000", + "20061107T040000", + "20061109T040000", + "20061114T040000", + "20061116T040000", + "20061121T040000", + "20061123T040000", + "20061128T040000", + "20061130T040000", + "20061205T040000", + "20061207T040000", + "20061212T040000", + "20061214T040000", + "20061219T040000", + "20061221T040000", + "20061226T040000", + "20061228T040000", + "20070102T040000", + "20070104T040000", + "20070109T040000", + "20070111T040000", + "20070116T040000", + "20070118T040000", + "20070123T040000", + "20070125T040000", + "20070130T040000", + "20070201T040000", + "20070206T040000", + "20070208T040000", + "20070213T040000", + "20070215T040000", + "20070220T040000", + "20070222T040000", + "20070227T040000", + "20070301T040000", + "20070306T040000", + "20070308T040000", + "20070313T040000", + "20070315T040000", + "20070320T040000", + "20070322T040000", + "20070327T040000", + "20070329T040000", + "20070403T040000", + "20070405T040000", + "20070410T040000", + "20070412T040000", + "20070417T040000", + "20070419T040000", + "20070424T040000", + "20070426T040000", + "20070501T040000", + "20070503T040000", + "20070508T040000", + "20070510T040000", + "20070515T040000", + "20070517T040000", + "20070522T040000", + "20070524T040000", + "20070529T040000", + "20070531T040000", + "20070605T040000", + "20070607T040000", + "20070612T040000", + "20070614T040000", + "20070619T040000", + "20070621T040000", + "20070626T040000", + "20070628T040000", + "20070703T040000", + "20070705T040000", + "20070710T040000", + "20070712T040000", + "20070717T040000", + "20070719T040000", + "20070724T040000", + "20070726T040000", + "20070731T040000", + "20070802T040000", + "20070807T040000", + "20070809T040000", + "20070814T040000", + "20070816T040000", + "20070821T040000", + "20070823T040000", + "20070828T040000", + "20070830T040000", + "20070904T040000", + "20070906T040000", + "20070911T040000", + "20070913T040000", + "20070918T040000", + "20070920T040000", + "20070925T040000", + "20070927T040000", + "20071002T040000", + "20071004T040000", + "20071009T040000", + "20071011T040000", + "20071016T040000", + "20071018T040000", + "20071023T040000", + "20071025T040000", + "20071030T040000", + "20071101T040000", + "20071106T040000", + "20071108T040000", + "20071113T040000", + "20071115T040000", + "20071120T040000", + "20071122T040000", + "20071127T040000", + "20071129T040000", + "20071204T040000", + "20071206T040000", + "20071211T040000", + "20071213T040000", + "20071218T040000", + "20071220T040000", + "20071225T040000", + "20071227T040000", + "20080101T040000", + "20080103T040000", + "20080108T040000", + "20080110T040000", + "20080115T040000", + "20080117T040000", + "20080122T040000", + "20080124T040000", + "20080129T040000", + "20080131T040000", + "20080205T040000", + "20080207T040000", + "20080212T040000", + "20080214T040000", + "20080219T040000", + "20080221T040000", + "20080226T040000", + "20080228T040000", + "20080304T040000", + "20080306T040000", + "20080311T040000", + "20080313T040000", + "20080318T040000", + "20080320T040000", + "20080325T040000", + "20080327T040000", + "20080401T040000", + "20080403T040000", + "20080408T040000", + "20080410T040000", + "20080415T040000", + "20080417T040000", + "20080422T040000", + "20080424T040000", + "20080429T040000", + "20080501T040000", + "20080506T040000", + "20080508T040000", + "20080513T040000", + "20080515T040000", + "20080520T040000", + "20080522T040000", + "20080527T040000", + "20080529T040000", + "20080603T040000", + "20080605T040000", + "20080610T040000", + "20080612T040000", + "20080617T040000", + "20080619T040000", + "20080624T040000", + "20080626T040000", + "20080701T040000", + "20080703T040000", + "20080708T040000", + "20080710T040000", + "20080715T040000", + "20080717T040000", + "20080722T040000", + "20080724T040000", + "20080729T040000", + "20080731T040000", + "20080805T040000", + "20080807T040000", + "20080812T040000", + "20080814T040000", + "20080819T040000", + "20080821T040000", + "20080826T040000", + "20080828T040000", + "20080902T040000", + "20080904T040000", + "20080909T040000", + "20080911T040000", + "20080916T040000", + "20080918T040000", + "20080923T040000", + "20080925T040000", + "20080930T040000", + "20081002T040000", + "20081007T040000", + "20081009T040000", + "20081014T040000", + "20081016T040000", + "20081021T040000", + "20081023T040000", + "20081028T040000", + "20081030T040000", + "20081104T040000", + "20081106T040000", + "20081111T040000", + "20081113T040000", + "20081118T040000", + "20081120T040000", + "20081125T040000", + "20081127T040000", + "20081202T040000", + "20081204T040000", + "20081209T040000", + "20081211T040000", + "20081216T040000", + "20081218T040000", + "20081223T040000", + "20081225T040000", + "20081230T040000", + }, "20081230T040000"); + } + + @MediumTest + public void testFromGoogleCalendar6() throws Exception { + // Weekly on all days + verifyRecurrence("20061003T060000", + "FREQ=WEEKLY;INTERVAL=1;BYDAY=SU,MO,TU,WE,TH,FR,SA;WKST=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20071003T060000", + new String[]{ + "20061003T060000", + "20061004T060000", + "20061005T060000", + "20061006T060000", + "20061007T060000", + "20061008T060000", + "20061009T060000", + "20061010T060000", + "20061011T060000", + "20061012T060000", + "20061013T060000", + "20061014T060000", + "20061015T060000", + "20061016T060000", + "20061017T060000", + "20061018T060000", + "20061019T060000", + "20061020T060000", + "20061021T060000", + "20061022T060000", + "20061023T060000", + "20061024T060000", + "20061025T060000", + "20061026T060000", + "20061027T060000", + "20061028T060000", + "20061029T060000", + "20061030T060000", + "20061031T060000", + "20061101T060000", + "20061102T060000", + "20061103T060000", + "20061104T060000", + "20061105T060000", + "20061106T060000", + "20061107T060000", + "20061108T060000", + "20061109T060000", + "20061110T060000", + "20061111T060000", + "20061112T060000", + "20061113T060000", + "20061114T060000", + "20061115T060000", + "20061116T060000", + "20061117T060000", + "20061118T060000", + "20061119T060000", + "20061120T060000", + "20061121T060000", + "20061122T060000", + "20061123T060000", + "20061124T060000", + "20061125T060000", + "20061126T060000", + "20061127T060000", + "20061128T060000", + "20061129T060000", + "20061130T060000", + "20061201T060000", + "20061202T060000", + "20061203T060000", + "20061204T060000", + "20061205T060000", + "20061206T060000", + "20061207T060000", + "20061208T060000", + "20061209T060000", + "20061210T060000", + "20061211T060000", + "20061212T060000", + "20061213T060000", + "20061214T060000", + "20061215T060000", + "20061216T060000", + "20061217T060000", + "20061218T060000", + "20061219T060000", + "20061220T060000", + "20061221T060000", + "20061222T060000", + "20061223T060000", + "20061224T060000", + "20061225T060000", + "20061226T060000", + "20061227T060000", + "20061228T060000", + "20061229T060000", + "20061230T060000", + "20061231T060000", + "20070101T060000", + "20070102T060000", + "20070103T060000", + "20070104T060000", + "20070105T060000", + "20070106T060000", + "20070107T060000", + "20070108T060000", + "20070109T060000", + "20070110T060000", + "20070111T060000", + "20070112T060000", + "20070113T060000", + "20070114T060000", + "20070115T060000", + "20070116T060000", + "20070117T060000", + "20070118T060000", + "20070119T060000", + "20070120T060000", + "20070121T060000", + "20070122T060000", + "20070123T060000", + "20070124T060000", + "20070125T060000", + "20070126T060000", + "20070127T060000", + "20070128T060000", + "20070129T060000", + "20070130T060000", + "20070131T060000", + "20070201T060000", + "20070202T060000", + "20070203T060000", + "20070204T060000", + "20070205T060000", + "20070206T060000", + "20070207T060000", + "20070208T060000", + "20070209T060000", + "20070210T060000", + "20070211T060000", + "20070212T060000", + "20070213T060000", + "20070214T060000", + "20070215T060000", + "20070216T060000", + "20070217T060000", + "20070218T060000", + "20070219T060000", + "20070220T060000", + "20070221T060000", + "20070222T060000", + "20070223T060000", + "20070224T060000", + "20070225T060000", + "20070226T060000", + "20070227T060000", + "20070228T060000", + "20070301T060000", + "20070302T060000", + "20070303T060000", + "20070304T060000", + "20070305T060000", + "20070306T060000", + "20070307T060000", + "20070308T060000", + "20070309T060000", + "20070310T060000", + "20070311T060000", + "20070312T060000", + "20070313T060000", + "20070314T060000", + "20070315T060000", + "20070316T060000", + "20070317T060000", + "20070318T060000", + "20070319T060000", + "20070320T060000", + "20070321T060000", + "20070322T060000", + "20070323T060000", + "20070324T060000", + "20070325T060000", + "20070326T060000", + "20070327T060000", + "20070328T060000", + "20070329T060000", + "20070330T060000", + "20070331T060000", + "20070401T060000", + "20070402T060000", + "20070403T060000", + "20070404T060000", + "20070405T060000", + "20070406T060000", + "20070407T060000", + "20070408T060000", + "20070409T060000", + "20070410T060000", + "20070411T060000", + "20070412T060000", + "20070413T060000", + "20070414T060000", + "20070415T060000", + "20070416T060000", + "20070417T060000", + "20070418T060000", + "20070419T060000", + "20070420T060000", + "20070421T060000", + "20070422T060000", + "20070423T060000", + "20070424T060000", + "20070425T060000", + "20070426T060000", + "20070427T060000", + "20070428T060000", + "20070429T060000", + "20070430T060000", + "20070501T060000", + "20070502T060000", + "20070503T060000", + "20070504T060000", + "20070505T060000", + "20070506T060000", + "20070507T060000", + "20070508T060000", + "20070509T060000", + "20070510T060000", + "20070511T060000", + "20070512T060000", + "20070513T060000", + "20070514T060000", + "20070515T060000", + "20070516T060000", + "20070517T060000", + "20070518T060000", + "20070519T060000", + "20070520T060000", + "20070521T060000", + "20070522T060000", + "20070523T060000", + "20070524T060000", + "20070525T060000", + "20070526T060000", + "20070527T060000", + "20070528T060000", + "20070529T060000", + "20070530T060000", + "20070531T060000", + "20070601T060000", + "20070602T060000", + "20070603T060000", + "20070604T060000", + "20070605T060000", + "20070606T060000", + "20070607T060000", + "20070608T060000", + "20070609T060000", + "20070610T060000", + "20070611T060000", + "20070612T060000", + "20070613T060000", + "20070614T060000", + "20070615T060000", + "20070616T060000", + "20070617T060000", + "20070618T060000", + "20070619T060000", + "20070620T060000", + "20070621T060000", + "20070622T060000", + "20070623T060000", + "20070624T060000", + "20070625T060000", + "20070626T060000", + "20070627T060000", + "20070628T060000", + "20070629T060000", + "20070630T060000", + "20070701T060000", + "20070702T060000", + "20070703T060000", + "20070704T060000", + "20070705T060000", + "20070706T060000", + "20070707T060000", + "20070708T060000", + "20070709T060000", + "20070710T060000", + "20070711T060000", + "20070712T060000", + "20070713T060000", + "20070714T060000", + "20070715T060000", + "20070716T060000", + "20070717T060000", + "20070718T060000", + "20070719T060000", + "20070720T060000", + "20070721T060000", + "20070722T060000", + "20070723T060000", + "20070724T060000", + "20070725T060000", + "20070726T060000", + "20070727T060000", + "20070728T060000", + "20070729T060000", + "20070730T060000", + "20070731T060000", + "20070801T060000", + "20070802T060000", + "20070803T060000", + "20070804T060000", + "20070805T060000", + "20070806T060000", + "20070807T060000", + "20070808T060000", + "20070809T060000", + "20070810T060000", + "20070811T060000", + "20070812T060000", + "20070813T060000", + "20070814T060000", + "20070815T060000", + "20070816T060000", + "20070817T060000", + "20070818T060000", + "20070819T060000", + "20070820T060000", + "20070821T060000", + "20070822T060000", + "20070823T060000", + "20070824T060000", + "20070825T060000", + "20070826T060000", + "20070827T060000", + "20070828T060000", + "20070829T060000", + "20070830T060000", + "20070831T060000", + "20070901T060000", + "20070902T060000", + "20070903T060000", + "20070904T060000", + "20070905T060000", + "20070906T060000", + "20070907T060000", + "20070908T060000", + "20070909T060000", + "20070910T060000", + "20070911T060000", + "20070912T060000", + "20070913T060000", + "20070914T060000", + "20070915T060000", + "20070916T060000", + "20070917T060000", + "20070918T060000", + "20070919T060000", + "20070920T060000", + "20070921T060000", + "20070922T060000", + "20070923T060000", + "20070924T060000", + "20070925T060000", + "20070926T060000", + "20070927T060000", + "20070928T060000", + "20070929T060000", + "20070930T060000", + "20071001T060000", + "20071002T060000", + }, "20071002T060000"); + } + + @MediumTest + public void testFromGoogleCalendar7() throws Exception { + // Every 3 days + verifyRecurrence("20061003T080000", + "FREQ=DAILY;INTERVAL=3;WKST=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090101T000000", + new String[]{ + "20061003T080000", + "20061006T080000", + "20061009T080000", + "20061012T080000", + "20061015T080000", + "20061018T080000", + "20061021T080000", + "20061024T080000", + "20061027T080000", + "20061030T080000", + "20061102T080000", + "20061105T080000", + "20061108T080000", + "20061111T080000", + "20061114T080000", + "20061117T080000", + "20061120T080000", + "20061123T080000", + "20061126T080000", + "20061129T080000", + "20061202T080000", + "20061205T080000", + "20061208T080000", + "20061211T080000", + "20061214T080000", + "20061217T080000", + "20061220T080000", + "20061223T080000", + "20061226T080000", + "20061229T080000", + "20070101T080000", + "20070104T080000", + "20070107T080000", + "20070110T080000", + "20070113T080000", + "20070116T080000", + "20070119T080000", + "20070122T080000", + "20070125T080000", + "20070128T080000", + "20070131T080000", + "20070203T080000", + "20070206T080000", + "20070209T080000", + "20070212T080000", + "20070215T080000", + "20070218T080000", + "20070221T080000", + "20070224T080000", + "20070227T080000", + "20070302T080000", + "20070305T080000", + "20070308T080000", + "20070311T080000", + "20070314T080000", + "20070317T080000", + "20070320T080000", + "20070323T080000", + "20070326T080000", + "20070329T080000", + "20070401T080000", + "20070404T080000", + "20070407T080000", + "20070410T080000", + "20070413T080000", + "20070416T080000", + "20070419T080000", + "20070422T080000", + "20070425T080000", + "20070428T080000", + "20070501T080000", + "20070504T080000", + "20070507T080000", + "20070510T080000", + "20070513T080000", + "20070516T080000", + "20070519T080000", + "20070522T080000", + "20070525T080000", + "20070528T080000", + "20070531T080000", + "20070603T080000", + "20070606T080000", + "20070609T080000", + "20070612T080000", + "20070615T080000", + "20070618T080000", + "20070621T080000", + "20070624T080000", + "20070627T080000", + "20070630T080000", + "20070703T080000", + "20070706T080000", + "20070709T080000", + "20070712T080000", + "20070715T080000", + "20070718T080000", + "20070721T080000", + "20070724T080000", + "20070727T080000", + "20070730T080000", + "20070802T080000", + "20070805T080000", + "20070808T080000", + "20070811T080000", + "20070814T080000", + "20070817T080000", + "20070820T080000", + "20070823T080000", + "20070826T080000", + "20070829T080000", + "20070901T080000", + "20070904T080000", + "20070907T080000", + "20070910T080000", + "20070913T080000", + "20070916T080000", + "20070919T080000", + "20070922T080000", + "20070925T080000", + "20070928T080000", + "20071001T080000", + "20071004T080000", + "20071007T080000", + "20071010T080000", + "20071013T080000", + "20071016T080000", + "20071019T080000", + "20071022T080000", + "20071025T080000", + "20071028T080000", + "20071031T080000", + "20071103T080000", + "20071106T080000", + "20071109T080000", + "20071112T080000", + "20071115T080000", + "20071118T080000", + "20071121T080000", + "20071124T080000", + "20071127T080000", + "20071130T080000", + "20071203T080000", + "20071206T080000", + "20071209T080000", + "20071212T080000", + "20071215T080000", + "20071218T080000", + "20071221T080000", + "20071224T080000", + "20071227T080000", + "20071230T080000", + "20080102T080000", + "20080105T080000", + "20080108T080000", + "20080111T080000", + "20080114T080000", + "20080117T080000", + "20080120T080000", + "20080123T080000", + "20080126T080000", + "20080129T080000", + "20080201T080000", + "20080204T080000", + "20080207T080000", + "20080210T080000", + "20080213T080000", + "20080216T080000", + "20080219T080000", + "20080222T080000", + "20080225T080000", + "20080228T080000", + "20080302T080000", + "20080305T080000", + "20080308T080000", + "20080311T080000", + "20080314T080000", + "20080317T080000", + "20080320T080000", + "20080323T080000", + "20080326T080000", + "20080329T080000", + "20080401T080000", + "20080404T080000", + "20080407T080000", + "20080410T080000", + "20080413T080000", + "20080416T080000", + "20080419T080000", + "20080422T080000", + "20080425T080000", + "20080428T080000", + "20080501T080000", + "20080504T080000", + "20080507T080000", + "20080510T080000", + "20080513T080000", + "20080516T080000", + "20080519T080000", + "20080522T080000", + "20080525T080000", + "20080528T080000", + "20080531T080000", + "20080603T080000", + "20080606T080000", + "20080609T080000", + "20080612T080000", + "20080615T080000", + "20080618T080000", + "20080621T080000", + "20080624T080000", + "20080627T080000", + "20080630T080000", + "20080703T080000", + "20080706T080000", + "20080709T080000", + "20080712T080000", + "20080715T080000", + "20080718T080000", + "20080721T080000", + "20080724T080000", + "20080727T080000", + "20080730T080000", + "20080802T080000", + "20080805T080000", + "20080808T080000", + "20080811T080000", + "20080814T080000", + "20080817T080000", + "20080820T080000", + "20080823T080000", + "20080826T080000", + "20080829T080000", + "20080901T080000", + "20080904T080000", + "20080907T080000", + "20080910T080000", + "20080913T080000", + "20080916T080000", + "20080919T080000", + "20080922T080000", + "20080925T080000", + "20080928T080000", + "20081001T080000", + "20081004T080000", + "20081007T080000", + "20081010T080000", + "20081013T080000", + "20081016T080000", + "20081019T080000", + "20081022T080000", + "20081025T080000", + "20081028T080000", + "20081031T080000", + "20081103T080000", + "20081106T080000", + "20081109T080000", + "20081112T080000", + "20081115T080000", + "20081118T080000", + "20081121T080000", + "20081124T080000", + "20081127T080000", + "20081130T080000", + "20081203T080000", + "20081206T080000", + "20081209T080000", + "20081212T080000", + "20081215T080000", + "20081218T080000", + "20081221T080000", + "20081224T080000", + "20081227T080000", + "20081230T080000", + }, "20081230T080000"); + } + + @SmallTest + public void testFromGoogleCalendar8() throws Exception { + // Annually on October 4 + verifyRecurrence("20061004T130000", + "FREQ=YEARLY;INTERVAL=1;WKST=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090101T000000", + new String[]{ + "20061004T130000", + "20071004T130000", + "20081004T130000", + }, "20081004T130000"); + } + + @MediumTest + public void testFromGoogleCalendar9() throws Exception { + // Monthly on the last Monday + verifyRecurrence("20061030T170000", + "FREQ=MONTHLY;INTERVAL=1;BYDAY=-1MO;WKST=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090101T000000", + new String[]{ + "20061030T170000", + "20061127T170000", + "20061225T170000", + "20070129T170000", + "20070226T170000", + "20070326T170000", + "20070430T170000", + "20070528T170000", + "20070625T170000", + "20070730T170000", + "20070827T170000", + "20070924T170000", + "20071029T170000", + "20071126T170000", + "20071231T170000", + "20080128T170000", + "20080225T170000", + "20080331T170000", + "20080428T170000", + "20080526T170000", + "20080630T170000", + "20080728T170000", + "20080825T170000", + "20080929T170000", + "20081027T170000", + "20081124T170000", + "20081229T170000", + }, "20081229T170000"); + } + + @SmallTest + public void testFromGoogleCalendar10() throws Exception { + // Every 7 weeks on Tuesday, Wednesday + verifyRecurrence("20061004T090000", + "FREQ=WEEKLY;UNTIL=20070223T010000Z;INTERVAL=7;BYDAY=TU,WE;WKST=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090101T000000", + new String[]{ + "20061004T090000", + "20061121T090000", + "20061122T090000", + "20070109T090000", + "20070110T090000", + }, "20070222T170000"); + } + + @SmallTest + public void testFromGoogleCalendar11() throws Exception { + // Monthly on day 31 + verifyRecurrence("20061031T160000", + "FREQ=MONTHLY;INTERVAL=1;WKST=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090101T000000", + new String[]{ + "20061031T160000", + "20061231T160000", + "20070131T160000", + "20070331T160000", + "20070531T160000", + "20070731T160000", + "20070831T160000", + "20071031T160000", + "20071231T160000", + "20080131T160000", + "20080331T160000", + "20080531T160000", + "20080731T160000", + "20080831T160000", + "20081031T160000", + "20081231T160000", + }, + "20081231T160000"); + } + + @SmallTest + public void testFromGoogleCalendar12() throws Exception { + // Every 2 months on the first Tuesday + verifyRecurrence("20061004T110000", + "FREQ=MONTHLY;INTERVAL=2;BYDAY=1TU;WKST=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090101T000000", + new String[]{ + "20061004T110000", + "20061205T110000", + "20070206T110000", + "20070403T110000", + "20070605T110000", + "20070807T110000", + "20071002T110000", + "20071204T110000", + "20080205T110000", + "20080401T110000", + "20080603T110000", + "20080805T110000", + "20081007T110000", + "20081202T110000", + }, + "20081202T110000"); + } + + @SmallTest + public void testYearly0() throws Exception { + verifyRecurrence("20080101T100000", + "FREQ=YEARLY;UNTIL=20090131T090000Z;BYMONTH=1;BYDAY=SU,MO", + null /* rdate */, null /* exrule */, null /* exdate */, + "20080101T000000", "20090130T000000", + new String[]{ + "20080101T100000", + "20080106T100000", + "20080107T100000", + "20080113T100000", + "20080114T100000", + "20080120T100000", + "20080121T100000", + "20080127T100000", + "20080128T100000", + "20090104T100000", + "20090105T100000", + "20090111T100000", + "20090112T100000", + "20090118T100000", + "20090119T100000", + "20090125T100000", + "20090126T100000", + }, "20090131T010000"); + } + + /** + * This test fails because of a bug in RecurrenceProcessor.expand(). We + * don't have time to fix the bug yet but we don't want to lose track of + * this test either. The "failing" prefix on the method name prevents this + * test from being run. Remove the "failing" prefix when the bug is fixed. + * + * @throws Exception + */ + @SmallTest + public void failingTestYearly1() throws Exception { + verifyRecurrence("20060101T100000", + "FREQ=YEARLY;COUNT=10;BYYEARDAY=1,100,200", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090131T000000", + new String[]{ + "20060101T100000", + "20060409T100000", + "20060718T100000", + "20070101T100000", + "20070409T100000", + "20070718T100000", + "20080101T100000", + "20080410T100000", + "20080719T100000", + "20090101T100000", + }); + } + + /** + * This test fails because of a bug in RecurrenceProcessor.expand(). We + * don't have time to fix the bug yet but we don't want to lose track of + * this test either. The "failing" prefix on the method name prevents this + * test from being run. Remove the "failing" prefix when the bug is fixed. + * + * @throws Exception + */ + @SmallTest + public void failingTestYearly2() throws Exception { + verifyRecurrence("20060101T100000", + "FREQ=YEARLY;COUNT=5;BYWEEKNO=6;BYDAY=MO;WKST=MO", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090131T000000", + new String[]{ + "20060101T100000", + "20060206T100000", + "20070205T100000", + "20080204T100000", + "20090209T100000", + }); + } + + /** + * This test fails because of a bug in RecurrenceProcessor.expand(). We + * don't have time to fix the bug yet but we don't want to lose track of + * this test either. The "failing" prefix on the method name prevents this + * test from being run. Remove the "failing" prefix when the bug is fixed. + * + * @throws Exception + */ + @SmallTest + public void failingTestYearly3() throws Exception { + verifyRecurrence("20060101T100000", + "FREQ=YEARLY;BYMONTH=3;BYDAY=TH", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090131T000000", + new String[]{ + "20060101T100000", + "20060302T100000", + "20060309T100000", + "20060316T100000", + "20060323T100000", + "20060330T100000", + "20070301T100000", + "20070308T100000", + "20070315T100000", + "20070322T100000", + "20070329T100000", + "20080306T100000", + "20080313T100000", + "20080320T100000", + "20080327T100000", + }); + } + + /** + * This test fails because of a bug in RecurrenceProcessor.expand(). We + * don't have time to fix the bug yet but we don't want to lose track of + * this test either. The "failing" prefix on the method name prevents this + * test from being run. Remove the "failing" prefix when the bug is fixed. + * + * @throws Exception + */ + @SmallTest + public void failingTestYearly4() throws Exception { + verifyRecurrence("19920101T100000", + "FREQ=YEARLY;INTERVAL=4;BYMONTH=11;BYDAY=TU;BYMONTHDAY=2,3,4,5,6,7,8", + null /* rdate */, null /* exrule */, null /* exdate */, + "19920101T000000", "20121231T000000", + new String[]{ + "19920101T100000", + "19921103T100000", + "19961105T100000", + "20001107T100000", + "20041102T100000", + "20081104T100000", + "20121106T100000", + }); + } + + /** + * Test repeating event from Exchange with count field. + * Time range covers the whole repetition. + * + * @throws Exception + */ + public void testCount1() throws Exception { + verifyRecurrence("20100324T153000", + "FREQ=WEEKLY;INTERVAL=1;COUNT=10;BYDAY=WE", + null /* rdate */, null /* exrule */, null /* exdate */, + "20100301T000000", "20100630T000000", + new String[]{ + "20100324T153000", + "20100331T153000", + "20100407T153000", + "20100414T153000", + "20100421T153000", + "20100428T153000", + "20100505T153000", + "20100512T153000", + "20100519T153000", + "20100526T153000", + }); + } + + /** + * Test repeating event from Exchange with count field. + * Time range covers the first part of the repetition. + * @throws Exception + */ + public void testCount2() throws Exception { + verifyRecurrence("20100324T153000", + "FREQ=WEEKLY;INTERVAL=1;COUNT=10;BYDAY=WE", + null /* rdate */, null /* exrule */, null /* exdate */, + "20100501T000000", "20100630T000000", + new String[]{ + "20100505T153000", + "20100512T153000", + "20100519T153000", + "20100526T153000", + }); + } + + /** + * Test repeating event from Exchange with count field. + * Time range is beyond the repetition. + * @throws Exception + */ + public void testCount3() throws Exception { + verifyRecurrence("20100324T153000", + "FREQ=WEEKLY;INTERVAL=1;COUNT=10;BYDAY=WE", + null /* rdate */, null /* exrule */, null /* exdate */, + "20100601T000000", "20100630T000000", + new String[]{}, + "20100526T153000" /* last */); + } + + /** + * Test repeating event from Exchange with count field. + * Time range is before the repetition + * @throws Exception + */ + public void testCount4() throws Exception { + verifyRecurrence("20100324T153000", + "FREQ=WEEKLY;INTERVAL=1;COUNT=10;BYDAY=WE", + null /* rdate */, null /* exrule */, null /* exdate */, + "20100101T000000", "20100301T000000", + new String[]{}, + null /* last */); + } + + + // These recurrence rules are used in the loop that measures the performance + // of recurrence expansion. + private static final String[] performanceRrules = new String[] { + "FREQ=DAILY;COUNT=100", + "FREQ=DAILY;INTERVAL=2;UNTIL=20080101T000000Z", + "FREQ=YEARLY;UNTIL=20090131T090000Z;BYMONTH=1;BYDAY=SU,MO,TU,WE,TH,FR,SA", + "FREQ=WEEKLY;INTERVAL=2;WKST=SU", + "FREQ=WEEKLY;COUNT=100;WKST=SU;BYDAY=MO,TU,WE,TH,FR", + "FREQ=MONTHLY;COUNT=100;BYDAY=1FR", + "FREQ=MONTHLY;INTERVAL=2;COUNT=100;BYDAY=1SU,-1SU", + "FREQ=MONTHLY;BYMONTHDAY=1,15", + "FREQ=MONTHLY;INTERVAL=3;COUNT=100;BYMONTHDAY=10,11,12,13,14", + "FREQ=YEARLY;COUNT=100;BYMONTH=6,7,8", + "FREQ=YEARLY;INTERVAL=2;BYMONTH=1,2,3,6,7,8", + "FREQ=YEARLY;COUNT=100;BYYEARDAY=1,100,200", + "FREQ=YEARLY;BYDAY=2MO", + "FREQ=YEARLY;BYWEEKNO=2,3,4;BYDAY=MO", + "FREQ=YEARLY;BYMONTH=3,4,5;BYDAY=TH", + "FREQ=MONTHLY;BYDAY=FR;BYMONTHDAY=13", + "FREQ=MONTHLY;BYDAY=SA;BYMONTHDAY=7,8,9,10,11,12,13", + "FREQ=YEARLY;INTERVAL=2;BYMONTH=11;BYDAY=TU;BYMONTHDAY=2,3,4,5,6,7,8", + "FREQ=WEEKLY;INTERVAL=2;COUNT=100;BYDAY=TU,SU;WKST=MO", + "FREQ=WEEKLY;INTERVAL=2;COUNT=100;BYDAY=TU,SU;WKST=SU", + }; + + /** + * This test never fails. It just runs for a while (about 10 seconds) + * in order to measure the performance of recurrence expansion. + * + * @throws Exception + */ + @LargeTest + public void performanceTextExpand() throws Exception { + String tz = "America/Los_Angeles"; + Time dtstart = new Time(tz); + Time rangeStart = new Time(tz); + Time rangeEnd = new Time(tz); + TreeSet out = new TreeSet(); + + dtstart.parse("20010101T000000"); + rangeStart.parse("20010101T000000"); + rangeEnd.parse("20090101T000000"); + long rangeStartMillis = rangeStart.toMillis(false /* use isDst */); + long rangeEndMillis = rangeEnd.toMillis(false /* use isDst */); + + long startTime = System.currentTimeMillis(); + for (int iterations = 0; iterations < 5; iterations++) { + RecurrenceProcessor rp = new RecurrenceProcessor(); + + int len = performanceRrules.length; + for (int i = 0; i < len; i++) { + String rrule = performanceRrules[i]; + //Log.i(TAG, "expanding rule: " + rrule); + RecurrenceSet recur = new RecurrenceSet(rrule, null, null, null); + + long [] dates = rp.expand(dtstart, recur, rangeStartMillis, rangeEndMillis); + //Log.i(TAG, "num instances: " + out.size()); + + // Also include the time to iterate through the expanded values + for (long date : dates) { + // Use the date so that this loop is not optimized away. + if (date == -1) { + Log.e(TAG, "unexpected date"); + break; + } + } + out.clear(); + } + } + + long endTime = System.currentTimeMillis(); + long elapsed = endTime - startTime; + Log.i(TAG, "testPerformanceExpand() expand() elapsed millis: " + elapsed); + } + + @LargeTest + public void performanceTestNormalize() throws Exception { + final int ITERATIONS = 50000; + + String tz = "America/Los_Angeles"; + Time date = new Time(tz); + date.parse("20090404T100000"); + + long startTime = System.currentTimeMillis(); + + for (int i = 0; i < ITERATIONS; i++) { + date.month += 1; + date.monthDay += 100; + date.normalize(true); + date.month -= 1; + date.monthDay -= 100; + date.normalize(true); + } + + long endTime = System.currentTimeMillis(); + long elapsed = endTime - startTime; + Log.i(TAG, "date: " + date.format2445()); + Log.i(TAG, "testPerformanceNormalize() normalize() elapsed millis: " + elapsed); + + // Time the unsafe version + date.parse("20090404T100000"); + startTime = System.currentTimeMillis(); + for (int i = 0; i < ITERATIONS; i++) { + date.month += 1; + date.monthDay += 100; + RecurrenceProcessor.unsafeNormalize(date); + date.month -= 1; + date.monthDay -= 100; + RecurrenceProcessor.unsafeNormalize(date); + } + + endTime = System.currentTimeMillis(); + elapsed = endTime - startTime; + Log.i(TAG, "date: " + date.format2445()); + Log.i(TAG, "testPerformanceNormalize() unsafeNormalize() elapsed millis: " + elapsed); + } + } diff --git a/tests/src/com/android/calendarcommon2/RecurrenceSetTest.java b/tests/src/com/android/calendarcommon2/RecurrenceSetTest.java new file mode 100644 index 0000000..f65c780 --- /dev/null +++ b/tests/src/com/android/calendarcommon2/RecurrenceSetTest.java @@ -0,0 +1,155 @@ +/* + * Copyright (C) 2009 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.calendarcommon2; + +import com.android.calendarcommon2.ICalendar; +import com.android.calendarcommon2.RecurrenceSet; + +import android.content.ContentValues; +import android.test.suitebuilder.annotation.SmallTest; +import android.util.Log; +import android.provider.CalendarContract; +import junit.framework.TestCase; + +/** + * Test some pim.RecurrenceSet functionality. + */ +public class RecurrenceSetTest extends TestCase { + + // Test a recurrence + @SmallTest + public void testRecurrenceSet0() throws Exception { + String recurrence = "DTSTART;TZID=America/New_York:20080221T070000\n" + + "DTEND;TZID=America/New_York:20080221T190000\n" + + "RRULE:FREQ=DAILY;UNTIL=20080222T000000Z\n" + + "EXDATE:20080222T120000Z"; + verifyPopulateContentValues(recurrence, "FREQ=DAILY;UNTIL=20080222T000000Z", null, + null, "20080222T120000Z", 1203595200000L, "America/New_York", "P43200S", 0, false); + } + + // Test 1 day all-day event + @SmallTest + public void testRecurrenceSet1() throws Exception { + String recurrence = "DTSTART;VALUE=DATE:20090821\nDTEND;VALUE=DATE:20090822\n" + + "RRULE:FREQ=YEARLY;WKST=SU"; + verifyPopulateContentValues(recurrence, "FREQ=YEARLY;WKST=SU", null, + null, null, 1250812800000L, "UTC", "P1D", 1, false); + } + + // Test 2 day all-day event + @SmallTest + public void testRecurrenceSet2() throws Exception { + String recurrence = "DTSTART;VALUE=DATE:20090821\nDTEND;VALUE=DATE:20090823\n" + + "RRULE:FREQ=YEARLY;WKST=SU"; + verifyPopulateContentValues(recurrence, "FREQ=YEARLY;WKST=SU", null, + null, null, 1250812800000L, "UTC", "P2D", 1, false); + } + + // Test multi-rule RRULE. + @SmallTest + public void testRecurrenceSet3() throws Exception { + String recurrence = "DTSTART;VALUE=DATE:20090821\n" + + "RRULE:FREQ=YEARLY;WKST=SU\n" + + "RRULE:FREQ=MONTHLY;COUNT=3\n" + + "DURATION:P2H"; + verifyPopulateContentValues(recurrence, "FREQ=YEARLY;WKST=SU\nFREQ=MONTHLY;COUNT=3", null, + null, null, 1250812800000L, "UTC", "P2H", 1 /*allDay*/, false); + // allDay=1 just means the start time is 00:00:00 UTC. + } + + // Test RDATE with VALUE=DATE. + @SmallTest + public void testRecurrenceSet4() throws Exception { + String recurrence = "DTSTART;TZID=America/Los_Angeles:20090821T010203\n" + + "RDATE;TZID=America/Los_Angeles;VALUE=DATE:20110601,20110602,20110603\n" + + "DURATION:P2H"; + verifyPopulateContentValues(recurrence, null, + //"TZID=America/Los_Angeles;VALUE=DATE:20110601,20110602,20110603", + "America/Los_Angeles;20110601,20110602,20110603", // incorrect + null, null, 1250841723000L, "America/Los_Angeles", "P2H", 0 /*allDay*/, false); + // allDay=1 just means the start time is 00:00:00 UTC. + } + + // Check generation of duration from events in different time zones. + @SmallTest + public void testRecurrenceSet5() throws Exception { + String recurrence = "DTSTART;TZID=America/Los_Angeles:20090821T070000\n" + + "DTEND;TZID=America/New_York:20090821T110000\n" + + "RRULE:FREQ=YEARLY\n"; + verifyPopulateContentValues(recurrence, "FREQ=YEARLY", null, + null, null, 1250863200000L, "America/Los_Angeles", "P3600S" /*P1H*/, 0 /*allDay*/, + false); + // TODO: would like to use P1H for duration + + String recurrence2 = "DTSTART;TZID=America/New_York:20090821T100000\n" + + "DTEND;TZID=America/Los_Angeles:20090821T080000\n" + + "RRULE:FREQ=YEARLY\n"; + verifyPopulateContentValues(recurrence, "FREQ=YEARLY", null, + null, null, 1250863200000L, "America/Los_Angeles", "P3600S" /*P1H*/, 0 /*allDay*/, + false); + // TODO: should we rigorously define which tzid becomes the "event timezone"? + } + + // Test a failure to parse the recurrence data + @SmallTest + public void testRecurrenceSetBadDstart() throws Exception { + String recurrence = "DTSTART;TZID=GMT+05:30:20080221T070000\n" + + "DTEND;TZID=GMT+05:30:20080221T190000\n" + + "RRULE:FREQ=DAILY;UNTIL=20080222T000000Z\n" + + "EXDATE:20080222T120000Z"; + verifyPopulateContentValues(recurrence, "FREQ=DAILY;UNTIL=20080222T000000Z", null, + null, "20080222T120000Z", 1203595200000L, "America/New_York", "P43200S", 0, true); + } + + @SmallTest + public void testRecurrenceSetBadRrule() throws Exception { + String recurrence = "DTSTART;TZID=America/New_York:20080221T070000\n" + + "DTEND;TZID=GMT+05:30:20080221T190000\n" + + "RRULE:FREQ=NEVER;UNTIL=20080222T000000Z\n" + + "EXDATE:20080222T120000Z"; + verifyPopulateContentValues(recurrence, "FREQ=DAILY;UNTIL=20080222T000000Z", null, + null, "20080222T120000Z", 1203595200000L, "America/New_York", "P43200S", 0, true); + } + + // run populateContentValues and verify the results + private void verifyPopulateContentValues(String recurrence, String rrule, String rdate, + String exrule, String exdate, long dtstart, String tzid, String duration, int allDay, + boolean badFormat) + throws ICalendar.FormatException { + ICalendar.Component recurrenceComponent = + new ICalendar.Component("DUMMY", null /* parent */); + ICalendar.parseComponent(recurrenceComponent, recurrence); + ContentValues values = new ContentValues(); + boolean result = RecurrenceSet.populateContentValues(recurrenceComponent, values); + Log.d("KS", "values " + values); + + if (badFormat) { + assertEquals(result, !badFormat); + return; + } + assertEquals(rrule, values.get(android.provider.CalendarContract.Events.RRULE)); + assertEquals(rdate, values.get(android.provider.CalendarContract.Events.RDATE)); + assertEquals(exrule, values.get(android.provider.CalendarContract.Events.EXRULE)); + assertEquals(exdate, values.get(android.provider.CalendarContract.Events.EXDATE)); + assertEquals(dtstart, (long) values.getAsLong(CalendarContract.Events.DTSTART)); + assertEquals(tzid, values.get(android.provider.CalendarContract.Events.EVENT_TIMEZONE)); + assertEquals(duration, values.get(android.provider.CalendarContract.Events.DURATION)); + assertEquals(allDay, + (int) values.getAsInteger(android.provider.CalendarContract.Events.ALL_DAY)); + } + +} -- cgit v1.2.3