browser_session_data_update.js (4083B)
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 various session data update scenarios against a single browsing context. 12 add_task(async function test_session_data_update() { 13 const tab1 = gBrowser.selectedTab; 14 await loadURL(tab1.linkedBrowser, TEST_PAGE); 15 const browsingContext1 = tab1.linkedBrowser.browsingContext; 16 17 const root = createRootMessageHandler("session-data-update"); 18 19 info("Add a new session data item, expect one return value"); 20 await root.updateSessionData([ 21 createSessionDataUpdate(["text-1"], "add", "category1"), 22 ]); 23 let processedUpdates = await getUpdates(root, browsingContext1); 24 is(processedUpdates.length, 1); 25 assertUpdate(processedUpdates.at(-1), ["text-1"], "category1"); 26 27 info("Add two session data items, expect one return value with both items"); 28 await root.updateSessionData([ 29 createSessionDataUpdate(["text-2"], "add", "category1"), 30 createSessionDataUpdate(["text-3"], "add", "category1"), 31 ]); 32 processedUpdates = await getUpdates(root, browsingContext1); 33 is(processedUpdates.length, 2); 34 assertUpdate( 35 processedUpdates.at(-1), 36 ["text-1", "text-2", "text-3"], 37 "category1" 38 ); 39 40 info("Try to add an existing data item, expect no update broadcast"); 41 await root.updateSessionData([ 42 createSessionDataUpdate(["text-1"], "add", "category1"), 43 ]); 44 processedUpdates = await getUpdates(root, browsingContext1); 45 is(processedUpdates.length, 2); 46 47 info("Add an existing and a new item"); 48 await root.updateSessionData([ 49 createSessionDataUpdate(["text-2", "text-4"], "add", "category1"), 50 ]); 51 processedUpdates = await getUpdates(root, browsingContext1); 52 is(processedUpdates.length, 3); 53 assertUpdate( 54 processedUpdates.at(-1), 55 ["text-1", "text-2", "text-3", "text-4"], 56 "category1" 57 ); 58 59 info("Remove an item, expect only the new item to return"); 60 await root.updateSessionData([ 61 createSessionDataUpdate(["text-3"], "remove", "category1"), 62 ]); 63 processedUpdates = await getUpdates(root, browsingContext1); 64 is(processedUpdates.length, 4); 65 assertUpdate( 66 processedUpdates.at(-1), 67 ["text-1", "text-2", "text-4"], 68 "category1" 69 ); 70 71 info("Remove a unknown item, expect no return value"); 72 await root.updateSessionData([ 73 createSessionDataUpdate(["text-unknown"], "remove", "category1"), 74 ]); 75 processedUpdates = await getUpdates(root, browsingContext1); 76 is(processedUpdates.length, 4); 77 assertUpdate( 78 processedUpdates.at(-1), 79 ["text-1", "text-2", "text-4"], 80 "category1" 81 ); 82 83 info("Remove an existing and a unknown item"); 84 await root.updateSessionData([ 85 createSessionDataUpdate(["text-2"], "remove", "category1"), 86 createSessionDataUpdate(["text-unknown"], "remove", "category1"), 87 ]); 88 processedUpdates = await getUpdates(root, browsingContext1); 89 is(processedUpdates.length, 5); 90 assertUpdate(processedUpdates.at(-1), ["text-1", "text-4"], "category1"); 91 92 info("Add and remove at once"); 93 await root.updateSessionData([ 94 createSessionDataUpdate(["text-5"], "add", "category1"), 95 createSessionDataUpdate(["text-4"], "remove", "category1"), 96 ]); 97 processedUpdates = await getUpdates(root, browsingContext1); 98 is(processedUpdates.length, 6); 99 assertUpdate(processedUpdates.at(-1), ["text-1", "text-5"], "category1"); 100 101 info("Adding and removing an item does not trigger any update"); 102 await root.updateSessionData([ 103 createSessionDataUpdate(["text-6"], "add", "category1"), 104 createSessionDataUpdate(["text-6"], "remove", "category1"), 105 ]); 106 processedUpdates = await getUpdates(root, browsingContext1); 107 // TODO: We could detect transactions which can't have any impact and fully 108 // ignore them. See Bug 1810807. 109 todo_is(processedUpdates.length, 6); 110 assertUpdate(processedUpdates.at(-1), ["text-1", "text-5"], "category1"); 111 112 root.destroy(); 113 });