browser_tab_groups_closed.js (5508B)
1 "use strict"; 2 3 const lazy = {}; 4 5 ChromeUtils.defineESModuleGetters(lazy, { 6 TabGroupTestUtils: "resource://testing-common/TabGroupTestUtils.sys.mjs", 7 }); 8 9 const ORIG_STATE = SessionStore.getBrowserState(); 10 11 add_task(async function test_closingTabGroupAddsClosedGroup() { 12 let win = await promiseNewWindowLoaded(); 13 14 let tabs = [ 15 BrowserTestUtils.addTab(win.gBrowser, "https://example.com"), 16 BrowserTestUtils.addTab(win.gBrowser, "https://example.com"), 17 BrowserTestUtils.addTab(win.gBrowser, "https://example.com"), 18 ]; 19 let group = win.gBrowser.addTabGroup(tabs); 20 await Promise.all( 21 tabs.map(async t => { 22 await BrowserTestUtils.browserLoaded(t.linkedBrowser); 23 await TabStateFlusher.flush(t.linkedBrowser); 24 }) 25 ); 26 27 let windowState = ss.getWindowState(win).windows[0]; 28 Assert.equal( 29 windowState.closedGroups.length, 30 0, 31 "Window state starts with no closed groups" 32 ); 33 34 await lazy.TabGroupTestUtils.removeTabGroup(group); 35 36 windowState = ss.getWindowState(win).windows[0]; 37 Assert.equal( 38 windowState.closedGroups.length, 39 1, 40 "Window state gains a closed group" 41 ); 42 Assert.equal( 43 windowState.closedGroups[0].tabs.length, 44 3, 45 "Closed group has 3 tabs" 46 ); 47 48 await BrowserTestUtils.closeWindow(win); 49 forgetClosedWindows(); 50 await SessionStoreTestUtils.promiseBrowserState(ORIG_STATE); 51 }); 52 53 add_task(async function test_closedTabGroupSkipsNotWorthSavingTabs() { 54 let win = await promiseNewWindowLoaded(); 55 56 const urls = ["https://example.com/", "about:blank"]; 57 const tabs = urls.map(url => BrowserTestUtils.addTab(win.gBrowser, url)); 58 let group = win.gBrowser.addTabGroup(tabs); 59 await Promise.all( 60 tabs.map(async (t, i) => { 61 await BrowserTestUtils.browserLoaded(t.linkedBrowser, { 62 wantLoad: urls[i], 63 }); 64 await TabStateFlusher.flush(t.linkedBrowser); 65 }) 66 ); 67 68 let windowState = ss.getWindowState(win).windows[0]; 69 Assert.equal( 70 windowState.closedGroups.length, 71 0, 72 "Window state starts with no closed groups" 73 ); 74 75 await lazy.TabGroupTestUtils.removeTabGroup(group); 76 77 windowState = ss.getWindowState(win).windows[0]; 78 Assert.equal( 79 windowState.closedGroups.length, 80 1, 81 "Window state gains a closed group" 82 ); 83 Assert.equal( 84 windowState.closedGroups[0].tabs.length, 85 1, 86 "Closed group has only 1 tab" 87 ); 88 89 await BrowserTestUtils.closeWindow(win); 90 forgetClosedWindows(); 91 await SessionStoreTestUtils.promiseBrowserState(ORIG_STATE); 92 }); 93 94 add_task(async function test_closedTabCountsRespectTabGroups() { 95 let win = await promiseNewWindowLoaded(); 96 97 Assert.equal( 98 SessionStore.getClosedTabCount(), 99 0, 100 "Session store starts with 0 closed tabs" 101 ); 102 Assert.equal( 103 SessionStore.getLastClosedTabCount(win), 104 0, 105 "Session store starts with 0 last closed tab count" 106 ); 107 108 await SessionStoreTestUtils.openAndCloseTab(win, "https://example.com"); 109 110 Assert.equal( 111 SessionStore.getClosedTabCount(), 112 1, 113 "Session store registers closed tab" 114 ); 115 Assert.equal( 116 SessionStore.getLastClosedTabCount(win), 117 1, 118 "Session store reports last closed tab count as 1" 119 ); 120 121 // We need to have at least one "worth saving" tab open in the window at time 122 // of close, or else the window will not be added to _closedWindows 123 BrowserTestUtils.addTab(win.gBrowser, "https://example.com"); 124 let tabs = [ 125 BrowserTestUtils.addTab(win.gBrowser, "https://example.com"), 126 BrowserTestUtils.addTab(win.gBrowser, "https://example.com"), 127 BrowserTestUtils.addTab(win.gBrowser, "https://example.com"), 128 ]; 129 let group = win.gBrowser.addTabGroup(tabs); 130 await Promise.all( 131 tabs.map(async t => { 132 await BrowserTestUtils.browserLoaded(t.linkedBrowser); 133 await TabStateFlusher.flush(t.linkedBrowser); 134 }) 135 ); 136 137 await lazy.TabGroupTestUtils.removeTabGroup(group); 138 139 Assert.equal( 140 SessionStore.getClosedTabCount(), 141 4, 142 "Session store registers tabs from closed group" 143 ); 144 Assert.equal( 145 SessionStore.getLastClosedTabCount(win), 146 3, 147 "Session store reports last closed tab count as 3" 148 ); 149 150 Assert.equal( 151 SessionStore.getClosedTabCountForWindow(win), 152 4, 153 "Session store correctly reports closed tab count for window" 154 ); 155 156 await BrowserTestUtils.closeWindow(win); 157 158 Assert.equal( 159 SessionStore.getClosedTabCountFromClosedWindows(), 160 4, 161 "Session store correctly reports closed tab count for closed windows" 162 ); 163 164 forgetClosedWindows(); 165 await SessionStoreTestUtils.promiseBrowserState(ORIG_STATE); 166 }); 167 168 add_task(async function test_purgingSessionHistoryClearsClosedTabGroups() { 169 let win = await promiseNewWindowLoaded(); 170 171 let tab = BrowserTestUtils.addTab(win.gBrowser, "https://example.com"); 172 await BrowserTestUtils.browserLoaded(tab.linkedBrowser); 173 await TabStateFlusher.flush(tab.linkedBrowser); 174 175 let group = win.gBrowser.addTabGroup([tab]); 176 await lazy.TabGroupTestUtils.removeTabGroup(group); 177 178 let windowState = ss.getWindowState(win).windows[0]; 179 Assert.equal( 180 windowState.closedGroups.length, 181 1, 182 "Window state gains a closed group" 183 ); 184 185 Services.obs.notifyObservers(null, "browser:purge-session-history"); 186 187 windowState = ss.getWindowState(win).windows[0]; 188 Assert.equal( 189 windowState.closedGroups.length, 190 0, 191 "Session history purge removes closed groups" 192 ); 193 194 await BrowserTestUtils.closeWindow(win); 195 forgetClosedWindows(); 196 await SessionStoreTestUtils.promiseBrowserState(ORIG_STATE); 197 });