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

add_task(async function test_window_incognito() {
  const url =
    "http://mochi.test:8888/browser/browser/components/extensions/test/browser/file_iframe_document.html";

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["http://mochi.test/"],
    },
    background() {
      let lastFocusedWindowId = null;
      // Catch focus change events to power the test below.
      browser.windows.onFocusChanged.addListener(function listener(
        eventWindowId
      ) {
        lastFocusedWindowId = eventWindowId;
        browser.windows.onFocusChanged.removeListener(listener);
      });

      browser.test.onMessage.addListener(async pbw => {
        browser.test.assertEq(
          browser.windows.WINDOW_ID_NONE,
          lastFocusedWindowId,
          "Focus on private window sends the event, but doesn't reveal windowId (without permissions)"
        );

        await browser.test.assertRejects(
          browser.windows.get(pbw.windowId),
          /Invalid window ID/,
          "should not be able to get incognito window"
        );
        await browser.test.assertRejects(
          browser.windows.remove(pbw.windowId),
          /Invalid window ID/,
          "should not be able to remove incognito window"
        );
        await browser.test.assertRejects(
          browser.windows.getCurrent(),
          /Invalid window/,
          "should not be able to get incognito top window"
        );
        await browser.test.assertRejects(
          browser.windows.getLastFocused(),
          /Invalid window/,
          "should not be able to get incognito focused window"
        );
        await browser.test.assertRejects(
          browser.windows.create({ incognito: true }),
          /Extension does not have permission for incognito mode/,
          "should not be able to create incognito window"
        );
        await browser.test.assertRejects(
          browser.windows.update(pbw.windowId, { focused: true }),
          /Invalid window ID/,
          "should not be able to update incognito window"
        );

        let windows = await browser.windows.getAll();
        browser.test.assertEq(
          1,
          windows.length,
          "unable to get incognito window"
        );

        browser.test.notifyPass("pass");
      });
    },
  });

  await extension.startup();

  // The tests expect the incognito window to be
  // created after the extension is started, so think
  // carefully when moving this line.
  let winData = await getIncognitoWindow(url);

  extension.sendMessage(winData.details);
  await extension.awaitFinish("pass");
  await BrowserTestUtils.closeWindow(winData.win);
  await extension.unload();
});