gecko-dev/dom/media/webaudio/test/test_mediaElementAudioSourceNodeCrossOrigin.html
Paul Adenot 02fe6e9c99 Bug 937718 - Test for MediaElementAudioSourceNode and cross-origin resources. r=roc
--HG--
extra : rebase_source : d5ff27560c2c67361baf6a3e767860060848638e
2014-12-24 13:46:58 +01:00

90 lines
2.5 KiB
HTML

<!DOCTYPE HTML>
<html>
<meta charset="utf-8">
<head>
<title>Test MediaStreamAudioSourceNode doesn't get data from cross-origin media resources</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<pre id="test">
<script class="testbody" type="text/javascript">
SimpleTest.waitForExplicitFinish();
var tests = [
// Not the same origin no CORS asked for, should have silence
{ url: "http://example.org:80/tests/dom/media/webaudio/test/small-shot.ogg",
cors: null,
expectSilence: true },
// Same origin, should have sound
{ url: "small-shot.ogg",
cors: null,
expectSilence: false },
// Cross-origin but we asked for CORS and the server answered with the right
// header, should have
{ url: "http://example.org:80/tests/dom/media/webaudio/test/corsServer.sjs",
cors: "anonymous",
expectSilence: false }
];
var testsRemaining = tests.length;
tests.forEach(function(e) {
e.ac = new AudioContext();
var a = new Audio();
a.loop = true;
if (e.cors) {
a.crossOrigin = e.cors;
}
a.src = e.url;
a.controls = true;
var measn = e.ac.createMediaElementSource(a);
var sp = e.ac.createScriptProcessor(2048, 1);
// Set a couple expandos to track the status of the test
sp.iterationsLeft = 20;
sp.seenSound = false;
measn.connect(sp);
a.play();
document.body.appendChild(a);
function checkFinished(sp) {
if (--sp.iterationsLeft == 0) {
sp.onaudioprocess = null;
a.pause();
var not = e.expectSilence ? "" : "not";
is(e.expectSilence, !sp.seenSound,
"Buffer is " + not + " silent as expected, for " +
e.url + " (cors: " + e.cors + ")");
if (--testsRemaining == 0) {
SimpleTest.finish();
}
}
}
function checkBufferSilent(e) {
var inputArrayBuffer = e.inputBuffer.getChannelData(0);
var silent = true;
for (var i = 0; i < inputArrayBuffer.length; i++) {
if (inputArrayBuffer[i] != 0.0) {
silent = false;
break;
}
}
// It is acceptable to find a full buffer of silence here, even if we expect
// sound, because Gecko's looping on media elements is not seamless and we
// can underrun. We are looking for at least one buffer of non-silent data.
e.target.seenSound = !silent || e.target.seenSound;
checkFinished(e.target);
return silent;
}
a.onplaying = function () {
sp.onaudioprocess = checkBufferSilent;
}
});
</script>
</pre>
</body>
</html>