diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /layout/base/tests/browser_onbeforeunload_only_after_interaction.js | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'layout/base/tests/browser_onbeforeunload_only_after_interaction.js')
-rw-r--r-- | layout/base/tests/browser_onbeforeunload_only_after_interaction.js | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/layout/base/tests/browser_onbeforeunload_only_after_interaction.js b/layout/base/tests/browser_onbeforeunload_only_after_interaction.js new file mode 100644 index 0000000000..b0a577335b --- /dev/null +++ b/layout/base/tests/browser_onbeforeunload_only_after_interaction.js @@ -0,0 +1,75 @@ +const { PromptTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/PromptTestUtils.sys.mjs" +); + +function pageScript() { + window.addEventListener( + "beforeunload", + function (event) { + var str = "Some text that causes the beforeunload dialog to be shown"; + event.returnValue = str; + return str; + }, + true + ); +} + +SpecialPowers.pushPrefEnv({ + set: [["dom.require_user_interaction_for_beforeunload", true]], +}); + +const PAGE_URL = + "data:text/html," + + encodeURIComponent("<script>(" + pageScript.toSource() + ")();</script>"); + +add_task(async function doClick() { + // The onbeforeunload dialog should appear. + let dialogPromise = PromptTestUtils.waitForPrompt(null, { + modalType: Services.prompt.MODAL_TYPE_CONTENT, + promptType: "confirmEx", + }); + + let openPagePromise = openPage(true); + let dialog = await dialogPromise; + Assert.ok(true, "Showed the beforeunload dialog."); + + await PromptTestUtils.handlePrompt(dialog, { buttonNumClick: 0 }); + await openPagePromise; +}); + +add_task(async function noClick() { + // The onbeforeunload dialog should NOT appear. + await openPage(false); + info("If we time out here, then the dialog was shown..."); +}); + +async function openPage(shouldClick) { + // Open about:blank in a new tab. + await BrowserTestUtils.withNewTab( + { gBrowser, url: "about:blank" }, + async function (browser) { + // Load the page. + BrowserTestUtils.startLoadingURIString(browser, PAGE_URL); + await BrowserTestUtils.browserLoaded(browser); + + if (shouldClick) { + await BrowserTestUtils.synthesizeMouse("body", 2, 2, {}, browser); + } + let hasInteractedWith = await SpecialPowers.spawn( + browser, + [""], + function () { + return content.document.userHasInteracted; + } + ); + is( + shouldClick, + hasInteractedWith, + "Click should update document interactivity state" + ); + // And then navigate away. + BrowserTestUtils.startLoadingURIString(browser, "http://example.com/"); + await BrowserTestUtils.browserLoaded(browser); + } + ); +} |