browser_undoCloseById_targetWindow.js (3029B)
1 "use strict"; 2 3 /** 4 * This test verifies SessionStore.undoCloseById behavior when passed the targetWindow argument 5 */ 6 7 async function openWindow(url) { 8 let win = await promiseNewWindowLoaded(); 9 let flags = Ci.nsIWebNavigation.LOAD_FLAGS_REPLACE_HISTORY; 10 BrowserTestUtils.startLoadingURIString(win.gBrowser.selectedBrowser, url, { 11 flags, 12 }); 13 await promiseBrowserLoaded(win.gBrowser.selectedBrowser, true, url); 14 return win; 15 } 16 17 async function closeWindow(win) { 18 TestUtils.waitForTick(); 19 let sessionStoreUpdated = TestUtils.topicObserved( 20 "sessionstore-closed-objects-changed" 21 ); 22 await BrowserTestUtils.closeWindow(win); 23 await sessionStoreUpdated; 24 } 25 26 function forgetTabsAndWindows() { 27 // Clear the lists of closed windows and tabs. 28 forgetClosedWindows(); 29 while (SessionStore.getClosedTabCount(window)) { 30 SessionStore.forgetClosedTab(window, 0); 31 } 32 } 33 34 add_task(async function test_undoCloseById_with_targetWindow() { 35 forgetTabsAndWindows(); 36 // Test that a tab closed in (currently open) window B, will correctly be opened in target window A. 37 // And that the closed record should be correctly removed from window B 38 const winA = window; 39 // Open a new window. 40 const winB = await openWindow("about:robots"); 41 await SimpleTest.promiseFocus(winB); 42 // Open and close a tab in the 2nd window 43 await openAndCloseTab(winB, "about:mozilla"); 44 is( 45 SessionStore.lastClosedObjectType, 46 "tab", 47 "The last closed object is a tab" 48 ); 49 // Record the first closedId created. 50 const closedId = SessionStore.getClosedTabData(winB)[0].closedId; 51 let tabRestored = BrowserTestUtils.waitForNewTab( 52 winA.gBrowser, 53 "about:mozilla" 54 ); 55 56 // Restore the tab into the first window, not the window it was closed in 57 SessionStore.undoCloseById(closedId, undefined, winA); 58 await tabRestored; 59 is(winA.gBrowser.selectedBrowser.currentURI.spec, "about:mozilla"); 60 61 // Verify the closed tab data is removed from the source window 62 is( 63 SessionStore.getClosedTabData(winB).length, 64 0, 65 "Record removed from the source window's closed tab data" 66 ); 67 68 BrowserTestUtils.removeTab(winA.gBrowser.selectedTab); 69 await closeWindow(winB); 70 }); 71 72 add_task(async function test_undoCloseById_with_nonExistent_targetWindow() { 73 // Test that restoring a tab to a non-existent targetWindow throws 74 forgetTabsAndWindows(); 75 await openAndCloseTab(window, "about:mozilla"); 76 is( 77 SessionStore.lastClosedObjectType, 78 "tab", 79 "The last closed object is a tab" 80 ); 81 // Record the first closedId created. 82 const closedId = SessionStore.getClosedTabData(window)[0].closedId; 83 84 // get a reference to a window that will be closed 85 const newWin = await BrowserTestUtils.openNewBrowserWindow(); 86 await SimpleTest.promiseFocus(newWin); 87 await BrowserTestUtils.closeWindow(newWin); 88 89 // Expect an exception trying to restore a tab to a non-existent window 90 Assert.throws(() => { 91 SessionStore.undoCloseById(closedId, undefined, newWin); 92 }, /NS_ERROR_ILLEGAL_VALUE/); 93 });