summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/mochitest/test_ext_webnavigation_incognito.html
blob: 45147365ee65f4faba011cb92ba379abb6796fab (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for simple WebExtension</title>
  <meta charset="utf-8">
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <script type="text/javascript" src="head.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>

<script type="text/javascript">
"use strict";

add_task(async function webnav_test_incognito() {
  // Monitor will fail if it gets any event.
  let monitor = ExtensionTestUtils.loadExtension({
    manifest: {
      "permissions": ["webNavigation", "*://mochi.test/*"],
    },
    background() {
      const EVENTS = [
        "onTabReplaced",
        "onBeforeNavigate",
        "onCommitted",
        "onDOMContentLoaded",
        "onCompleted",
        "onErrorOccurred",
        "onReferenceFragmentUpdated",
        "onHistoryStateUpdated",
      ];

      function onEvent(event, details) {
        browser.test.fail(`not_allowed - Got ${event} ${details.url} ${details.frameId} ${details.parentFrameId}`);
      }

      let listeners = {};
      for (let event of EVENTS) {
        listeners[event] = onEvent.bind(null, event);
        browser.webNavigation[event].addListener(listeners[event]);
      }

      browser.test.onMessage.addListener(async (message, tabId) => {
        // try to access the private window
        await browser.test.assertRejects(browser.webNavigation.getAllFrames({tabId}),
                                         /Invalid tab ID/,
                                         "should not be able to get incognito frames");
        await browser.test.assertRejects(browser.webNavigation.getFrame({tabId, frameId: 0}),
                                         /Invalid tab ID/,
                                         "should not be able to get incognito frames");
        browser.test.notifyPass("completed");
      });
    },
  });

  // extension loads a private window and waits for the onCompleted event.
  let extension = ExtensionTestUtils.loadExtension({
    incognitoOverride: "spanning",
    manifest: {
      permissions: ["tabs", "webNavigation", "*://mochi.test/*"],
    },
    async background() {
      const BASE = "http://mochi.test:8888/tests/toolkit/components/extensions/test/mochitest";
      const url = BASE + "/file_WebNavigation_page1.html";
      let window;

      browser.webNavigation.onCompleted.addListener(async (details) => {
        if (details.url !== url) {
          return;
        }
        browser.test.log(`spanning - Got onCompleted ${details.url} ${details.frameId} ${details.parentFrameId}`);
        browser.test.sendMessage("completed");
      });
      browser.test.onMessage.addListener(async () => {
        await browser.windows.remove(window.id);
        browser.test.notifyPass("done");
      });
      window = await browser.windows.create({url, incognito: true});
      let tabs = await browser.tabs.query({active: true, windowId: window.id});
      browser.test.sendMessage("tabId", tabs[0].id);
    },
  });

  await monitor.startup();
  await extension.startup();

  await extension.awaitMessage("completed");
  let tabId = await extension.awaitMessage("tabId");

  await monitor.sendMessage("tab", tabId);
  await monitor.awaitFinish("completed");

  await extension.sendMessage("close");
  await extension.awaitFinish("done");

  await extension.unload();
  await monitor.unload();
});

</script>

</body>
</html>