diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /browser/components/sessionstore/test/browser_closed_objects_changed_notifications_windows.js | |
parent | Initial commit. (diff) | |
download | firefox-esr-upstream.tar.xz firefox-esr-upstream.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'browser/components/sessionstore/test/browser_closed_objects_changed_notifications_windows.js')
-rw-r--r-- | browser/components/sessionstore/test/browser_closed_objects_changed_notifications_windows.js | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/browser/components/sessionstore/test/browser_closed_objects_changed_notifications_windows.js b/browser/components/sessionstore/test/browser_closed_objects_changed_notifications_windows.js new file mode 100644 index 0000000000..9f2a24793d --- /dev/null +++ b/browser/components/sessionstore/test/browser_closed_objects_changed_notifications_windows.js @@ -0,0 +1,129 @@ +"use strict"; + +/** + * This test is for the sessionstore-closed-objects-changed notifications. + */ + +requestLongerTimeout(2); + +const MAX_WINDOWS_UNDO_PREF = "browser.sessionstore.max_windows_undo"; +const TOPIC = "sessionstore-closed-objects-changed"; + +let notificationsCount = 0; + +async function openWindow(url) { + let win = await promiseNewWindowLoaded(); + let flags = Ci.nsIWebNavigation.LOAD_FLAGS_REPLACE_HISTORY; + BrowserTestUtils.loadURIString(win.gBrowser.selectedBrowser, url, { flags }); + await promiseBrowserLoaded(win.gBrowser.selectedBrowser, true, url); + return win; +} + +async function closeWindow(win) { + await awaitNotification(() => BrowserTestUtils.closeWindow(win)); +} + +async function openAndCloseWindow(url) { + let win = await openWindow(url); + await closeWindow(win); +} + +function countingObserver() { + notificationsCount++; +} + +function assertNotificationCount(count) { + is( + notificationsCount, + count, + "The expected number of notifications was received." + ); +} + +async function awaitNotification(callback) { + let notification = TestUtils.topicObserved(TOPIC); + executeSoon(callback); + await notification; +} + +add_task(async function test_closedObjectsChangedNotifications() { + // Create a closed window so that when we do the purge we know to expect a notification + await openAndCloseWindow("about:robots"); + + // Forget any previous closed windows or tabs from other tests that may have + // run in the same session. + await awaitNotification(() => + Services.obs.notifyObservers(null, "browser:purge-session-history") + ); + + // Add an observer to count the number of notifications. + Services.obs.addObserver(countingObserver, TOPIC); + + info("Opening and closing initial window."); + await openAndCloseWindow("about:robots"); + assertNotificationCount(1); + + // Store state with a single closed window for use in later tests. + let closedState = Cu.cloneInto(JSON.parse(ss.getBrowserState()), {}); + + info("Undoing close of initial window."); + let win = SessionStore.undoCloseWindow(0); + await promiseDelayedStartupFinished(win); + assertNotificationCount(2); + + // Open a second window. + let win2 = await openWindow("about:mozilla"); + + info("Closing both windows."); + await closeWindow(win); + assertNotificationCount(3); + await closeWindow(win2); + assertNotificationCount(4); + + info(`Changing the ${MAX_WINDOWS_UNDO_PREF} pref.`); + registerCleanupFunction(function () { + Services.prefs.clearUserPref(MAX_WINDOWS_UNDO_PREF); + }); + await awaitNotification(() => + Services.prefs.setIntPref(MAX_WINDOWS_UNDO_PREF, 1) + ); + assertNotificationCount(5); + + info("Forgetting a closed window."); + await awaitNotification(() => SessionStore.forgetClosedWindow()); + assertNotificationCount(6); + + info("Opening and closing another window."); + await openAndCloseWindow("about:robots"); + assertNotificationCount(7); + + info("Setting browser state to trigger change onIdleDaily."); + let state = Cu.cloneInto(JSON.parse(ss.getBrowserState()), {}); + state._closedWindows[0].closedAt = 1; + await promiseBrowserState(state); + assertNotificationCount(8); + + info("Sending idle-daily"); + await awaitNotification(() => + Services.obs.notifyObservers(null, "idle-daily") + ); + assertNotificationCount(9); + + info("Opening and closing another window."); + await openAndCloseWindow("about:robots"); + assertNotificationCount(10); + + info("Purging session history."); + await awaitNotification(() => + Services.obs.notifyObservers(null, "browser:purge-session-history") + ); + assertNotificationCount(11); + + info("Setting window state."); + win = await openWindow("about:mozilla"); + await awaitNotification(() => SessionStore.setWindowState(win, closedState)); + assertNotificationCount(12); + + Services.obs.removeObserver(countingObserver, TOPIC); + await BrowserTestUtils.closeWindow(win); +}); |