browser_closed_tabs_windows.js (9541B)
1 const ORIG_STATE = ss.getBrowserState(); 2 3 const multiWindowState = { 4 windows: [ 5 { 6 tabs: [ 7 { 8 entries: [ 9 { 10 url: "https://example.com#win-0-tab-0", 11 triggeringPrincipal_base64, 12 }, 13 ], 14 }, 15 ], 16 _closedTabs: [ 17 { 18 state: { 19 entries: [ 20 { 21 url: "https://example.com#win-0-closed-0", 22 triggeringPrincipal_base64, 23 }, 24 ], 25 }, 26 }, 27 { 28 state: { 29 entries: [ 30 { 31 url: "https://example.com#win-0-closed-1", 32 triggeringPrincipal_base64, 33 }, 34 ], 35 }, 36 }, 37 ], 38 selected: 1, 39 }, 40 { 41 tabs: [ 42 { 43 entries: [ 44 { 45 url: "https://example.org#win-1-tab-0", 46 triggeringPrincipal_base64, 47 }, 48 ], 49 }, 50 { 51 entries: [ 52 { 53 url: "https://example.org#win-1-tab-1", 54 triggeringPrincipal_base64, 55 }, 56 ], 57 }, 58 ], 59 _closedTabs: [ 60 { 61 state: { 62 entries: [ 63 { 64 url: "https://example.org#win-1-closed-0", 65 triggeringPrincipal_base64, 66 }, 67 ], 68 }, 69 }, 70 ], 71 selected: 1, 72 }, 73 ], 74 _closedWindows: [ 75 { 76 tabs: [ 77 { 78 entries: [ 79 { 80 url: "https://example.org#closedWin-0-tab-0", 81 triggeringPrincipal_base64, 82 }, 83 ], 84 }, 85 ], 86 _closedTabs: [ 87 { 88 state: { 89 entries: [ 90 { 91 url: "https://example.org##closedWin-0-closed-0", 92 triggeringPrincipal_base64, 93 }, 94 ], 95 }, 96 }, 97 ], 98 }, 99 ], 100 }; 101 102 add_setup(async function testSetup() { 103 await SessionStoreTestUtils.promiseBrowserState(multiWindowState); 104 }); 105 106 add_task(async function test_ClosedTabMethods() { 107 let sessionStoreUpdated; 108 const browserWindows = BrowserWindowTracker.orderedWindows; 109 Assert.equal( 110 browserWindows.length, 111 multiWindowState.windows.length, 112 `We expect ${multiWindowState.windows} open browser windows` 113 ); 114 info( 115 `After setup, current tab URI: ${browserWindows[0].gBrowser.currentURI.spec}` 116 ); 117 Assert.equal( 118 browserWindows[0].gBrowser.currentURI.spec, 119 "https://example.com/#win-0-tab-0", 120 "The first tab of the first window we restored is current" 121 ); 122 123 for (let idx = 0; idx < browserWindows.length; idx++) { 124 const win = browserWindows[idx]; 125 const winData = multiWindowState.windows[idx]; 126 info(`window ${idx}: ${win.gBrowser.selectedBrowser.currentURI.spec}`); 127 Assert.equal( 128 winData._closedTabs.length, 129 SessionStore.getClosedTabDataForWindow(win).length, 130 `getClosedTabDataForWindow() for open window ${idx} returned the expected number of objects` 131 ); 132 } 133 134 let closedCount; 135 closedCount = SessionStore.getClosedTabCountForWindow(browserWindows[0]); 136 Assert.equal(2, closedCount, "2 closed tab for this window"); 137 138 closedCount = SessionStore.getClosedTabCountForWindow(browserWindows[1]); 139 Assert.equal(1, closedCount, "1 closed tab for this window"); 140 141 closedCount = SessionStore.getClosedTabCount(); 142 // 3 closed tabs from open windows, 1 closed tab from the closed window 143 Assert.equal(4, closedCount, "4 closed tab for all windows"); 144 145 let allWindowsClosedTabs = SessionStore.getClosedTabData(); 146 Assert.equal( 147 SessionStore.getClosedTabCount({ closedTabsFromClosedWindows: false }), 148 allWindowsClosedTabs.length, 149 "getClosedTabData returned the correct number of entries" 150 ); 151 for (let tabData of allWindowsClosedTabs) { 152 Assert.ok(tabData.sourceWindowId, "each tab has a sourceWindowId property"); 153 } 154 155 closedCount = SessionStore.getClosedTabCountFromClosedWindows(); 156 Assert.equal(1, closedCount, "1 closed tabs from closed windows"); 157 158 // *********************************** 159 info("check with the all-windows pref off"); 160 await SpecialPowers.pushPrefEnv({ 161 set: [["browser.sessionstore.closedTabsFromAllWindows", false]], 162 }); 163 164 info("With this pref off, the closed tab count is that of the top window"); 165 closedCount = SessionStore.getClosedTabCount({ 166 closedTabsFromClosedWindows: false, 167 }); // Equivalent to SS.getClosedTabCountForWindow(browserWindows[0]), 168 Assert.equal(closedCount, 2, `2 closed tabs from the top (first) window`); 169 Assert.equal( 170 SessionStore.getClosedTabCount(), // includes closed tabs from closed windows, but only the top open window 171 3, 172 `3 closed tabs when counting the top window and the closed window` 173 ); 174 175 allWindowsClosedTabs = SessionStore.getClosedTabData(); // Equivalent to SS.getClosedTabDataForWindow(browserWindows[0]), 176 Assert.equal( 177 allWindowsClosedTabs.length, 178 2, 179 "getClosedTabData returned the number of entries for the top window" 180 ); 181 SpecialPowers.popPrefEnv(); 182 await TestUtils.waitForTick(); 183 // 184 // *********************************** 185 186 closedCount = SessionStore.getClosedTabCount(); 187 Assert.equal( 188 4, 189 closedCount, 190 "Sanity-check, with the pref on, we're back to 4 closed tabs" 191 ); 192 193 // *********************************** 194 info("check with the closed-tabs-from-closed-windows pref off"); 195 await SpecialPowers.pushPrefEnv({ 196 set: [["browser.sessionstore.closedTabsFromClosedWindows", false]], 197 }); 198 199 info( 200 "With this pref off, the closed tab count is that of only the only windows" 201 ); 202 closedCount = SessionStore.getClosedTabCount(); // Equivalent to SS.getClosedTabCountForWindow(browserWindows[0]), 203 Assert.equal(3, closedCount, `3 closed tabs from the open windows`); 204 205 allWindowsClosedTabs = SessionStore.getClosedTabData(); 206 Assert.equal( 207 allWindowsClosedTabs.length, 208 3, 209 "getClosedTabData returned the number of entries for the top window" 210 ); 211 SpecialPowers.popPrefEnv(); 212 await TestUtils.waitForTick(); 213 // 214 // *********************************** 215 216 info("forget the closed tab from the closed window"); 217 sessionStoreUpdated = TestUtils.topicObserved( 218 "sessionstore-closed-objects-changed" 219 ); 220 SessionStore.forgetClosedTab( 221 { sourceClosedId: SessionStore.getClosedWindowData()[0].closedId }, 222 0 223 ); 224 await sessionStoreUpdated; 225 226 closedCount = SessionStore.getClosedTabCountFromClosedWindows(); 227 Assert.equal( 228 0, 229 closedCount, 230 "0 closed tabs from closed windows after forgetting them" 231 ); 232 closedCount = SessionStore.getClosedTabCount(); 233 Assert.equal( 234 3, 235 closedCount, 236 "3 closed tabs now that the closed tab from the closed window is forgotten" 237 ); 238 239 info("restore one of the closed tabs from an open window"); 240 sessionStoreUpdated = TestUtils.topicObserved( 241 "sessionstore-closed-objects-changed" 242 ); 243 244 SessionStore.undoCloseTab(browserWindows[0], 0); 245 await sessionStoreUpdated; 246 247 Assert.equal( 248 1, 249 SessionStore.getClosedTabCountForWindow(browserWindows[0]), 250 "Now theres one closed tab in the first window" 251 ); 252 Assert.equal( 253 1, 254 SessionStore.getClosedTabCountForWindow(browserWindows[1]), 255 "Theres still one closed tab in the 2nd window" 256 ); 257 258 Assert.equal( 259 2, 260 SessionStore.getClosedTabCount(), 261 "Theres a total for 2 closed tabs" 262 ); 263 264 // Bug 1836198 - sometimes this reports 1 not 2. 265 Assert.equal( 266 2, 267 SessionStore.getClosedTabData().length, 268 "We get the right number of tab entries from getClosedTabData()" 269 ); 270 271 info("forget the last closed tab in the first window"); 272 sessionStoreUpdated = TestUtils.topicObserved( 273 "sessionstore-closed-objects-changed" 274 ); 275 SessionStore.forgetClosedTab(browserWindows[0], 0); 276 await sessionStoreUpdated; 277 278 Assert.equal( 279 0, 280 SessionStore.getClosedTabCountForWindow(browserWindows[0]), 281 "The first window has 0 closed tabs after forgetting the last tab" 282 ); 283 Assert.equal( 284 1, 285 SessionStore.getClosedTabCountForWindow(browserWindows[1]), 286 "Theres still one closed tab in the 2nd window" 287 ); 288 289 Assert.equal( 290 1, 291 SessionStore.getClosedTabCount(), 292 "Theres a total of one closed tabs" 293 ); 294 Assert.equal( 295 1, 296 SessionStore.getClosedTabData().length, 297 "We get the right number of tab entries from getClosedTabData()" 298 ); 299 300 info( 301 "Close the 2nd window. This makes its 1 closed tab a 'closed tab from closed window'" 302 ); 303 await promiseAllButPrimaryWindowClosed(); 304 305 Assert.equal( 306 0, 307 SessionStore.getClosedTabCountForWindow(browserWindows[0]), 308 "Closed tab count is unchanged after closing the other browser window" 309 ); 310 311 Assert.equal( 312 0, 313 SessionStore.getClosedTabCount({ closedTabsFromClosedWindows: false }), 314 "Theres now 0 closed tabs from open windows after closing the other browser window which had the last one" 315 ); 316 317 Assert.equal( 318 1, 319 SessionStore.getClosedTabCount({ closedTabsFromClosedWindows: true }), 320 "Theres now 1 closed tabs including closed windows after closing the other browser window which had the last one" 321 ); 322 323 Assert.equal( 324 0, 325 SessionStore.getClosedTabData().length, 326 "We get the right number of tab entries from getClosedTabData()" 327 ); 328 329 Assert.equal( 330 1, 331 SessionStore.getClosedTabCountFromClosedWindows(), 332 "There's 1 closed tabs from closed windows" 333 ); 334 335 // Cleanup. 336 await promiseBrowserState(ORIG_STATE); 337 });