<html>
<head>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
</head>
<body>
<script>
var scope = "";
var activeWorker;

promise_test(async (test) => {
    if (window.testRunner) {
        testRunner.setUseSeparateServiceWorkerProcess(true);
        await fetch("").then(() => { }, () => { });
    }

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

promise_test(async (test) => {
    var promise = new Promise((resolve, reject) => {
        navigator.serviceWorker.addEventListener("message", (event) => {
            resolve(event.data);
        });
    });

    activeWorker.postMessage("ONLINE");
    var result = await promise;

    assert_equals(result, "PASS");
}, "Test service worker online/offline");
</script>
</body>
</html>
