summaryrefslogtreecommitdiffstats
path: root/comm/calendar/test/unit/test_items.js
blob: fb7fa38ec5040be8b0f303b9d368bdf44fcc839c (plain)
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/* 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 { XPCOMUtils } = ChromeUtils.importESModule("resource://gre/modules/XPCOMUtils.sys.mjs");

var { CalendarTestUtils } = ChromeUtils.import(
  "resource://testing-common/calendar/CalendarTestUtils.jsm"
);

XPCOMUtils.defineLazyModuleGetters(this, {
  CalAlarm: "resource:///modules/CalAlarm.jsm",
  CalAttachment: "resource:///modules/CalAttachment.jsm",
  CalAttendee: "resource:///modules/CalAttendee.jsm",
  CalEvent: "resource:///modules/CalEvent.jsm",
  CalTodo: "resource:///modules/CalTodo.jsm",
});

function run_test() {
  do_calendar_startup(really_run_test);
}

function really_run_test() {
  test_aclmanager();
  test_calendar();
  test_immutable();
  test_attendee();
  test_attachment();
  test_lastack();
  test_categories();
  test_alarm();
  test_isEvent();
  test_isTodo();
  test_recurring_event_properties();
  test_recurring_todo_properties();
  test_recurring_event_exception_properties();
  test_recurring_todo_exception_properties();
}

function test_aclmanager() {
  let mockCalendar = {
    QueryInterface: ChromeUtils.generateQI(["calICalendar"]),

    get superCalendar() {
      return this;
    },
    get aclManager() {
      return this;
    },

    getItemEntry(item) {
      if (item.id == "withentry") {
        return itemEntry;
      }
      return null;
    },
  };

  let itemEntry = {
    QueryInterface: ChromeUtils.generateQI(["calIItemACLEntry"]),
    userCanModify: true,
    userCanRespond: false,
    userCanViewAll: true,
    userCanViewDateAndTime: false,
  };

  let event = new CalEvent();
  event.id = "withentry";
  event.calendar = mockCalendar;

  equal(event.aclEntry.userCanModify, itemEntry.userCanModify);
  equal(event.aclEntry.userCanRespond, itemEntry.userCanRespond);
  equal(event.aclEntry.userCanViewAll, itemEntry.userCanViewAll);
  equal(event.aclEntry.userCanViewDateAndTime, itemEntry.userCanViewDateAndTime);

  let parentEntry = new CalEvent();
  parentEntry.id = "parententry";
  parentEntry.calendar = mockCalendar;
  parentEntry.parentItem = event;

  equal(parentEntry.aclEntry.userCanModify, itemEntry.userCanModify);
  equal(parentEntry.aclEntry.userCanRespond, itemEntry.userCanRespond);
  equal(parentEntry.aclEntry.userCanViewAll, itemEntry.userCanViewAll);
  equal(parentEntry.aclEntry.userCanViewDateAndTime, itemEntry.userCanViewDateAndTime);

  event = new CalEvent();
  event.id = "noentry";
  event.calendar = mockCalendar;
  equal(event.aclEntry, null);
}

function test_calendar() {
  let event = new CalEvent();
  let parentEntry = new CalEvent();

  let mockCalendar = {
    QueryInterface: ChromeUtils.generateQI(["calICalendar"]),
    id: "one",
  };

  parentEntry.calendar = mockCalendar;
  event.parentItem = parentEntry;

  notEqual(event.calendar, null);
  equal(event.calendar.id, "one");
}

function test_attachment() {
  let e = new CalEvent();

  let a = new CalAttachment();
  a.rawData = "horst";

  let b = new CalAttachment();
  b.rawData = "bruno";

  e.addAttachment(a);
  equal(e.getAttachments().length, 1);

  e.addAttachment(b);
  equal(e.getAttachments().length, 2);

  e.removeAttachment(a);
  equal(e.getAttachments().length, 1);

  e.removeAllAttachments();
  equal(e.getAttachments().length, 0);
}

function test_attendee() {
  let e = new CalEvent();
  equal(e.getAttendeeById("unknown"), null);
  equal(e.getAttendees().length, 0);

  let a = new CalAttendee();
  a.id = "mailto:horst";

  let b = new CalAttendee();
  b.id = "mailto:bruno";

  e.addAttendee(a);
  equal(e.getAttendees().length, 1);
  equal(e.getAttendeeById("mailto:horst"), a);

  e.addAttendee(b);
  equal(e.getAttendees().length, 2);

  let comp = e.icalComponent;
  let aprop = comp.getFirstProperty("ATTENDEE");
  equal(aprop.value, "mailto:horst");
  aprop = comp.getNextProperty("ATTENDEE");
  equal(aprop.value, "mailto:bruno");
  equal(comp.getNextProperty("ATTENDEE"), null);

  e.removeAttendee(a);
  equal(e.getAttendees().length, 1);
  equal(e.getAttendeeById("mailto:horst"), null);

  e.removeAllAttendees();
  equal(e.getAttendees().length, 0);
}

function test_categories() {
  let e = new CalEvent();

  equal(e.getCategories().length, 0);

  let cat = ["a", "b", "c"];
  e.setCategories(cat);

  cat[0] = "err";
  equal(e.getCategories().join(","), "a,b,c");

  let comp = e.icalComponent;
  let getter = comp.getFirstProperty.bind(comp);

  cat[0] = "a";
  while (cat.length) {
    equal(cat.shift(), getter("CATEGORIES").value);
    getter = comp.getNextProperty.bind(comp);
  }
}

function test_alarm() {
  let e = new CalEvent();
  let alarm = new CalAlarm();

  alarm.action = "DISPLAY";
  alarm.related = Ci.calIAlarm.ALARM_RELATED_ABSOLUTE;
  alarm.alarmDate = cal.createDateTime();

  e.addAlarm(alarm);
  let ecomp = e.icalComponent;
  let vcomp = ecomp.getFirstSubcomponent("VALARM");
  equal(vcomp.serializeToICS(), alarm.icalString);

  let alarm2 = alarm.clone();

  e.addAlarm(alarm2);

  equal(e.getAlarms().length, 2);
  e.deleteAlarm(alarm);
  equal(e.getAlarms().length, 1);
  equal(e.getAlarms()[0], alarm2);

  e.clearAlarms();
  equal(e.getAlarms().length, 0);
}

function test_immutable() {
  let event = new CalEvent();

  let date = cal.createDateTime();
  date.timezone = cal.timezoneService.getTimezone("Europe/Berlin");
  event.alarmLastAck = date;

  let org = new CalAttendee();
  org.id = "one";
  event.organizer = org;

  let alarm = new CalAlarm();
  alarm.action = "DISPLAY";
  alarm.description = "foo";
  alarm.related = Ci.calIAlarm.ALARM_RELATED_START;
  alarm.offset = cal.createDuration("PT1S");
  event.addAlarm(alarm);

  event.setProperty("X-NAME", "X-VALUE");
  event.setPropertyParameter("X-NAME", "X-PARAM", "X-PARAMVAL");

  event.setCategories(["a", "b", "c"]);

  equal(event.alarmLastAck.timezone.tzid, cal.dtz.UTC.tzid);

  event.makeImmutable();

  // call again, should not throw
  event.makeImmutable();

  ok(!event.alarmLastAck.isMutable);
  ok(!org.isMutable);
  ok(!alarm.isMutable);

  throws(() => {
    event.alarmLastAck = cal.createDateTime();
  }, /Can not modify immutable data container/);
  throws(() => {
    event.calendar = null;
  }, /Can not modify immutable data container/);
  throws(() => {
    event.parentItem = null;
  }, /Can not modify immutable data container/);
  throws(() => {
    event.setCategories(["d", "e", "f"]);
  }, /Can not modify immutable data container/);

  let event2 = event.clone();
  event2.organizer.id = "two";

  equal(org.id, "one");
  equal(event2.organizer.id, "two");

  equal(event2.getProperty("X-NAME"), "X-VALUE");
  equal(event2.getPropertyParameter("X-NAME", "X-PARAM"), "X-PARAMVAL");

  event2.setPropertyParameter("X-NAME", "X-PARAM", null);
  equal(event2.getPropertyParameter("X-NAME", "X-PARAM"), null);

  // TODO more clone checks
}

function test_lastack() {
  let e = new CalEvent();

  e.alarmLastAck = cal.createDateTime("20120101T010101");

  // Our items don't support this yet
  //  equal(e.getProperty("X-MOZ-LASTACK"), "20120101T010101");

  let comp = e.icalComponent;
  let prop = comp.getFirstProperty("X-MOZ-LASTACK");

  equal(prop.value, "20120101T010101Z");

  prop.value = "20120101T010102Z";

  e.icalComponent = comp;

  equal(e.alarmLastAck.icalString, "20120101T010102Z");
}

/**
 * Test isEvent() returns the correct value for events and todos.
 */
