diff --git a/browser/base/content/test/tabPrompts/browser.ini b/browser/base/content/test/tabPrompts/browser.ini index f869dc4da1d5..b5e8361e18aa 100644 --- a/browser/base/content/test/tabPrompts/browser.ini +++ b/browser/base/content/test/tabPrompts/browser.ini @@ -4,6 +4,7 @@ support-files = file_beforeunload_stop.html skip-if = verify && debug && (os == 'linux') [browser_confirmFolderUpload.js] [browser_contentOrigins.js] +support-files = file_beforeunload_stop.html [browser_multiplePrompts.js] [browser_openPromptInBackgroundTab.js] support-files = openPromptOffTimeout.html diff --git a/browser/base/content/test/tabPrompts/browser_contentOrigins.js b/browser/base/content/test/tabPrompts/browser_contentOrigins.js index 792d40a630ff..68b396aa575d 100644 --- a/browser/base/content/test/tabPrompts/browser_contentOrigins.js +++ b/browser/base/content/test/tabPrompts/browser_contentOrigins.js @@ -7,16 +7,18 @@ const { PromptTestUtils } = ChromeUtils.import( "resource://testing-common/PromptTestUtils.jsm" ); +const TEST_ROOT = getRootDirectory(gTestPath).replace( + "chrome://mochitests/content", + "https://example.com" +); + async function checkAlert( pageToLoad, expectedTitle, expectedIcon = "chrome://global/skin/icons/defaultFavicon.svg" ) { - return BrowserTestUtils.withNewTab(pageToLoad, async browser => { - let promptPromise = PromptTestUtils.waitForPrompt(browser, { - modalType: Ci.nsIPrompt.MODAL_TYPE_CONTENT, - }); - let spawnPromise = SpecialPowers.spawn(browser, [], () => { + function openFn(browser) { + return SpecialPowers.spawn(browser, [], () => { if (content.document.nodePrincipal.isSystemPrincipal) { // Can't eval in privileged contexts due to CSP, just call directly: content.alert("Test"); @@ -25,6 +27,33 @@ async function checkAlert( content.eval("alert('Test')"); } }); + } + return checkDialog(pageToLoad, openFn, expectedTitle, expectedIcon); +} + +async function checkBeforeunload( + pageToLoad, + expectedTitle, + expectedIcon = "chrome://global/skin/icons/defaultFavicon.svg" +) { + async function openFn(browser) { + let tab = gBrowser.getTabForBrowser(browser); + await BrowserTestUtils.synthesizeMouseAtCenter( + "body", + {}, + browser.browsingContext + ); + return gBrowser.removeTab(tab); // trigger beforeunload. + } + return checkDialog(pageToLoad, openFn, expectedTitle, expectedIcon); +} + +async function checkDialog(pageToLoad, openFn, expectedTitle, expectedIcon) { + return BrowserTestUtils.withNewTab(pageToLoad, async browser => { + let promptPromise = PromptTestUtils.waitForPrompt(browser, { + modalType: Ci.nsIPrompt.MODAL_TYPE_CONTENT, + }); + let spawnPromise = openFn(browser); let dialog = await promptPromise; let doc = dialog.ui.prompt.document; @@ -116,4 +145,8 @@ add_task(async function test_check_prompt_origin_display() { { l10nId: "common-dialog-title-system" }, "chrome://branding/content/icon32.png" ); + + await checkBeforeunload(TEST_ROOT + "file_beforeunload_stop.html", { + value: "example.com", + }); }); diff --git a/toolkit/components/prompts/src/Prompter.jsm b/toolkit/components/prompts/src/Prompter.jsm index 4b2e2b24efbe..12ccc50665cc 100644 --- a/toolkit/components/prompts/src/Prompter.jsm +++ b/toolkit/components/prompts/src/Prompter.jsm @@ -1147,6 +1147,8 @@ class ModalPrompter { if (windowUtils) { windowUtils.enterModalState(); } + } else if (args.inPermitUnload) { + args.promptPrincipal = this.browsingContext.currentWindowGlobal.documentPrincipal; } // It is technically possible for multiple prompts to be sent from a single