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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 <!doctype html>
2 <html>
3 <head>
4 <title>Test decodeAudioData promises</title>
5
6 <script src="../resources/js-test.js"></script>
7 <script src="resources/compatibility.js"></script>
8 <script src="resources/audio-testing.js"></script>
9 </head>
10
11 <body>
12 <script>
13 description("Basic tests for decodeAudioData promise.");
14
15 window.jsTestIsAsync = true;
16
17 var audit = Audit.createTaskRunner();
18
19 var context = new AudioContext();
20 var validAudioFile = "resources/media/24bit-44khz.wav";
21 var invalidAudioFile = "resources/media/invalid-audio-file.txt";
22 var encodedAudioData;
23
24 // Test that a null audioBuffer causes the promise to be rejected with an In validStateError.
25 audit.defineTask("null-audiobuffer", function (done) {
26 context.decodeAudioData(null).then(function () {
27 testFailed("decodeAudioData(null) incorrectly resolved promise successfu lly.");
28 }, function (e) {
29 testPassed("decodeAudioData(null) correctly rejected promise: " + e.toSt ring());
30 }).then(done);
31 });
32
33 // Utility to load an encoded audio file from |url| and decode it. |success | and |failure| are
34 // functions to handle the then and else cases of the promise returned by de codeAudioData.
35 function runDecode(url, success, failure, done) {
36 var request = new XMLHttpRequest();
37 request.open("GET", url, true);
38 request.responseType = "arraybuffer";
39
40 request.onload = function () {
41 context.decodeAudioData(request.response)
42 .then(success, failure)
43 .then(done);
44 };
45
46 request.send();
47 }
48
49 // Decode a valid encoded file and verify that the promise succeeds correctl y.
50 audit.defineTask('decode-valid-file', function (done) {
51 var url = validAudioFile;
52 runDecode(url,
53 function (buffer) {
54 testPassed("Correctly succeeded in decoding " + url);
55 },
56 function (e) {
57 testFailed("Incorrectly failed to decode " + url + ": " + e.toString() );
58 }, done);
59 });
60
61 // Decode a valid encoded file and verify that the promise is rejected corre ctly.
62 audit.defineTask("decode-invalid-file", function (done) {
63 var url = invalidAudioFile;
64 runDecode(url,
65 function (buffer) {
66 testFailed("Incorrectly succeeded in decoding " + url);
67 },
68 function (e) {
69 testPassed("Correctly failed to decode " + url + ": " + e.toString());
70 }, done);
71 });
72
73 // Decode a valid file and verify that the promise is fulfilled and the succ essCallback is
74 // invoked and both have identical decode audio buffers.
75 audit.defineTask("promise-and-success-callback", function (done) {
76 var request = new XMLHttpRequest();
77 request.open("GET", validAudioFile, true);
78 request.responseType = "arraybuffer";
79
80 request.onload = function () {
81 var p;
82 var prefix = "Decoding valid file: ";
83 p = context.decodeAudioData(request.response,
84 function (callbackBuffer) {
85 p.then(function (promiseBuffer) {
86 if (promiseBuffer == callbackBuffer)
87 testPassed(prefix + "Promise and successCallback succeeded with identical buffers");
88 else
89 testFailed(prefix + "Promise and successCallback succeeded with NON-identical buffers");
90 }).then(done);
91 },
92 function (e) {
93 p.then(function () {
94 testFailed(prefix + "Promise fulfilled but errorCallback invoked!! !");
95 }, function () {
96 testFailed(prefix + "Expected decoding to succeed but promise reje cted and errorCallback invoked!");
97 }).then(done);
98 });
99 };
100
101 request.send();
102 });
103
104 // Decode an invalid file and verify that the promise is rejected and the er rorCallback is
105 // invoked.
106 audit.defineTask("promise-and-error-callback", function (done) {
107 var request = new XMLHttpRequest();
108 request.open("GET", invalidAudioFile, true);
109 request.responseType = "arraybuffer";
110
111 request.onload = function () {
112 var p;
113 var prefix = "Decoding invalid file: ";
114 p = context.decodeAudioData(request.response,
115 function () {
116 p.then(function () {
117 testFailed(prefix + "Expected decoding to fail but promise fulfill ed and successCallback invoked!!!");
118 }, function () {
119 testFailed(prefix + "Expected decoding to fail but promise rejecte d and successCallback invoked!!!");
120 }).then(done);
121 },
122 function () {
123 p.then(function () {
124 testFailed(prefix + "Promise erroneously fulfilled but errorCallba ck correctly invoked!!!");
125 }, function () {
126 testPassed(prefix + "Promise correctly rejected and errorCallback correctly invoked");
127 }).then(done);
128 });
129 };
130
131 request.send();
132 });
133
134 // Just load up a file so we can run decodeAudioData on it
135 audit.defineTask("load-data", function (done) {
136 var request = new XMLHttpRequest();
137 request.open("GET", "resources/media/vbr-128kbps-44khz.m4a", true);
138 request.responseType = "arraybuffer";
139
140 request.onload = function () {
141 encodedAudioData = request.response;
142 done();
143 };
144
145 request.send();
146 });
147
148 // If the context is closing before decodeAudioData has finished decoding, w e should reject the
149 // promise from decodeAudioData.
150 audit.defineTask("close-context-with-pending-decode", function (done) {
151 context.decodeAudioData(encodedAudioData).then(function () {
152 testFailed("Expected context to close before decoding the audio data");
153 }, function (e) {
154 testPassed("Correctly failed to decode: " + e);
155 }).then(done);
156 context.close();
157 });
158
159 audit.defineTask("finish", function (done) {
160 finishJSTest();
161 done();
162 });
163
164 audit.runTasks(
165 "null-audiobuffer",
166 "decode-valid-file",
167 "decode-invalid-file",
168 "promise-and-success-callback",
169 "promise-and-error-callback",
170 "load-data",
171 "close-context-with-pending-decode",
172 "finish"
173 );
174
175 successfullyParsed = true;
176 </script>
177 </body>
178 </html>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698