browser_session_data_browser_element.js (3545B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const TEST_PAGE = "https://example.com/document-builder.sjs?html=tab"; 7 8 /** 9 * Check that message handlers are not created for parent process browser 10 * elements, even if they have the type="content" attribute (eg used for the 11 * DevTools toolbox), as well as for webextension contexts. 12 */ 13 add_task(async function test_session_data_broadcast() { 14 // Prepare: 15 // - one content tab 16 // - one browser type content 17 // - one browser type chrome 18 // - one sidebar webextension 19 // We only expect session data to be applied to the content tab 20 const tab1 = BrowserTestUtils.addTab(gBrowser, TEST_PAGE); 21 const contentBrowser1 = tab1.linkedBrowser; 22 await BrowserTestUtils.browserLoaded(contentBrowser1); 23 const parentBrowser1 = createParentBrowserElement(tab1, "content"); 24 const parentBrowser2 = createParentBrowserElement(tab1, "chrome"); 25 const { extension: extension1, sidebarBrowser: extSidebarBrowser1 } = 26 await installSidebarExtension(); 27 28 const root = createRootMessageHandler("session-id-event"); 29 30 // When the windowglobal command.sys.mjs module applies the session data 31 // browser_session_data_browser_element, it will emit an event. 32 // Collect the events to detect which MessageHandlers have been started. 33 info("Watch events emitted when session data is applied"); 34 const sessionDataEvents = []; 35 const onRootEvent = function (evtName, wrappedEvt) { 36 if (wrappedEvt.name === "received-session-data") { 37 sessionDataEvents.push(wrappedEvt.data.contextId); 38 } 39 }; 40 root.on("message-handler-event", onRootEvent); 41 42 info("Add a new session data item, expect one return value"); 43 await root.addSessionDataItem({ 44 moduleName: "command", 45 category: "browser_session_data_browser_element", 46 contextDescriptor: { 47 type: ContextDescriptorType.All, 48 }, 49 values: [true], 50 }); 51 52 function hasSessionData(browsingContext) { 53 return sessionDataEvents.includes(browsingContext.id); 54 } 55 56 info( 57 "Check that only the content tab window global received the session data" 58 ); 59 is(hasSessionData(contentBrowser1.browsingContext), true); 60 is(hasSessionData(parentBrowser1.browsingContext), false); 61 is(hasSessionData(parentBrowser2.browsingContext), false); 62 is(hasSessionData(extSidebarBrowser1.browsingContext), false); 63 64 const tab2 = BrowserTestUtils.addTab(gBrowser, TEST_PAGE); 65 const contentBrowser2 = tab2.linkedBrowser; 66 await BrowserTestUtils.browserLoaded(contentBrowser2); 67 const parentBrowser3 = createParentBrowserElement(contentBrowser2, "content"); 68 const parentBrowser4 = createParentBrowserElement(contentBrowser2, "chrome"); 69 70 const { extension: extension2, sidebarBrowser: extSidebarBrowser2 } = 71 await installSidebarExtension(); 72 73 info("Wait until the session data was applied to the new tab"); 74 await TestUtils.waitForCondition(() => 75 sessionDataEvents.includes(contentBrowser2.browsingContext.id) 76 ); 77 78 info("Check that parent browser elements did not apply the session data"); 79 is(hasSessionData(parentBrowser3.browsingContext), false); 80 is(hasSessionData(parentBrowser4.browsingContext), false); 81 82 info( 83 "Check that extension did not apply the session data, " + 84 extSidebarBrowser2.browsingContext.id 85 ); 86 is(hasSessionData(extSidebarBrowser2.browsingContext), false); 87 88 root.destroy(); 89 90 gBrowser.removeTab(tab1); 91 gBrowser.removeTab(tab2); 92 await extension1.unload(); 93 await extension2.unload(); 94 });