• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.calendarcommon;
18 
19 import com.android.calendarcommon.ICalendar;
20 import com.android.calendarcommon.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);
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);
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);
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*/);
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*/);
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         // TODO: would like to use P1H for duration
96 
97         String recurrence2 = "DTSTART;TZID=America/New_York:20090821T100000\n"
98             + "DTEND;TZID=America/Los_Angeles:20090821T080000\n"
99             + "RRULE:FREQ=YEARLY\n";
100         verifyPopulateContentValues(recurrence, "FREQ=YEARLY", null,
101                 null, null, 1250863200000L, "America/Los_Angeles", "P3600S" /*P1H*/, 0 /*allDay*/);
102         // TODO: should we rigorously define which tzid becomes the "event timezone"?
103     }
104 
105 
106     // 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)107     private void verifyPopulateContentValues(String recurrence, String rrule, String rdate,
108             String exrule, String exdate, long dtstart, String tzid, String duration, int allDay)
109             throws ICalendar.FormatException {
110         ICalendar.Component recurrenceComponent =
111                 new ICalendar.Component("DUMMY", null /* parent */);
112         ICalendar.parseComponent(recurrenceComponent, recurrence);
113         ContentValues values = new ContentValues();
114         RecurrenceSet.populateContentValues(recurrenceComponent, values);
115         Log.d("KS", "values " + values);
116 
117         assertEquals(rrule, values.get(android.provider.CalendarContract.Events.RRULE));
118         assertEquals(rdate, values.get(android.provider.CalendarContract.Events.RDATE));
119         assertEquals(exrule, values.get(android.provider.CalendarContract.Events.EXRULE));
120         assertEquals(exdate, values.get(android.provider.CalendarContract.Events.EXDATE));
121         assertEquals(dtstart, (long) values.getAsLong(CalendarContract.Events.DTSTART));
122         assertEquals(tzid, values.get(android.provider.CalendarContract.Events.EVENT_TIMEZONE));
123         assertEquals(duration, values.get(android.provider.CalendarContract.Events.DURATION));
124         assertEquals(allDay,
125                 (int) values.getAsInteger(android.provider.CalendarContract.Events.ALL_DAY));
126     }
127 }
128