function test_isEvent() {
  let event = new CalEvent();
  let todo = new CalTodo();

  Assert.ok(event.isEvent(), "isEvent() returns true for events");
  Assert.ok(!todo.isEvent(), "isEvent() returns false for todos");
}

/**
 * Test isTodo() returns the correct value for events and todos.
 */
function test_isTodo() {
  let todo = new CalTodo();
  let event = new CalEvent();

  Assert.ok(todo.isTodo(), "isTodo() returns true for todos");
  Assert.ok(!event.isTodo(), "isTodo() returns false for events");
}

/**
 * Function for testing that the "properties" property of each supplied
 * calItemBase occurrence includes those inherited from the parent.
 *
 * @param {calItemBase[]} items - A list of item occurrences to test.
 * @param {calItemBase} parent - The item to use as the parent.
 * @param {object} [overrides] - A set of key value pairs than can be passed
 *                                 to indicate what to expect for some properties.
 */
function doPropertiesTest(items, parent, overrides = {}) {
  let skippedProps = ["DTSTART", "DTEND"];
  let toString = value =>
    value && value instanceof Ci.calIDateTime ? value.icalString : value && value.toString();

  for (let item of items) {
    info(`Testing occurrence with recurrenceId="${item.recurrenceId.icalString}...`);

    let parentProperties = new Map(parent.properties);
    let itemProperties = new Map(item.properties);
    for (let [name, value] of parentProperties.entries()) {
      if (!skippedProps.includes(name)) {
        if (overrides[name]) {
          Assert.equal(
            toString(itemProperties.get(name)),
            toString(overrides[name]),
            `"${name}" value is value expected by overrides`
          );
        } else {
          Assert.equal(
            toString(itemProperties.get(name)),
            toString(value),
            `"${name}" value is same as parent`
          );
        }
      }
    }
  }
}

