summaryrefslogtreecommitdiffstats
path: root/remote/cdp/test/browser/page/browser_javascriptDialog_beforeunload.js
diff options
context:
space:
mode:
Diffstat (limited to 'remote/cdp/test/browser/page/browser_javascriptDialog_beforeunload.js')
-rw-r--r--remote/cdp/test/browser/page/browser_javascriptDialog_beforeunload.js54
1 files changed, 54 insertions, 0 deletions
diff --git a/remote/cdp/test/browser/page/browser_javascriptDialog_beforeunload.js b/remote/cdp/test/browser/page/browser_javascriptDialog_beforeunload.js
new file mode 100644
index 0000000000..7cab579e2c
--- /dev/null
+++ b/remote/cdp/test/browser/page/browser_javascriptDialog_beforeunload.js
@@ -0,0 +1,54 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+// Test beforeunload dialog events.
+add_task(async function ({ client, tab }) {
+ info("Allow to trigger onbeforeunload without user interaction");
+ await new Promise(resolve => {
+ const options = {
+ set: [["dom.require_user_interaction_for_beforeunload", false]],
+ };
+ SpecialPowers.pushPrefEnv(options, resolve);
+ });
+
+ const { Page } = client;
+
+ info("Enable the page domain");
+ await Page.enable();
+
+ info("Attach a valid onbeforeunload handler");
+ await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
+ content.onbeforeunload = () => true;
+ });
+
+ info("Trigger the beforeunload again but reject the prompt");
+ const { type } = await triggerBeforeUnload(Page, tab, false);
+ is(type, "beforeunload", "dialog event contains the correct type");
+
+ info("Trigger the beforeunload again and accept the prompt");
+ const onTabClose = BrowserTestUtils.waitForEvent(tab, "TabClose");
+ await triggerBeforeUnload(Page, tab, true);
+
+ info("Wait for the TabClose event");
+ await onTabClose;
+});
+
+function triggerBeforeUnload(Page, tab, accept) {
+ // We use then here because after clicking on the close button, nothing
+ // in the main block of the function will be executed until the prompt
+ // is accepted or rejected. Attaching a then to this promise still works.
+
+ const onDialogOpen = Page.javascriptDialogOpening().then(
+ async dialogEvent => {
+ await Page.handleJavaScriptDialog({ accept });
+ return dialogEvent;
+ }
+ );
+
+ info("Click on the tab close icon");
+ tab.closeButton.click();
+
+ return onDialogOpen;
+}