summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webaudio/the-audio-api/the-audioworklet-interface/audioworklet-messageport.https.html
blob: 546bd1d0d0e34480113290bcc5008f0240be8d4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<!DOCTYPE html>
<html>
  <head>
    <title>
      Test MessagePort in AudioWorkletNode and AudioWorkletProcessor
    </title>
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
    <script src="/webaudio/resources/audit.js"></script>
  </head>
  <body>
    <script id="layout-test-code">
      let audit = Audit.createTaskRunner();

      let context = new AudioContext();

      let filePath = 'processors/port-processor.js';

      // 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.
      audit.define(
          'Test postMessage from AudioWorkletProcessor to AudioWorkletNode',
          (task, should) => {
            let porterWorkletNode =
                new AudioWorkletNode(context, 'port-processor');

            // Upon the creation of PortProcessor, it will post a message to the
            // node with 'created' status.
            porterWorkletNode.port.onmessage = (event) => {
              should(event.data.state,
                     'The initial message from PortProcessor')
                  .beEqualTo('created');
              task.done();
            };
          });

      // PortProcessor is supposed to echo the message back to the
      // AudioWorkletNode.
      audit.define(
          'Test postMessage from AudioWorkletNode to AudioWorkletProcessor',
          (task, should) => {
            let porterWorkletNode =
                new AudioWorkletNode(context, 'port-processor');

            porterWorkletNode.port.onmessage = (event) => {
              // Ignore if the delivered message has |state|. This is already
              // tested in the previous task.
              if (event.data.state)
                return;

              should(event.data.message,
                     'The response from PortProcessor')
                  .beEqualTo('hello');
              task.done();
            };

            porterWorkletNode.port.postMessage('hello');
          });

      context.audioWorklet.addModule(filePath).then(() => {
        audit.run();
      });
    </script>
  </body>
</html>