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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var { AppConstants } = ChromeUtils.importESModule("resource://gre/modules/AppConstants.sys.mjs");
var { FileUtils } = ChromeUtils.importESModule("resource://gre/modules/FileUtils.sys.mjs");
var { updateAppInfo } = ChromeUtils.importESModule("resource://testing-common/AppInfo.sys.mjs");
ChromeUtils.defineModuleGetter(this, "NetUtil", "resource://gre/modules/NetUtil.jsm");
updateAppInfo();
var { cal } = ChromeUtils.import("resource:///modules/calendar/calUtils.jsm");
function createDate(aYear, aMonth, aDay, aHasTime, aHour, aMinute, aSecond, aTimezone) {
let date = Cc["@mozilla.org/calendar/datetime;1"].createInstance(Ci.calIDateTime);
date.resetTo(
aYear,
aMonth,
aDay,
aHour || 0,
aMinute || 0,
aSecond || 0,
aTimezone || cal.dtz.UTC
);
date.isDate = !aHasTime;
return date;
}
function createEventFromIcalString(icalString) {
if (/^BEGIN:VCALENDAR/.test(icalString)) {
let parser = Cc["@mozilla.org/calendar/ics-parser;1"].createInstance(Ci.calIIcsParser);
parser.parseString(icalString);
let items = parser.getItems();
cal.ASSERT(items.length == 1);
return items[0].QueryInterface(Ci.calIEvent);
}
let event = Cc["@mozilla.org/calendar/event;1"].createInstance(Ci.calIEvent);
event.icalString = icalString;
return event;
}
function createTodoFromIcalString(icalString) {
let todo = Cc["@mozilla.org/calendar/todo;1"].createInstance(Ci.calITodo);
todo.icalString = icalString;
return todo;
}
function getMemoryCal() {
return Cc["@mozilla.org/calendar/calendar;1?type=memory"].createInstance(
Ci.calISyncWriteCalendar
);
}
function getStorageCal() {
// Whenever we get the storage calendar we need to request a profile,
// otherwise the cleanup functions will not run
do_get_profile();
// create URI
let db = Services.dirsvc.get("TmpD", Ci.nsIFile);
db.append("test_storage.sqlite");
let uri = Services.io.newFileURI(db);
// Make sure timezone service is initialized
Cc["@mozilla.org/calendar/timezone-service;1"].getService(Ci.calIStartupService).startup(null);
// create storage calendar
let stor = Cc["@mozilla.org/calendar/calendar;1?type=storage"].createInstance(
Ci.calISyncWriteCalendar
);
stor.uri = uri;
stor.id = cal.getUUID();
return stor;
}
/**
* Return an item property as string.
*
* @param aItem
* @param string aProp possible item properties: start, end, duration,
* generation, title,
* id, calendar, creationDate, lastModifiedTime,
* stampTime, priority, privacy, status,
* alarmLastAck, recurrenceStartDate
* and any property that can be obtained using getProperty()
*/
function getProps(aItem, aProp) {
let value = null;
switch (aProp) {
case "start":
value = aItem.startDate || aItem.entryDate || null;
break;
case "end":
value = aItem.endDate || aItem.dueDate || null;
break;
case "duration":
value = aItem.duration || null;
break;
case "generation":
value = aItem.generation;
break;
case "title":
value = aItem.title;
break;
case "id":
value = aItem.id;
break;
case "calendar":
value = aItem.calendar.id;
break;
case "creationDate":
value = aItem.creationDate;
break;
case "lastModifiedTime":
value = aItem.lastModifiedTime;
break;
case "stampTime":
value = aItem.stampTime;
break;
case "priority":
value = aItem.priority;
break;
case "privacy":
value = aItem.privacy;
break;
case "status":
value = aItem.status;
break;
case "alarmLastAck":
value = aItem.alarmLastAck;
break;
case "recurrenceStartDate":
value = aItem.recurrenceStartDate;
break;
default:
value = aItem.getProperty(aProp);
}
if (value) {
return value.toString();
}
return null;
}
function compareItemsSpecific(aLeftItem, aRightItem, aPropArray) {
if (!aPropArray) {
// left out: "id", "calendar", "lastModifiedTime", "generation",
// "stampTime" as these are expected to change
aPropArray = [
"start",
"end",
"duration",
"title",
"priority",
"privacy",
"creationDate",
"status",
"alarmLastAck",
"recurrenceStartDate",
];
}
if (aLeftItem instanceof Ci.calIEvent) {
aLeftItem.QueryInterface(Ci.calIEvent);
} else if (aLeftItem instanceof Ci.calITodo) {
aLeftItem.QueryInterface(Ci.calITodo);
}
for (let i = 0; i < aPropArray.length; i++) {
equal(getProps(aLeftItem, aPropArray[i]), getProps(aRightItem, aPropArray[i]));
}
}
/**
* Unfold ics lines by removing any \r\n or \n followed by a linear whitespace
* (space or htab).
*
* @param aLine The line to unfold
* @returns The unfolded line
*/
function ics_unfoldline(aLine) {
return aLine.replace(/\r?\n[ \t]/g, "");
}
/**
* Dedent the template string tagged with this function to make indented data
* easier to read. Usage:
*
* let data = dedent`
* This is indented data it will be unindented so that the first line has
* no leading spaces and the second is indented by two spaces.
* `;
*
* @param strings The string fragments from the template string
* @param ...values The interpolated values
* @returns The interpolated, dedented string
*/
function dedent(strings, ...values) {
let parts = [];
// Perform variable interpolation
let minIndent = Infinity;
for (let [i, string] of strings.entries()) {
let innerparts = string.split("\n");
if (i == 0) {
innerparts.shift();
}
if (i == strings.length - 1) {
innerparts.pop();
}
for (let [j, ip] of innerparts.entries()) {
let match = ip.match(/^(\s*)\S*/);
if (j != 0) {
minIndent = Math.min(minIndent, match[1].length);
}
}
parts.push(innerparts);
}
return parts
.map((part, i) => {
return (
part
.map((line, j) => {
return j == 0 && i > 0 ? line : line.substr(minIndent);
})
.join("\n") + (i < values.length ? values[i] : "")
);
})
.join("");
}
/**
* Read a JSON file and return the JS object
*/
function readJSONFile(aFile) {
let stream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream);
try {
stream.init(aFile, FileUtils.MODE_RDONLY, FileUtils.PERMS_FILE, 0);
let bytes = NetUtil.readInputStream(stream, stream.available());
let data = JSON.parse(new TextDecoder().decode(bytes));
return data;
} catch (ex) {
dump("readJSONFile: Error reading JSON file: " + ex);
} finally {
stream.close();
}
return false;
}
function do_load_timezoneservice(callback) {
do_test_pending();
cal.timezoneService.startup({
onResult() {
do_test_finished();
callback();
},
});
}
function do_load_calmgr(callback) {
do_test_pending();
cal.manager.startup({
onResult() {
do_test_finished();
callback();
},
});
}
function do_calendar_startup(callback) {
let obs = {
observe() {
Services.obs.removeObserver(this, "calendar-startup-done");
do_test_finished();
executeSoon(callback);
},
};
let startupService = Cc["@mozilla.org/calendar/startup-service;1"].getService(
Ci.nsISupports
).wrappedJSObject;
if (startupService.started) {
callback();
} else {
do_test_pending();
Services.obs.addObserver(obs, "calendar-startup-done");
if (this._profileInitialized) {
Services.obs.notifyObservers(null, "profile-after-change", "xpcshell-do-get-profile");
} else {
do_get_profile(true);
}
}
}
/**
* Monkey patch the function with the name x on obj and overwrite it with func.
* The first parameter of this function is the original function that can be
* called at any time.
*
* @param obj The object the function is on.
* @param name The string name of the function.
* @param func The function to monkey patch with.
*/
function monkeyPatch(obj, x, func) {
let old = obj[x];
obj[x] = function () {
let parent = old.bind(obj);
let args = Array.from(arguments);
args.unshift(parent);
try {
return func.apply(obj, args);
} catch (e) {
console.error(e);
throw e;
}
};
}
/**
* Asserts the properties of an actual extract parser result to what was
* expected.
*
* @param {object} actual - Mostly the actual output of parse().
* @param {object} expected - The expected output.
* @param {string} level - The variable name to refer to report on.
*/
function compareExtractResults(actual, expected, level = "") {
for (let [key, value] of Object.entries(expected)) {
let qualifiedKey = [level, Array.isArray(expected) ? `[${key}]` : `.${key}`].join("");
if (value && typeof value == "object") {
Assert.ok(actual[key], `${qualifiedKey} is not null`);
compareExtractResults(actual[key], value, qualifiedKey);
continue;
}
Assert.equal(actual[key], value, `${qualifiedKey} has value "${value}"`);
}
}
|