tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit f4fb3c2ef4e6383f19b633ede50187a8fdf17cf9
parent 4ce984884b6a1e75d5395cc77eff68079851671b
Author: Stephen Thompson <sthompson@mozilla.com>
Date:   Mon,  3 Nov 2025 21:03:31 +0000

Bug 1997422 - clean up Session Restore state after every mochitest browser test module r=sessionstore-reviewers,nsharpley

Browser tests are intended to run from a specific starting point: a window with a fresh tab. We generally do cleanup in each test module to make sure that we return to that state.

Session Restore keeps track of a number of pieces of browser state beyond the scope of a starting window and fresh tab:
- closed tabs in the test window
- closed windows
- closed tab groups in the test window
- saved tab groups

This patch will automatically clean these up after each test module so that the side effects from previous tests won't affect future test modules.

Also fixes up the test browser_662812.js which appears to have been passing only because previous tests left closed tabs that could be reopened.

Differential Revision: https://phabricator.services.mozilla.com/D270729

Diffstat:
Mbrowser/components/sessionstore/test/browser_662812.js | 22++++++++++------------
Mtesting/mochitest/browser-test.js | 37+++++++++++++++++++++++++++++++++++++
2 files changed, 47 insertions(+), 12 deletions(-)

diff --git a/browser/components/sessionstore/test/browser_662812.js b/browser/components/sessionstore/test/browser_662812.js @@ -1,18 +1,18 @@ /* Any copyright is dedicated to the Public Domain. http://creativecommons.org/publicdomain/zero/1.0/ */ -function test() { +async function test() { waitForExplicitFinish(); window.addEventListener( "SSWindowStateBusy", - function () { + () => { let state = ss.getWindowState(window); ok(state.windows[0].busy, "window is busy"); window.addEventListener( "SSWindowStateReady", - function () { + () => { let state2 = ss.getWindowState(window); ok(!state2.windows[0].busy, "window is not busy"); @@ -30,14 +30,12 @@ function test() { // create a new tab let tab = BrowserTestUtils.addTab(gBrowser, "about:mozilla"); let browser = tab.linkedBrowser; - - // close and restore it - browser.addEventListener( - "load", - function () { - gBrowser.removeTab(tab); - ss.undoCloseTab(window, 0); - }, - { capture: true, once: true } + await BrowserTestUtils.browserLoaded(browser); + await TabStateFlusher.flush(browser); + const sessionStoreClosedObjectsChanged = TestUtils.topicObserved( + "sessionstore-closed-objects-changed" ); + gBrowser.removeTab(tab); + await sessionStoreClosedObjectsChanged; + ss.undoCloseTab(window, 0); } diff --git a/testing/mochitest/browser-test.js b/testing/mochitest/browser-test.js @@ -788,6 +788,41 @@ Tester.prototype = { } }, + /** + * In the event that a test module left any traces in Session Restore state, + * clean those up so that each test module starts execution with the same + * fresh Session Restore state. + */ + resetSessionState() { + // Forget all closed windows. + while (window.SessionStore.getClosedWindowCount() > 0) { + window.SessionStore.forgetClosedWindow(0); + } + + // Forget all closed tabs for the test window. + const closedTabCount = + window.SessionStore.getClosedTabCountForWindow(window); + for (let i = 0; i < closedTabCount; i++) { + try { + window.SessionStore.forgetClosedTab(window, 0); + } catch (err) { + // This will fail if there are tab groups in here + } + } + + // Forget saved tab groups. + const savedTabGroups = window.SessionStore.getSavedTabGroups(); + savedTabGroups.forEach(tabGroup => + window.SessionStore.forgetSavedTabGroup(tabGroup.id) + ); + + // Forget closed tab groups in the test window. + const closedTabGroups = window.SessionStore.getClosedTabGroups(window); + closedTabGroups.forEach(tabGroup => + window.SessionStore.forgetClosedTabGroup(window, tabGroup.id) + ); + }, + async notifyProfilerOfTestEnd() { // Note the test run time let name = this.currentTest.path; @@ -950,6 +985,8 @@ Tester.prototype = { window.SpecialPowers.cleanupAllClipboard(); + this.resetSessionState(); + if (gConfig.cleanupCrashes) { let gdir = Services.dirsvc.get("UAppData", Ci.nsIFile); gdir.append("Crash Reports");