browser_session_data_update_user_contexts.js (3710B)
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 const { assertUpdate, createSessionDataUpdate, getUpdates } = 9 SessionDataUpdateHelpers; 10 11 // Test session data update scenarios using the UserContext ContextDescriptor type. 12 add_task(async function test_session_data_update_user_contexts() { 13 info("Open a new tab"); 14 const tab = await addTab(TEST_PAGE); 15 const browsingContext = tab.linkedBrowser.browsingContext; 16 const root = createRootMessageHandler("session-data-update-user-contexts"); 17 18 info("Add three items: one globally, one in a context one in a user context"); 19 await root.updateSessionData([ 20 createSessionDataUpdate(["global"], "add", "category1"), 21 createSessionDataUpdate(["browsing context"], "add", "category1", { 22 type: ContextDescriptorType.TopBrowsingContext, 23 id: browsingContext.browserId, 24 }), 25 createSessionDataUpdate(["user context"], "add", "category1", { 26 type: ContextDescriptorType.UserContext, 27 id: browsingContext.originAttributes.userContextId, 28 }), 29 ]); 30 31 let processedUpdates = await getUpdates(root, browsingContext); 32 is(processedUpdates.length, 1); 33 assertUpdate( 34 processedUpdates.at(-1), 35 ["global", "browsing context", "user context"], 36 "category1" 37 ); 38 39 info("Open a new tab on the same test URL"); 40 const tab2 = await addTab(TEST_PAGE); 41 const browsingContext2 = tab2.linkedBrowser.browsingContext; 42 43 // Make sure that this browsing context has only 2 items 44 processedUpdates = await getUpdates(root, browsingContext2); 45 is(processedUpdates.length, 1); 46 assertUpdate( 47 processedUpdates.at(-1), 48 ["global", "user context"], 49 "category1" 50 ); 51 52 root.destroy(); 53 }); 54 55 add_task(async function test_session_data_update_with_two_user_contexts() { 56 info("Open a new tab in the default user context"); 57 const tab1 = await addTab(TEST_PAGE); 58 const browsingContext1 = tab1.linkedBrowser.browsingContext; 59 60 info("Create a non-default user context"); 61 const userContext = ContextualIdentityService.create("test"); 62 const userContextId = userContext.userContextId; 63 64 info("Open a new tab in the non-default user context"); 65 const tab2 = BrowserTestUtils.addTab(gBrowser, TEST_PAGE, { 66 userContextId, 67 }); 68 const browsingContext2 = tab2.linkedBrowser.browsingContext; 69 70 const root = createRootMessageHandler( 71 "session-data-update-two-user-contexts" 72 ); 73 74 info("Add an item in the non-default user context"); 75 await root.updateSessionData([ 76 createSessionDataUpdate(["non-default user context"], "add", "category1", { 77 type: ContextDescriptorType.UserContext, 78 id: userContextId, 79 }), 80 ]); 81 82 let processedUpdates = await getUpdates(root, browsingContext1); 83 is(processedUpdates.length, 0); 84 85 processedUpdates = await getUpdates(root, browsingContext2); 86 is(processedUpdates.length, 1); 87 assertUpdate( 88 processedUpdates.at(-1), 89 ["non-default user context"], 90 "category1" 91 ); 92 93 info("Open a new tab in the non-default user context"); 94 const tab3 = BrowserTestUtils.addTab(gBrowser, TEST_PAGE, { 95 userContextId, 96 }); 97 await BrowserTestUtils.browserLoaded(tab3.linkedBrowser); 98 const browsingContext3 = tab3.linkedBrowser.browsingContext; 99 100 // Make sure that this browsing context also has the session data item 101 processedUpdates = await getUpdates(root, browsingContext3); 102 is(processedUpdates.length, 1); 103 assertUpdate( 104 processedUpdates.at(-1), 105 ["non-default user context"], 106 "category1" 107 ); 108 109 root.destroy(); 110 111 BrowserTestUtils.removeTab(tab2); 112 BrowserTestUtils.removeTab(tab3); 113 });