"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