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 /browser/base/content/test/general/browser_beforeunload_duplicate_dialogs.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 '')
-rw-r--r-- | browser/base/content/test/general/browser_beforeunload_duplicate_dialogs.js | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/browser/base/content/test/general/browser_beforeunload_duplicate_dialogs.js b/browser/base/content/test/general/browser_beforeunload_duplicate_dialogs.js new file mode 100644 index 0000000000..8a77f01ce4 --- /dev/null +++ b/browser/base/content/test/general/browser_beforeunload_duplicate_dialogs.js @@ -0,0 +1,114 @@ +const TEST_PAGE = + "http://mochi.test:8888/browser/browser/base/content/test/general/file_double_close_tab.html"; + +const CONTENT_PROMPT_SUBDIALOG = Services.prefs.getBoolPref( + "prompts.contentPromptSubDialog", + false +); + +var expectingDialog = false; +var wantToClose = true; +var resolveDialogPromise; + +function onTabModalDialogLoaded(node) { + ok( + !CONTENT_PROMPT_SUBDIALOG, + "Should not be using content prompt subdialogs." + ); + ok(expectingDialog, "Should be expecting this dialog."); + expectingDialog = false; + if (wantToClose) { + // This accepts the dialog, closing it + node.querySelector(".tabmodalprompt-button0").click(); + } else { + // This keeps the page open + node.querySelector(".tabmodalprompt-button1").click(); + } + if (resolveDialogPromise) { + resolveDialogPromise(); + } +} + +function onCommonDialogLoaded(promptWindow) { + ok(CONTENT_PROMPT_SUBDIALOG, "Should be using content prompt subdialogs."); + ok(expectingDialog, "Should be expecting this dialog."); + expectingDialog = false; + let dialog = promptWindow.Dialog; + if (wantToClose) { + // This accepts the dialog, closing it. + dialog.ui.button0.click(); + } else { + // This keeps the page open + dialog.ui.button1.click(); + } + if (resolveDialogPromise) { + resolveDialogPromise(); + } +} + +SpecialPowers.pushPrefEnv({ + set: [["dom.require_user_interaction_for_beforeunload", false]], +}); + +// Listen for the dialog being created +Services.obs.addObserver(onTabModalDialogLoaded, "tabmodal-dialog-loaded"); +Services.obs.addObserver(onCommonDialogLoaded, "common-dialog-loaded"); +registerCleanupFunction(() => { + Services.prefs.clearUserPref("browser.tabs.warnOnClose"); + Services.obs.removeObserver(onTabModalDialogLoaded, "tabmodal-dialog-loaded"); + Services.obs.removeObserver(onCommonDialogLoaded, "common-dialog-loaded"); +}); + +add_task(async function closeLastTabInWindow() { + let newWin = await promiseOpenAndLoadWindow({}, true); + let firstTab = newWin.gBrowser.selectedTab; + await promiseTabLoadEvent(firstTab, TEST_PAGE); + let windowClosedPromise = BrowserTestUtils.domWindowClosed(newWin); + expectingDialog = true; + // close tab: + firstTab.closeButton.click(); + await windowClosedPromise; + ok(!expectingDialog, "There should have been a dialog."); + ok(newWin.closed, "Window should be closed."); +}); + +add_task(async function closeWindowWithMultipleTabsIncludingOneBeforeUnload() { + Services.prefs.setBoolPref("browser.tabs.warnOnClose", false); + let newWin = await promiseOpenAndLoadWindow({}, true); + let firstTab = newWin.gBrowser.selectedTab; + await promiseTabLoadEvent(firstTab, TEST_PAGE); + await promiseTabLoadEvent( + BrowserTestUtils.addTab(newWin.gBrowser), + // eslint-disable-next-line @microsoft/sdl/no-insecure-url + "http://example.com/" + ); + let windowClosedPromise = BrowserTestUtils.domWindowClosed(newWin); + expectingDialog = true; + newWin.BrowserTryToCloseWindow(); + await windowClosedPromise; + ok(!expectingDialog, "There should have been a dialog."); + ok(newWin.closed, "Window should be closed."); + Services.prefs.clearUserPref("browser.tabs.warnOnClose"); +}); + +add_task(async function closeWindoWithSingleTabTwice() { + let newWin = await promiseOpenAndLoadWindow({}, true); + let firstTab = newWin.gBrowser.selectedTab; + await promiseTabLoadEvent(firstTab, TEST_PAGE); + let windowClosedPromise = BrowserTestUtils.domWindowClosed(newWin); + expectingDialog = true; + wantToClose = false; + let firstDialogShownPromise = new Promise((resolve, reject) => { + resolveDialogPromise = resolve; + }); + firstTab.closeButton.click(); + await firstDialogShownPromise; + info("Got initial dialog, now trying again"); + expectingDialog = true; + wantToClose = true; + resolveDialogPromise = null; + firstTab.closeButton.click(); + await windowClosedPromise; + ok(!expectingDialog, "There should have been a dialog."); + ok(newWin.closed, "Window should be closed."); +}); |