Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / webaudio / offlineaudiocontext-promise-basic.html
blob9fff58fb9f7007bb27c06a19817a4bf588eb8cb8
1 <!doctype html>
2 <html>
3 <head>
4 <script src="../resources/js-test.js"></script>
5 <script src="resources/audio-testing.js"/>
6 <script src="resources/compatibility.js"></script>
7 <title>OfflineAudioContext.startRendering Promise</title>
8 </head>
10 <body>
11 <script>
12 description("Test OfflineAudioContext.startRendering Promise")
14 window.jsTestIsAsync = true;
16 var context;
17 var promiseData;
19 var sampleRate = 48000;
20 var renderSeconds = 1;
21 var renderFrames = sampleRate * renderSeconds;
22 var contextChannels = 2;
24 var audit = Audit.createTaskRunner();
26 audit.defineTask('reject-promise', function (done) {
27 // Create a context and verify that calling startRendering more than once causes a rejected
28 // promise.
29 var context = new OfflineAudioContext(contextChannels, renderFrames, sampleRate);
30 context.startRendering();
31 context.startRendering()
32 .then(function () {
33 testFailed("Promise not rejected when calling startRendering twice");
34 }, function () {
35 testPassed("Promise correctly rejected when calling startRendering twice");
37 .then(done);
38 });
40 audit.defineTask('promise-results', function (done) {
41 // Create an offline context and verify that buffer returned by the promise matches the
42 // expected results.
43 context = new OfflineAudioContext(contextChannels, renderFrames, sampleRate);
45 var buffer = context.createBuffer(contextChannels, renderFrames, sampleRate);
46 for (var k = 0; k < renderFrames; ++k) {
47 buffer.getChannelData(0)[k] = 1;
48 buffer.getChannelData(1)[k] = 2;
51 var source = context.createBufferSource();
52 source.buffer = buffer;
53 source.connect(context.destination);
54 source.start();
56 context.startRendering().then(handlePromise).then(done);
57 });
59 audit.defineTask('finish', function (done) {
60 done();
61 finishJSTest();
62 });
64 audit.runTasks('reject-promise', 'promise-results', 'finish');
66 function handlePromise(buffer)
68 renderedData = buffer;
69 var success = true;
71 shouldBeEqualToString("context.state", "closed");
72 shouldBeEqualToNumber("renderedData.numberOfChannels", contextChannels);
73 shouldBeEqualToNumber("renderedData.length", renderFrames);
75 for (var channel = 0; channel < contextChannels; ++channel) {
76 var data = renderedData.getChannelData(channel);
77 for (var k = 0; k < renderFrames; ++k) {
78 if (data[k] != channel + 1) {
79 success = false;
80 badChannel = channel;
81 badFrame = k;
82 break;
85 if (!success)
86 break;
89 if (success) {
90 testPassed("Rendered data matches expected results");
91 testPassed("OfflineAudioContext.startRendering promise succeeded");
92 } else {
93 testFailed("Rendered data does not match expected results for channel " +
94 badChannel + ", frame " + badFrame);
95 testFailed("OfflineAudioContext.startRendering promise");
99 successfullyParsed = true;
100 </script>
102 </body>
103 </html>