summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/mochitest/test_ext_browserAction_openPopup_incognito_window.html
blob: 8036d973982b4ed69bb3587b82a0b15f1ef1419c (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<!DOCTYPE HTML>
<html>
<head>
  <title>action.openPopup Incognito Test</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script type="text/javascript" src="head.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
</head>
<body>

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

let extensionData = {
  manifest: {
    browser_specific_settings: {
      gecko: {
        id: "open-popup@tests.mozilla.org",
      }
    },
    browser_action: {
      default_popup: "popup.html",
    },
    permissions: ["activeTab"]
  },

  useAddonManager: "android-only",
};

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

async function getIncognitoWindow() {
  // Since events will be limited based on incognito, we need a
  // spanning extension to get the tab id so we can test access failure.

  let windowWatcher = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["tabs"],
    },
    background: function() {
      browser.windows.create({ incognito: true }).then(({ id: windowId }) => {
        browser.test.onMessage.addListener(async data => {
          if (data === "close") {
            await browser.windows.remove(windowId);
            browser.test.sendMessage("window-closed");
          }
        });

        browser.test.sendMessage("window-id", windowId);
      });
    },
    incognitoOverride: "spanning",
  });

  await windowWatcher.startup();
  let windowId = await windowWatcher.awaitMessage("window-id");

  return {
    windowId,
    close: async () => {
      windowWatcher.sendMessage("close");
      await windowWatcher.awaitMessage("window-closed");
      await windowWatcher.unload();
    },
  };
}

async function testWithIncognitoOverride(incognitoOverride) {
  let extension = ExtensionTestUtils.loadExtension({
    ...extensionData,

    incognitoOverride,

    background: async function() {
      browser.test.onMessage.addListener(async ({ windowId, incognitoOverride }) => {
        const openPromise = browser.browserAction.openPopup({ windowId });

        if (incognitoOverride === "not_allowed") {
          await browser.test.assertRejects(
            openPromise,
            /Invalid window ID/,
            "Should prevent open popup call for incognito window"
          );
        } else {
          try {
            browser.test.assertEq(await openPromise, undefined, "openPopup resolved");
          } catch (e) {
            browser.test.fail(`Unexpected error: ${e}`);
          }
        }

        browser.test.sendMessage("incognitoWindow");
      });
    },

    files: {
      "popup.html": `<!DOCTYPE html><meta charset="utf-8"><script src="popup.js"><\/script>`,
      "popup.js"() {
        browser.test.sendMessage("popup");
      },
    },
  });

  await extension.startup();

  let incognitoWindow = await getIncognitoWindow();
  await extension.sendMessage({ windowId: incognitoWindow.windowId, incognitoOverride });

  await extension.awaitMessage("incognitoWindow");

  // Wait for the popup to open - bug 1800100
  if (incognitoOverride === "spanning") {
    await extension.awaitMessage("popup");
  }

  await extension.unload();

  await incognitoWindow.close();
}

add_task(async function test_browserAction_openPopup_incognito_window_spanning() {
  if (AppConstants.platform == "android") {
    // TODO bug 1372178: Cannot open private windows from an extension.
    todo(false, "Cannot open private windows on Android");
    return;
  }

  await testWithIncognitoOverride("spanning");
});

add_task(async function test_browserAction_openPopup_incognito_window_not_allowed() {
  if (AppConstants.platform == "android") {
    // TODO bug 1372178: Cannot open private windows from an extension.
    todo(false, "Cannot open private windows on Android");
    return;
  }


  await testWithIncognitoOverride("not_allowed");
});
</script>

</body>
</html>