<!DOCTYPE html>
<html>
<head>
<title>Cache Storage: network process crash</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<script>
var scope = "/WebKit/service-workers/resources";

function withFrame(url)
{
    return new Promise((resolve) => {
        let frame = document.createElement('iframe');
        frame.src = url;
        frame.onload = function() { resolve(frame); };
        document.body.appendChild(frame);
    });
}

async function registerServiceWorker()
{
    var registration = await navigator.serviceWorker.register("fetchEvent-worker.js", { scope : scope });
    var activeWorker = registration.active;
    if (activeWorker)
        return;
    activeWorker = registration.installing;
    return new Promise(resolve => {
        activeWorker.addEventListener('statechange', () => {
            if (activeWorker.state === "activated")
                resolve(registration);
        });
    });
}

promise_test(async (test) => {
    const registration = await registerServiceWorker();
    // Enable preload to validate preload persistency.
    await registration.navigationPreload.enable();
    await registration.navigationPreload.setHeaderValue("test");
}, "Setup worker");

promise_test(async (test) => {
    const frame = await withFrame(scope + "/empty.html");
    assert_not_equals(frame.contentWindow.navigator.serviceWorker.controller, null);
    frame.remove();
}, "Frame being controlled");

promise_test(async (test) => {
    if (window.internals)
        await internals.storeRegistrationsOnDisk();

    if (window.testRunner && window.testRunner.terminateNetworkProcess)
        testRunner.terminateNetworkProcess();

    let count = 0;
    while (count++ < 100) {
        const frame = await withFrame(scope + "/empty.html");
        if (frame.contentWindow.navigator.serviceWorker.controller) {
            frame.remove();
            break;
        }
        frame.remove();
        await new Promise(resolve => setTimeout(resolve, 50));
    }
    assert_true(count < 100);

    const registration = await navigator.serviceWorker.getRegistration(scope);

    // Validate preload persistency.
    const state = await registration.navigationPreload.getState();
    assert_true(state.enabled, "state.enabled");
    assert_equals(state.headerValue, "test");
}, "Frame being controlled after network process crash");
</script>
</body>
</html>

