summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/browser/browser_webapi_addon_listener.js
blob: 3692644714e4c4d3c664abec46543f70d876a260 (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
const TESTPAGE = `${SECURE_TESTROOT}webapi_addon_listener.html`;

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [["extensions.webapi.testing", true]],
  });
});

async function getListenerEvents(browser) {
  let result = await SpecialPowers.spawn(browser, [], async function () {
    return content.document.getElementById("result").textContent;
  });

  return result.split("\n").map(JSON.parse);
}

const RESTARTLESS_ID = "restartless@tests.mozilla.org";
const INSTALL_ID = "install@tests.mozilla.org";
const CANCEL_ID = "cancel@tests.mozilla.org";

let provider = new MockProvider();
provider.createAddons([
  {
    id: RESTARTLESS_ID,
    name: "Restartless add-on",
    operationsRequiringRestart: AddonManager.OP_NEED_RESTART_NONE,
  },
  {
    id: CANCEL_ID,
    name: "Add-on for uninstall cancel",
  },
]);

// Test enable/disable events for restartless
add_task(async function test_restartless() {
  await BrowserTestUtils.withNewTab(TESTPAGE, async function (browser) {
    let addon = await promiseAddonByID(RESTARTLESS_ID);
    is(addon.userDisabled, false, "addon is enabled");

    // disable it
    await addon.disable();
    is(addon.userDisabled, true, "addon was disabled successfully");

    // re-enable it
    await addon.enable();
    is(addon.userDisabled, false, "addon was re-enabled successfuly");

    let events = await getListenerEvents(browser);
    let expected = [
      { id: RESTARTLESS_ID, event: "onDisabling" },
      { id: RESTARTLESS_ID, event: "onDisabled" },
      { id: RESTARTLESS_ID, event: "onEnabling" },
      { id: RESTARTLESS_ID, event: "onEnabled" },
    ];
    Assert.deepEqual(events, expected, "Got expected disable/enable events");
  });
});

// Test install events
add_task(async function test_restartless() {
  await BrowserTestUtils.withNewTab(TESTPAGE, async function (browser) {
    let addon = new MockAddon(
      INSTALL_ID,
      "installme",
      null,
      AddonManager.OP_NEED_RESTART_NONE
    );
    let install = new MockInstall(null, null, addon);

    let installPromise = new Promise(resolve => {
      install.addTestListener({
        onInstallEnded: resolve,
      });
    });

    provider.addInstall(install);
    install.install();

    await installPromise;

    let events = await getListenerEvents(browser);
    let expected = [
      { id: INSTALL_ID, event: "onInstalling" },
      { id: INSTALL_ID, event: "onInstalled" },
    ];
    Assert.deepEqual(events, expected, "Got expected install events");
  });
});

// Test uninstall
add_task(async function test_uninstall() {
  await BrowserTestUtils.withNewTab(TESTPAGE, async function (browser) {
    let addon = await promiseAddonByID(RESTARTLESS_ID);
    isnot(addon, null, "Found add-on for uninstall");

    addon.uninstall();

    let events = await getListenerEvents(browser);
    let expected = [
      { id: RESTARTLESS_ID, event: "onUninstalling" },
      { id: RESTARTLESS_ID, event: "onUninstalled" },
    ];
    Assert.deepEqual(events, expected, "Got expected uninstall events");
  });
});

// Test cancel of uninstall.
add_task(async function test_cancel() {
  await BrowserTestUtils.withNewTab(TESTPAGE, async function (browser) {
    let addon = await promiseAddonByID(CANCEL_ID);
    isnot(addon, null, "Found add-on for cancelling uninstall");

    addon.uninstall();

    let events = await getListenerEvents(browser);
    let expected = [{ id: CANCEL_ID, event: "onUninstalling" }];
    Assert.deepEqual(events, expected, "Got expected uninstalling event");

    addon.cancelUninstall();
    events = await getListenerEvents(browser);
    expected.push({ id: CANCEL_ID, event: "onOperationCancelled" });
    Assert.deepEqual(events, expected, "Got expected cancel event");
  });
});