summaryrefslogtreecommitdiffstats
path: root/comm/calendar/test/unit/test_alarmservice.js
blob: df61dc029b554cb54b26f0f85a68362f24727aa7 (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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/* 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 { TestUtils } = ChromeUtils.importESModule("resource://testing-common/TestUtils.sys.mjs");

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

var EXPECT_NONE = 0;
var EXPECT_FIRED = 1;
var EXPECT_TIMER = 2;

function do_check_xor(a, b, aMessage) {
  return ok((a && !b) || (!a && b), aMessage);
}

var alarmObserver = {
  QueryInterface: ChromeUtils.generateQI(["calIAlarmServiceObserver"]),

  service: null,
  firedMap: {},
  expectedMap: {},
  pendingOps: {},

  onAlarm(aItem, aAlarm) {
    this.firedMap[aItem.hashId] = this.firedMap[aItem.hashId] || {};
    this.firedMap[aItem.hashId][aAlarm.icalString] = true;
  },

  onNotification(item) {},

  onRemoveAlarmsByItem(aItem) {
    if (aItem.hashId in this.firedMap) {
      delete this.firedMap[aItem.hashId];
    }
  },

  onRemoveAlarmsByCalendar() {},

  onAlarmsLoaded(aCalendar) {
    this.checkLoadStatus();
    if (aCalendar.id in this.pendingOps) {
      this.pendingOps[aCalendar.id].call();
    }
  },

  async doOnAlarmsLoaded(aCalendar) {
    this.checkLoadStatus();
    if (
      aCalendar.id in this.service.mLoadedCalendars &&
      this.service.mLoadedCalendars[aCalendar.id]
    ) {
      // the calendar's alarms have already been loaded
    } else {
      await new Promise(resolve => {
        // the calendar hasn't been fully loaded yet, set as a pending operation
        this.pendingOps[aCalendar.id] = resolve;
      });
    }
  },

  getTimer(aCalendarId, aItemId, aAlarmStr) {
    return aCalendarId in this.service.mTimerMap &&
      aItemId in this.service.mTimerMap[aCalendarId] &&
      aAlarmStr in this.service.mTimerMap[aCalendarId][aItemId]
      ? this.service.mTimerMap[aCalendarId][aItemId][aAlarmStr]
      : null;
  },

  expectResult(aCalendar, aItem, aAlarm, aExpected) {
    let expectedAndTitle = {
      expected: aExpected,
      title: aItem.title,
    };
    this.expectedMap[aCalendar.id] = this.expectedMap[aCalendar.id] || {};
    this.expectedMap[aCalendar.id][aItem.hashId] =
      this.expectedMap[aCalendar.id][aItem.hashId] || {};
    this.expectedMap[aCalendar.id][aItem.hashId][aAlarm.icalString] = expectedAndTitle;
  },

  expectOccurrences(aCalendar, aItem, aAlarm, aExpectedArray) {
    // we need to be earlier than the first occurrence
    let date = aItem.startDate.clone();
    date.second -= 1;

    for (let expected of aExpectedArray) {
      let occ = aItem.recurrenceInfo.getNextOccurrence(date);
      occ.QueryInterface(Ci.calIEvent);
      date = occ.startDate;
      this.expectResult(aCalendar, occ, aAlarm, expected);
    }
  },

  checkExpected(aMessage) {
    for (let calId in this.expectedMap) {
      for (let id in this.expectedMap[calId]) {
        for (let icalString in this.expectedMap[calId][id]) {
          let expectedAndTitle = this.expectedMap[calId][id][icalString];
          // if no explicit message has been passed, take the item title
          let message = typeof aMessage == "string" ? aMessage : expectedAndTitle.title;
          // only alarms expected as fired should exist in our fired alarm map
          do_check_xor(
            expectedAndTitle.expected != EXPECT_FIRED,
            id in this.firedMap && icalString in this.firedMap[id],
            message + "; check fired"
          );
          // only alarms expected as timers should exist in the service's timer map
          do_check_xor(
            expectedAndTitle.expected != EXPECT_TIMER,
            !!this.getTimer(calId, id, icalString),
            message + "; check timer"
          );
        }
      }
    }
  },

  checkLoadStatus() {
    for (let calId in this.service.mLoadedCalendars) {
      if (!this.service.mLoadedCalendars[calId]) {
        // at least one calendar hasn't finished loading alarms
        ok(this.service.isLoading);
        return;
      }
    }
    ok(!this.service.isLoading);
  },

  clear() {
    this.firedMap = {};
    this.pendingOps = {};
    this.expectedMap = {};
  },
};

add_setup(async function () {
  do_get_profile();
  await new Promise(resolve =>
    do_calendar_startup(() => {
      alarmObserver.service = Cc["@mozilla.org/calendar/alarm-service;1"].getService(
        Ci.calIAlarmService
      ).wrappedJSObject;
      ok(!alarmObserver.service.mStarted);
      alarmObserver.service.startup(null);
      ok(alarmObserver.service.mStarted);

      // we need to replace the existing observers with our observer
      for (let obs of alarmObserver.service.mObservers.values()) {
        alarmObserver.service.removeObserver(obs);
      }
      alarmObserver.service.addObserver(alarmObserver);
      resolve();
    })
  );
});

function createAlarmFromDuration(aOffset) {
  let alarm = new CalAlarm();

  alarm.related = Ci.calIAlarm.ALARM_RELATED_START;
  alarm.offset = cal.createDuration(aOffset);

  return alarm;
}

function createEventWithAlarm(aCalendar, aStart, aEnd, aOffset, aRRule) {
  let alarm = null;
  let item = new CalEvent();

  item.id = cal.getUUID();
  item.calendar = aCalendar;
  item.startDate = aStart || cal.dtz.now();
  item.endDate = aEnd || cal.dtz.now();
  if (aOffset) {
    alarm = createAlarmFromDuration(aOffset);
    item.addAlarm(alarm);
  }
  if (aRRule) {
    item.recurrenceInfo = new CalRecurrenceInfo(item);
    item.recurrenceInfo.appendRecurrenceItem(cal.createRecurrenceRule(aRRule));
  }
  return [item, alarm];
}

async function addTestItems(aCalendar) {
  let item, alarm;

  // alarm on an item starting more than a month in the past should not fire
  let date = cal.dtz.now();
  date.day -= 32;
  [item, alarm] = createEventWithAlarm(aCalendar, date, date, "P7D");
  item.title = "addTestItems Test 1";
  alarmObserver.expectResult(aCalendar, item, alarm, EXPECT_NONE);
  await aCalendar.addItem(item);

  // alarm 15 minutes ago should fire
  date = cal.dtz.now();
  [item, alarm] = createEventWithAlarm(aCalendar, date, date, "-PT15M");
  item.title = "addTestItems Test 2";
  alarmObserver.expectResult(aCalendar, item, alarm, EXPECT_FIRED);
  await aCalendar.addItem(item);

  // alarm within 6 hours should have a timer set
  [item, alarm] = createEventWithAlarm(aCalendar, date, date, "PT1H");
  item.title = "addTestItems Test 3";
  alarmObserver.expectResult(aCalendar, item, alarm, EXPECT_TIMER);
  await aCalendar.addItem(item);

  // alarm more than 6 hours in the future should not have a timer set
  [item, alarm] = createEventWithAlarm(aCalendar, date, date, "PT7H");
  item.title = "addTestItems Test 4";
  alarmObserver.expectResult(aCalendar, item, alarm, EXPECT_NONE);
  await aCalendar.addItem(item);

  // test multiple alarms on an item
  [item, alarm] = createEventWithAlarm(aCalendar, date, date);
  item.title = "addTestItems Test 5";
  const firedOffsets = [
    ["-PT1H", EXPECT_FIRED],
    ["-PT15M", EXPECT_FIRED],
    ["PT1H", EXPECT_TIMER],
    ["PT7H", EXPECT_NONE],
    ["P7D", EXPECT_NONE],
  ];

  firedOffsets.forEach(([offset, expected]) => {
    alarm = createAlarmFromDuration(offset);
    item.addAlarm(alarm);
    alarmObserver.expectResult(aCalendar, item, alarm, expected);
  });
  await aCalendar.addItem(item);

  // Bug 1344068 - Alarm with lastAck on exception, should take parent lastAck.
  // Alarm 15 minutes ago should fire.
  date = cal.dtz.now();
  [item, alarm] = createEventWithAlarm(aCalendar, date, date, "-PT15M", "RRULE:FREQ=DAILY;COUNT=1");
  item.title = "addTestItems Test 6";

  // Parent item is acknowledged before alarm, so it should fire.
  let lastAck = item.startDate.clone();
  lastAck.hour -= 1;
  item.alarmLastAck = lastAck;

  // Occurrence is acknowledged after alarm (start date), so if the alarm
  // service wrongly uses the exception occurrence then we catch it.
  let occ = item.recurrenceInfo.getOccurrenceFor(item.startDate);
  occ.alarmLastAck = item.startDate.clone();
  item.recurrenceInfo.modifyException(occ, true);

  alarmObserver.expectOccurrences(aCalendar, item, alarm, [EXPECT_FIRED]);
  await aCalendar.addItem(item);

  // daily repeating event starting almost 2 full days ago. The alarms on the first 2 occurrences
  // should fire, and a timer should be set for the next occurrence only
  date = cal.dtz.now();
  date.hour -= 47;
  [item, alarm] = createEventWithAlarm(aCalendar, date, date, "-PT15M", "RRULE:FREQ=DAILY");
  item.title = "addTestItems Test 7";
  alarmObserver.expectOccurrences(aCalendar, item, alarm, [
    EXPECT_FIRED,
    EXPECT_FIRED,
    EXPECT_TIMER,
    EXPECT_NONE,
    EXPECT_NONE,
  ]);
  await aCalendar.addItem(item);

  // monthly repeating event starting 2 months and a day ago. The alarms on the first 2 occurrences
  // should be ignored, the alarm on the next occurrence only should fire.
  // Missing recurrences of the event in particular days of the year generate exceptions to the
  // regular sequence of alarms.
  date = cal.dtz.now();
  let statusAlarmSequences = {
    reg: [EXPECT_NONE, EXPECT_NONE, EXPECT_FIRED, EXPECT_NONE, EXPECT_NONE],
    excep1: [EXPECT_NONE, EXPECT_FIRED, EXPECT_NONE, EXPECT_NONE, EXPECT_NONE],
    excep2: [EXPECT_NONE, EXPECT_NONE, EXPECT_NONE, EXPECT_NONE, EXPECT_NONE],
  };
  let expected = [];
  if (date.day == 1) {
    // Exceptions for missing occurrences on months with 30 days when the event starts on 31st.
    let sequence = [
      "excep1",
      "reg",
      "excep2",
      "excep1",
      "reg",
      "excep1",
      "reg",
      "excep1",
      "reg",
      "excep2",
      "excep1",
      "reg",
    ][date.month];
    expected = statusAlarmSequences[sequence];
  } else if (date.day == 30 && (date.month == 2 || date.month == 3)) {
    // Exceptions for missing occurrences or different start date caused by February.
    let leapYear = date.endOfYear.yearday == 366;
    expected = leapYear ? statusAlarmSequences.reg : statusAlarmSequences.excep1;
  } else if (date.day == 31 && date.month == 2) {
    // Exceptions for missing occurrences caused by February.
    expected = statusAlarmSequences.excep1;
  } else {
    // Regular sequence of alarms expected for all the others days.
    expected = statusAlarmSequences.reg;
  }
  date.month -= 2;
  date.day -= 1;
  [item, alarm] = createEventWithAlarm(aCalendar, date, date, "-PT15M", "RRULE:FREQ=MONTHLY");
  item.title = "addTestItems Test 8";
  alarmObserver.expectOccurrences(aCalendar, item, alarm, expected);
  await aCalendar.addItem(item);
}

async function doModifyItemTest(aCalendar) {
  let item, alarm;

  // begin with item starting before the alarm date range
  let date = cal.dtz.now();
  date.day -= 32;
  [item, alarm] = createEventWithAlarm(aCalendar, date, date, "PT0S");
  await aCalendar.addItem(item);
  alarmObserver.expectResult(aCalendar, item, alarm, EXPECT_NONE);
  alarmObserver.checkExpected("doModifyItemTest Test 1");

  // move event into the fired range
  let oldItem = item.clone();
  date.day += 31;
  item.startDate = date.clone();
  item.generation++;
  await aCalendar.modifyItem(item, oldItem);
  alarmObserver.expectResult(aCalendar, item, alarm, EXPECT_FIRED);
  alarmObserver.checkExpected("doModifyItemTest Test 2");

  // move event into the timer range
  oldItem = item.clone();
  date.hour += 25;
  item.startDate = date.clone();
  item.generation++;
  await aCalendar.modifyItem(item, oldItem);
  alarmObserver.expectResult(aCalendar, item, alarm, EXPECT_TIMER);
  alarmObserver.checkExpected("doModifyItemTest Test 3");

  // move event past the timer range
  oldItem = item.clone();
  date.hour += 6;
  item.startDate = date.clone();
  item.generation++;
  await aCalendar.modifyItem(item, oldItem);
  alarmObserver.expectResult(aCalendar, item, alarm, EXPECT_NONE);
  alarmObserver.checkExpected("doModifyItemTest Test 4");

  // re-move the event in the timer range and verify that the timer
  // doesn't change when the timezone changes to floating (bug 1300493).
  oldItem = item.clone();
  date.hour -= 6;
  item.startDate = date.clone();
  item.generation++;
  await aCalendar.modifyItem(item, oldItem);
  alarmObserver.expectResult(aCalendar, item, alarm, EXPECT_TIMER);
  alarmObserver.checkExpected("doModifyItemTest Test 5");
  let oldTimer = alarmObserver.getTimer(aCalendar.id, item.hashId, alarm.icalString);
  oldItem = item.clone();
  // change the timezone to floating
  item.startDate.timezone = cal.dtz.floating;
  item.generation++;
  await aCalendar.modifyItem(item, oldItem);
  // the alarm must still be timer and with the same value (apart from milliseconds)
  alarmObserver.expectResult(aCalendar, item, alarm, EXPECT_TIMER);
  alarmObserver.checkExpected("doModifyItemTest Test 5, floating timezone");
  let newTimer = alarmObserver.getTimer(aCalendar.id, item.hashId, alarm.icalString);
  ok(
    newTimer.delay - oldTimer.delay <= 1000,
    "doModifyItemTest Test 5, floating timezone; check timer value"
  );
}

async function doDeleteItemTest(aCalendar) {
  alarmObserver.clear();
  let item, alarm;
  let item2, alarm2;

  // create a fired alarm and a timer
  let date = cal.dtz.now();
  [item, alarm] = createEventWithAlarm(aCalendar, date, date, "-PT5M");
  [item2, alarm2] = createEventWithAlarm(aCalendar, date, date, "PT1H");
  item.title = "doDeleteItemTest item Test 1";
  item2.title = "doDeleteItemTest item2 Test 1";
  await aCalendar.addItem(item);
  await aCalendar.addItem(item2);
  alarmObserver.expectResult(aCalendar, item, alarm, EXPECT_FIRED);
  alarmObserver.expectResult(aCalendar, item2, alarm2, EXPECT_TIMER);
  alarmObserver.checkExpected();

  // item deletion should clear the fired alarm and timer
  await aCalendar.deleteItem(item);
  await aCalendar.deleteItem(item2);
  alarmObserver.expectResult(aCalendar, item, alarm, EXPECT_NONE);
  alarmObserver.expectResult(aCalendar, item2, alarm2, EXPECT_NONE);
  alarmObserver.checkExpected("doDeleteItemTest, cleared fired alarm and timer");
}

async function doAcknowledgeTest(aCalendar) {
  alarmObserver.clear();
  let item, alarm;
  let item2, alarm2;

  // create the fired alarms
  let date = cal.dtz.now();
  [item, alarm] = createEventWithAlarm(aCalendar, date, date, "-PT5M");
  [item2, alarm2] = createEventWithAlarm(aCalendar, date, date, "-PT5M");
  item.title = "doAcknowledgeTest item Test 1";
  item2.title = "doAcknowledgeTest item2 Test 1";
  await aCalendar.addItem(item);
  await aCalendar.addItem(item2);
  alarmObserver.expectResult(aCalendar, item, alarm, EXPECT_FIRED);
  alarmObserver.expectResult(aCalendar, item2, alarm2, EXPECT_FIRED);
  alarmObserver.checkExpected();

  // test snooze alarm
  alarmObserver.service.snoozeAlarm(item, alarm, cal.createDuration("PT1H"));
  alarmObserver.expectResult(aCalendar, item, alarm, EXPECT_TIMER);
  alarmObserver.checkExpected("doAcknowledgeTest, test snooze alarm");

  // the snoozed alarm timer delay should be close to an hour
  let tmr = alarmObserver.getTimer(aCalendar.id, item.hashId, alarm.icalString);
  ok(
    Math.abs(tmr.delay - 3600000) <= 1000,
    "doAcknowledgeTest, snoozed alarm timer delay close to an hour"
  );

  // test dismiss alarm
  alarmObserver.service.dismissAlarm(item2, alarm2);
  alarmObserver.expectResult(aCalendar, item2, alarm2, EXPECT_NONE);
  alarmObserver.checkExpected("doAcknowledgeTest, test dismiss alarm");
}

async function doRunTest(aOnCalendarCreated) {
  alarmObserver.clear();

  let memory = cal.manager.createCalendar("memory", Services.io.newURI("moz-memory-calendar://"));
  memory.id = cal.getUUID();

  if (aOnCalendarCreated) {
    await aOnCalendarCreated(memory);
  }

  cal.manager.registerCalendar(memory);
  await alarmObserver.doOnAlarmsLoaded(memory);
  return memory;
}

/**
 * Test the initial alarm loading of a calendar with existing data.
 */
add_task(async function test_loadCalendar() {
  await doRunTest(async memory => addTestItems(memory));
  alarmObserver.checkExpected();
});

/**
 * Test adding alarm data to a calendar already registered.
 */
add_task(async function test_addItems() {
  let memory = await doRunTest();
  await addTestItems(memory);
  alarmObserver.checkExpected();
});

/**
 * Test response to modification of alarm data.
 */
add_task(async function test_modifyItems() {
  let memory = await doRunTest();
  await doModifyItemTest(memory);
  await doDeleteItemTest(memory);
  await doAcknowledgeTest(memory);
});

/**
 * Test an array of timers has expected delay values.
 *
 * @param {nsITimer[]} timers - An array of nsITimer.
 * @param {number[]} expected - Expected delays in seconds.
 */
function matchTimers(timers, expected) {
  let delays = timers.map(timer => timer.delay / 1000);
  let matched = true;
  for (let i = 0; i < delays.length; i++) {
    if (Math.abs(delays[i] - expected[i]) > 2) {
      matched = false;

      break;
    }
  }
  ok(matched, `Delays=${delays} should match Expected=${expected}`);
}

/**
 * Test notification timers are set up correctly when add/modify/remove a
 * calendar item.
 */
add_task(async function test_notificationTimers() {
  let memory = await doRunTest();
  // Add an item.
  let date = cal.dtz.now();
  date.hour += 1;
  let item, oldItem;
  [item] = createEventWithAlarm(memory, date, date, null);
  await memory.addItem(item);
  equal(
    alarmObserver.service.mNotificationTimerMap[item.calendar.id],
    undefined,
    "should have no notification timer"
  );

  // Set the pref to have one notifiaction.
  Services.prefs.setCharPref("calendar.notifications.times", "-PT1H");
  oldItem = item.clone();
  date.hour += 1;
  item.startDate = date.clone();
  item.generation++;
  await memory.modifyItem(item, oldItem);
  // Should have one notification timer
  matchTimers(alarmObserver.service.mNotificationTimerMap[item.calendar.id][item.hashId], [3600]);

  // Set the pref to have three notifiactions.
  Services.prefs.setCharPref("calendar.notifications.times", "END:PT2M,PT0M,END:-PT30M,-PT5M");
  oldItem = item.clone();
  date.hour -= 1;
  item.startDate = date.clone();
  date.hour += 1;
  item.endDate = date.clone();
  item.generation++;
  await memory.modifyItem(item, oldItem);
  // Should have four notification timers.
  matchTimers(alarmObserver.service.mNotificationTimerMap[item.calendar.id][item.hashId], [
    3300, // 55 minutes
    3600, // 60 minutes
    5400, // 90 minutes, which is 30 minutes before the end (END:-PT30M)
    7320, // 122 minutes, which is 2 minutes after the end (END:PT2M)
  ]);

  alarmObserver.service.removeFiredNotificationTimer(item);
  // Should have three notification timers.
  matchTimers(
    alarmObserver.service.mNotificationTimerMap[item.calendar.id][item.hashId],
    [3600, 5400, 7320]
  );

  await memory.deleteItem(item);
  equal(
    alarmObserver.service.mNotificationTimerMap[item.calendar.id],
    undefined,
    "notification timers should be removed"
  );

  Services.prefs.clearUserPref("calendar.notifications.times");
});

/**
 * Test notification timers are set up correctly according to the calendar level
 * notifications.times config.
 */
add_task(async function test_calendarLevelNotificationTimers() {
  let loaded = false;
  let item;
  let memory = await doRunTest();

  if (!loaded) {
    loaded = true;
    // Set the global pref to have one notifiaction.
    Services.prefs.setCharPref("calendar.notifications.times", "-PT1H");

    // Add an item.
    let date = cal.dtz.now();
    date.hour += 2;
    [item] = createEventWithAlarm(memory, date, date, null);
    await memory.addItem(item);

    // Should have one notification timer.
    matchTimers(alarmObserver.service.mNotificationTimerMap[item.calendar.id][item.hashId], [3600]);
    // Set the calendar level pref to have two notification timers.
    memory.setProperty("notifications.times", "-PT5M,PT0M");
  }

  await TestUtils.waitForCondition(
    () => alarmObserver.service.mNotificationTimerMap[item.calendar.id]?.[item.hashId].length == 2
  );
  // Should have two notification timers
  matchTimers(alarmObserver.service.mNotificationTimerMap[item.calendar.id][item.hashId], [
    6900, // 105 minutes
    7200, // 120 minutes
  ]);

  Services.prefs.clearUserPref("calendar.notifications.times");
});

registerCleanupFunction(() => {
  Services.prefs.clearUserPref("calendar.notifications.times");
});