<!DOCTYPE html>
<html>
  <head>
    <title>
      Test the invocation order of AudioWorklet.addModule() and BaseAudioContext
    </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();

      setup(() => {
        let sampleRate = 48000;
        let realtimeContext = new AudioContext();
        let offlineContext = new OfflineAudioContext(1, sampleRate, sampleRate);

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

        // Test if the browser does not crash upon addModule() call after the
        // realtime context construction.
        audit.define(
            {label: 'module-loading-after-realtime-context-creation'},
            (task, should) => {
              let dummyWorkletNode =
                  new AudioWorkletNode(realtimeContext, 'dummy');
              dummyWorkletNode.connect(realtimeContext.destination);
              should(dummyWorkletNode instanceof AudioWorkletNode,
                     '"dummyWorkletNode" is an instance of AudioWorkletNode ' +
                     'from realtime context')
                  .beTrue();
              task.done();
            });

        // Test if the browser does not crash upon addModule() call after the
        // offline context construction.
        audit.define(
            {label: 'module-loading-after-offline-context-creation'},
            (task, should) => {
              let dummyWorkletNode =
                  new AudioWorkletNode(offlineContext, 'dummy');
              dummyWorkletNode.connect(offlineContext.destination);
              should(dummyWorkletNode instanceof AudioWorkletNode,
                     '"dummyWorkletNode" is an instance of AudioWorkletNode ' +
                     'from offline context')
                  .beTrue();
              task.done();
            });

        Promise.all([
            realtimeContext.audioWorklet.addModule(filePath),
            offlineContext.audioWorklet.addModule(filePath)
          ]).then(() => {
            audit.run();
          });
      });
    </script>
  </body>
</html>
