• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2007 The Android Open Source Project
2 package com.android.providers.calendar;
3 
4 import android.pim.ICalendar;
5 import android.test.suitebuilder.annotation.SmallTest;
6 import junit.framework.TestCase;
7 
8 import java.util.List;
9 
10 public class ICalendarTest extends TestCase {
11 
12     @SmallTest
testAddParameter()13     public void testAddParameter() throws Exception {
14         ICalendar.Property prop = new ICalendar.Property("prop1", "value1");
15         assertEquals(0, prop.getParameterNames().size());
16         prop.addParameter(new ICalendar.Parameter("param1", "foo"));
17         assertEquals(1, prop.getParameterNames().size());
18         prop.addParameter(new ICalendar.Parameter("param1", "bar"));
19         assertEquals(1, prop.getParameterNames().size());
20         prop.addParameter(new ICalendar.Parameter("param2", "baaz"));
21         assertEquals(2, prop.getParameterNames().size());
22         prop.addParameter(new ICalendar.Parameter("param1", "quux"));
23         assertEquals(2, prop.getParameterNames().size());
24         prop.addParameter(new ICalendar.Parameter("param3", "accent"));
25         assertEquals(3, prop.getParameterNames().size());
26         assertEquals("prop1;param1=foo;param1=bar;param1=quux;"
27                 + "param2=baaz;param3=accent:value1", prop.toString());
28     }
29 
30     @SmallTest
testAddProperty()31     public void testAddProperty() throws Exception {
32         String text = "BEGIN:DUMMY\n" +
33                 "prop2:value3\n" +
34                 "prop1:value1\n" +
35                 "prop1:value2\n" +
36                 "END:DUMMY\n";
37 
38         ICalendar.Component component = new ICalendar.Component("DUMMY", null);
39         // properties should be listed in insertion order, by property name.
40         component.addProperty(new ICalendar.Property("prop2", "value3"));
41         component.addProperty(new ICalendar.Property("prop1", "value1"));
42         component.addProperty(new ICalendar.Property("prop1", "value2"));
43         assertEquals(text, component.toString());
44     }
45 
46     @SmallTest
testAddComponent()47     public void testAddComponent() throws Exception {
48         String text = "BEGIN:DUMMY\n" +
49                 "prop1:value1\n" +
50                 "prop1:value12\n" +
51                 "BEGIN:DUMMY2\n" +
52                 "prop2:value2\n" +
53                 "END:DUMMY2\n" +
54                 "END:DUMMY\n";
55 
56         ICalendar.Component parent = new ICalendar.Component("DUMMY", null);
57         // properties should precede components
58         ICalendar.Component child = new ICalendar.Component("DUMMY2", parent);
59         child.addProperty(new ICalendar.Property("prop2", "value2"));
60         parent.addChild(child);
61         parent.addProperty(new ICalendar.Property("prop1", "value1"));
62         parent.addProperty(new ICalendar.Property("prop1", "value12"));
63         assertEquals(text, parent.toString());
64     }
65 
66     @SmallTest
testParseBasicComponent()67     public void testParseBasicComponent() throws Exception {
68         String text = "BEGIN:DUMMY\n" +
69                 "PROP1;PARAM1=foo;PARAM2=bar:VALUE1\n" +
70                 "PROP1;PARAM1=baaz;PARAM1=quux:VALUE2\n" +
71                 "PROP2:VALUE3\n" +
72                 "END:DUMMY\n";
73 
74         ICalendar.Component component = ICalendar.parseComponent(text);
75 
76         assertEquals("DUMMY", component.getName());
77         assertNull(component.getComponents());
78         assertEquals(2, component.getPropertyNames().size());
79         ICalendar.Property prop1 = component.getFirstProperty("PROP1");
80         assertEquals(2, prop1.getParameterNames().size());
81         assertEquals("foo", prop1.getFirstParameter("PARAM1").value);
82         assertEquals("bar", prop1.getFirstParameter("PARAM2").value);
83         List<ICalendar.Property> props = component.getProperties("PROP1");
84         assertEquals(2, props.size());
85         List<ICalendar.Parameter> params = props.get(1).getParameters("PARAM1");
86         assertEquals("baaz", params.get(0).value);
87         assertEquals("quux", params.get(1).value);
88     }
89 
90     @SmallTest
testParseChildComponent()91     public void testParseChildComponent() throws Exception {
92         String childText = "BEGIN:CHILD\n" +
93                 "PROP1;PARAM1=foo;PARAM2=bar:VALUE1\n" +
94                 "PROP1;PARAM1=baaz;PARAM1=quux:VALUE2\n" +
95                 "PROP2:VALUE3\n" +
96                 "END:CHILD\n";
97 
98         String completeText = "BEGIN:DUMMY\n" +
99                 childText +
100                 "END:DUMMY\n";
101 
102         ICalendar.Component component = new ICalendar.Component("DUMMY", null);
103         component = ICalendar.parseComponent(component, childText);
104         assertEquals("DUMMY", component.getName());
105         assertEquals(1, component.getComponents().size());
106         assertEquals(completeText, component.toString());
107     }
108 
109     @SmallTest
testParseBareEvent()110     public void testParseBareEvent() throws Exception {
111         String text = "BEGIN:VEVENT\nEND:VEVENT\n";
112         ICalendar.Component event = ICalendar.parseEvent(text);
113 
114         assertEquals("VEVENT", event.getName());
115         assertNull(event.getComponents());
116         assertEquals(0, event.getPropertyNames().size());
117     }
118 
119     @SmallTest
testParseEvent1()120     public void testParseEvent1() throws Exception {
121         String text = "BEGIN:VEVENT\n" +
122                 "DTSTART:19970714T170000Z\n" +
123                 "DTEND:19970715T035959Z\n" +
124                 "SUMMARY:Bastille Day Party\n" +
125                 "END:VEVENT\n";
126 
127         ICalendar.Component event = ICalendar.parseEvent(text);
128 
129         assertEquals("VEVENT", event.getName());
130         assertNull(event.getComponents());
131         assertEquals(3, event.getPropertyNames().size());
132         assertEquals(1, event.getProperties("DTSTART").size());
133         assertEquals("19970714T170000Z", event.getFirstProperty("DTSTART").getValue());
134         assertEquals(0, event.getFirstProperty("DTSTART").getParameterNames().size());
135         assertEquals(1, event.getProperties("DTEND").size());
136         assertEquals(0, event.getFirstProperty("DTEND").getParameterNames().size());
137         assertEquals("19970715T035959Z", event.getFirstProperty("DTEND").getValue());
138         assertEquals(1, event.getProperties("SUMMARY").size());
139         assertEquals(0, event.getFirstProperty("SUMMARY").getParameterNames().size());
140         assertEquals("Bastille Day Party", event.getFirstProperty("SUMMARY").getValue());
141     }
142 
143     @SmallTest
testParseEvent2()144     public void testParseEvent2() throws Exception {
145         String text = "BEGIN:VEVENT\n" +
146                 "DTSTART;TZID=America/Los_Angeles:19970714T170000\n" +
147                 "DURATION:+P3600S\n" +
148                 "SUMMARY;FOO=1;BAR=2:Bastille Day Party\n" +
149                 "END:VEVENT\n";
150 
151         ICalendar.Component event = ICalendar.parseEvent(text);
152 
153         assertEquals("VEVENT", event.getName());
154         assertNull(event.getComponents());
155         assertEquals(3, event.getPropertyNames().size());
156         assertEquals(1, event.getProperties("DTSTART").size());
157         assertEquals("19970714T170000", event.getFirstProperty("DTSTART").getValue());
158         assertEquals(1, event.getFirstProperty("DTSTART").getParameterNames().size());
159         assertEquals(1, event.getProperties("SUMMARY").size());
160     }
161 
162     @SmallTest
testParseInvalidProperty()163     public void testParseInvalidProperty() throws Exception {
164         String text = "BEGIN:VEVENT\n" +
165                 "FOO;BAR\n" + // invalid line
166                 "END:VEVENT\n";
167 
168         ICalendar.Component event = ICalendar.parseEvent(text);
169 
170         assertEquals("VEVENT", event.getName());
171         assertNull(event.getComponents());
172         assertEquals(0, event.getPropertyNames().size());
173     }
174 
175     @SmallTest
testParseEventDoesNotStartWithBegin()176     public void testParseEventDoesNotStartWithBegin() throws Exception {
177         String text = "NOTBEGIN:DUMMY\n" +
178                 "END:DUMMY\n";
179 
180         try {
181             ICalendar.parseEvent(text);
182             fail("expected exception not thrown");
183         } catch (ICalendar.FormatException e) {
184             assertEquals("Expected " + ICalendar.Component.VEVENT, e.getMessage());
185         }
186     }
187 
188     @SmallTest
testParseCalendarDoesNotStartWithBegin()189     public void testParseCalendarDoesNotStartWithBegin() throws Exception {
190         String text = "NOTBEGIN:DUMMY\n" +
191                 "END:DUMMY\n";
192 
193         try {
194             ICalendar.parseCalendar(text);
195             fail("expected exception not thrown");
196         } catch (ICalendar.FormatException e) {
197             assertEquals("Expected " + ICalendar.Component.VCALENDAR, e.getMessage());
198         }
199     }
200 
201     @SmallTest
testParseComponentDoesNotStartWithBegin()202     public void testParseComponentDoesNotStartWithBegin() throws Exception {
203         String text = "NOTBEGIN:DUMMY\n" +
204                 "END:DUMMY\n";
205 
206         ICalendar.Component component = ICalendar.parseComponent(text);
207         assertNull(component);
208     }
209 
210     @SmallTest
testParseUnexpectedEndComponent()211     public void testParseUnexpectedEndComponent() throws Exception {
212         String text = "BEGIN:PARENT\n" +
213                 "END:BADPARENT\n";
214 
215         ICalendar.Component component = ICalendar.parseComponent(text);
216         assertNotNull(component);
217     }
218 
219     @SmallTest
testParseNoEndComponent()220     public void testParseNoEndComponent() throws Exception {
221         String text = "BEGIN:DUMMY\n" +
222                 "END:\n";
223 
224         ICalendar.Component component = ICalendar.parseComponent(text);
225         assertNotNull(component);
226     }
227 
228     @SmallTest
testNormalize()229     public void testNormalize() throws Exception {
230         String text = "BEGIN:VEVENT\n" +
231                 "RRULE:FREQ=SECONDLY;BYSECOND=0,1,2,\r\n 3,4,5\r\n ,6,7,8\r\n" +
232                 "END:VEVENT\n";
233 
234         ICalendar.Component event = ICalendar.parseEvent(text);
235 
236         assertEquals("VEVENT", event.getName());
237         assertNull(event.getComponents());
238         assertEquals(1, event.getPropertyNames().size());
239         assertEquals(1, event.getProperties("RRULE").size());
240         assertEquals("FREQ=SECONDLY;BYSECOND=0,1,2,3,4,5,6,7,8", event.getFirstProperty("RRULE").getValue());
241 
242     }
243 
244     @SmallTest
testNormalizeBadSep()245     public void testNormalizeBadSep() throws Exception {
246         String text = "BEGIN:VEVENT\n" +
247                 "RRULE:FREQ=SECONDLY;BYSECOND=0,1,2,\n 3,4,5\n ,6,7,8\n" +
248                 "END:VEVENT\n";
249 
250         ICalendar.Component event = ICalendar.parseEvent(text);
251 
252         assertEquals("VEVENT", event.getName());
253         assertNull(event.getComponents());
254         assertEquals(1, event.getPropertyNames().size());
255         assertEquals(1, event.getProperties("RRULE").size());
256         assertEquals("FREQ=SECONDLY;BYSECOND=0,1,2,3,4,5,6,7,8", event.getFirstProperty("RRULE").getValue());
257     }
258 
259 
260     @SmallTest
testBad()261     public void testBad() throws Exception {
262         String text = "BEGIN:VEVENT\n" +
263                 "RRULE=foo\n" +
264                 "END:VEVENT\n";
265 
266         ICalendar.Component event = ICalendar.parseEvent(text);
267 
268         // Note that parseEvent doesn't throw the FormatException you might expect because
269         // ICalendar.parseComponentImpl catches the exception due to misformatted GData.
270         // TODO: update this test after cleaning up the ICalendar behavior
271     }
272 }
273