From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- dom/tests/browser/browser_hasbeforeunload.js | 875 +++++++++++++++++++++++++++ 1 file changed, 875 insertions(+) create mode 100644 dom/tests/browser/browser_hasbeforeunload.js (limited to 'dom/tests/browser/browser_hasbeforeunload.js') diff --git a/dom/tests/browser/browser_hasbeforeunload.js b/dom/tests/browser/browser_hasbeforeunload.js new file mode 100644 index 0000000000..de86a1ea7a --- /dev/null +++ b/dom/tests/browser/browser_hasbeforeunload.js @@ -0,0 +1,875 @@ +"use strict"; + +const PAGE_URL = + "http://example.com/browser/dom/tests/browser/beforeunload_test_page.html"; + +/** + * Adds 1 or more inert beforeunload event listeners in this browser. + * By default, will target the top-level content window, but callers + * can specify the index of a subframe to target. See prepareSubframes + * for an idea of how the subframes are structured. + * + * @param {} browser + * The browser to add the beforeunload event listener in. + * @param {int} howMany + * How many beforeunload event listeners to add. Note that these + * beforeunload event listeners are inert and will not actually + * prevent the host window from navigating. + * @param {optional int} frameDepth + * The depth of the frame to add the event listener to. Defaults + * to 0, which is the top-level content window. + * @return {Promise} + */ +function addBeforeUnloadListeners(browser, howMany = 1, frameDepth = 0) { + return controlFrameAt(browser, frameDepth, { + name: "AddBeforeUnload", + howMany, + }); +} + +/** + * Adds 1 or more inert beforeunload event listeners in this browser on + * a particular subframe. By default, this will target the first subframe + * under the top-level content window, but callers can specify the index + * of a subframe to target. See prepareSubframes for an idea of how the + * subframes are structured. + * + * Note that this adds the beforeunload event listener on the "outer" window, + * by doing: + * + * iframe.addEventListener("beforeunload", ...); + * + * @param {} browser + * The browser to add the beforeunload event listener in. + * @param {int} howMany + * How many beforeunload event listeners to add. Note that these + * beforeunload event listeners are inert and will not actually + * prevent the host window from navigating. + * @param {optional int} frameDepth + * The depth of the frame to add the event listener to. Defaults + * to 1, which is the first subframe inside the top-level content + * window. Setting this to 0 will throw. + * @return {Promise} + */ +function addOuterBeforeUnloadListeners(browser, howMany = 1, frameDepth = 1) { + if (frameDepth == 0) { + throw new Error( + "When adding a beforeunload listener on an outer " + + "window, the frame you're targeting needs to be at " + + "depth > 0." + ); + } + + return controlFrameAt(browser, frameDepth, { + name: "AddOuterBeforeUnload", + howMany, + }); +} + +/** + * Removes 1 or more inert beforeunload event listeners in this browser. + * This assumes that addBeforeUnloadListeners has been called previously + * for the target frame. + * + * By default, will target the top-level content window, but callers + * can specify the index of a subframe to target. See prepareSubframes + * for an idea of how the subframes are structured. + * + * @param {} browser + * The browser to remove the beforeunload event listener from. + * @param {int} howMany + * How many beforeunload event listeners to remove. + * @param {optional int} frameDepth + * The depth of the frame to remove the event listener from. Defaults + * to 0, which is the top-level content window. + * @return {Promise} + */ +function removeBeforeUnloadListeners(browser, howMany = 1, frameDepth = 0) { + return controlFrameAt(browser, frameDepth, { + name: "RemoveBeforeUnload", + howMany, + }); +} + +/** + * Removes 1 or more inert beforeunload event listeners in this browser on + * a particular subframe. By default, this will target the first subframe + * under the top-level content window, but callers can specify the index + * of a subframe to target. See prepareSubframes for an idea of how the + * subframes are structured. + * + * Note that this removes the beforeunload event listener on the "outer" window, + * by doing: + * + * iframe.removeEventListener("beforeunload", ...); + * + * @param {} browser + * The browser to remove the beforeunload event listener from. + * @param {int} howMany + * How many beforeunload event listeners to remove. + * @param {optional int} frameDepth + * The depth of the frame to remove the event listener from. Defaults + * to 1, which is the first subframe inside the top-level content + * window. Setting this to 0 will throw. + * @return {Promise} + */ +function removeOuterBeforeUnloadListeners( + browser, + howMany = 1, + frameDepth = 1 +) { + if (frameDepth == 0) { + throw new Error( + "When removing a beforeunload listener from an outer " + + "window, the frame you're targeting needs to be at " + + "depth > 0." + ); + } + + return controlFrameAt(browser, frameDepth, { + name: "RemoveOuterBeforeUnload", + howMany, + }); +} + +/** + * Navigates a content window to a particular URL and waits for it to + * finish loading that URL. + * + * By default, will target the top-level content window, but callers + * can specify the index of a subframe to target. See prepareSubframes + * for an idea of how the subframes are structured. + * + * @param {} browser + * The browser that will have the navigation occur within it. + * @param {string} url + * The URL to send the content window to. + * @param {optional int} frameDepth + * The depth of the frame to navigate. Defaults to 0, which is + * the top-level content window. + * @return {Promise} + */ +function navigateSubframe(browser, url, frameDepth = 0) { + let navigatePromise = controlFrameAt(browser, frameDepth, { + name: "Navigate", + url, + }); + let subframeLoad = BrowserTestUtils.browserLoaded(browser, true); + return Promise.all([navigatePromise, subframeLoad]); +} + +/** + * Removes the