summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/tabs/browser_removeTabs_skipPermitUnload.js
blob: 1016ead94c0c4f8e2783d21511cea41fe2060b25 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/**
 * Tests for waiting for beforeunload before replacing a session.
 */

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

// The first two urls are intentionally different domains to force pages
// to load in different tabs.
const TEST_PATH = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content",
  "https://example.com"
);
const TEST_URL = "https://example.com/";

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_URL2 = BUILDER_URL + encodeURI(PAGE_MARKUP);

let win;
let nonBeforeUnloadTab;
let beforeUnloadTab;

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

  // Run tests in a new window to avoid affecting the main test window.
  win = await BrowserTestUtils.openNewBrowserWindow();

  BrowserTestUtils.loadURIString(win.gBrowser.selectedBrowser, TEST_URL);
  await BrowserTestUtils.browserLoaded(
    win.gBrowser.selectedBrowser,
    false,
    TEST_URL
  );
  nonBeforeUnloadTab = win.gBrowser.selectedTab;
  beforeUnloadTab = await BrowserTestUtils.openNewForegroundTab(
    win.gBrowser,
    TEST_URL2
  );

  registerCleanupFunction(async () => {
    await BrowserTestUtils.closeWindow(win);
  });
});

add_task(async function test_runBeforeUnloadForTabs() {
  let unloadDialogPromise = PromptTestUtils.handleNextPrompt(
    win,
    {
      modalType: Ci.nsIPrompt.MODAL_TYPE_CONTENT,
      promptType: "confirmEx",
    },
    // Click the cancel button.
    { buttonNumClick: 1 }
  );

  let unloadBlocked = await win.gBrowser.runBeforeUnloadForTabs(
    win.gBrowser.tabs
  );

  await unloadDialogPromise;

  Assert.ok(unloadBlocked, "Should have reported the unload was blocked");
  Assert.equal(win.gBrowser.tabs.length, 2, "Should have left all tabs open");

  unloadDialogPromise = PromptTestUtils.handleNextPrompt(
    win,
    {
      modalType: Ci.nsIPrompt.MODAL_TYPE_CONTENT,
      promptType: "confirmEx",
    },
    // Click the ok button.
    { buttonNumClick: 0 }
  );

  unloadBlocked = await win.gBrowser.runBeforeUnloadForTabs(win.gBrowser.tabs);

  await unloadDialogPromise;

  Assert.ok(!unloadBlocked, "Should have reported the unload was not blocked");
  Assert.equal(win.gBrowser.tabs.length, 2, "Should have left all tabs open");
});

add_task(async function test_skipPermitUnload() {
  let closePromise = BrowserTestUtils.waitForTabClosing(beforeUnloadTab);

  await win.gBrowser.removeAllTabsBut(nonBeforeUnloadTab, {
    animate: false,
    skipPermitUnload: true,
  });

  await closePromise;

  Assert.equal(win.gBrowser.tabs.length, 1, "Should have left one tab open");
});