4 <title>Test AudioContext.close()
</title>
5 <script src=
"../resources/js-test.js"></script>
6 <script src=
"resources/compatibility.js"></script>
7 <script src=
"resources/audio-testing.js"></script>
12 description("Basic functionality test of closing an AudioContext");
13 window
.jsTestIsAsync
= true;
23 var wave
= new Float32Array(1);
25 var audit
= Audit
.createTaskRunner();
27 // Task: test online context (1).
28 audit
.defineTask('test-online-context-1', function (done
) {
30 // Create a context and verify that the various states are correct and
31 // that close() exists.
32 shouldNotThrow("context = new AudioContext()");
33 shouldBeEqualToString("context.state", "running");
35 // Create gain and oscillator for testing later.
36 shouldNotThrow("osc = context.createOscillator()");
37 shouldNotThrow("gain = context.createGain()");
38 destination
= context
.destination
;
39 shouldNotThrow("gain.connect(context.destination)");
41 // Close the context. When the promise is resolved, continue the next
45 testPassed("context.close() was correctly resolved");
46 shouldNotThrow("gain.disconnect(destination)");
49 testFailed("context.close() was erroneously rejected");
55 // Task: test online context (2).
56 audit
.defineTask('test-online-context-2', function (done
) {
58 // Context is closed, so verify that we cannot create any more nodes,
60 shouldThrow("context.createAnalyser()");
61 shouldThrow("context.createBiquadFilter()");
63 // createBuffer is an exception because it's not really tied in any way
64 // to an audio context. And it's useful to be able to create a buffer
65 // inside the oncomplete event of an offline context to use for testing
67 shouldNotThrow("context.createBuffer(1, 1, 48000)");
69 shouldThrow("context.createBufferSource()");
70 shouldThrow("context.createChannelMerger()");
71 shouldThrow("context.createChannelSplitter()");
72 shouldThrow("context.createConvolver()");
73 shouldThrow("context.createDelay()");
74 shouldThrow("context.createDynamicsCompressor()");
75 shouldThrow("context.createGain()");
76 shouldThrow("context.createOscillator()");
77 shouldThrow("context.createPanner()");
78 shouldThrow("context.createPeriodicWave(wave, wave)");
79 shouldThrow("context.createScriptProcessor()");
80 shouldThrow("context.createStereoPanner()");
81 shouldThrow("context.createWaveShaper()");
83 shouldThrow("osc.connect(gain)");
84 shouldNotThrow("gain.disconnect()");
86 // Can't resume a context that is closed (released).
87 context
.resume().then(
89 testFailed("Attempt to resume a closed context erroneously succeeded");
92 testPassed("Attempt to resume a closed context was correctly rejected");
97 // Task: test online context (3).
98 audit
.defineTask('test-online-context-3', function (done
) {
100 // Try closing the context again. The promise should be rejected.
101 context
.close().then(
103 testFailed("Closing context again erroneously resolved successfully.");
106 testPassed("Closing context again correctly rejected promise.");
107 // Finally, run GC. The context should be gone, but this seems difficult to verify.
109 shouldBeNull("context.destination");
114 // Task: test offline context (1).
115 audit
.defineTask('test-offline-context-1', function (done
) {
117 // For an offline context, just check that if we try to close the context,
118 // nothing happens except that the promise returned by close() is rejected.
119 shouldNotThrow("offline = new OfflineAudioContext(1, 1000, 48000)");
120 shouldBeEqualToString("offline.state", "suspended");
121 offline
.close().then(
123 testFailed("Closing offline context erroneously resolved");
126 if (e
.name
=== "InvalidAccessError") {
127 testPassed("Closing offline context correctly rejected: " + e
);
129 testFailed("Closing offline context correctly rejected but expected InvalidAccessError, not: " + e
);
135 // Task: test offline context (2).
136 audit
.defineTask('test-offline-context-2', function (done
) {
139 offline
.close().then(
141 testFailed("Closing offline context again erroneously resolved");
144 testPassed("Closing offline context again correctly rejected");
149 // Render the context, and check for a valid state
150 offline
.oncomplete = function (event
) {
151 shouldBeEqualToString("event.target.state", "closed");
154 shouldNotThrow("offline.startRendering()");
160 audit
.defineTask('finish-test', function (done
) {
166 'test-online-context-1',
167 'test-online-context-2',
168 'test-online-context-3',
169 'test-offline-context-1',
170 'test-offline-context-2',
174 successfullyParsed
= true;