<!-- webkit-test-runner [ UsesBackForwardCache=true ] -->
<!DOCTYPE html>
<html>
<body>
<script src="/js-test-resources/js-test.js"></script>
<script>
description("Tests that a page with a pending service worker promise is able to enter the page cache.");
jsTestIsAsync = true;

let restoredFromPageCache = false;

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

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

    registration.unregister().then(() => {
        testPassed("Unregistered successfuly");
        shouldBeTrue("!!restoredFromPageCache");
        finishJSTest();
    }, () => {
        testFailed("Failed to unregister");
        finishJSTest();
    });
});

onload = () => {
    navigator.serviceWorker.register("resources/basic-fetch-worker.js", { scope: "/workers/service/resources/" }).then((_registration) => {
        registration = _registration;
        window.location = "/navigation/resources/page-cache-helper.html";
    });
}
</script>
</body>
</html>
