<!-- webkit-test-runner [ UsesBackForwardCache=true ] -->
<head>
<script src="../../resources/js-test.js"></script>
</head>
<body>
<script>
description("Tests that pages with workers can go into the page cache.");
jsTestIsAsync = true;

let restoredFromPageCache = false;

window.addEventListener("pageshow", function(event) {
  debug("pageshow - " + (event.persisted ? "" : "not ") + "from page cache");
  if (event.persisted) {
      testPassed("Page entered page cache");
      restoredFromPageCache = true;
  }
});

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

let firstMessage = true;
let messageCountAfterResume = 0;
onload = () => {
    worker = new Worker('resources/worker-setInterval.js');
    worker.postMessage("");

    worker.onmessage = function(evt) {
        if (firstMessage) {
            firstMessage = false;
            testPassed("Received message before restoring from page cache.");
            window.location = "../history/resources/page-cache-helper.html";
            return;
        }
        if (restoredFromPageCache) {
            messageCountAfterResume++;
            if (messageCountAfterResume == 10) {
                restoredFromPageCache = false;
                testPassed("Received message after restoring from page cache.");
                finishJSTest();
            }
        }
    };
}
</script>
</body>
</html>
