<!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 Permissions::query() works for shared workers")

jsTestIsAsync = true;

var expectedData = null;
var receivedPostMessageResponse = true;

var worker = new SharedWorker('shared-worker-permission-query.js');
worker.port.onmessage = function(message) {
    window.message = message;
    shouldBeEqualToString("message.data", expectedData);
    receivedPostMessageResponse = true;
}

async function defaultTest()
{
    receivedPostMessageResponse = false;
    expectedData = "prompt";
    worker.port.postMessage(1);
    await shouldBecomeEqual("receivedPostMessageResponse", "true");
}

async function grantTest() 
{   
    receivedPostMessageResponse = false;
    expectedData = "granted";
    internals.withUserGesture(() => {
        // Permission is granted by default when requestPermission() is called.
        window.Notification.requestPermission().then((result)=> {
            requestPermissionResult = result;
            shouldBeEqualToString("requestPermissionResult", "granted");
            worker.port.postMessage(2);
        }); 
    });
    await shouldBecomeEqual("receivedPostMessageResponse", "true");
}

async function denyTest() 
{
    receivedPostMessageResponse = false;
    expectedData = "denied";
    internals.withUserGesture(() => {
        testRunner.denyWebNotificationPermissionOnPrompt(testURL);
        window.Notification.requestPermission().then((result)=> {
            requestPermissionResult = result;
            shouldBeEqualToString("requestPermissionResult", "denied");
            worker.port.postMessage(3);
        }); 
    });
    await shouldBecomeEqual("receivedPostMessageResponse", "true");
}

(async function () {
    await defaultTest();
    await grantTest();
    await denyTest();
    finishJSTest();
})();

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