test_OfflineAudioContext.html (3687B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>Test OfflineAudioContext</title> 5 <script src="/tests/SimpleTest/SimpleTest.js"></script> 6 <script type="text/javascript" src="webaudio.js"></script> 7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 8 </head> 9 <body> 10 <pre id="test"> 11 <script class="testbody" type="text/javascript"> 12 13 var renderedBuffer = null; 14 var finished = 0; 15 16 function finish() { 17 finished++; 18 if (finished == 2) { 19 SimpleTest.finish(); 20 } 21 } 22 23 function setOrCompareRenderedBuffer(aRenderedBuffer) { 24 if (renderedBuffer) { 25 is(renderedBuffer, aRenderedBuffer, "Rendered buffers from the event and the promise should be the same"); 26 finish(); 27 } else { 28 renderedBuffer = aRenderedBuffer; 29 } 30 } 31 32 SimpleTest.waitForExplicitFinish(); 33 addLoadEvent(function() { 34 let ctxs = [ 35 new OfflineAudioContext(2, 100, 22050), 36 new OfflineAudioContext({length: 100, sampleRate: 22050}), 37 new OfflineAudioContext({channels: 2, length: 100, sampleRate: 22050}), 38 ]; 39 40 for (let ctx of ctxs) { 41 ok(ctx instanceof EventTarget, "OfflineAudioContexts must be EventTargets"); 42 is(ctx.length, 100, "OfflineAudioContext.length is equal to the value passed to the ctor."); 43 44 var buf = ctx.createBuffer(2, 100, ctx.sampleRate); 45 for (var i = 0; i < 2; ++i) { 46 for (var j = 0; j < 100; ++j) { 47 buf.getChannelData(i)[j] = Math.sin(2 * Math.PI * 200 * j / ctx.sampleRate); 48 } 49 } 50 } 51 52 is(ctxs[1].destination.channelCount, 1, "OfflineAudioContext defaults to to correct channelCount."); 53 54 let ctx = ctxs[0]; 55 56 expectException(function() { 57 new OfflineAudioContext(2, 100, 0); 58 }, DOMException.NOT_SUPPORTED_ERR); 59 expectException(function() { 60 new OfflineAudioContext(2, 100, -1); 61 }, DOMException.NOT_SUPPORTED_ERR); 62 expectException(function() { 63 new OfflineAudioContext(0, 100, 44100); 64 }, DOMException.NOT_SUPPORTED_ERR); 65 new OfflineAudioContext(32, 100, 44100); 66 expectException(function() { 67 new OfflineAudioContext(33, 100, 44100); 68 }, DOMException.NOT_SUPPORTED_ERR); 69 expectException(function() { 70 new OfflineAudioContext(2, 0, 44100); 71 }, DOMException.NOT_SUPPORTED_ERR); 72 expectTypeError(function() { 73 new OfflineAudioContext({}); 74 }); 75 expectTypeError(function() { 76 new OfflineAudioContext({sampleRate: 44100}); 77 }); 78 expectTypeError(function() { 79 new OfflineAudioContext({length: 44100*40}); 80 }); 81 82 var src = ctx.createBufferSource(); 83 src.buffer = buf; 84 src.start(0); 85 src.connect(ctx.destination); 86 87 ctx.addEventListener("complete", function(e) { 88 ok(e instanceof OfflineAudioCompletionEvent, "Correct event received"); 89 is(e.renderedBuffer.numberOfChannels, 2, "Correct expected number of buffers"); 90 ok(renderedBuffer != null, "The event should be fired after the promise callback."); 91 expectNoException(function() { 92 ctx.startRendering().then(function() { 93 ok(false, "Promise should not resolve when startRendering is called a second time on an OfflineAudioContext") 94 finish(); 95 }).catch(function() { 96 ok(true, "Promise should reject when startRendering is called a second time on an OfflineAudioContext") 97 finish(); 98 }); 99 }); 100 compareBuffers(e.renderedBuffer, buf); 101 setOrCompareRenderedBuffer(e.renderedBuffer); 102 103 }); 104 105 expectNoException(function() { 106 ctx.startRendering().then(function(b) { 107 is(renderedBuffer, null, "The promise callback should be called first."); 108 setOrCompareRenderedBuffer(b); 109 }).catch(function () { 110 ok(false, "The promise from OfflineAudioContext.startRendering should never be rejected"); 111 }); 112 }); 113 }); 114 115 </script> 116 </pre> 117 </body> 118 </html>