summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_alarms_replaces.js
blob: 96f61acb57fa054f71e6a9ea5b87e652c4ee1df6 (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";


add_task(function* test_duplicate_alarm_name_replaces_alarm() {
  function backgroundScript() {
    let count = 0;

    browser.alarms.onAlarm.addListener(async alarm => {
      if (alarm.name === "master alarm") {
        browser.alarms.create("child alarm", {delayInMinutes: 0.05});
        let results = await browser.alarms.getAll();

        browser.test.assertEq(2, results.length, "exactly two alarms exist");
        browser.test.assertEq("master alarm", results[0].name, "first alarm has the expected name");
        browser.test.assertEq("child alarm", results[1].name, "second alarm has the expected name");

        if (count++ === 3) {
          await browser.alarms.clear("master alarm");
          await browser.alarms.clear("child alarm");

          browser.test.notifyPass("alarm-duplicate");
        }
      } else {
        browser.test.fail("duplicate named alarm replaced existing alarm");
        browser.test.notifyFail("alarm-duplicate");
      }
    });

    browser.alarms.create("master alarm", {delayInMinutes: 0.025, periodInMinutes: 0.025});
  }

  let extension = ExtensionTestUtils.loadExtension({
    background: `(${backgroundScript})()`,
    manifest: {
      permissions: ["alarms"],
    },
  });

  yield extension.startup();
  yield extension.awaitFinish("alarm-duplicate");
  yield extension.unload();
});