processor-construction-port.https.html (2136B)
1 <!doctype html> 2 <title>Test processor port assignment on processor callback function construction</title> 3 <script src=/resources/testharness.js></script> 4 <script src=/resources/testharnessreport.js></script> 5 <script> 6 // https://webaudio.github.io/web-audio-api/#AudioWorkletProcessor-instantiation 7 8 const get_context_for_node_name = async (node_name) => { 9 const context = new AudioContext(); 10 const filePath = `processors/construction-port-${node_name}.js`; 11 await context.audioWorklet.addModule(filePath); 12 return context; 13 } 14 15 const test_throws = async ({node_name, thrower} = {}) => { 16 const context = await get_context_for_node_name(node_name); 17 const node = new AudioWorkletNode(context, node_name); 18 const event = await new Promise((resolve) => { 19 node.port.onmessage = resolve; 20 }); 21 assert_true(event.data.threw, `${thrower} should throw`); 22 assert_equals(event.data.errorName, "TypeError"); 23 assert_true(event.data.isTypeError, "exception should be TypeError"); 24 }; 25 26 const throw_tests = [ 27 { 28 test_name: 'super() after new AudioWorkletProcessor()', 29 node_name: 'super-after-new', 30 thrower: 'super()' 31 }, 32 { 33 test_name: 'new AudioWorkletProcessor() after super()', 34 node_name: 'new-after-super', 35 thrower: 'new AudioWorkletProcessor()' 36 }, 37 { 38 test_name: 'new AudioWorkletProcessor() after new AudioWorkletProcessor()', 39 node_name: 'new-after-new', 40 thrower: 'new AudioWorkletProcessor()' 41 } 42 ]; 43 for (const test_info of throw_tests) { 44 promise_test(async () => test_throws(test_info), test_info.test_name); 45 } 46 47 promise_test(async (t) => { 48 const node_name = 'singleton'; 49 const context = await get_context_for_node_name(node_name); 50 const node1 = new AudioWorkletNode(context, node_name); 51 const node2 = new AudioWorkletNode(context, node_name); 52 node2.onmessage = t.unreached_func("node2 should not receive a message"); 53 let count = 0; 54 await new Promise((resolve) => { 55 node1.port.onmessage = t.step_func((event) => { 56 assert_less_than(count, 2, "message count"); 57 if (++count == 2) { resolve(); }; 58 }); 59 }); 60 }, 'Singleton AudioWorkletProcessor'); 61 </script>