diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /layout/base/tests/browser_onbeforeunload_only_after_interaction_in_frame.js | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'layout/base/tests/browser_onbeforeunload_only_after_interaction_in_frame.js')
-rw-r--r-- | layout/base/tests/browser_onbeforeunload_only_after_interaction_in_frame.js | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/layout/base/tests/browser_onbeforeunload_only_after_interaction_in_frame.js b/layout/base/tests/browser_onbeforeunload_only_after_interaction_in_frame.js new file mode 100644 index 0000000000..6f73a2ece2 --- /dev/null +++ b/layout/base/tests/browser_onbeforeunload_only_after_interaction_in_frame.js @@ -0,0 +1,96 @@ +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], + ["security.allow_eval_with_system_principal", true], + ], +}); + +const FRAME_URL = + "data:text/html," + encodeURIComponent("<body>Just a frame</body>"); + +const PAGE_URL = + "data:text/html," + + encodeURIComponent( + "<iframe src='" + + FRAME_URL + + "'></iframe><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.loadURIString(browser, PAGE_URL); + await BrowserTestUtils.browserLoaded(browser); + + let frameBC = browser.browsingContext.children[0]; + if (shouldClick) { + await BrowserTestUtils.synthesizeMouse("body", 2, 2, {}, frameBC); + } + let hasInteractedWith = await SpecialPowers.spawn( + frameBC, + [], + function () { + return [ + content.document.userHasInteracted, + content.document.userHasInteracted, + ]; + } + ); + is( + shouldClick, + hasInteractedWith[0], + "Click should update parent interactivity state" + ); + is( + shouldClick, + hasInteractedWith[1], + "Click should update frame interactivity state" + ); + // And then navigate away. + BrowserTestUtils.loadURIString(browser, "http://example.com/"); + await BrowserTestUtils.browserLoaded(browser); + } + ); +} |