<!-- webkit-test-runner [ UsesBackForwardCache=true ] -->
<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas"></canvas>
<script src="/resources/js-test-pre.js"></script>
<script>
description('Tests that a page with a capture track goes into the page cache.');
window.jsTestIsAsync = true;

var restoredFromPageCache = false;

window.addEventListener("pageshow", function(event) {
    debug("pageshow - " + (event.persisted ? "" : "not ") + "from cache");

    if (event.persisted) {
        testPassed("Page did enter and was restored from the page cache");
        restoredFromPageCache = true;

        if (window.internals)
            debug("page media state is '" + internals.pageMediaState() + "'");

        if (!canvasStream.active)
            testFailed("Canvas stream is not active");

        if (gumStream.active)
            testFailed("GUM stream is active");
    }
}, false);

window.addEventListener("pagehide", function(event) {
    debug("pagehide - " + (event.persisted ? "" : "not ") + "entering cache");
    if (!event.persisted) {
        testFailed("Page did not enter the page cache.");
        finishJSTest();
    }
}, false);

var gumStream;
var canvasStream;

window.addEventListener('load', async function() {
    gumStream = await navigator.mediaDevices.getUserMedia({ audio: true });
    gumStream.getAudioTracks()[0].onended = finishJSTest;
    canvasStream = myCanvas.captureStream();
    // This needs to happen in a setTimeout because a navigation inside the onload handler would
    // not create a history entry.
    setTimeout(function() {
      // Force a back navigation back to this page.
      window.location.href = "resources/page-cache-helper.html";
    }, 0);
}, false);

</script>
<script src="/resources/js-test-post.js"></script>
</body>
</html>
