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

// Tests allowScriptsToClose option
add_task(async function test_allowScriptsToClose() {
  const files = {
    "dummy.html": "<meta charset=utf-8><script src=close.js></script>",
    "close.js": function () {
      window.close();
      if (!window.closed) {
        browser.test.sendMessage("close-failed");
      }
    },
  };

  function background() {
    browser.test.onMessage.addListener((msg, options) => {
      function listener(_, { status }, { url }) {
        if (status == "complete" && url == options.url) {
          browser.tabs.onUpdated.removeListener(listener);
          browser.tabs.executeScript({ file: "close.js" });
        }
      }
      options.url = browser.runtime.getURL(options.url);
      browser.windows.create(options);
      if (msg === "create+execute") {
        browser.tabs.onUpdated.addListener(listener);
      }
    });
    browser.test.notifyPass();
  }

  const example = "http://example.com/";
  const manifest = { permissions: ["tabs", example] };

  const extension = ExtensionTestUtils.loadExtension({
    files,
    background,
    manifest,
  });
  await SpecialPowers.pushPrefEnv({
    set: [["dom.allow_scripts_to_close_windows", false]],
  });

  await extension.startup();
  await extension.awaitFinish();

  extension.sendMessage("create", { url: "dummy.html" });
  let win = await BrowserTestUtils.waitForNewWindow();
  await BrowserTestUtils.windowClosed(win);
  info("script allowed to close the window");

  extension.sendMessage("create+execute", { url: example });
  win = await BrowserTestUtils.waitForNewWindow();
  await BrowserTestUtils.windowClosed(win);
  info("script allowed to close the window");

  extension.sendMessage("create+execute", {
    url: example,
    allowScriptsToClose: true,
  });
  win = await BrowserTestUtils.waitForNewWindow();
  await BrowserTestUtils.windowClosed(win);
  info("script allowed to close the window");

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