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
|
/* 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 { cal } = ChromeUtils.import("resource:///modules/calendar/calUtils.jsm");
var { CalendarTestUtils } = ChromeUtils.import(
"resource://testing-common/calendar/CalendarTestUtils.jsm"
);
var { CalEvent } = ChromeUtils.import("resource:///modules/CalEvent.jsm");
var { PromiseUtils } = ChromeUtils.importESModule("resource://gre/modules/PromiseUtils.sys.mjs");
var { updateAppInfo } = ChromeUtils.importESModule("resource://testing-common/AppInfo.sys.mjs");
updateAppInfo();
// The tests in this directory each do the same thing, with slight variations as needed for each
// calendar provider. The core of the test lives in this file and the tests call it when ready.
do_get_profile();
add_setup(async () => {
await new Promise(resolve => cal.manager.startup({ onResult: resolve }));
await new Promise(resolve => cal.timezoneService.startup({ onResult: resolve }));
cal.manager.addCalendarObserver(calendarObserver);
});
let calendarObserver = {
QueryInterface: ChromeUtils.generateQI(["calIObserver"]),
/* calIObserver */
_batchCount: 0,
_batchRequired: true,
onStartBatch(calendar) {
info(`onStartBatch ${calendar?.id} ${++this._batchCount}`);
Assert.equal(calendar, this._expectedCalendar);
Assert.equal(this._batchCount, 1, "onStartBatch must not occur in a batch");
},
onEndBatch(calendar) {
info(`onEndBatch ${calendar?.id} ${this._batchCount--}`);
Assert.equal(calendar, this._expectedCalendar);
Assert.equal(this._batchCount, 0, "onEndBatch must occur in a batch");
},
onLoad(calendar) {
info(`onLoad ${calendar.id}`);
Assert.equal(this._batchCount, 0, "onLoad must not occur in a batch");
Assert.equal(calendar, this._expectedCalendar);
if (this._onLoadPromise) {
this._onLoadPromise.resolve();
}
},
onAddItem(item) {
info(`onAddItem ${item.calendar.id} ${item.id}`);
if (this._batchRequired) {
Assert.equal(this._batchCount, 1, "onAddItem must occur in a batch");
}
if (this._onAddItemPromise) {
this._onAddItemPromise.resolve();
}
},
onModifyItem(newItem, oldItem) {
info(`onModifyItem ${newItem.calendar.id} ${newItem.id}`);
if (this._batchRequired) {
Assert.equal(this._batchCount, 1, "onModifyItem must occur in a batch");
}
if (this._onModifyItemPromise) {
this._onModifyItemPromise.resolve();
}
},
onDeleteItem(deletedItem) {
info(`onDeleteItem ${deletedItem.calendar.id} ${deletedItem.id}`);
if (this._onDeleteItemPromise) {
this._onDeleteItemPromise.resolve();
}
},
onError(calendar, errNo, message) {},
onPropertyChanged(calendar, name, value, oldValue) {},
onPropertyDeleting(calendar, name) {},
};
/**
* Create and register a calendar.
*
* @param {string} type - The calendar provider to use.
* @param {string} url - URL of the server.
* @param {boolean} useCache - Should this calendar have offline storage?
* @returns {calICalendar}
*/
function createCalendar(type, url, useCache) {
let calendar = cal.manager.createCalendar(type, Services.io.newURI(url));
calendar.name = type + (useCache ? " with cache" : " without cache");
calendar.id = cal.getUUID();
calendar.setProperty("cache.enabled", useCache);
cal.manager.registerCalendar(calendar);
calendar = cal.manager.getCalendarById(calendar.id);
calendarObserver._expectedCalendar = calendar;
info(`Created calendar ${calendar.id}`);
return calendar;
}
/**
* Creates an event and adds it to the given calendar.
*
* @param {calICalendar} calendar
* @returns {calIEvent}
*/
async function runAddItem(calendar) {
let event = new CalEvent();
event.id = "6b7dd6f6-d6f0-4e93-a953-bb5473c4c47a";
event.title = "New event";
event.startDate = cal.createDateTime("20200303T205500Z");
event.endDate = cal.createDateTime("20200303T210200Z");
calendarObserver._onAddItemPromise = PromiseUtils.defer();
calendarObserver._onModifyItemPromise = PromiseUtils.defer();
await calendar.addItem(event);
await Promise.any([
calendarObserver._onAddItemPromise.promise,
calendarObserver._onModifyItemPromise.promise,
]);
return event;
}
/**
* Modifies the event from runAddItem.
*
* @param {calICalendar} calendar
*/
async function runModifyItem(calendar) {
let event = await calendar.getItem("6b7dd6f6-d6f0-4e93-a953-bb5473c4c47a");
let clone = event.clone();
clone.title = "Modified event";
calendarObserver._onModifyItemPromise = PromiseUtils.defer();
await calendar.modifyItem(clone, event);
await calendarObserver._onModifyItemPromise.promise;
}
/**
* Deletes the event from runAddItem.
*
* @param {calICalendar} calendar
*/
async function runDeleteItem(calendar) {
let event = await calendar.getItem("6b7dd6f6-d6f0-4e93-a953-bb5473c4c47a");
calendarObserver._onDeleteItemPromise = PromiseUtils.defer();
await calendar.deleteItem(event);
await calendarObserver._onDeleteItemPromise.promise;
}
|