Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(617)

Side by Side Diff: third_party/WebKit/LayoutTests/webaudio/decodeAudioData/decode-audio-data-basic.html

Issue 2697733004: Refactor decode-audio-data-basic.html to use testharness (Closed)
Patch Set: Comments and clang-format (clean up #2) Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 know sample
Raymond Toy 2017/02/15 23:15:52 Typo: "know" -> "known"
hongchan 2017/02/15 23:40:35 Done.
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 ])
Raymond Toy 2017/02/15 23:15:52 This works because should().beResolved() and .beRe
hongchan 2017/02/15 23:40:36 Yes. The order can be flaky as noted, but it shoul
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/error; it is easier to apply should()
73 // assertion.
74 let successOrErrorCallback = (callbackArg) => {
75 should(callbackArg instanceof AudioBuffer,
76 'Decoding valid file by callback function')
Raymond Toy 2017/02/15 23:15:52 A comment stating what callbackArg should be is us
hongchan 2017/02/15 23:40:35 Done. Yes, you're correct.
77 .message('successCallback invoked correctly',
78 'errorCallback incorrectly invoked with ' + callbackArg);
79 bufferByCallback = callbackArg;
42 }; 80 };
43 81
44 request.send(); 82 // Step 1: Decode a file with callback functions.
45 } 83 let step1 = context.decodeAudioData(validArrayBuffer,
84 successOrErrorCallback,
85 successOrErrorCallback);
46 86
47 // Compare that two audio buffers are the same 87 // Step 2: Then decode a file with promise pattern.
48 function audioBuffersCompareEqual(actualBuffer, expectedBuffer) { 88 let step2 = should(step1, 'Decoding a file via promise')
49 var success; 89 .beResolved()
90 .then((audioBuffer) => {
91 bufferByPromise = audioBuffer;
92 });
50 93
51 success = Should("Decoded buffer length (frames)", actualBuffer.length).be EqualTo(expectedBuffer.length); 94 // Step 3: compare two buffers from Step 1 and Step 2.
Raymond Toy 2017/02/15 23:15:52 Is it guaranteed that the callbacks have fired by
hongchan 2017/02/15 23:40:36 Not guaranteed, and that needs to be fixed in a fo
Raymond Toy 2017/02/16 03:16:30 A TODO with a crbug link is good enough.
52 95 step2.then(() => {
53 success = Should("Decoded buffer duration (sec)", 96 should(bufferByCallback === bufferByPromise,
54 actualBuffer.duration).beEqualTo(expectedBuffer.duration) && success; 97 'Two buffers decoded by callback function and promise')
55 98 .message('are identical', 'are different');
56 success = Should("Decoded buffer rate (Hz)", 99 task.done();
57 actualBuffer.sampleRate).beEqualTo(expectedBuffer.sampleRate) && success ; 100 });
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 }); 101 });
79 102
80 // Decode a valid encoded file and verify that the promise succeeds correctl y. 103 // Decode an invalid file and verify that the promise is rejected and the
81 audit.defineTask('decode-valid-file', function (done) { 104 // errorCallback is invoked.
82 var url = validAudioFile; 105 audit.define("promise-and-error-callback", (task, should) => {
83 var prefix = "Decode valid file with promise: "; 106 let successOrErrorCallback = (callbackArg) => {
84 runDecode(url, 107 should(callbackArg instanceof Error,
85 function (buffer) { 108 'Decoding invalid file with promise and callback:')
86 // Save the buffer for later testing. 109 .message('errorCallback invoked correctly with ' + callbackArg,
87 referenceDecodedAudioBuffer = buffer; 110 'successCallback should not have invoked');
88 testPassed(prefix + "Correctly succeeded in decoding " + url); 111 };
89 }, 112
90 function (e) { 113 let decodeAudioDataPromise = context.decodeAudioData(
91 testFailed(prefix + "Incorrectly failed to decode " + url + ": " + e.t oString()); 114 invalidArrayBuffer, successOrErrorCallback, successOrErrorCallback);
92 }, 115
93 done); 116 should(decodeAudioDataPromise, 'decodeAudioData promise')
117 .beRejected('EncodingError')
118 .then(() => task.done());
94 }); 119 });
95 120
96 // Decode a invalid encoded file and verify that the promise is rejected cor rectly. 121 // decodeAudioData() should be functional even after the associated context
97 audit.defineTask("decode-invalid-file", function (done) { 122 // is closed.
98 var url = invalidAudioFile; 123 // TODO(hongchan): This test needs to use the same context for decoding.
99 var prefix = "Decode invalid file with promise: "; 124 // See crbug.com/692650
Raymond Toy 2017/02/15 23:15:53 I think you can say TODO(crbug.com/692650).
hongchan 2017/02/15 23:40:35 Done. Is this style conventional?
Raymond Toy 2017/02/16 03:16:30 I've seen this in other places, perhaps in chromiu
100 runDecode(url, 125 audit.define('close-context-with-pending-decode', (task, should) => {
101 function (buffer) { 126 let resolveOrReject = (promiseArg) => {
Raymond Toy 2017/02/15 23:15:52 A comment on what resolveOrReject means would be u
hongchan 2017/02/15 23:40:36 Done.
102 testFailed(prefix + "Incorrectly succeeded in decoding " + url); 127 let didDecode = promiseArg instanceof AudioBuffer;
103 }, 128
104 function (e) { 129 if (didDecode) {
105 testPassed(prefix + "Correctly failed to decode " + url + ": " + e.toS tring()); 130 // Compare two decoded AudioBuffers.
106 }, 131 let actual = promiseArg;
107 done); 132 let expected = referenceDecodedAudioBuffer;
133 should(actual.length, 'Decoded buffer length (frames)')
134 .beEqualTo(expected.length);
135 should(actual.duration, 'Decoded buffer duration (sec)')
136 .beEqualTo(expected.duration);
137 should(actual.sampleRate, 'Decoded buffer sample rate (Hz)')
138 .beEqualTo(expected.sampleRate);
139 should(actual.numberOfChannels,
140 'Number of channels in decoded buffer')
141 .beEqualTo(expected.numberOfChannels);
142 for (let c = 0; c < expected.numberOfChannels; ++c) {
143 let actualChannelData = actual.getChannelData(c);
144 let expectedChannelData = expected.getChannelData(c);
145 should(actualChannelData, 'Decoded buffer channel #' + c)
146 .beEqualToArray(expectedChannelData,
147 'the expected channel #' + c);
Raymond Toy 2017/02/15 23:15:53 I didn't know you could do this! Nice!
hongchan 2017/02/15 23:40:36 :D
148 }
149 should(task.state, 'The buffer')
150 .message('correctly decoded after the context has been closed',
151 'decoding succeeded but the data is incorrect');
152 }
153
154 should(didDecode, 'Decoding ArrayBuffer after context has been closed')
155 .message('completed successfully', 'failed : ' + promiseArg);
156 };
157
158 let onlineContext = new AudioContext();
159 onlineContext.close()
160 .then(() => { return context.decodeAudioData(validArrayBuffer); })
161 .then(resolveOrReject, resolveOrReject)
162 .then(() => { task.done(); });
108 }); 163 });
109 164
110 // Decode a valid file and verify that the promise is fulfilled and the succ essCallback is 165 audit.run();
111 // invoked and both have identical decode audio buffers. 166 </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> 167 </body>
216 </html> 168 </html>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698