browser_windowStateContainer.js (5241B)
1 "use strict"; 2 3 requestLongerTimeout(2); 4 5 add_setup(async function () { 6 await SpecialPowers.pushPrefEnv({ 7 set: [["dom.ipc.processCount", 1]], 8 }); 9 }); 10 11 function promiseTabsRestored(win, nExpected) { 12 return new Promise(resolve => { 13 let nReceived = 0; 14 function handler() { 15 if (++nReceived === nExpected) { 16 win.gBrowser.tabContainer.removeEventListener( 17 "SSTabRestored", 18 handler, 19 true 20 ); 21 resolve(); 22 } 23 } 24 win.gBrowser.tabContainer.addEventListener("SSTabRestored", handler, true); 25 }); 26 } 27 28 add_task(async function () { 29 let win = await BrowserTestUtils.openNewBrowserWindow(); 30 31 // Create 4 tabs with different userContextId. 32 for (let userContextId = 1; userContextId < 5; userContextId++) { 33 let tab = BrowserTestUtils.addTab(win.gBrowser, "http://example.com/", { 34 userContextId, 35 }); 36 await promiseBrowserLoaded(tab.linkedBrowser); 37 await TabStateFlusher.flush(tab.linkedBrowser); 38 } 39 40 // Move the default tab of window to the end. 41 // We want the 1st tab to have non-default userContextId, so later when we 42 // restore into win2 we can test restore into an existing tab with different 43 // userContextId. 44 win.gBrowser.moveTabToEnd(win.gBrowser.tabs[0]); 45 46 let winState = ss.getWindowState(win); 47 48 for (let i = 0; i < 4; i++) { 49 Assert.equal( 50 winState.windows[0].tabs[i].userContextId, 51 i + 1, 52 "1st Window: tabs[" + i + "].userContextId should exist." 53 ); 54 } 55 56 let win2 = await BrowserTestUtils.openNewBrowserWindow(); 57 58 // Create tabs with different userContextId, but this time we create them with 59 // fewer tabs and with different order with win. 60 for (let userContextId = 3; userContextId > 0; userContextId--) { 61 let tab = BrowserTestUtils.addTab(win2.gBrowser, "http://example.com/", { 62 userContextId, 63 }); 64 await promiseBrowserLoaded(tab.linkedBrowser); 65 await TabStateFlusher.flush(tab.linkedBrowser); 66 } 67 68 let tabsRestored = promiseTabsRestored(win2, 5); 69 await setWindowState(win2, winState, true); 70 await tabsRestored; 71 72 for (let i = 0; i < 4; i++) { 73 let browser = win2.gBrowser.tabs[i].linkedBrowser; 74 await ContentTask.spawn( 75 browser, 76 { expectedId: i + 1 }, 77 async function (args) { 78 Assert.equal( 79 docShell.getOriginAttributes().userContextId, 80 args.expectedId, 81 "The docShell has the correct userContextId" 82 ); 83 84 Assert.equal( 85 content.document.nodePrincipal.originAttributes.userContextId, 86 args.expectedId, 87 "The document has the correct userContextId" 88 ); 89 } 90 ); 91 } 92 93 // Test the last tab, which doesn't have userContextId. 94 let browser = win2.gBrowser.tabs[4].linkedBrowser; 95 await SpecialPowers.spawn( 96 browser, 97 [{ expectedId: 0 }], 98 async function (args) { 99 Assert.equal( 100 docShell.getOriginAttributes().userContextId, 101 args.expectedId, 102 "The docShell has the correct userContextId" 103 ); 104 105 Assert.equal( 106 content.document.nodePrincipal.originAttributes.userContextId, 107 args.expectedId, 108 "The document has the correct userContextId" 109 ); 110 } 111 ); 112 113 await BrowserTestUtils.closeWindow(win); 114 await BrowserTestUtils.closeWindow(win2); 115 }); 116 117 add_task(async function () { 118 let win = await BrowserTestUtils.openNewBrowserWindow(); 119 await TabStateFlusher.flush(win.gBrowser.selectedBrowser); 120 121 let tab = BrowserTestUtils.addTab(win.gBrowser, "http://example.com/", { 122 userContextId: 1, 123 }); 124 await promiseBrowserLoaded(tab.linkedBrowser); 125 await TabStateFlusher.flush(tab.linkedBrowser); 126 127 // win should have 1 default tab, and 1 container tab. 128 Assert.equal(win.gBrowser.tabs.length, 2, "win should have 2 tabs"); 129 130 let winState = ss.getWindowState(win); 131 132 for (let i = 0; i < 2; i++) { 133 Assert.equal( 134 winState.windows[0].tabs[i].userContextId, 135 i, 136 "1st Window: tabs[" + i + "].userContextId should be " + i 137 ); 138 } 139 140 let win2 = await BrowserTestUtils.openNewBrowserWindow(); 141 142 let tab2 = BrowserTestUtils.addTab(win2.gBrowser, "http://example.com/", { 143 userContextId: 1, 144 }); 145 await promiseBrowserLoaded(tab2.linkedBrowser); 146 await TabStateFlusher.flush(tab2.linkedBrowser); 147 148 // Move the first normal tab to end, so the first tab of win2 will be a 149 // container tab. 150 win2.gBrowser.moveTabToEnd(win2.gBrowser.tabs[0]); 151 await TabStateFlusher.flush(win2.gBrowser.tabs[0].linkedBrowser); 152 153 let tabsRestored = promiseTabsRestored(win2, 2); 154 await setWindowState(win2, winState, true); 155 await tabsRestored; 156 157 for (let i = 0; i < 2; i++) { 158 let browser = win2.gBrowser.tabs[i].linkedBrowser; 159 await ContentTask.spawn(browser, { expectedId: i }, async function (args) { 160 Assert.equal( 161 docShell.getOriginAttributes().userContextId, 162 args.expectedId, 163 "The docShell has the correct userContextId" 164 ); 165 166 Assert.equal( 167 content.document.nodePrincipal.originAttributes.userContextId, 168 args.expectedId, 169 "The document has the correct userContextId" 170 ); 171 }); 172 } 173 174 await BrowserTestUtils.closeWindow(win); 175 await BrowserTestUtils.closeWindow(win2); 176 });