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

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

Powered by Google App Engine
This is Rietveld 408576698