chromium/third_party/blink/web_tests/webaudio/OfflineAudioContext/offlineaudiocontext-promise-basic.html

<!DOCTYPE html>
<html>
  <head>
    <title>
      OfflineAudioContext.startRendering Promise
    </title>
    <script src="../../resources/testharness.js"></script>
    <script src="../../resources/testharnessreport.js"></script>
    <script src="../resources/audit.js"></script>
  </head>
  <body>
    <script id="layout-test-code">
      let context;
      let promiseData;

      let sampleRate = 48000;
      let renderSeconds = 1;
      let renderFrames = sampleRate * renderSeconds;
      let contextChannels = 2;

      let audit = Audit.createTaskRunner();

      audit.define('reject-promise', (task, should) => {
        // Create a context and verify that calling startRendering more than
        // once causes a rejected promise.
        let context =
            new OfflineAudioContext(contextChannels, renderFrames, sampleRate);
        context.startRendering();
        should(
            context.startRendering(),
            'Promise from calling startRendering twice')
            .beRejected()
            .then(() => task.done());
      });

      audit.define('promise-results', (task, should) => {
        // Create an offline context and verify that buffer returned by the
        // promise matches the expected results.
        context =
            new OfflineAudioContext(contextChannels, renderFrames, sampleRate);

        let buffer =
            context.createBuffer(contextChannels, renderFrames, sampleRate);
        for (let k = 0; k < renderFrames; ++k) {
          buffer.getChannelData(0)[k] = 1;
          buffer.getChannelData(1)[k] = 2;
        }

        let source = context.createBufferSource();
        source.buffer = buffer;
        source.connect(context.destination);
        source.start();

        context.startRendering()
            .then(buffer => {
              return handlePromise(should, buffer);
            })
            .then(() => task.done());
      });

      audit.run();

      function handlePromise(should, renderedData) {
        should(context.state, 'context.state').beEqualTo('closed');
        should(renderedData.numberOfChannels, 'renderedData.numberOfChannels')
            .beEqualTo(contextChannels);
        should(renderedData.length, 'renderedData.length')
            .beEqualTo(renderFrames);

        for (let channel = 0; channel < renderedData.numberOfChannels;
             ++channel) {
          should(
              renderedData.getChannelData(channel), `Output channel ${channel}`)
              .beConstantValueOf(channel + 1);
        }
      }
    </script>
  </body>
</html>