browser_resources_platform_messages.js (4986B)
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 PLATFORM_MESSAGE 7 // Reproduces assertions from: devtools/shared/webconsole/test/chrome/test_nsiconsolemessage.html 8 9 add_task(async function () { 10 // Disable the preloaded process as it creates processes intermittently 11 // which forces the emission of RDP requests we aren't correctly waiting for. 12 await pushPref("dom.ipc.processPrelaunch.enabled", false); 13 14 await testPlatformMessagesResources(); 15 await testPlatformMessagesResourcesWithIgnoreExistingResources(); 16 }); 17 18 async function testPlatformMessagesResources() { 19 const { client, resourceCommand, targetCommand } = 20 await initMultiProcessResourceCommand(); 21 22 const cachedMessages = [ 23 "This is a cached message", 24 "This is another cached message", 25 ]; 26 const liveMessages = [ 27 "This is a live message", 28 "This is another live message", 29 ]; 30 const expectedMessages = [...cachedMessages, ...liveMessages]; 31 const receivedMessages = []; 32 33 info( 34 "Log some messages *before* calling ResourceCommand.watchResources in order to assert the behavior of already existing messages." 35 ); 36 Services.console.logStringMessage(expectedMessages[0]); 37 Services.console.logStringMessage(expectedMessages[1]); 38 39 let done; 40 const onAllMessagesReceived = new Promise(resolve => (done = resolve)); 41 const onAvailable = resources => { 42 for (const resource of resources) { 43 if (!expectedMessages.includes(resource.message)) { 44 continue; 45 } 46 47 is( 48 resource.targetFront, 49 targetCommand.targetFront, 50 "The targetFront property is the expected one" 51 ); 52 53 receivedMessages.push(resource.message); 54 is( 55 resource.message, 56 expectedMessages[receivedMessages.length - 1], 57 `Received the expected «${resource.message}» message, in the expected order` 58 ); 59 60 // timeStamp are the result of a number in microsecond divided by 1000. 61 // so we can't expect a precise number of decimals, or even if there would 62 // be decimals at all. 63 ok( 64 resource.timeStamp.toString().match(/^\d+(\.\d{1,3})?$/), 65 `The resource has a timeStamp property ${resource.timeStamp}` 66 ); 67 68 const isCachedMessage = receivedMessages.length <= cachedMessages.length; 69 is( 70 resource.isAlreadyExistingResource, 71 isCachedMessage, 72 "isAlreadyExistingResource has the expected value" 73 ); 74 75 if (receivedMessages.length == expectedMessages.length) { 76 done(); 77 } 78 } 79 }; 80 81 await resourceCommand.watchResources( 82 [resourceCommand.TYPES.PLATFORM_MESSAGE], 83 { 84 onAvailable, 85 } 86 ); 87 88 info( 89 "Now log messages *after* the call to ResourceCommand.watchResources and after having received all existing messages" 90 ); 91 Services.console.logStringMessage(expectedMessages[2]); 92 Services.console.logStringMessage(expectedMessages[3]); 93 94 info("Waiting for all expected messages to be received"); 95 await onAllMessagesReceived; 96 ok(true, "All the expected messages were received"); 97 98 Services.console.reset(); 99 targetCommand.destroy(); 100 await client.close(); 101 } 102 103 async function testPlatformMessagesResourcesWithIgnoreExistingResources() { 104 const { client, resourceCommand, targetCommand } = 105 await initMultiProcessResourceCommand(); 106 107 info( 108 "Check whether onAvailable will not be called with existing platform messages" 109 ); 110 const expectedMessages = ["This is 1st message", "This is 2nd message"]; 111 Services.console.logStringMessage(expectedMessages[0]); 112 Services.console.logStringMessage(expectedMessages[1]); 113 114 const availableResources = []; 115 await resourceCommand.watchResources( 116 [resourceCommand.TYPES.PLATFORM_MESSAGE], 117 { 118 onAvailable: resources => { 119 for (const resource of resources) { 120 if (!expectedMessages.includes(resource.message)) { 121 continue; 122 } 123 124 availableResources.push(resource); 125 } 126 }, 127 ignoreExistingResources: true, 128 } 129 ); 130 is( 131 availableResources.length, 132 0, 133 "onAvailable wasn't called for existing platform messages" 134 ); 135 136 info( 137 "Check whether onAvailable will be called with the future platform messages" 138 ); 139 Services.console.logStringMessage(expectedMessages[0]); 140 Services.console.logStringMessage(expectedMessages[1]); 141 142 await waitUntil(() => availableResources.length === expectedMessages.length); 143 for (let i = 0; i < expectedMessages.length; i++) { 144 const resource = availableResources[i]; 145 const { message } = resource; 146 const expected = expectedMessages[i]; 147 is(message, expected, `Message[${i}] is correct`); 148 is( 149 resource.isAlreadyExistingResource, 150 false, 151 "isAlreadyExistingResource is false since we ignore existing resources" 152 ); 153 } 154 155 Services.console.reset(); 156 targetCommand.destroy(); 157 await client.close(); 158 }