browser_resources_server_sent_events.js (2982B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 // Test the ResourceCommand API around SERVER SENT EVENTS. 7 8 const ResourceCommand = require("resource://devtools/shared/commands/resource/resource-command.js"); 9 10 const targets = { 11 TOP_LEVEL_DOCUMENT: "top-level-document", 12 IN_PROCESS_IFRAME: "in-process-frame", 13 OUT_PROCESS_IFRAME: "out-process-frame", 14 }; 15 16 add_task(async function () { 17 info("Testing the top-level document"); 18 await testServerSentEventResources(targets.TOP_LEVEL_DOCUMENT); 19 info("Testing the in-process iframe"); 20 await testServerSentEventResources(targets.IN_PROCESS_IFRAME); 21 info("Testing the out-of-process iframe"); 22 await testServerSentEventResources(targets.OUT_PROCESS_IFRAME); 23 }); 24 25 async function testServerSentEventResources(target) { 26 const tab = await addTab(URL_ROOT_SSL + "sse_frontend.html"); 27 28 const { client, resourceCommand, targetCommand } = 29 await initResourceCommand(tab); 30 31 const availableResources = []; 32 33 function onResourceAvailable(resources) { 34 availableResources.push(...resources); 35 } 36 37 await resourceCommand.watchResources( 38 [resourceCommand.TYPES.SERVER_SENT_EVENT], 39 { onAvailable: onResourceAvailable } 40 ); 41 42 openConnectionInContext(tab, target); 43 44 info("Check available resources"); 45 // We expect only 2 resources 46 await waitUntil(() => availableResources.length === 2); 47 48 info("Check resource details"); 49 // To make sure the channel id are the same 50 const httpChannelId = availableResources[0].httpChannelId; 51 52 ok(httpChannelId, "The channel id is set"); 53 is(typeof httpChannelId, "number", "The channel id is a number"); 54 55 assertResource(availableResources[0], { 56 messageType: "eventReceived", 57 httpChannelId, 58 data: { 59 payload: "Why so serious?", 60 eventName: "message", 61 lastEventId: "", 62 retry: 5000, 63 }, 64 }); 65 66 assertResource(availableResources[1], { 67 messageType: "eventSourceConnectionClosed", 68 httpChannelId, 69 }); 70 71 await resourceCommand.unwatchResources( 72 [resourceCommand.TYPES.SERVER_SENT_EVENT], 73 { onAvailable: onResourceAvailable } 74 ); 75 76 await targetCommand.destroy(); 77 await client.close(); 78 BrowserTestUtils.removeTab(tab); 79 } 80 81 function assertResource(resource, expected) { 82 is( 83 resource.resourceType, 84 ResourceCommand.TYPES.SERVER_SENT_EVENT, 85 "Resource type is correct" 86 ); 87 88 checkObject(resource, expected); 89 } 90 91 async function openConnectionInContext(tab, target) { 92 let browsingContext = tab.linkedBrowser.browsingContext; 93 if (target !== targets.TOP_LEVEL_DOCUMENT) { 94 browsingContext = await SpecialPowers.spawn( 95 tab.linkedBrowser, 96 [target], 97 async _target => { 98 const iframe = content.document.getElementById(_target); 99 return iframe.browsingContext; 100 } 101 ); 102 } 103 await SpecialPowers.spawn(browsingContext, [], async () => { 104 await content.wrappedJSObject.openConnection(); 105 }); 106 }