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

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: Fix idl; small cleanups Created 5 years, 9 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..fad1553a8799e1f62b6e236862fe22951e7f365e
--- /dev/null
+++ b/LayoutTests/webaudio/decode-audio-data-promise.html
@@ -0,0 +1,178 @@
+<!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 audit = Audit.createTaskRunner();
+
+ var context = new AudioContext();
+ var validAudioFile = "resources/media/24bit-44khz.wav";
+ var invalidAudioFile = "resources/media/invalid-audio-file.txt";
+ var encodedAudioData;
+
+ // 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);
+ });
+
+ // 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();
+ }
+
+ // 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) {
+ 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,
+ 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!");
+ }).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,
+ function () {
+ p.then(function () {
+ testFailed(prefix + "Expected decoding to fail but promise fulfilled and successCallback invoked!!!");
+ }, function () {
+ testFailed(prefix + "Expected decoding to fail but promise rejected and successCallback invoked!!!");
+ }).then(done);
+ },
+ function () {
+ p.then(function () {
+ testFailed(prefix + "Promise erroneously fulfilled but errorCallback correctly invoked!!!");
+ }, function () {
+ testPassed(prefix + "Promise correctly rejected and errorCallback correctly invoked");
+ }).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", "resources/media/vbr-128kbps-44khz.m4a", 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.decodeAudioData(encodedAudioData).then(function () {
+ testFailed("Expected context to close before decoding the audio data");
+ }, function (e) {
+ testPassed("Correctly failed to decode: " + e);
+ }).then(done);
+ context.close();
+ });
+
+ 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