| OLD | NEW |
| 1 <!doctype html> | 1 <!doctype html> |
| 2 <html> | 2 <html> |
| 3 <head> | 3 <head> |
| 4 <script src="../../resources/js-test.js"></script> | 4 <script src="../../resources/testharness.js"></script> |
| 5 <script src="../resources/audit-util.js"></script> | 5 <script src="../../resources/testharnessreport.js"></script> |
| 6 <script src="../resources/audio-testing.js"></script> | 6 <script src="../resources/audit.js"></script> |
| 7 <title>Test decodeAudioData promises</title> | 7 <title>Test decodeAudioData promises</title> |
| 8 </head> | 8 </head> |
| 9 | |
| 10 <body> | 9 <body> |
| 11 <script> | 10 <script> |
| 12 description("Basic tests for decodeAudioData promise."); | 11 // Use offline context for decoding because we want a fixed known sample |
| 13 window.jsTestIsAsync = true; | 12 // rate, independent of the hardware because the test file is encoded at |
| 13 // 44.1 kHz. If we don't, decodeAudioData() will resample the data messing |
| 14 // up the assumptions in this test. The length is unimportant. |
| 15 let context = new OfflineAudioContext(1, 1, 44100); |
| 14 | 16 |
| 15 // Use offline context for decoding because we want a fixed know sample rate
, independent of the | 17 // Test file URLs. |
| 16 // hardware because the test file is encoded at 44.1 kHz. If we don't decod
eAudioData will | 18 let validAudioFileUrl = '../resources/media/24bit-44khz.wav'; |
| 17 // resample the data messing up the assumptions in this test. The length is
unimportant. | 19 let invalidAudioFileUrl = '../resources/media/invalid-audio-file.txt'; |
| 18 var context = new OfflineAudioContext(1, 1, 44100); | |
| 19 | 20 |
| 20 // Test files for decodeAudioData | 21 // Global storage for array buffers from XHR. |
| 21 var validAudioFile = "../resources/media/24bit-44khz.wav"; | 22 let validArrayBuffer; |
| 22 var invalidAudioFile = "../resources/media/invalid-audio-file.txt"; | 23 let invalidArrayBuffer; |
| 23 | 24 |
| 24 // Decoded data from validAudioFile | 25 // Decoded data from validAudioFile. |
| 25 var referenceDecodedAudioBuffer; | 26 let referenceDecodedAudioBuffer; |
| 26 // Encoded audio data for testing decodeAudioData after the context has been
closed. | |
| 27 var encodedAudioData; | |
| 28 // Decoded data from decodeAudioData after the context has been closed. | |
| 29 var decodedAudioBufferAfterClose; | |
| 30 | 27 |
| 31 // Utility to load an encoded audio file from |url| and decode it. |success
| and |failure| are | 28 let audit = Audit.createTaskRunner(); |
| 32 // functions to handle the then and else cases of the promise returned by de
codeAudioData. | |
| 33 function runDecode(url, success, failure, done) { | |
| 34 var request = new XMLHttpRequest(); | |
| 35 request.open("GET", url, true); | |
| 36 request.responseType = "arraybuffer"; | |
| 37 | 29 |
| 38 request.onload = function () { | 30 // Preload ArrayBuffer and the reference AudioBuffer from URLs. |
| 39 context.decodeAudioData(request.response) | 31 audit.define('preload-arraybuffer', (task, should) => { |
| 40 .then(success, failure) | 32 Promise |
| 41 .then(done); | 33 .all([ |
| 34 Audit.loadFileFromUrl(validAudioFileUrl), |
| 35 Audit.loadFileFromUrl(invalidAudioFileUrl) |
| 36 ]) |
| 37 .then((arrayBuffers) => { |
| 38 validArrayBuffer = arrayBuffers[0]; |
| 39 invalidArrayBuffer = arrayBuffers[1]; |
| 40 context.decodeAudioData(validArrayBuffer).then((audioBuffer) => { |
| 41 referenceDecodedAudioBuffer = audioBuffer; |
| 42 task.done(); |
| 43 }) |
| 44 }); |
| 45 }); |
| 46 |
| 47 // Decode a valid encoded file and verify that the promise succeeds |
| 48 // correctly. |
| 49 audit.define('decode-valid-file', (task, should) => { |
| 50 // Note that the order of completion for each promise is undefined and |
| 51 // we do not care about it in this test. |
| 52 Promise |
| 53 .all([ |
| 54 should(context.decodeAudioData(validArrayBuffer), |
| 55 'Decoding a valid audio file') |
| 56 .beResolved(), |
| 57 should(context.decodeAudioData(invalidArrayBuffer), |
| 58 'Decoding an invalid audio file') |
| 59 .beRejected('EncodingError'), |
| 60 should(context.decodeAudioData(null), 'Decoding null AudioBuffer') |
| 61 .beRejected() |
| 62 ]) |
| 63 .then(() => task.done()); |
| 64 }); |
| 65 |
| 66 // Decode a valid file and verify that the promise is fulfilled and the |
| 67 // successCallback is invoked and both have identical decoded audio buffers. |
| 68 audit.define("promise-and-success-callback", (task, should) => { |
| 69 let bufferByCallback; |
| 70 let bufferByPromise; |
| 71 |
| 72 // Use one callback for success and error. |callbackArg| is a parameter |
| 73 // for callback functions; it is a decoded audio buffer for success case |
| 74 // and an error object for failure case. |
| 75 let successOrErrorCallback = (callbackArg) => { |
| 76 should(callbackArg instanceof AudioBuffer, |
| 77 'Decoding valid file by callback function') |
| 78 .message('successCallback invoked correctly', |
| 79 'errorCallback incorrectly invoked with ' + callbackArg); |
| 80 bufferByCallback = callbackArg; |
| 42 }; | 81 }; |
| 43 | 82 |
| 44 request.send(); | 83 // Step 1: Decode a file with callback functions. |
| 45 } | 84 let step1 = context.decodeAudioData(validArrayBuffer, |
| 85 successOrErrorCallback, |
| 86 successOrErrorCallback); |
| 46 | 87 |
| 47 // Compare that two audio buffers are the same | 88 // Step 2: Then decode a file with promise pattern. |
| 48 function audioBuffersCompareEqual(actualBuffer, expectedBuffer) { | 89 let step2 = should(step1, 'Decoding a file via promise') |
| 49 var success; | 90 .beResolved() |
| 91 .then((audioBuffer) => { |
| 92 bufferByPromise = audioBuffer; |
| 93 }); |
| 50 | 94 |
| 51 success = Should("Decoded buffer length (frames)", actualBuffer.length).be
EqualTo(expectedBuffer.length); | 95 // Step 3: compare two buffers from Step 1 and Step 2. |
| 52 | 96 step2.then(() => { |
| 53 success = Should("Decoded buffer duration (sec)", | 97 should(bufferByCallback === bufferByPromise, |
| 54 actualBuffer.duration).beEqualTo(expectedBuffer.duration) && success; | 98 'Two buffers decoded by callback function and promise') |
| 55 | 99 .message('are identical', 'are different'); |
| 56 success = Should("Decoded buffer rate (Hz)", | 100 task.done(); |
| 57 actualBuffer.sampleRate).beEqualTo(expectedBuffer.sampleRate) && success
; | 101 }); |
| 58 | |
| 59 success = Should("Number of channels in decoded buffer", | |
| 60 actualBuffer.numberOfChannels).beEqualTo(expectedBuffer.numberOfChannels
) && success; | |
| 61 | |
| 62 for (var c = 0; c < expectedBuffer.numberOfChannels; ++c) { | |
| 63 var actualData = actualBuffer.getChannelData(c); | |
| 64 var expectedData = expectedBuffer.getChannelData(c); | |
| 65 success = Should("Decoded buffer channel " + c, actualData).beEqualToArr
ay(expectedData) && | |
| 66 success; | |
| 67 } | |
| 68 | |
| 69 return success; | |
| 70 } | |
| 71 // Tests | |
| 72 var audit = Audit.createTaskRunner(); | |
| 73 | |
| 74 // Test that a null audioBuffer causes the promise to be rejected with an In
validStateError. | |
| 75 audit.defineTask("null-audiobuffer", function (done) { | |
| 76 Should("decodeAudioData(null)", context.decodeAudioData(null)).beRejected(
) | |
| 77 .then(done); | |
| 78 }); | 102 }); |
| 79 | 103 |
| 80 // Decode a valid encoded file and verify that the promise succeeds correctl
y. | 104 // Decode an invalid file and verify that the promise is rejected and the |
| 81 audit.defineTask('decode-valid-file', function (done) { | 105 // errorCallback is invoked. |
| 82 var url = validAudioFile; | 106 audit.define("promise-and-error-callback", (task, should) => { |
| 83 var prefix = "Decode valid file with promise: "; | 107 let successOrErrorCallback = (callbackArg) => { |
| 84 runDecode(url, | 108 should(callbackArg instanceof Error, |
| 85 function (buffer) { | 109 'Decoding invalid file with promise and callback:') |
| 86 // Save the buffer for later testing. | 110 .message('errorCallback invoked correctly with ' + callbackArg, |
| 87 referenceDecodedAudioBuffer = buffer; | 111 'successCallback should not have invoked'); |
| 88 testPassed(prefix + "Correctly succeeded in decoding " + url); | 112 }; |
| 89 }, | 113 |
| 90 function (e) { | 114 let decodeAudioDataPromise = context.decodeAudioData( |
| 91 testFailed(prefix + "Incorrectly failed to decode " + url + ": " + e.t
oString()); | 115 invalidArrayBuffer, successOrErrorCallback, successOrErrorCallback); |
| 92 }, | 116 |
| 93 done); | 117 should(decodeAudioDataPromise, 'decodeAudioData promise') |
| 118 .beRejected('EncodingError') |
| 119 .then(() => task.done()); |
| 94 }); | 120 }); |
| 95 | 121 |
| 96 // Decode a invalid encoded file and verify that the promise is rejected cor
rectly. | 122 // decodeAudioData() should be functional even after the associated context |
| 97 audit.defineTask("decode-invalid-file", function (done) { | 123 // is closed. |
| 98 var url = invalidAudioFile; | 124 // TODO(crbug.com/692650) |
| 99 var prefix = "Decode invalid file with promise: "; | 125 audit.define('close-context-with-pending-decode', (task, should) => { |
| 100 runDecode(url, | 126 // Use one handler for resolve and reject. |promiseArg| is a parameter for |
| 101 function (buffer) { | 127 // handlers; it is a decoded audio buffer for success case and an error |
| 102 testFailed(prefix + "Incorrectly succeeded in decoding " + url); | 128 // object for failure case. |
| 103 }, | 129 let resolveOrReject = (promiseArg) => { |
| 104 function (e) { | 130 let didDecode = promiseArg instanceof AudioBuffer; |
| 105 testPassed(prefix + "Correctly failed to decode " + url + ": " + e.toS
tring()); | 131 |
| 106 }, | 132 if (didDecode) { |
| 107 done); | 133 // Compare two decoded AudioBuffers. |
| 134 let actual = promiseArg; |
| 135 let expected = referenceDecodedAudioBuffer; |
| 136 should(actual.length, 'Decoded buffer length (frames)') |
| 137 .beEqualTo(expected.length); |
| 138 should(actual.duration, 'Decoded buffer duration (sec)') |
| 139 .beEqualTo(expected.duration); |
| 140 should(actual.sampleRate, 'Decoded buffer sample rate (Hz)') |
| 141 .beEqualTo(expected.sampleRate); |
| 142 should(actual.numberOfChannels, |
| 143 'Number of channels in decoded buffer') |
| 144 .beEqualTo(expected.numberOfChannels); |
| 145 for (let c = 0; c < expected.numberOfChannels; ++c) { |
| 146 let actualChannelData = actual.getChannelData(c); |
| 147 let expectedChannelData = expected.getChannelData(c); |
| 148 should(actualChannelData, 'Decoded buffer channel #' + c) |
| 149 .beEqualToArray(expectedChannelData, |
| 150 'the expected channel #' + c); |
| 151 } |
| 152 should(task.state, 'The buffer') |
| 153 .message('correctly decoded after the context has been closed', |
| 154 'decoding succeeded but the data is incorrect'); |
| 155 } |
| 156 |
| 157 should(didDecode, 'Decoding ArrayBuffer after context has been closed') |
| 158 .message('completed successfully', 'failed : ' + promiseArg); |
| 159 }; |
| 160 |
| 161 let onlineContext = new AudioContext(); |
| 162 onlineContext.close() |
| 163 .then(() => { return context.decodeAudioData(validArrayBuffer); }) |
| 164 .then(resolveOrReject, resolveOrReject) |
| 165 .then(() => { task.done(); }); |
| 108 }); | 166 }); |
| 109 | 167 |
| 110 // Decode a valid file and verify that the promise is fulfilled and the succ
essCallback is | 168 audit.run(); |
| 111 // invoked and both have identical decode audio buffers. | 169 </script> |
| 112 audit.defineTask("promise-and-success-callback", function (done) { | |
| 113 var request = new XMLHttpRequest(); | |
| 114 request.open("GET", validAudioFile, true); | |
| 115 request.responseType = "arraybuffer"; | |
| 116 | |
| 117 request.onload = function () { | |
| 118 var prefix = "Decoding valid file with promise and callback: "; | |
| 119 // The buffer returned by the success callback | |
| 120 var callbackBuffer; | |
| 121 // The buffer returned by the promise | |
| 122 var promiseBuffer; | |
| 123 | |
| 124 context.decodeAudioData(request.response, function (buffer) { | |
| 125 testPassed(prefix + "successCallback invoked correctly"); | |
| 126 callbackBuffer = buffer; | |
| 127 }, function (e) { | |
| 128 testFailed(prefix + "errorCallback incorrectly invoked with " + e); | |
| 129 }) | |
| 130 .then(function (buffer) { | |
| 131 testPassed(prefix + "Promise correctly fulfilled"); | |
| 132 promiseBuffer = buffer; | |
| 133 }, function (e) { | |
| 134 testFailed(prefix + "Promise incorrectly rejected with " + e); | |
| 135 }) | |
| 136 .then(function () { | |
| 137 if (promiseBuffer === callbackBuffer) | |
| 138 testPassed(prefix + "Promise and successCallback returned the same
buffer"); | |
| 139 else | |
| 140 testFailed(prefix + | |
| 141 "Promise and successCallback returned different buffers: " + | |
| 142 promiseBuffer + " " + callbackBuffer); | |
| 143 }) | |
| 144 .then(done); | |
| 145 }; | |
| 146 | |
| 147 request.send(); | |
| 148 }); | |
| 149 | |
| 150 // Decode an invalid file and verify that the promise is rejected and the er
rorCallback is | |
| 151 // invoked. | |
| 152 audit.defineTask("promise-and-error-callback", function(done) { | |
| 153 var request = new XMLHttpRequest(); | |
| 154 request.open("GET", invalidAudioFile, true); | |
| 155 request.responseType = "arraybuffer"; | |
| 156 | |
| 157 request.onload = function() { | |
| 158 var prefix = "Decoding invalid file with promise and callback:"; | |
| 159 | |
| 160 Should(prefix, context.decodeAudioData(request.response, function () { | |
| 161 testFailed(prefix + " successCallback invoked but should not have be
en"); | |
| 162 }, function (e) { | |
| 163 testPassed(prefix + " errorCallback invoked correctly with: " + e); | |
| 164 })).beRejected().then(done, done); | |
| 165 }; | |
| 166 | |
| 167 request.send(); | |
| 168 }); | |
| 169 | |
| 170 // Just load up a file so we can run decodeAudioData on it | |
| 171 audit.defineTask("load-data", function (done) { | |
| 172 var request = new XMLHttpRequest(); | |
| 173 request.open("GET", validAudioFile, true); | |
| 174 request.responseType = "arraybuffer"; | |
| 175 | |
| 176 request.onload = function () { | |
| 177 encodedAudioData = request.response; | |
| 178 done(); | |
| 179 }; | |
| 180 | |
| 181 request.send(); | |
| 182 }); | |
| 183 | |
| 184 // If the context is closing before decodeAudioData has finished decoding, w
e should reject the | |
| 185 // promise from decodeAudioData. | |
| 186 audit.defineTask("close-context-with-pending-decode", function (done) { | |
| 187 var onlineContext = new AudioContext(); | |
| 188 onlineContext.close() | |
| 189 .then(function () { | |
| 190 return context.decodeAudioData(encodedAudioData); | |
| 191 }) | |
| 192 .then(function (buffer) { | |
| 193 // Compare this buffer with the reference decoded buffer (that we ob
tained earlier). Pass | |
| 194 // if they're identical. | |
| 195 if (audioBuffersCompareEqual(buffer, referenceDecodedAudioBuffer)) | |
| 196 testPassed("Correctly decoded data after the context has been clos
ed"); | |
| 197 else | |
| 198 testFailed("decodeAudioData succeeded, but data is incorrect"); | |
| 199 }, | |
| 200 function (e) { | |
| 201 testFailed("Failed to decode valid file after context has been close
d: " + e); | |
| 202 }) | |
| 203 .then(done, done); | |
| 204 }); | |
| 205 | |
| 206 audit.defineTask("finish", function (done) { | |
| 207 finishJSTest(); | |
| 208 done(); | |
| 209 }); | |
| 210 | |
| 211 audit.runTasks(); | |
| 212 | |
| 213 successfullyParsed = true; | |
| 214 </script> | |
| 215 </body> | 170 </body> |
| 216 </html> | 171 </html> |
| OLD | NEW |