summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/browser/browser_ext_webNavigation_eventpage.js
blob: d898cb96a4a2a8da5e600b040459957374aa5eb7 (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
"use strict";

add_task(async function webnav_test_eventpage() {
  await SpecialPowers.pushPrefEnv({
    set: [["extensions.eventPages.enabled", true]],
  });

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["webNavigation", "*://mochi.test/*"],
      background: { persistent: false },
    },
    background() {
      const EVENTS = [
        "onTabReplaced",
        "onBeforeNavigate",
        "onCommitted",
        "onDOMContentLoaded",
        "onCompleted",
        "onErrorOccurred",
        "onReferenceFragmentUpdated",
        "onHistoryStateUpdated",
      ];

      for (let event of EVENTS) {
        browser.webNavigation[event].addListener(() => {});
      }
      browser.test.sendMessage("ready");
    },
  });

  // onTabReplaced is never persisted, it is an empty event handler.
  const EVENTS = [
    "onBeforeNavigate",
    "onCommitted",
    "onDOMContentLoaded",
    "onCompleted",
    "onErrorOccurred",
    "onReferenceFragmentUpdated",
    "onHistoryStateUpdated",
  ];

  await extension.startup();
  await extension.awaitMessage("ready");
  for (let event of EVENTS) {
    assertPersistentListeners(extension, "webNavigation", event, {
      primed: false,
    });
  }

  await extension.terminateBackground();
  for (let event of EVENTS) {
    assertPersistentListeners(extension, "webNavigation", event, {
      primed: true,
    });
  }

  // wake up the background, we don't really care which event does it,
  // we're just verifying the state after.
  let newWin = await BrowserTestUtils.openNewBrowserWindow();
  await extension.awaitMessage("ready");
  for (let event of EVENTS) {
    assertPersistentListeners(extension, "webNavigation", event, {
      primed: false,
    });
  }

  await BrowserTestUtils.closeWindow(newWin);

  await extension.unload();
  await SpecialPowers.popPrefEnv();
});