browser_closed_objects_changed_notifications_windows.js (3870B)
1 "use strict"; 2 3 /** 4 * This test is for the sessionstore-closed-objects-changed notifications. 5 */ 6 7 requestLongerTimeout(2); 8 9 const MAX_WINDOWS_UNDO_PREF = "browser.sessionstore.max_windows_undo"; 10 const TOPIC = "sessionstore-closed-objects-changed"; 11 12 let notificationsCount = 0; 13 14 async function openWindow(url) { 15 let win = await promiseNewWindowLoaded(); 16 let flags = Ci.nsIWebNavigation.LOAD_FLAGS_REPLACE_HISTORY; 17 BrowserTestUtils.startLoadingURIString(win.gBrowser.selectedBrowser, url, { 18 flags, 19 }); 20 await promiseBrowserLoaded(win.gBrowser.selectedBrowser, true, url); 21 return win; 22 } 23 24 async function closeWindow(win) { 25 await awaitNotification(() => BrowserTestUtils.closeWindow(win)); 26 } 27 28 async function openAndCloseWindow(url) { 29 let win = await openWindow(url); 30 await closeWindow(win); 31 } 32 33 function countingObserver() { 34 notificationsCount++; 35 } 36 37 function assertNotificationCount(count) { 38 is( 39 notificationsCount, 40 count, 41 "The expected number of notifications was received." 42 ); 43 } 44 45 async function awaitNotification(callback) { 46 let notification = TestUtils.topicObserved(TOPIC); 47 executeSoon(callback); 48 await notification; 49 } 50 51 add_task(async function test_closedObjectsChangedNotifications() { 52 // Create a closed window so that when we do the purge we know to expect a notification 53 await openAndCloseWindow("about:robots"); 54 55 // Forget any previous closed windows or tabs from other tests that may have 56 // run in the same session. 57 await awaitNotification(() => 58 Services.obs.notifyObservers(null, "browser:purge-session-history") 59 ); 60 61 // Add an observer to count the number of notifications. 62 Services.obs.addObserver(countingObserver, TOPIC); 63 64 info("Opening and closing initial window."); 65 await openAndCloseWindow("about:robots"); 66 assertNotificationCount(1); 67 68 // Store state with a single closed window for use in later tests. 69 let closedState = Cu.cloneInto(JSON.parse(ss.getBrowserState()), {}); 70 71 info("Undoing close of initial window."); 72 let win = SessionStore.undoCloseWindow(0); 73 await promiseDelayedStartupFinished(win); 74 assertNotificationCount(2); 75 76 // Open a second window. 77 let win2 = await openWindow("about:mozilla"); 78 79 info("Closing both windows."); 80 await closeWindow(win); 81 assertNotificationCount(3); 82 await closeWindow(win2); 83 assertNotificationCount(4); 84 85 info(`Changing the ${MAX_WINDOWS_UNDO_PREF} pref.`); 86 registerCleanupFunction(function () { 87 Services.prefs.clearUserPref(MAX_WINDOWS_UNDO_PREF); 88 }); 89 await awaitNotification(() => 90 Services.prefs.setIntPref(MAX_WINDOWS_UNDO_PREF, 1) 91 ); 92 assertNotificationCount(5); 93 94 info("Forgetting a closed window."); 95 await awaitNotification(() => SessionStore.forgetClosedWindow()); 96 assertNotificationCount(6); 97 98 info("Opening and closing another window."); 99 await openAndCloseWindow("about:robots"); 100 assertNotificationCount(7); 101 102 info("Setting browser state to trigger change onIdleDaily."); 103 let state = Cu.cloneInto(JSON.parse(ss.getBrowserState()), {}); 104 state._closedWindows[0].closedAt = 1; 105 await promiseBrowserState(state); 106 assertNotificationCount(8); 107 108 info("Sending idle-daily"); 109 await awaitNotification(() => 110 Services.obs.notifyObservers(null, "idle-daily") 111 ); 112 assertNotificationCount(9); 113 114 info("Opening and closing another window."); 115 await openAndCloseWindow("about:robots"); 116 assertNotificationCount(10); 117 118 info("Purging session history."); 119 await awaitNotification(() => 120 Services.obs.notifyObservers(null, "browser:purge-session-history") 121 ); 122 assertNotificationCount(11); 123 124 info("Setting window state."); 125 win = await openWindow("about:mozilla"); 126 await awaitNotification(() => SessionStore.setWindowState(win, closedState)); 127 assertNotificationCount(12); 128 129 Services.obs.removeObserver(countingObserver, TOPIC); 130 await BrowserTestUtils.closeWindow(win); 131 });