audioworklet-messageport.https.html (2702B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title> 5 Test MessagePort in AudioWorkletNode and AudioWorkletProcessor 6 </title> 7 <script src="/resources/testharness.js"></script> 8 <script src="/resources/testharnessreport.js"></script> 9 </head> 10 <body> 11 <script> 12 13 // This test verifies the correct operation of MessagePort communication 14 // between the main thread and the AudioWorklet environment. It checks: 15 // 1. Messages can be sent/received via the AudioWorkletGlobalScope's 16 // shared port (context.audioWorklet.port) 17 // 2. Messages can be sent/received via individual AudioWorkletProcessor 18 // instance ports (node.port) 19 // 3. AudioWorkletProcessor instances send initialization messages 20 // upon creation 21 promise_test(async () => { 22 const context = new AudioContext(); 23 const filePath = 'processors/port-processor.js'; 24 await context.audioWorklet.addModule(filePath); 25 26 const globalOnMessagePromise = new Promise((resolve) => { 27 context.audioWorklet.port.onmessage = (event) => { 28 resolve(event.data); 29 }; 30 context.audioWorklet.port.postMessage('hello world'); 31 }); 32 33 assert_equals( 34 await globalOnMessagePromise, 35 'hello world', 36 'The response from AudioWorkletGlobalScope'); 37 38 // Creates an AudioWorkletNode and sets an EventHandler on MessagePort 39 // object. The associated PortProcessor will post a message upon its 40 // construction. Test if the message is received correctly. 41 const nodeCreationOnMessagePromise = new Promise((resolve) => { 42 const node = new AudioWorkletNode(context, 'port-processor'); 43 node.port.onmessage = (event) => { 44 resolve(event.data.state); 45 }; 46 }); 47 48 assert_equals( 49 await nodeCreationOnMessagePromise, 50 'created', 51 'The initial message from PortProcessor'); 52 53 const nodeOnMessagePromise = new Promise((resolve) => { 54 const node = new AudioWorkletNode(context, 'port-processor'); 55 node.port.onmessage = (event) => { 56 if (!event.data.state) { 57 // Ignore if the delivered message has |state|. This is already 58 // tested in the previous task. 59 resolve(event.data.message); 60 } 61 }; 62 node.port.postMessage('hello'); 63 }); 64 65 assert_equals( 66 await nodeOnMessagePromise, 67 'hello', 68 'The response from PortProcessor'); 69 }, 'Test MessagePort in AudioWorkletNode and AudioWorkletProcessor'); 70 </script> 71 </body> 72 </html>