no bug - Import translations from android-l10n r=release a=l10n CLOSED TREE
[gecko.git] / layout / base / tests / browser_disableDialogs_onbeforeunload.js
blobf060d1db515a010fb40a527a19c3a97b68938545
1 const { PromptTestUtils } = ChromeUtils.importESModule(
2   "resource://testing-common/PromptTestUtils.sys.mjs"
3 );
5 function pageScript() {
6   window.addEventListener(
7     "beforeunload",
8     function (event) {
9       var str = "Some text that causes the beforeunload dialog to be shown";
10       event.returnValue = str;
11       return str;
12     },
13     true
14   );
17 SpecialPowers.pushPrefEnv({
18   set: [["dom.require_user_interaction_for_beforeunload", false]],
19 });
21 const PAGE_URL =
22   "data:text/html," +
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",
30   });
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;
38 });
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...");
44 });
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) {
51       // Load the page.
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]();
58       });
59       // And then navigate away.
60       BrowserTestUtils.startLoadingURIString(browser, "http://example.com/");
61       await BrowserTestUtils.browserLoaded(browser);
62     }
63   );