<!DOCTYPE html>
<html>
<head>
<script src="/resources/js-test-pre.js"></script>
<script src="/resources/notifications-test-pre.js"></script>
</head>
<body>
<script>

description("This test checks that the Permissions API change event works for service workers")

jsTestIsAsync = true;

(async function() {
    expectedData = null;
    receivedPostMessageResponse = false;

    navigator.serviceWorker.register('permission-status-onchange-event-service-worker.js');
    navigator.serviceWorker.addEventListener('message', (message) => {
        window.message = message;
        shouldBeEqualToString("message.data", expectedData);
        receivedPostMessageResponse = true;
    });

    receivedPostMessageResponse = false;
    expectedData = "prompt";
    navigator.serviceWorker.ready.then((registration) => {
        registration.active.postMessage(1);
    });
    await shouldBecomeEqual("receivedPostMessageResponse", "true");

    receivedPostMessageResponse = false;
    expectedData = "granted";
    testRunner.setGeolocationPermission(true);
    await shouldBecomeEqual("receivedPostMessageResponse", "true");
    finishJSTest();
})();

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