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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/LayoutTests/webaudio/decodeAudioData/decode-audio-data-basic.html
diff --git a/third_party/WebKit/LayoutTests/webaudio/decodeAudioData/decode-audio-data-basic.html b/third_party/WebKit/LayoutTests/webaudio/decodeAudioData/decode-audio-data-basic.html
index 7442dcfdb1d00e3efd9bfd09b06a8292be2e70a4..b1d95e2f6c018c7833db456a7bcf725c80b57a5b 100644
--- a/third_party/WebKit/LayoutTests/webaudio/decodeAudioData/decode-audio-data-basic.html
+++ b/third_party/WebKit/LayoutTests/webaudio/decodeAudioData/decode-audio-data-basic.html
@@ -1,17 +1,15 @@
<!doctype html>
<html>
<head>
- <script src="../../resources/js-test.js"></script>
- <script src="../resources/audit-util.js"></script>
- <script src="../resources/audio-testing.js"></script>
+ <script src="../../resources/testharness.js"></script>
+ <script src="../../resources/testharnessreport.js"></script>
+ <script src="../resources/audit.js"></script>
+ <!-- <script src="../resources/audio-testing.js"></script> -->
<title>Test decodeAudioData promises</title>
</head>
<body>
<script>
- description("Basic tests for decodeAudioData promise.");
- window.jsTestIsAsync = true;
-
// Use offline context for decoding because we want a fixed know sample rate, independent of the
// hardware because the test file is encoded at 44.1 kHz. If we don't decodeAudioData will
// resample the data messing up the assumptions in this test. The length is unimportant.
@@ -28,189 +26,157 @@
// Decoded data from decodeAudioData after the context has been closed.
var decodedAudioBufferAfterClose;
- // Utility to load an encoded audio file from |url| and decode it. |success| and |failure| are
- // functions to handle the then and else cases of the promise returned by decodeAudioData.
- function runDecode(url, success, failure, done) {
- var request = new XMLHttpRequest();
- request.open("GET", url, true);
- request.responseType = "arraybuffer";
-
- request.onload = function () {
- context.decodeAudioData(request.response)
- .then(success, failure)
- .then(done);
- };
-
- request.send();
- }
-
// Compare that two audio buffers are the same
- function audioBuffersCompareEqual(actualBuffer, expectedBuffer) {
- var success;
-
- success = Should("Decoded buffer length (frames)", actualBuffer.length).beEqualTo(expectedBuffer.length);
-
- success = Should("Decoded buffer duration (sec)",
- actualBuffer.duration).beEqualTo(expectedBuffer.duration) && success;
-
- success = Should("Decoded buffer rate (Hz)",
- actualBuffer.sampleRate).beEqualTo(expectedBuffer.sampleRate) && success;
-
- success = Should("Number of channels in decoded buffer",
- actualBuffer.numberOfChannels).beEqualTo(expectedBuffer.numberOfChannels) && success;
-
- for (var c = 0; c < expectedBuffer.numberOfChannels; ++c) {
- var actualData = actualBuffer.getChannelData(c);
- var expectedData = expectedBuffer.getChannelData(c);
- success = Should("Decoded buffer channel " + c, actualData).beEqualToArray(expectedData) &&
- success;
+ function audioBuffersCompareEqual(actualBuffer, expectedBuffer, should) {
+ should(actualBuffer.length, 'Decoded buffer length (frames)')
+ .beEqualTo(expectedBuffer.length);
+ should(actualBuffer.duration, 'Decoded buffer duration (sec)')
+ .beEqualTo(expectedBuffer.duration);
+ should(actualBuffer.sampleRate, 'Decoded buffer sample rate (Hz)')
+ .beEqualTo(expectedBuffer.sampleRate);
+ should(actualBuffer.numberOfChannels,
+ 'Number of channels in decoded buffer')
+ .beEqualTo(expectedBuffer.numberOfChannels);
+ for (let c = 0; c < expectedBuffer.numberOfChannels; ++c) {
+ let actualChannelData = actualBuffer.getChannelData(c);
+ let expectedChannelData = expectedBuffer.getChannelData(c);
+ should(actualChannelData, 'Decoded buffer channel #' + c)
+ .beEqualToArray(expectedChannelData, 'the expected channel #' + c);
}
-
- return success;
}
+
// Tests
var audit = Audit.createTaskRunner();
// Test that a null audioBuffer causes the promise to be rejected with an InvalidStateError.
- audit.defineTask("null-audiobuffer", function (done) {
- Should("decodeAudioData(null)", context.decodeAudioData(null)).beRejected()
- .then(done);
+ audit.define('null-audiobuffer', function (task, should) {
+ should(context.decodeAudioData(null), 'Decoding null AudioBuffer')
+ .beRejected()
+ .then(() => task.done());
});
// Decode a valid encoded file and verify that the promise succeeds correctly.
- audit.defineTask('decode-valid-file', function (done) {
- var url = validAudioFile;
- var prefix = "Decode valid file with promise: ";
- runDecode(url,
- function (buffer) {
- // Save the buffer for later testing.
- referenceDecodedAudioBuffer = buffer;
- testPassed(prefix + "Correctly succeeded in decoding " + url);
- },
- function (e) {
- testFailed(prefix + "Incorrectly failed to decode " + url + ": " + e.toString());
- },
- done);
+ audit.define('decode-valid-file', function (task, should) {
+ Audit.loadFileAtUrl(validAudioFile).then((response) => {
+ should(context.decodeAudioData(response),
+ 'Decoding a valid audio file')
+ .beResolved()
+ .then((audioBuffer) => {
+ referenceDecodedAudioBuffer = audioBuffer;
+ task.done();
+ });
+ });
});
// Decode a invalid encoded file and verify that the promise is rejected correctly.
- audit.defineTask("decode-invalid-file", function (done) {
- var url = invalidAudioFile;
- var prefix = "Decode invalid file with promise: ";
- runDecode(url,
- function (buffer) {
- testFailed(prefix + "Incorrectly succeeded in decoding " + url);
- },
- function (e) {
- testPassed(prefix + "Correctly failed to decode " + url + ": " + e.toString());
- },
- done);
+ audit.define("decode-invalid-file", function (task, should) {
+ Audit.loadFileAtUrl(invalidAudioFile).then((response) => {
+ should(context.decodeAudioData(response),
+ 'Decoding an invalid audio file')
+ .beRejected('EncodingError')
+ .then(() => task.done());
+ });
});
// Decode a valid file and verify that the promise is fulfilled and the successCallback is
// invoked and both have identical decode audio buffers.
- audit.defineTask("promise-and-success-callback", function (done) {
- var request = new XMLHttpRequest();
- request.open("GET", validAudioFile, true);
- request.responseType = "arraybuffer";
-
- request.onload = function () {
- var prefix = "Decoding valid file with promise and callback: ";
- // The buffer returned by the success callback
- var callbackBuffer;
- // The buffer returned by the promise
- var promiseBuffer;
-
- context.decodeAudioData(request.response, function (buffer) {
- testPassed(prefix + "successCallback invoked correctly");
- callbackBuffer = buffer;
- }, function (e) {
- testFailed(prefix + "errorCallback incorrectly invoked with " + e);
- })
- .then(function (buffer) {
- testPassed(prefix + "Promise correctly fulfilled");
- promiseBuffer = buffer;
- }, function (e) {
- testFailed(prefix + "Promise incorrectly rejected with " + e);
- })
- .then(function () {
- if (promiseBuffer === callbackBuffer)
- testPassed(prefix + "Promise and successCallback returned the same buffer");
- else
- testFailed(prefix +
- "Promise and successCallback returned different buffers: " +
- promiseBuffer + " " + callbackBuffer);
- })
- .then(done);
- };
-
- request.send();
+ audit.define("promise-and-success-callback", function (task, should) {
+ Audit.loadFileAtUrl(validAudioFile).then((response) => {
+ let bufferByCallback;
+ let bufferByPromise;
+
+ // Use one callback for success/error; it is easier to apply should()
+ // assertion.
+ let callbackFunc = (callbackResult) => {
+ should(callbackResult instanceof AudioBuffer,
+ 'Decoding valid file by callback function')
+ .message('successCallback invoked correctly',
+ 'errorCallback incorrectly invoked with '
+ + callbackResult);
+ bufferByCallback = callbackResult;
+ };
+
+ // Step 1: Decode a file with callback functions.
+ let step1 = context.decodeAudioData(response,
+ callbackFunc,
+ callbackFunc);
+
+ // Step 2: Then decode a file with promise pattern.
+ let step2 = should(step1, 'Decoding a file via promise')
+ .beResolved()
+ .then((audioBuffer) => {
+ bufferByPromise = audioBuffer;
+ });
+
+ // Step 3: compare two buffers from Step 1 and Step 2.
+ step2.then(() => {
+ should(bufferByCallback === bufferByPromise,
+ 'Two buffers decoded by callback function and promise')
+ .message('are identical', 'are different');
+ task.done();
+ });
+ });
});
// Decode an invalid file and verify that the promise is rejected and the errorCallback is
// invoked.
- audit.defineTask("promise-and-error-callback", function(done) {
- var request = new XMLHttpRequest();
- request.open("GET", invalidAudioFile, true);
- request.responseType = "arraybuffer";
-
- request.onload = function() {
- var prefix = "Decoding invalid file with promise and callback:";
-
- Should(prefix, context.decodeAudioData(request.response, function () {
- testFailed(prefix + " successCallback invoked but should not have been");
- }, function (e) {
- testPassed(prefix + " errorCallback invoked correctly with: " + e);
- })).beRejected().then(done, done);
- };
-
- request.send();
+ audit.define("promise-and-error-callback", function (task, should) {
+ Audit.loadFileAtUrl(invalidAudioFile).then((response) => {
+
+ let callbackFunc = (callbackResult) => {
+ should(callbackResult instanceof Error,
+ 'Decoding invalid file with promise and callback:')
+ .message('errorCallback invoked correctly with ' + callbackResult,
+ 'successCallback should not have invoked');
+ };
+
+ let decodeAudioDataPromise =
+ context.decodeAudioData(response, callbackFunc, callbackFunc);
+
+ should(decodeAudioDataPromise, 'decodeAudioData promise')
+ .beRejected('EncodingError')
+ .then(() => task.done());
+ });
});
// Just load up a file so we can run decodeAudioData on it
- audit.defineTask("load-data", function (done) {
- var request = new XMLHttpRequest();
- request.open("GET", validAudioFile, true);
- request.responseType = "arraybuffer";
-
- request.onload = function () {
- encodedAudioData = request.response;
- done();
- };
-
- request.send();
+ audit.define('load-data', function (task, should) {
+ Audit.loadFileAtUrl(validAudioFile).then((response) => {
+ encodedAudioData = response;
+ task.done();
+ });
});
// If the context is closing before decodeAudioData has finished decoding, we should reject the
// promise from decodeAudioData.
- audit.defineTask("close-context-with-pending-decode", function (done) {
- var onlineContext = new AudioContext();
- onlineContext.close()
- .then(function () {
- return context.decodeAudioData(encodedAudioData);
- })
- .then(function (buffer) {
- // Compare this buffer with the reference decoded buffer (that we obtained earlier). Pass
- // if they're identical.
- if (audioBuffersCompareEqual(buffer, referenceDecodedAudioBuffer))
- testPassed("Correctly decoded data after the context has been closed");
- else
- testFailed("decodeAudioData succeeded, but data is incorrect");
- },
- function (e) {
- testFailed("Failed to decode valid file after context has been closed: " + e);
- })
- .then(done, done);
- });
+ audit.define('close-context-with-pending-decode', function (task, should) {
+ let onlineContext = new AudioContext();
+ let promiseFromContextClose = onlineContext.close();
+
+ let resolveOrReject = (callbackResult) => {
+ let didDecode = callbackResult instanceof AudioBuffer;
+ if (didDecode) {
+ audioBuffersCompareEqual(callbackResult,
+ referenceDecodedAudioBuffer,
+ should);
+ should(task.state, 'The buffer')
+ .message('correctly decoded after the context has been closed',
+ 'decoding succeeded but the data is incorrect');
+ }
+ should(didDecode, 'Decoding valid file')
+ .message('completed successfully',
+ 'failed after context has been closed: ' + callbackResult);
+ };
- audit.defineTask("finish", function (done) {
- finishJSTest();
- done();
+ promiseFromContextClose
+ .then(() => {
+ return context.decodeAudioData(encodedAudioData);
+ })
+ .then(resolveOrReject, resolveOrReject)
+ .then(() => task.done());
});
- audit.runTasks();
-
- successfullyParsed = true;
+ audit.run();
</script>
</body>
</html>

Powered by Google App Engine
This is Rietveld 408576698