browser_webconsole_stubs_platform_messages.js (2981B)
1 /* Any copyright is dedicated to the Public Domain. 2 * http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const { 7 STUBS_UPDATE_ENV, 8 createCommandsForMainProcess, 9 getCleanedPacket, 10 getSerializedPacket, 11 getStubFile, 12 writeStubsToFile, 13 } = require(`${CHROME_URL_ROOT}stub-generator-helpers`); 14 15 const STUB_FILE = "platformMessage.js"; 16 17 add_task(async function () { 18 const isStubsUpdate = Services.env.get(STUBS_UPDATE_ENV) == "true"; 19 info(`${isStubsUpdate ? "Update" : "Check"} ${STUB_FILE}`); 20 21 const generatedStubs = await generatePlatformMessagesStubs(); 22 23 if (isStubsUpdate) { 24 await writeStubsToFile(STUB_FILE, generatedStubs); 25 ok(true, `${STUB_FILE} was updated`); 26 return; 27 } 28 29 const existingStubs = getStubFile(STUB_FILE); 30 31 const FAILURE_MSG = 32 "The platformMessage stubs file needs to be updated by running `" + 33 `mach test ${getCurrentTestFilePath()} --headless --setenv WEBCONSOLE_STUBS_UPDATE=true` + 34 "`"; 35 36 if (generatedStubs.size !== existingStubs.rawPackets.size) { 37 ok(false, FAILURE_MSG); 38 return; 39 } 40 41 let failed = false; 42 for (const [key, packet] of generatedStubs) { 43 const packetStr = getSerializedPacket(packet, { 44 sortKeys: true, 45 replaceActorIds: true, 46 }); 47 const existingPacketStr = getSerializedPacket( 48 existingStubs.rawPackets.get(key), 49 { sortKeys: true, replaceActorIds: true } 50 ); 51 is(packetStr, existingPacketStr, `"${key}" packet has expected value`); 52 failed = failed || packetStr !== existingPacketStr; 53 } 54 55 if (failed) { 56 ok(false, FAILURE_MSG); 57 } else { 58 ok(true, "Stubs are up to date"); 59 } 60 }); 61 62 async function generatePlatformMessagesStubs() { 63 const stubs = new Map(); 64 65 const commands = await createCommandsForMainProcess(); 66 await commands.targetCommand.startListening(); 67 const resourceCommand = commands.resourceCommand; 68 69 // The resource-watcher only supports a single call to watch/unwatch per 70 // instance, so we attach a unique watch callback, which will forward the 71 // resource to `handlePlatformMessage`, dynamically updated for each command. 72 let handlePlatformMessage = function () {}; 73 74 const onPlatformMessageAvailable = resources => { 75 for (const resource of resources) { 76 handlePlatformMessage(resource); 77 } 78 }; 79 await resourceCommand.watchResources( 80 [resourceCommand.TYPES.PLATFORM_MESSAGE], 81 { 82 onAvailable: onPlatformMessageAvailable, 83 } 84 ); 85 86 for (const [key, string] of getPlatformMessages()) { 87 const onPlatformMessage = new Promise(resolve => { 88 handlePlatformMessage = resolve; 89 }); 90 91 Services.console.logStringMessage(string); 92 93 const packet = await onPlatformMessage; 94 stubs.set(key, getCleanedPacket(key, packet)); 95 } 96 97 await commands.destroy(); 98 99 return stubs; 100 } 101 102 function getPlatformMessages() { 103 return new Map([ 104 ["platform-simple-message", "foobar test"], 105 ["platform-longString-message", `a\n${"a".repeat(20000)}`], 106 ]); 107 }