1 const { PromptTestUtils } = ChromeUtils.importESModule(
2 "resource://testing-common/PromptTestUtils.sys.mjs"
5 function pageScript() {
6 window.addEventListener(
9 var str = "Some text that causes the beforeunload dialog to be shown";
10 event.returnValue = str;
17 SpecialPowers.pushPrefEnv({
18 set: [["dom.require_user_interaction_for_beforeunload", false]],
23 encodeURIComponent("<script>(" + pageScript.toSource() + ")();</script>");
25 add_task(async function enableDialogs() {
26 // The onbeforeunload dialog should appear.
27 let dialogPromise = PromptTestUtils.waitForPrompt(null, {
28 modalType: Services.prompt.MODAL_TYPE_CONTENT,
29 promptType: "confirmEx",
32 let openPagePromise = openPage(true);
33 let dialog = await dialogPromise;
34 Assert.ok(true, "Showed the beforeunload dialog.");
36 await PromptTestUtils.handlePrompt(dialog, { buttonNumClick: 0 });
37 await openPagePromise;
40 add_task(async function disableDialogs() {
41 // The onbeforeunload dialog should NOT appear.
42 await openPage(false);
43 info("If we time out here, then the dialog was shown...");
46 async function openPage(enableDialogs) {
47 // Open about:blank in a new tab.
48 await BrowserTestUtils.withNewTab(
49 { gBrowser, url: "about:blank" },
50 async function (browser) {
52 BrowserTestUtils.startLoadingURIString(browser, PAGE_URL);
53 await BrowserTestUtils.browserLoaded(browser);
54 // Load the content script in the frame.
55 let methodName = enableDialogs ? "enableDialogs" : "disableDialogs";
56 await SpecialPowers.spawn(browser, [methodName], async function (name) {
57 content.windowUtils[name]();
59 // And then navigate away.
60 BrowserTestUtils.startLoadingURIString(browser, "http://example.com/");
61 await BrowserTestUtils.browserLoaded(browser);