browser_undoCloseById.js (5021B)
1 /* eslint-disable mozilla/no-arbitrary-setTimeout */ 2 "use strict"; 3 4 /** 5 * This test is for the undoCloseById function. 6 */ 7 8 async function openWindow(url) { 9 let win = await promiseNewWindowLoaded(); 10 let flags = Ci.nsIWebNavigation.LOAD_FLAGS_REPLACE_HISTORY; 11 BrowserTestUtils.startLoadingURIString(win.gBrowser.selectedBrowser, url, { 12 flags, 13 }); 14 await promiseBrowserLoaded(win.gBrowser.selectedBrowser, true, url); 15 return win; 16 } 17 18 async function closeWindow(win) { 19 await BrowserTestUtils.closeWindow(win); 20 // Wait 20 ms to allow SessionStorage a chance to register the closed window. 21 await new Promise(resolve => setTimeout(resolve, 20)); 22 } 23 24 function getLastClosedTabData(win) { 25 const closedTabs = SessionStore.getClosedTabData(win); 26 return closedTabs[closedTabs.length - 1]; 27 } 28 29 add_task(async function test_undoCloseById() { 30 // Clear the lists of closed windows and tabs. 31 forgetClosedWindows(); 32 for (const win of SessionStore.getWindows()) { 33 while (SessionStore.getClosedTabCountForWindow(win)) { 34 SessionStore.forgetClosedTab(win, 0); 35 } 36 } 37 SessionStore.resetNextClosedId(); 38 39 // Open a new window. 40 let win = await openWindow("about:robots"); 41 42 // Open and close a tab. 43 await openAndCloseTab(win, "about:mozilla"); 44 is( 45 SessionStore.lastClosedObjectType, 46 "tab", 47 "The last closed object is a tab" 48 ); 49 50 // Record the first closedId created. 51 is(1, SessionStore.getClosedTabCount(), "We have 1 closed tab"); 52 let initialClosedId = SessionStore.getClosedTabDataForWindow(win)[0].closedId; 53 54 // Open and close another window. 55 let win2 = await openWindow("about:mozilla"); 56 await closeWindow(win2); // closedId == initialClosedId + 1 57 is( 58 SessionStore.lastClosedObjectType, 59 "window", 60 "The last closed object is a window" 61 ); 62 63 // Open and close another tab in the first window. 64 await openAndCloseTab(win, "about:robots"); // closedId == initialClosedId + 2 65 is( 66 SessionStore.lastClosedObjectType, 67 "tab", 68 "The last closed object is a tab" 69 ); 70 71 // Undo closing the second tab. 72 let tab = SessionStore.undoCloseById(initialClosedId + 2); 73 await promiseBrowserLoaded(tab.linkedBrowser); 74 is( 75 tab.linkedBrowser.currentURI.spec, 76 "about:robots", 77 "The expected tab was re-opened" 78 ); 79 80 let notTab = SessionStore.undoCloseById(initialClosedId + 2); 81 is(notTab, undefined, "Re-opened tab cannot be unClosed again by closedId"); 82 83 // Now the last closed object should be a window again. 84 is( 85 SessionStore.lastClosedObjectType, 86 "window", 87 "The last closed object is a window" 88 ); 89 90 // Undo closing the first tab. 91 let tab2 = SessionStore.undoCloseById(initialClosedId); 92 await promiseBrowserLoaded(tab2.linkedBrowser); 93 is( 94 tab2.linkedBrowser.currentURI.spec, 95 "about:mozilla", 96 "The expected tab was re-opened" 97 ); 98 99 // Close the two tabs we re-opened. 100 await promiseRemoveTabAndSessionState(tab); // closedId == initialClosedId + 3 101 is( 102 SessionStore.lastClosedObjectType, 103 "tab", 104 "The last closed object is a tab" 105 ); 106 await promiseRemoveTabAndSessionState(tab2); // closedId == initialClosedId + 4 107 is( 108 SessionStore.lastClosedObjectType, 109 "tab", 110 "The last closed object is a tab" 111 ); 112 113 // Open another new window. 114 let win3 = await openWindow("about:mozilla"); 115 116 // Close both windows. 117 await closeWindow(win); // closedId == initialClosedId + 5 118 is( 119 SessionStore.lastClosedObjectType, 120 "window", 121 "The last closed object is a window" 122 ); 123 await closeWindow(win3); // closedId == initialClosedId + 6 124 is( 125 SessionStore.lastClosedObjectType, 126 "window", 127 "The last closed object is a window" 128 ); 129 130 // Undo closing the second window. 131 win = SessionStore.undoCloseById(initialClosedId + 6); 132 await BrowserTestUtils.waitForEvent(win, "load"); 133 134 // Make sure we wait until this window is restored. 135 await BrowserTestUtils.waitForEvent( 136 win.gBrowser.tabContainer, 137 "SSTabRestored" 138 ); 139 140 is( 141 win.gBrowser.selectedBrowser.currentURI.spec, 142 "about:mozilla", 143 "The expected window was re-opened" 144 ); 145 146 let notWin = SessionStore.undoCloseById(initialClosedId + 6); 147 is( 148 notWin, 149 undefined, 150 "Re-opened window cannot be unClosed again by closedId" 151 ); 152 153 // Close the window again. 154 await closeWindow(win); 155 is( 156 SessionStore.lastClosedObjectType, 157 "window", 158 "The last closed object is a window" 159 ); 160 161 // Undo closing the first window. 162 win = SessionStore.undoCloseById(initialClosedId + 5); 163 164 await BrowserTestUtils.waitForEvent(win, "load"); 165 166 // Make sure we wait until this window is restored. 167 await BrowserTestUtils.waitForEvent( 168 win.gBrowser.tabContainer, 169 "SSTabRestored" 170 ); 171 172 is( 173 win.gBrowser.selectedBrowser.currentURI.spec, 174 "about:robots", 175 "The expected window was re-opened" 176 ); 177 178 // Close the window again. 179 await closeWindow(win); 180 is( 181 SessionStore.lastClosedObjectType, 182 "window", 183 "The last closed object is a window" 184 ); 185 });