summaryrefslogtreecommitdiffstats
path: root/third_party/WebKit/LayoutTests/webaudio/offlineaudiocontext-promise.html
blob: 7c8c07a0a946a940a69b2c21e87211530ec23cee (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
67
68
69
70
71
72
<!doctype html>
<html>
  <head>
    <script src="../resources/js-test.js"></script>
    <script src="resources/compatibility.js"></script>
    <script src="resources/audio-testing.js"></script>
    <title>OfflineAudioContext.startRendering Promise with oncomplete</title>
  </head>

  <body>
    <script>
      description("Test OfflineAudioContext.startRendering Promise with oncomplete");

      var context;
      var promise;
      var renderedData;
      var promiseData;

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

      function compareData() {
        // The spec implies that the same buffer is returned by both oncomplete and the promise.
        // Check that they are identical.
        if (renderedData === promiseData) {
          testPassed("AudioBuffer returned by oncomplete and promise are identical");
        } else {
          testFailed("AudioBuffer returned by oncomplete and promise are NOT identical");
        }
        finishJSTest();
      }

      function checkResult (event) {
        renderedData = event.renderedBuffer;
        promise.then(function (result) {
          promiseData = result;
          compareData();
        });
      }

      // Create an offline context and verify that both the oncomplete and promise are returned with
      // the same stuff.
      function runTest() {
        window.jsTestIsAsync = true;
        
        context = new OfflineAudioContext(contextChannels, renderFrames, sampleRate);

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

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

        context.oncomplete = checkResult;

        promise = context.startRendering();
        
      }

      runTest();
      successfullyParsed = true;
    </script>
    
  </body>
</html>