1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.calendarcommon2;
18 
19 import com.android.calendarcommon2.ICalendar;
20 import com.android.calendarcommon2.RecurrenceSet;
21 
22 import android.content.ContentValues;
23 import android.test.suitebuilder.annotation.SmallTest;
24 import android.util.Log;
25 import android.provider.CalendarContract;
26 import junit.framework.TestCase;
27 
28 /**
29  * Test some pim.RecurrenceSet functionality.
30  */
31 public class RecurrenceSetTest extends TestCase {
32 
33     // Test a recurrence
34     @SmallTest
testRecurrenceSet0()35     public void testRecurrenceSet0() throws Exception {
36         String recurrence = "DTSTART;TZID=America/New_York:20080221T070000\n"
37                 + "DTEND;TZID=America/New_York:20080221T190000\n"
38                 + "RRULE:FREQ=DAILY;UNTIL=20080222T000000Z\n"
39                 + "EXDATE:20080222T120000Z";
40         verifyPopulateContentValues(recurrence, "FREQ=DAILY;UNTIL=20080222T000000Z", null,
41                 null, "20080222T120000Z", 1203595200000L, "America/New_York", "P43200S", 0, false);
42     }
43 
44     // Test 1 day all-day event
45     @SmallTest
testRecurrenceSet1()46     public void testRecurrenceSet1() throws Exception {
47         String recurrence = "DTSTART;VALUE=DATE:20090821\nDTEND;VALUE=DATE:20090822\n"
48                 + "RRULE:FREQ=YEARLY;WKST=SU";
49         verifyPopulateContentValues(recurrence, "FREQ=YEARLY;WKST=SU", null,
50                 null, null, 1250812800000L, "UTC", "P1D", 1, false);
51     }
52 
53     // Test 2 day all-day event
54     @SmallTest
testRecurrenceSet2()55     public void testRecurrenceSet2() throws Exception {
56         String recurrence = "DTSTART;VALUE=DATE:20090821\nDTEND;VALUE=DATE:20090823\n"
57                 + "RRULE:FREQ=YEARLY;WKST=SU";
58         verifyPopulateContentValues(recurrence, "FREQ=YEARLY;WKST=SU", null,
59                 null, null, 1250812800000L, "UTC",  "P2D", 1, false);
60     }
61 
62     // Test multi-rule RRULE.
63     @SmallTest
testRecurrenceSet3()64     public void testRecurrenceSet3() throws Exception {
65         String recurrence = "DTSTART;VALUE=DATE:20090821\n"
66                 + "RRULE:FREQ=YEARLY;WKST=SU\n"
67                 + "RRULE:FREQ=MONTHLY;COUNT=3\n"
68                 + "DURATION:P2H";
69         verifyPopulateContentValues(recurrence, "FREQ=YEARLY;WKST=SU\nFREQ=MONTHLY;COUNT=3", null,
70                 null, null, 1250812800000L, "UTC", "P2H", 1 /*allDay*/, false);
71         // allDay=1 just means the start time is 00:00:00 UTC.
72     }
73 
74     // Test RDATE with VALUE=DATE.
75     @SmallTest
testRecurrenceSet4()76     public void testRecurrenceSet4() throws Exception {
77         String recurrence = "DTSTART;TZID=America/Los_Angeles:20090821T010203\n"
78                 + "RDATE;TZID=America/Los_Angeles;VALUE=DATE:20110601,20110602,20110603\n"
79                 + "DURATION:P2H";
80         verifyPopulateContentValues(recurrence, null,
81                 //"TZID=America/Los_Angeles;VALUE=DATE:20110601,20110602,20110603",
82                 "America/Los_Angeles;20110601,20110602,20110603", // incorrect
83                 null, null, 1250841723000L, "America/Los_Angeles", "P2H", 0 /*allDay*/, false);
84         // allDay=1 just means the start time is 00:00:00 UTC.
85     }
86 
87     // Check generation of duration from events in different time zones.
88     @SmallTest
testRecurrenceSet5()89     public void testRecurrenceSet5() throws Exception {
90         String recurrence = "DTSTART;TZID=America/Los_Angeles:20090821T070000\n"
91                 + "DTEND;TZID=America/New_York:20090821T110000\n"
92                 + "RRULE:FREQ=YEARLY\n";
93         verifyPopulateContentValues(recurrence, "FREQ=YEARLY", null,
94                 null, null, 1250863200000L, "America/Los_Angeles", "P3600S" /*P1H*/, 0 /*allDay*/,
95                 false);
96         // TODO: would like to use P1H for duration
97 
98         String recurrence2 = "DTSTART;TZID=America/New_York:20090821T100000\n"
99             + "DTEND;TZID=America/Los_Angeles:20090821T080000\n"
100             + "RRULE:FREQ=YEARLY\n";
101         verifyPopulateContentValues(recurrence, "FREQ=YEARLY", null,
102                 null, null, 1250863200000L, "America/Los_Angeles", "P3600S" /*P1H*/, 0 /*allDay*/,
103                 false);
104         // TODO: should we rigorously define which tzid becomes the "event timezone"?
105     }
106 
107     // Test a failure to parse the recurrence data
108     @SmallTest
testRecurrenceSetBadDstart()109     public void testRecurrenceSetBadDstart() throws Exception {
110         String recurrence = "DTSTART;TZID=GMT+05:30:20080221T070000\n"
111                 + "DTEND;TZID=GMT+05:30:20080221T190000\n"
112                 + "RRULE:FREQ=DAILY;UNTIL=20080222T000000Z\n"
113                 + "EXDATE:20080222T120000Z";
114         verifyPopulateContentValues(recurrence, "FREQ=DAILY;UNTIL=20080222T000000Z", null,
115                 null, "20080222T120000Z", 1203595200000L, "America/New_York", "P43200S", 0, true);
116     }
117 
118     @SmallTest
testRecurrenceSetBadRrule()119     public void testRecurrenceSetBadRrule() throws Exception {
120         String recurrence = "DTSTART;TZID=America/New_York:20080221T070000\n"
121                 + "DTEND;TZID=GMT+05:30:20080221T190000\n"
122                 + "RRULE:FREQ=NEVER;UNTIL=20080222T000000Z\n"
123                 + "EXDATE:20080222T120000Z";
124         verifyPopulateContentValues(recurrence, "FREQ=DAILY;UNTIL=20080222T000000Z", null,
125                 null, "20080222T120000Z", 1203595200000L, "America/New_York", "P43200S", 0, true);
126     }
127 
128     // run populateContentValues and verify the results
verifyPopulateContentValues(String recurrence, String rrule, String rdate, String exrule, String exdate, long dtstart, String tzid, String duration, int allDay, boolean badFormat)129     private void verifyPopulateContentValues(String recurrence, String rrule, String rdate,
130             String exrule, String exdate, long dtstart, String tzid, String duration, int allDay,
131             boolean badFormat)
132             throws ICalendar.FormatException {
133         ICalendar.Component recurrenceComponent =
134                 new ICalendar.Component("DUMMY", null /* parent */);
135         ICalendar.parseComponent(recurrenceComponent, recurrence);
136         ContentValues values = new ContentValues();
137         boolean result = RecurrenceSet.populateContentValues(recurrenceComponent, values);
138         Log.d("KS", "values " + values);
139 
140         if (badFormat) {
141             assertEquals(result, !badFormat);
142             return;
143         }
144         assertEquals(rrule, values.get(android.provider.CalendarContract.Events.RRULE));
145         assertEquals(rdate, values.get(android.provider.CalendarContract.Events.RDATE));
146         assertEquals(exrule, values.get(android.provider.CalendarContract.Events.EXRULE));
147         assertEquals(exdate, values.get(android.provider.CalendarContract.Events.EXDATE));
148         assertEquals(dtstart, (long) values.getAsLong(CalendarContract.Events.DTSTART));
149         assertEquals(tzid, values.get(android.provider.CalendarContract.Events.EVENT_TIMEZONE));
150         assertEquals(duration, values.get(android.provider.CalendarContract.Events.DURATION));
151         assertEquals(allDay,
152                 (int) values.getAsInteger(android.provider.CalendarContract.Events.ALL_DAY));
153     }
154 
155 }
156