<!DOCTYPE html>
<html>
<head>
    <script src="../../../resources/js-test-pre.js"></script>
    <script src="../../media-resources/media-file.js"></script>
</head>
<body>
<pre id="console"></pre>
<script>
    description("Ensure that audio is not rendered when tainted by a remote audio resource when CORS is not enabled.");
    window.jsTestIsAsync = true;

    function go() {
        let audio = new Audio();
        let mediaFile = findMediaFile("audio", "../../media/resources/1000Hz-sin");
        let type = mimeTypeForExtension(mediaFile.split('.').pop());
        audio.src = "http://localhost:8080/security/resources/redirect-allow-star.py?url=" + encodeURIComponent("http://127.0.0.1:8080/security/resources/video-cross-origin-allow.py?name=" + mediaFile + "&type=" + type);

        context = new AudioContext();
        let mediaSource = context.createMediaElementSource(audio);
        let analyser = context.createAnalyser();
        analyser.fftSize = 32;

        mediaSource.connect(analyser);

        context.resume().then(() => {
            audio.play();
        });

        window.outputArray = new Float32Array(analyser.frequencyBinCount);
        window.silentArray = new Float32Array(analyser.frequencyBinCount);
        silentArray.fill(-Infinity);

        var intervalToken = setInterval(() => {
            analyser.getFloatFrequencyData(outputArray);
        }, 30);

        audio.addEventListener("ended", event => {
            clearInterval(intervalToken);
            context.suspend().then(() => {
                shouldBe("outputArray", "silentArray");
                finishJSTest();
            });
        });
    }
    window.addEventListener('load', go);
</script>
<script src="../../../resources/js-test-post.js"></script>
</body>
</html>
