browser_sessionStoreContainer.js (4536B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 add_task(async function () { 8 for (let i = 0; i < 3; ++i) { 9 let tab = BrowserTestUtils.addTab(gBrowser, "http://example.com/", { 10 userContextId: i, 11 }); 12 let browser = tab.linkedBrowser; 13 14 await promiseBrowserLoaded(browser); 15 16 let tab2 = gBrowser.duplicateTab(tab); 17 Assert.equal(tab2.getAttribute("usercontextid") || "", i); 18 let browser2 = tab2.linkedBrowser; 19 await promiseTabRestored(tab2); 20 21 await SpecialPowers.spawn( 22 browser2, 23 [{ expectedId: i }], 24 async function (args) { 25 let loadContext = docShell.QueryInterface(Ci.nsILoadContext); 26 Assert.equal( 27 loadContext.originAttributes.userContextId, 28 args.expectedId, 29 "The docShell has the correct userContextId" 30 ); 31 } 32 ); 33 34 BrowserTestUtils.removeTab(tab); 35 BrowserTestUtils.removeTab(tab2); 36 } 37 }); 38 39 add_task(async function () { 40 let tab = BrowserTestUtils.addTab(gBrowser, "http://example.com/", { 41 userContextId: 1, 42 }); 43 let browser = tab.linkedBrowser; 44 45 await promiseBrowserLoaded(browser); 46 47 gBrowser.selectedTab = tab; 48 49 let tab2 = gBrowser.duplicateTab(tab); 50 let browser2 = tab2.linkedBrowser; 51 await promiseTabRestored(tab2); 52 53 await SpecialPowers.spawn( 54 browser2, 55 [{ expectedId: 1 }], 56 async function (args) { 57 Assert.equal( 58 docShell.getOriginAttributes().userContextId, 59 args.expectedId, 60 "The docShell has the correct userContextId" 61 ); 62 } 63 ); 64 65 BrowserTestUtils.removeTab(tab); 66 BrowserTestUtils.removeTab(tab2); 67 }); 68 69 add_task(async function () { 70 let tab = BrowserTestUtils.addTab(gBrowser, "http://example.com/", { 71 userContextId: 1, 72 }); 73 let browser = tab.linkedBrowser; 74 75 await promiseBrowserLoaded(browser); 76 77 gBrowser.removeTab(tab); 78 79 let tab2 = ss.undoCloseTab(window, 0); 80 Assert.equal(tab2.getAttribute("usercontextid"), 1); 81 await promiseTabRestored(tab2); 82 await SpecialPowers.spawn( 83 tab2.linkedBrowser, 84 [{ expectedId: 1 }], 85 async function (args) { 86 Assert.equal( 87 docShell.getOriginAttributes().userContextId, 88 args.expectedId, 89 "The docShell has the correct userContextId" 90 ); 91 } 92 ); 93 94 BrowserTestUtils.removeTab(tab2); 95 }); 96 97 // Opens "uri" in a new tab with the provided userContextId and focuses it. 98 // Returns the newly opened tab. 99 async function openTabInUserContext(userContextId) { 100 // Open the tab in the correct userContextId. 101 let tab = BrowserTestUtils.addTab(gBrowser, "http://example.com", { 102 userContextId, 103 }); 104 105 // Select tab and make sure its browser is focused. 106 gBrowser.selectedTab = tab; 107 tab.ownerGlobal.focus(); 108 109 let browser = gBrowser.getBrowserForTab(tab); 110 await BrowserTestUtils.browserLoaded(browser); 111 return { tab, browser }; 112 } 113 114 function waitForNewCookie() { 115 return new Promise(resolve => { 116 Services.obs.addObserver(function observer(subj, topic) { 117 let notification = subj.QueryInterface(Ci.nsICookieNotification); 118 if (notification.action == Ci.nsICookieNotification.COOKIE_ADDED) { 119 Services.obs.removeObserver(observer, topic); 120 resolve(); 121 } 122 }, "session-cookie-changed"); 123 }); 124 } 125 126 add_task(async function test() { 127 const USER_CONTEXTS = ["default", "personal", "work"]; 128 129 // Make sure userContext is enabled. 130 await SpecialPowers.pushPrefEnv({ 131 set: [["privacy.userContext.enabled", true]], 132 }); 133 134 Services.cookies.removeAll(); 135 136 for (let userContextId of Object.keys(USER_CONTEXTS)) { 137 // Load the page in 3 different contexts and set a cookie 138 // which should only be visible in that context. 139 let cookie = USER_CONTEXTS[userContextId]; 140 141 // Open our tab in the given user context. 142 let { tab, browser } = await openTabInUserContext(userContextId); 143 144 await Promise.all([ 145 waitForNewCookie(), 146 SpecialPowers.spawn( 147 browser, 148 [cookie], 149 passedCookie => (content.document.cookie = passedCookie) 150 ), 151 ]); 152 153 // Ensure the tab's session history is up-to-date. 154 await TabStateFlusher.flush(browser); 155 156 // Remove the tab. 157 gBrowser.removeTab(tab); 158 } 159 160 let state = JSON.parse(SessionStore.getBrowserState()); 161 is( 162 state.cookies.length, 163 USER_CONTEXTS.length, 164 "session restore should have each container's cookie" 165 ); 166 });