From 39f4cca70c479f2d8f564b6004cf4f4444ad51bc Mon Sep 17 00:00:00 2001 From: Meg Viar Date: Fri, 6 Jun 2025 19:44:41 +0000 Subject: [PATCH] Bug 1965258 - Clear active infobar when user closes the window the infobar is displayed in before interacting with it a=dmeehan DONTBUILD Original Revision: https://phabricator.services.mozilla.com/D250465 Differential Revision: https://phabricator.services.mozilla.com/D252856 --- .../asrouter/modules/InfoBar.sys.mjs | 11 ++++++++ .../tests/browser/browser_asrouter_infobar.js | 25 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/browser/components/asrouter/modules/InfoBar.sys.mjs b/browser/components/asrouter/modules/InfoBar.sys.mjs index 8372dedc4fd5..71800930cc35 100644 --- a/browser/components/asrouter/modules/InfoBar.sys.mjs +++ b/browser/components/asrouter/modules/InfoBar.sys.mjs @@ -280,6 +280,17 @@ export const InfoBar = { } if (!universalInNewWin) { this._activeInfobar = { message, dispatch }; + // If the window closes before the user interacts with the active infobar, + // clear it + win.addEventListener( + "unload", + () => { + if (InfoBar._activeInfobar?.message === message) { + InfoBar._activeInfobar = null; + } + }, + { once: true } + ); } return notification; diff --git a/browser/components/asrouter/tests/browser/browser_asrouter_infobar.js b/browser/components/asrouter/tests/browser/browser_asrouter_infobar.js index 029ea3fe11aa..c0d44e52c8e9 100644 --- a/browser/components/asrouter/tests/browser/browser_asrouter_infobar.js +++ b/browser/components/asrouter/tests/browser/browser_asrouter_infobar.js @@ -526,3 +526,28 @@ add_task(async function test_dismissable_button_action() { Assert.ok(!infobar.notification, "Infobar was dismissed after button click"); }); + +add_task(async function clear_activeInfobar_on_window_close() { + let message = (await CFRMessageProvider.getMessages()).find( + m => m.id === "INFOBAR_ACTION_86" + ); + Assert.ok(message.id, "Found the message"); + + let dispatchStub = sinon.stub(); + let testWin = await BrowserTestUtils.openNewBrowserWindow(); + let testBrowser = testWin.gBrowser.selectedBrowser; + + await InfoBar.showInfoBarMessage(testBrowser, message, dispatchStub); + Assert.ok( + InfoBar._activeInfobar, + "InfoBar._activeInfobar should be set after showing infobar" + ); + + await BrowserTestUtils.closeWindow(testWin); + await BrowserTestUtils.waitForCondition( + () => !InfoBar._activeInfobar, + "InfoBar._activeInfobar should be cleared when the window unloads" + ); + testWin.close(); + sinon.restore(); +});