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>
12 description("Test OfflineAudioContext.startRendering Promise")
14 window
.jsTestIsAsync
= true;
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
29 var context
= new OfflineAudioContext(contextChannels
, renderFrames
, sampleRate
);
30 context
.startRendering();
31 context
.startRendering()
33 testFailed("Promise not rejected when calling startRendering twice");
35 testPassed("Promise correctly rejected when calling startRendering twice");
40 audit
.defineTask('promise-results', function (done
) {
41 // Create an offline context and verify that buffer returned by the promise matches the
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
);
56 context
.startRendering().then(handlePromise
).then(done
);
59 audit
.defineTask('finish', function (done
) {
64 audit
.runTasks('reject-promise', 'promise-results', 'finish');
66 function handlePromise(buffer
)
68 renderedData
= buffer
;
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) {
90 testPassed("Rendered data matches expected results");
91 testPassed("OfflineAudioContext.startRendering promise succeeded");
93 testFailed("Rendered data does not match expected results for channel " +
94 badChannel
+ ", frame " + badFrame
);
95 testFailed("OfflineAudioContext.startRendering promise");
99 successfullyParsed
= true;