test_decodeAudioDataPromise.html (1754B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>Test the decodeAudioData API with Promise</title> 5 6 <script src="/tests/SimpleTest/SimpleTest.js"></script> 7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 8 <script src="webaudio.js"></script> 9 </head> 10 <body> 11 <pre id="test"> 12 <script class="testbody" type="text/javascript"> 13 14 SimpleTest.waitForExplicitFinish(); 15 addLoadEvent(function() { 16 17 var finished = 0; 18 19 function finish() { 20 if (++finished == 2) { 21 SimpleTest.finish(); 22 } 23 } 24 25 var ac = new AudioContext(); 26 // Test that a the promise is rejected with an invalid source buffer. 27 expectNoException(function() { 28 var p = ac.decodeAudioData(" "); 29 ok(p instanceof Promise, "AudioContext.decodeAudioData should return a Promise"); 30 p.then(function() { 31 ok(false, "Promise should not resolve with an invalid source buffer."); 32 finish(); 33 }).catch(function(e) { 34 ok(true, "Promise should be rejected with an invalid source buffer."); 35 ok(e.name == "TypeError", "The error should be TypeError"); 36 finish(); 37 }) 38 }); 39 40 // Test that a the promise is resolved with a valid source buffer. 41 var xhr = new XMLHttpRequest(); 42 xhr.open("GET", "ting-44.1k-1ch.ogg", true); 43 xhr.responseType = "arraybuffer"; 44 xhr.onload = function() { 45 var p = ac.decodeAudioData(xhr.response); 46 ok(p instanceof Promise, "AudioContext.decodeAudioData should return a Promise"); 47 p.then(function(data) { 48 ok(data instanceof AudioBuffer, "Promise should resolve, passing an AudioBuffer"); 49 ok(true, "Promise should resolve with a valid source buffer."); 50 finish(); 51 }).catch(function() { 52 ok(false, "Promise should not be rejected with a valid source buffer."); 53 finish(); 54 }); 55 }; 56 xhr.send(); 57 }); 58 59 </script> 60 </pre> 61 </body> 62 </html>