/**
 * Test the "properties" property of a recurring CalEvent inherits parent
 * properties properly.
 */
function test_recurring_event_properties() {
  let event = new CalEvent(CalendarTestUtils.dedent`
      BEGIN:VEVENT
      DTSTAMP:20210716T000000Z
      UID:c1a6cfe7-7fbb-4bfb-a00d-861e07c649a5
      SUMMARY:Parent Event
      CATEGORIES:Business
      LOCATION: Mochitest
      DTSTART:20210716T000000Z
      DTEND:20210716T110000Z
      RRULE:FREQ=DAILY;UNTIL=20210719T110000Z
      DESCRIPTION:This is the main event.
      END:VEVENT
    `);
  let occurrences = event.recurrenceInfo.getOccurrences(
    cal.createDateTime("20210701"),
    cal.createDateTime("20210731"),
    Infinity
  );
  doPropertiesTest(occurrences, event.parentItem);
}

/**
 * Test the "properties" property of a recurring CalEvent exception inherits
 * parent properties properly.
 */
function test_recurring_event_exception_properties() {
  let event = new CalEvent(CalendarTestUtils.dedent`
      BEGIN:VEVENT
      DTSTAMP:20210716T000000Z
      UID:c1a6cfe7-7fbb-4bfb-a00d-861e07c649a5
      SUMMARY:Parent Event
      CATEGORIES:Business
      LOCATION: Mochitest
      DTSTART:20210716T000000Z
      DTEND:20210716T110000Z
      RRULE:FREQ=DAILY;UNTIL=20210719T110000Z
      DESCRIPTION:This is the main event.
      END:VEVENT
    `);
  let occurrences = event.recurrenceInfo.getOccurrences(
    cal.createDateTime("20210701"),
    cal.createDateTime("20210731"),
    Infinity
  );
  let target = occurrences[0].clone();
  let newDescription = "This is an exception.";
  target.setProperty("DESCRIPTION", newDescription);
  event.parentItem.recurrenceInfo.modifyException(target);
  target = event.parentItem.recurrenceInfo.getExceptionFor(target.recurrenceId);
  Assert.ok(target);
  doPropertiesTest([target], event.parentItem, { DESCRIPTION: newDescription });
}

/**
 * Test the "properties" property of a recurring CalTodo inherits parent
 * properties properly.
 */
function test_recurring_todo_properties() {
  let task = new CalTodo(CalendarTestUtils.dedent`
      BEGIN:VTODO
      DTSTAMP:20210716T225440Z
      UID:673e125d-fe6b-465d-8a38-9c9373ca9705
      SUMMARY:Main Task
      RRULE:FREQ=DAILY;UNTIL=20210719T230000Z
      DTSTART;TZID=America/Port_of_Spain:20210716T190000
      PERCENT-COMPLETE:0
      LOCATION:Mochitest
      DESCRIPTION:This is the main task.
      END:VTODO
    `);
  let occurrences = task.recurrenceInfo.getOccurrences(
    cal.createDateTime("20210701"),
    cal.createDateTime("20210731"),
    Infinity
  );
  doPropertiesTest(occurrences, task.parentItem);
}

/**
 * Test the "properties" property of a recurring CalTodo exception inherits
 * parent properties properly.
 */
function test_recurring_todo_exception_properties() {
  let task = new CalTodo(CalendarTestUtils.dedent`
      BEGIN:VTODO
      DTSTAMP:20210716T225440Z
      UID:673e125d-fe6b-465d-8a38-9c9373ca9705
      SUMMARY:Main Task
      RRULE:FREQ=DAILY;UNTIL=20210719T230000Z
      DTSTART;TZID=America/Port_of_Spain:20210716T190000
      PERCENT-COMPLETE:0
      LOCATION:Mochitest
      DESCRIPTION:This is the main task.
      END:VTODO
    `);
  let occurrences = task.recurrenceInfo.getOccurrences(
    cal.createDateTime("20210701"),
    cal.createDateTime("20210731"),
    Infinity
  );
  let target = occurrences[0].clone();
  let newDescription = "This is an exception.";
  target.setProperty("DESCRIPTION", newDescription);
  task.parentItem.recurrenceInfo.modifyException(target);
  target = task.parentItem.recurrenceInfo.getExceptionFor(target.recurrenceId);
  Assert.ok(target);
  doPropertiesTest([target], task.parentItem, { DESCRIPTION: newDescription });
}