Source code

Revision control

Copy as Markdown

Other Tools

Test Info:

<!DOCTYPE html>
<html>
<head>
<title>
Test MessagePort in AudioWorkletNode and AudioWorkletProcessor
</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<script>
const filePath = 'processors/port-processor.js';
// Global-to-worklet via shared port test
promise_test(async (t) => {
const context = new AudioContext();
t.add_cleanup(() => context.close());
await context.audioWorklet.addModule(filePath);
const globalOnMessagePromise = new Promise((resolve) => {
context.audioWorklet.port.onmessage = (event) => {
resolve(event.data);
};
context.audioWorklet.port.postMessage('hello world');
});
assert_equals(
await globalOnMessagePromise,
'hello world',
'The response from AudioWorkletGlobalScope');
}, 'AudioWorkletGlobalScope: message via shared port');
// Worklet-to-node (initialization message from Worklet constructor) test
promise_test(async (t) => {
const context = new AudioContext();
t.add_cleanup(() => context.close());
await context.audioWorklet.addModule(filePath);
// Creates an AudioWorkletNode and sets an EventHandler on MessagePort
// object. The associated PortProcessor will post a message upon its
// construction. Test if the message is received correctly.
const nodeCreationOnMessagePromise = new Promise((resolve) => {
const node = new AudioWorkletNode(context, 'port-processor');
node.port.onmessage = (event) => {
resolve(event.data.state);
};
});
assert_equals(
await nodeCreationOnMessagePromise,
'created',
'The initial message from PortProcessor');
}, 'AudioWorkletProcessor: initialization message');
// Node-to-worklet echo test
promise_test(async (t) => {
const context = new AudioContext();
t.add_cleanup(() => context.close());
await context.audioWorklet.addModule(filePath);
const nodeOnMessagePromise = new Promise((resolve) => {
const node = new AudioWorkletNode(context, 'port-processor');
node.port.onmessage = (event) => {
if (!event.data.state) {
// Ignore if the delivered message has |state|. This is already
// tested in the previous task.
resolve(event.data.message);
}
};
node.port.postMessage('hello');
});
assert_equals(
await nodeOnMessagePromise,
'hello',
'The response from PortProcessor');
}, 'AudioWorkletProcessor: message via instance port');
</script>
</body>
</html>