summaryrefslogtreecommitdiffstats
path: root/toolkit/components/contextualidentity/tests/browser/browser_closeContainerTabs.js
blob: 16fa28e30639b8f6b13aa5745d0d16ce106b8b92 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { PromptTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/PromptTestUtils.sys.mjs"
);

const BUILDER_URL = "https://example.com/document-builder.sjs?html=";
const PAGE_MARKUP = `
<html>
<head>
  <script>
    window.onbeforeunload = function() {
      return true;
    };
  </script>
</head>
<body>TEST PAGE</body>
</html>
`;
const TEST_URL = BUILDER_URL + encodeURI(PAGE_MARKUP);

add_task(async function test_skipPermitUnload() {
  await SpecialPowers.pushPrefEnv({
    set: [["dom.require_user_interaction_for_beforeunload", false]],
  });

  info("Create a test user context");
  const userContextId = ContextualIdentityService.create("test").userContextId;

  // Run tests in a new window to avoid affecting the main test window.
  const win = await BrowserTestUtils.openNewBrowserWindow();
  registerCleanupFunction(async () => {
    await BrowserTestUtils.closeWindow(win);
  });

  info("Create a tab owned by the test user context");
  const tab = await BrowserTestUtils.addTab(win.gBrowser, TEST_URL, {
    userContextId,
  });
  registerCleanupFunction(() => win.gBrowser.removeTab(tab));
  await BrowserTestUtils.browserLoaded(tab.linkedBrowser);

  info("Call closeContainerTabs without skipPermitUnload");
  const unloadDialogPromise = PromptTestUtils.handleNextPrompt(
    tab.linkedBrowser,
    {
      modalType: Ci.nsIPrompt.MODAL_TYPE_CONTENT,
      promptType: "confirmEx",
    },
    // Click the cancel button.
    { buttonNumClick: 1 }
  );

  const tabCountBeforeClose = win.gBrowser.tabs.length;
  ContextualIdentityService.closeContainerTabs(userContextId);

  info("Wait for the unload dialog");
  await unloadDialogPromise;
  is(
    win.gBrowser.tabs.length,
    tabCountBeforeClose,
    "Tab was not closed because of the before unload prompt"
  );

  info("Call closeContainerTabs with skipPermitUnload");
  const closePromise = BrowserTestUtils.waitForTabClosing(tab);
  ContextualIdentityService.closeContainerTabs(userContextId, {
    skipPermitUnload: true,
  });

  info("Wait for the tab to be closed");
  await closePromise;
  is(
    win.gBrowser.tabs.length,
    tabCountBeforeClose - 1,
    "Tab was closed after ignoring the before unload prompt"
  );
});