4 <title>Test statechange event
</title>
5 <script src=
"../resources/js-test.js"></script>
6 <script src=
"resources/audio-testing.js"/>
7 <script src=
"resources/compatibility.js"></script>
12 description("Test statechange event is properly signaled")
14 window
.jsTestIsAsync
= true;
16 var secondsToRender
= 2;
17 var sampleRate
= 48000;
19 var stateChangeCount
= 0;
23 function checkStateChange (e
) {
24 contextState
= e
.currentTarget
.state
;
26 switch (stateChangeCount
) {
28 shouldBeEqualToString("contextState", "running");
31 shouldBeEqualToString("contextState", "closed");
34 testFailed("Expected only two state changes but got " + stateChangeCount
);
39 function finalCheck() {
40 // Final check that we got the right number of state changes and the correct final state.
41 shouldBeEqualToNumber("stateChangeCount", 2);
42 shouldBeEqualToString("context.state", "closed");
47 // Create an offline context with a source passing through a convolver. The convolver is
48 // just to waste some time.
49 context
= new OfflineAudioContext(1, secondsToRender
* sampleRate
, sampleRate
);
50 var buffer
= createImpulseBuffer(context
, sampleRate
);
51 var source
= context
.createBufferSource();
52 var conv
= context
.createConvolver();
54 source
.buffer
= buffer
;
55 conv
.normalize
= false;
59 conv
.connect(context
.destination
);
63 context
.onstatechange
= checkStateChange
;
65 context
.startRendering().then(function () {
66 testPassed("context finished rendering")
69 // Don't want to set an oncomplete for the context and don't want to use the promise because
70 // the order of the state change event and resolving the promise is not specified. Thus,
71 // just wait for a bit and then finish the test. We assume the offline context runs faster
73 setTimeout(finalCheck
, secondsToRender
* 1000);