1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is Mozilla Calendar tests code.
15 *
16 * The Initial Developer of the Original Code is
17 * Michiel van Leeuwen <mvl@exedo.nl>.
18 * Portions created by the Initial Developer are Copyright (C) 2006
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Contributor(s):
22 * Daniel Boelzle <daniel.boelzle@sun.com>
23 * Sebastian Schwieger <sebo.moz@googlemail.com>
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38
39 const Cc = Components.classes;
40 const Ci = Components.interfaces;
41
42 /// Shortcut to the timezone service
getTimezoneService
43 function getTimezoneService() {
44 if (getTimezoneService.mObject === undefined) {
45 getTimezoneService.mObject = Cc["@mozilla.org/calendar/timezone-service;1"]
46 .getService(Ci.calITimezoneService);
47 }
48 return getTimezoneService.mObject;
49 }
50
51 /// @return the UTC timezone.
UTC
52 function UTC() {
53 if (UTC.mObject === undefined) {
54 UTC.mObject = getTimezoneService().UTC;
55 }
56 return UTC.mObject;
57 }
58
59 /// @return the floating timezone.
floating
60 function floating() {
61 if (floating.mObject === undefined) {
62 floating.mObject = getTimezoneService().floating;
63 }
64 return floating.mObject;
65 }
66
createDate
67 function createDate(aYear, aMonth, aDay) {
68 var cd = Cc["@mozilla.org/calendar/datetime;1"]
69 .createInstance(Ci.calIDateTime);
70 cd.resetTo(aYear, aMonth, aDay, 0, 0, 0, UTC());
71 cd.isDate = true;
72 return cd;
73 }
74
createEventFromIcalString
75 function createEventFromIcalString(icalString) {
76 var event = Cc["@mozilla.org/calendar/event;1"]
77 .createInstance(Ci.calIEvent);
78 event.icalString = icalString;
79 return event;
80 }
81
createTodoFromIcalString
82 function createTodoFromIcalString(icalString) {
83 var todo = Cc["@mozilla.org/calendar/todo;1"]
84 .createInstance(Ci.calITodo);
85 todo.icalString = icalString;
86 return todo;
87 }
88
getMemoryCal
89 function getMemoryCal() {
90 // create memory calendar
91 var cal = Cc["@mozilla.org/calendar/calendar;1?type=memory"]
92 .createInstance(Ci.calICalendar);
93
94 // remove existing items
95 var calendar = cal.QueryInterface(Ci.calICalendarProvider);
96 try {
97 calendar.deleteCalendar(calendar, null);
98 } catch (e) {
99 print("*** error purging calendar: " + e);
100 }
101 return cal;
102 }
103
getStorageCal
104 function getStorageCal() {
105 var dirSvc = Cc["@mozilla.org/file/directory_service;1"]
106 .getService(Ci.nsIProperties);
107 var db = dirSvc.get("TmpD", Ci.nsIFile);
108 db.append("test_storage.sqlite");
109
110 // create URI
111 var ioSvc = Cc["@mozilla.org/network/io-service;1"]
112 .getService(Ci.nsIIOService);
113 var uri = ioSvc.newFileURI(db);
114
115 // create storage calendar
116 var cal = Cc["@mozilla.org/calendar/calendar;1?type=storage"]
117 .createInstance(Ci.calICalendar);
118 cal.uri = uri;
119
120 // remove existing items
121 var calendar = cal.QueryInterface(Ci.calICalendarProvider);
122 try {
123 calendar.deleteCalendar(calendar, null);
124 } catch (e) {
125 print("*** error purging calendar: " + e);
126 }
127 return cal;
128 }
129
130 /**
131 * Return an item property as string.
132 * @param aItem
133 * @param string aProp possible item properties: start, end, duration,
134 * generation, title,
135 * id, calendar, creationDate, lastModifiedTime,
136 * stampTime, priority, privacy, status,
137 * alarmOffset, alarmRelated,
138 * alarmLastAck, recurrenceStartDate
139 * and any property that can be obtained using getProperty()
140 */
getProps
141 function getProps(aItem, aProp) {
142 var value = null;
143 switch (aProp) {
144 case "start":
145 value = aItem.startDate || aItem.entryDate || null;
146 break;
147 case "end":
148 value = aItem.endDate || aItem.dueDate || null;
149 break;
150 case "duration":
151 value = aItem.duration || null;
152 break;
153 case "generation":
154 value = aItem.generation;
155 break;
156 case "title":
157 value = aItem.title;
158 break;
159 case "id":
160 value = aItem.id;
161 break;
162 case "calendar":
163 value = aItem.calendar.id;
164 break;
165 case "creationDate":
166 value = aItem.creationDate;
167 break;
168 case "lastModifiedTime":
169 value = aItem.lastModifiedTime;
170 break;
171 case "stampTime":
172 value = aItem.stampTime;
173 break;
174 case "priority":
175 value = aItem.priority;
176 break;
177 case "privacy":
178 value = aItem.privacy;
179 break;
180 case "status":
181 value = aItem.status;
182 break;
183 case "alarmOffset":
184 value = aItem.alarmOffset;
185 break;
186 case "alarmRelated":
187 value = aItem.alarmRelated;
188 break;
189 case "alarmLastAck":
190 value = aItem.alarmLastAck;
191 break;
192 case "recurrenceStartDate":
193 value = aItem.recurrenceStartDate;
194 break;
195 default:
196 value = aItem.getProperty(aProp);
197 }
198 if (value) {
199 return value.toString();
200 } else {
201 return null;
202 }
203 }
204
compareItems
205 function compareItems(aLeftItem, aRightItem, aPropArray) {
206 if (!aPropArray) {
207 // left out: "id", "calendar", "lastModifiedTime", "generation",
208 // "stampTime" as these are expected to change
209 aPropArray = ["start", "end", "duration",
210 "title", "priority", "privacy", "creationDate",
211 "status", "alarmOffset", "alarmRelated", "alarmLastAck",
212 "recurrenceStartDate"];
213 }
214 for (var i = 0; i < aPropArray.length; i++) {
215 do_check_eq(getProps(aLeftItem, aPropArray[i]),
216 getProps(aRightItem,
217 aPropArray[i]));
218 }
219 }
220
221 // Support do_check_true and do_check_false on MOZILLA_1_8_BRANCH
222 if (typeof do_check_true != "function") {
do_check_true
223 do_check_true = function do_check_true(condition) {
224 do_check_eq(condition, true);
225 }
226 }
227 if (typeof do_check_false != "function") {
do_check_false
228 do_check_false = function do_check_false(condition) {
229 do_check_eq(condition, false);
230 }
231 }