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

Unified Diff: LayoutTests/webaudio/decode-audio-data-promise.html

Issue 1006963003: AudioContext.decodeAudioData returns a Promise (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Compile with oilpan Created 5 years, 8 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: LayoutTests/webaudio/decode-audio-data-promise.html
diff --git a/LayoutTests/webaudio/decode-audio-data-promise.html b/LayoutTests/webaudio/decode-audio-data-promise.html
new file mode 100644
index 0000000000000000000000000000000000000000..e529f2f549c51200a8a28c7816bbef413f3654d1
--- /dev/null
+++ b/LayoutTests/webaudio/decode-audio-data-promise.html
@@ -0,0 +1,232 @@
+<!doctype html>
+<html>
+<head>
+ <title>Test decodeAudioData promises</title>
+
+ <script src="../resources/js-test.js"></script>
+ <script src="resources/compatibility.js"></script>
+ <script src="resources/audio-testing.js"></script>
+</head>
+
+<body>
+ <script>
+ description("Basic tests for decodeAudioData promise.");
+
+ window.jsTestIsAsync = true;
+
+ var context = new AudioContext();
+ // Test files for decodeAudioData
+ var validAudioFile = "resources/media/24bit-44khz.wav";
+ var invalidAudioFile = "resources/media/invalid-audio-file.txt";
+
+ // Decoded data from validAudioFile
+ var referenceDecodedAudioBuffer;
+ // Encoded audio data for testing decodeAudioData after the context has been closed.
+ var encodedAudioData;
+ // 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) {
+ if (actualBuffer.length != expectedBuffer.length) {
+ testFailed("Decoded buffer length is " + actualBuffer.length + ", but expected " + expectedBuffer.length);
+ return false;
+ }
+
+ if (actualBuffer.duration != expectedBuffer.duration) {
+ testFailed("Decoded buffer duration is " + actualBuffer.duration + ", but expected " + expectedBuffer.duration);
+ return false;
+ }
+
+ if (actualBuffer.sampleRate != expectedBuffer.sampleRate) {
+ //testFailed("Decoded buffer rate is " + actualBuffer.sampleRate + ", but expected " + expectedBuffer.sampleRate);
+ return false;
+ }
+
+ if (actualBuffer.numberOfChannels != expectedBuffer.numberOfChannels) {
+ //testFailed("Decoded buffer has " + actualBuffer.numberOfChannels + "channels , but expected " + expectedBuffer.numberOfChannels);
+ return false;
+ }
+
+ for (var c = 0; c < expectedBuffer.numberOfChannels; ++c) {
+ var actualData = actualBuffer.getChannelData(c);
+ var expectedData = expectedBuffer.getChannelData(c);
+ for (var k = 0; k < expectedData.length; ++k) {
+ if (actualData[k] != expectedData[k]) {
+ //testFailed("Decoded buffer differs from expected at channel " + c + " frame " + k + ": " + actualData[k] + ", expected " + expectedData[k]);
+ return false;
+ }
+ }
+ }
+
+ return true;
+ }
+
+ // 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) {
+ context.decodeAudioData(null).then(function () {
+ testFailed("decodeAudioData(null) incorrectly resolved promise successfully.");
+ }, function (e) {
+ testPassed("decodeAudioData(null) correctly rejected promise: " + e.toString());
+ }).then(done);
+ });
+
+ // Decode a valid encoded file and verify that the promise succeeds correctly.
+ audit.defineTask('decode-valid-file', function (done) {
+ var url = validAudioFile;
+ runDecode(url,
+ function (buffer) {
+ // Save the buffer for later testing.
+ referenceDecodedAudioBuffer = buffer;
+ testPassed("Correctly succeeded in decoding " + url);
+ },
+ function (e) {
+ testFailed("Incorrectly failed to decode " + url + ": " + e.toString());
+ }, done);
+ });
+
+ // Decode a valid encoded file and verify that the promise is rejected correctly.
+ audit.defineTask("decode-invalid-file", function (done) {
+ var url = invalidAudioFile;
+ runDecode(url,
+ function (buffer) {
+ testFailed("Incorrectly succeeded in decoding " + url);
+ },
+ function (e) {
+ testPassed("Correctly failed to decode " + url + ": " + e.toString());
+ }, 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 p;
+ var prefix = "Decoding valid file: ";
+ p = context.decodeAudioData(request.response,
yhirano 2015/04/10 04:55:23 context.decodeAudioData(request.response, function
Raymond Toy 2015/04/10 21:00:45 Done.
+ function (callbackBuffer) {
+ p.then(function (promiseBuffer) {
+ if (promiseBuffer == callbackBuffer)
+ testPassed(prefix + "Promise and successCallback succeeded with identical buffers");
+ else
+ testFailed(prefix + "Promise and successCallback succeeded with NON-identical buffers");
+ }).then(done);
+ },
+ function (e) {
+ p.then(function () {
+ testFailed(prefix + "Promise fulfilled but errorCallback invoked!!!");
+ }, function () {
+ testFailed(prefix + "Expected decoding to succeed but promise rejected and errorCallback invoked:" + e);
+ }).then(done);
+ });
+ };
+
+ request.send();
+ });
+
+ // 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 p;
+ var prefix = "Decoding invalid file: ";
+ p = context.decodeAudioData(request.response,
yhirano 2015/04/10 04:55:23 context.decodeAudioData(request.response, function
Raymond Toy 2015/04/10 21:00:45 Done.
+ function () {
+ p.then(function () {
+ testFailed(prefix + "Expected decoding to fail but promise fulfilled and successCallback invoked!!!");
+ }, function (e) {
+ testFailed(prefix + "Expected decoding to fail but promise rejected and successCallback invoked:" + e + "!!!");
+ }).then(done);
+ },
+ function () {
+ p.then(function () {
+ testFailed(prefix + "Promise erroneously fulfilled but errorCallback correctly invoked!!!");
+ }, function (e) {
+ testPassed(prefix + "Promise correctly rejected and errorCallback correctly invoked: " + e);
+ }).then(done);
+ });
+ };
+
+ request.send();
+ });
+
+ // 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();
+ });
+
+ // 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) {
+ context.close()
yhirano 2015/04/10 04:55:23 You don't need nested |then|. context.clonse().th
Raymond Toy 2015/04/10 21:00:45 Done.
+ .then(function () {
+ context.decodeAudioData(encodedAudioData).then(function (b) {
+ decodedAudioBufferAfterClose = b;
+ // Compare this buffer with the previously decoded buffer. Pass if they're identical.
+ if (audioBuffersCompareEqual(decodedAudioBufferAfterClose, 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);
+ });
+ });
+
+ audit.defineTask("finish", function (done) {
+ finishJSTest();
+ done();
+ });
+
+ audit.runTasks(
+ "null-audiobuffer",
+ "decode-valid-file",
+ "decode-invalid-file",
+ "promise-and-success-callback",
+ "promise-and-error-callback",
+ "load-data",
+ "close-context-with-pending-decode",
+ "finish"
+ );
+
+ successfullyParsed = true;
+ </script>
+</body>
+</html>

Powered by Google App Engine
This is Rietveld 408576698