browser_session_data_update_contexts.js (6032B)
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 involving 2 browsing contexts, and using 12 // the TopBrowsingContext ContextDescriptor type. 13 add_task(async function test_session_data_update_contexts() { 14 const tab1 = gBrowser.selectedTab; 15 await loadURL(tab1.linkedBrowser, TEST_PAGE); 16 const browsingContext1 = tab1.linkedBrowser.browsingContext; 17 18 const root = createRootMessageHandler("session-data-update-contexts"); 19 20 info("Add several items over 2 separate updates for all contexts"); 21 await root.updateSessionData([ 22 createSessionDataUpdate(["text-1"], "add", "category1"), 23 ]); 24 await root.updateSessionData([ 25 createSessionDataUpdate(["text-2"], "add", "category1"), 26 createSessionDataUpdate(["text-3"], "add", "category1"), 27 ]); 28 29 info("Check we processed two distinct updates in browsingContext 1"); 30 let processedUpdates = await getUpdates(root, browsingContext1); 31 is(processedUpdates.length, 2); 32 assertUpdate( 33 processedUpdates.at(-1), 34 ["text-1", "text-2", "text-3"], 35 "category1" 36 ); 37 38 info("Open a new tab on the same test URL"); 39 const tab2 = await addTab(TEST_PAGE); 40 const browsingContext2 = tab2.linkedBrowser.browsingContext; 41 42 processedUpdates = await getUpdates(root, browsingContext2); 43 is(processedUpdates.length, 1); 44 assertUpdate( 45 processedUpdates.at(-1), 46 ["text-1", "text-2", "text-3"], 47 "category1" 48 ); 49 50 info("Add two items: one globally and one in a single context"); 51 await root.updateSessionData([ 52 createSessionDataUpdate(["text-4"], "add", "category1"), 53 createSessionDataUpdate(["text-5"], "add", "category1", { 54 type: ContextDescriptorType.TopBrowsingContext, 55 id: browsingContext2.browserId, 56 }), 57 ]); 58 59 processedUpdates = await getUpdates(root, browsingContext1); 60 is(processedUpdates.length, 3); 61 assertUpdate( 62 processedUpdates.at(-1), 63 ["text-1", "text-2", "text-3", "text-4"], 64 "category1" 65 ); 66 67 processedUpdates = await getUpdates(root, browsingContext2); 68 is(processedUpdates.length, 2); 69 assertUpdate( 70 processedUpdates.at(-1), 71 ["text-1", "text-2", "text-3", "text-4", "text-5"], 72 "category1" 73 ); 74 75 info("Remove two items: one globally and one in a single context"); 76 await root.updateSessionData([ 77 createSessionDataUpdate(["text-1"], "remove", "category1"), 78 createSessionDataUpdate(["text-5"], "remove", "category1", { 79 type: ContextDescriptorType.TopBrowsingContext, 80 id: browsingContext2.browserId, 81 }), 82 ]); 83 84 processedUpdates = await getUpdates(root, browsingContext1); 85 is(processedUpdates.length, 4); 86 assertUpdate( 87 processedUpdates.at(-1), 88 ["text-2", "text-3", "text-4"], 89 "category1" 90 ); 91 92 processedUpdates = await getUpdates(root, browsingContext2); 93 is(processedUpdates.length, 3); 94 assertUpdate( 95 processedUpdates.at(-1), 96 ["text-2", "text-3", "text-4"], 97 "category1" 98 ); 99 100 info( 101 "Add session data item to all contexts and remove this event for one context (2 steps)" 102 ); 103 104 info("First step: add an item to browsingContext1"); 105 await root.updateSessionData([ 106 createSessionDataUpdate(["text-6"], "add", "category1", { 107 type: ContextDescriptorType.TopBrowsingContext, 108 id: browsingContext1.browserId, 109 }), 110 ]); 111 112 info( 113 "Second step: remove the item from browsingContext1, and add it globally" 114 ); 115 await root.updateSessionData([ 116 createSessionDataUpdate(["text-6"], "remove", "category1", { 117 type: ContextDescriptorType.TopBrowsingContext, 118 id: browsingContext1.browserId, 119 }), 120 createSessionDataUpdate(["text-6"], "add", "category1"), 121 ]); 122 123 processedUpdates = await getUpdates(root, browsingContext1); 124 is(processedUpdates.length, 6); 125 assertUpdate( 126 processedUpdates.at(-1), 127 ["text-2", "text-3", "text-4", "text-6"], 128 "category1" 129 ); 130 131 processedUpdates = await getUpdates(root, browsingContext2); 132 is(processedUpdates.length, 4); 133 assertUpdate( 134 processedUpdates.at(-1), 135 ["text-2", "text-3", "text-4", "text-6"], 136 "category1" 137 ); 138 139 info( 140 "Remove the event, which has also an individual subscription, for all contexts (2 steps)" 141 ); 142 143 info("First step: Add the same item for browsingContext1 and globally"); 144 await root.updateSessionData([ 145 createSessionDataUpdate(["text-7"], "add", "category1", { 146 type: ContextDescriptorType.TopBrowsingContext, 147 id: browsingContext1.browserId, 148 }), 149 createSessionDataUpdate(["text-7"], "add", "category1"), 150 ]); 151 processedUpdates = await getUpdates(root, browsingContext1); 152 is(processedUpdates.length, 7); 153 // We will find text-7 twice here, the module is responsible for not applying 154 // the same session data item twice. Each item corresponds to a different 155 // descriptor which matched browsingContext1. 156 assertUpdate( 157 processedUpdates.at(-1), 158 ["text-2", "text-3", "text-4", "text-6", "text-7", "text-7"], 159 "category1" 160 ); 161 162 processedUpdates = await getUpdates(root, browsingContext2); 163 is(processedUpdates.length, 5); 164 assertUpdate( 165 processedUpdates.at(-1), 166 ["text-2", "text-3", "text-4", "text-6", "text-7"], 167 "category1" 168 ); 169 170 info("Second step: Remove the item globally"); 171 await root.updateSessionData([ 172 createSessionDataUpdate(["text-7"], "remove", "category1"), 173 ]); 174 175 processedUpdates = await getUpdates(root, browsingContext1); 176 is(processedUpdates.length, 8); 177 assertUpdate( 178 processedUpdates.at(-1), 179 ["text-2", "text-3", "text-4", "text-6", "text-7"], 180 "category1" 181 ); 182 183 processedUpdates = await getUpdates(root, browsingContext2); 184 is(processedUpdates.length, 6); 185 assertUpdate( 186 processedUpdates.at(-1), 187 ["text-2", "text-3", "text-4", "text-6"], 188 "category1" 189 ); 190 191 root.destroy(); 192 193 gBrowser.removeTab(tab2); 194 });