<!DOCTYPE html>
<html lang="en">
<head>
    <script src="/js-test-resources/js-test.js"></script>
    <script src="resources/util.js"></script>
</head>
<body>
<script>
    description("Test for cookie blocking when web workers import cross-site scripts.");
    jsTestIsAsync = true;

    const partitionHost = "127.0.0.1:8000";
    const thirdPartyOrigin = "http://localhost:8000";
    const thirdPartyBaseUrl = thirdPartyOrigin + "/resourceLoadStatistics/resources";
    const firstPartyCookieName = "firstPartyCookie";
    const subPathToSetFirstPartyCookie = "/set-cookie.py?name=" + firstPartyCookieName + "&value=value";
    const returnUrl = "http://127.0.0.1:8000/resourceLoadStatistics/blocking-in-web-worker-script-import.html";
    const subPathToGetCookies = "/get-cookies.py?name1=" + firstPartyCookieName;

    function openIframe(url, onLoadHandler) {
        const element = document.createElement("iframe");
        element.src = url;
        if (onLoadHandler) {
            element.onload = onLoadHandler;
        }
        document.body.appendChild(element);
    }

    let receivedMessages = 0;
    function receiveMessage(event) {
        if (event.data.indexOf("PASS") === -1)
            testFailed(event.data.replace("FAIL ", ""));
        else
            testPassed(event.data.replace("PASS ", ""));

        ++receivedMessages;
        if (receivedMessages >= 2)
            setEnableFeature(false, finishJSTest);
        else
            runTest();
    }

    function runTest() {
        switch (document.location.hash) {
            case "#step1":
                document.location.href = thirdPartyBaseUrl + subPathToSetFirstPartyCookie + "#" + returnUrl + "#step2";
                break;
            case "#step2":
                document.location.hash = "step3";
                openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=Should receive first-party cookie.", runTest);
                break;
            case "#step3":
                document.location.hash = "step4";
                let w1 = new Worker("resources/worker-importing-localhost-script.js");
                w1.onmessage = receiveMessage;
                w1.postMessage("shouldReceiveCookies=true");
                break;
            case "#step4":
                document.location.hash = "step5";
                testRunner.setStatisticsPrevalentResource(thirdPartyOrigin, true, function() {
                    if (!testRunner.isStatisticsPrevalentResource(thirdPartyOrigin))
                        testFailed("Host did not get set as prevalent resource.");
                    testRunner.statisticsUpdateCookieBlocking(runTest);
                });
                break;
            case "#step5":
                let w2 = new Worker("resources/worker-importing-localhost-script.js");
                w2.onmessage = receiveMessage;
                w2.postMessage("shouldNotReceiveCookies");
                break;
        }
    }

    if (document.location.host === partitionHost && document.location.hash === "" && window.testRunner && window.internals) {
        setEnableFeature(true, function() {
            document.location.hash = "step1";
            testRunner.dumpChildFramesAsText();
            if (testRunner.isStatisticsPrevalentResource(thirdPartyOrigin))
                testFailed("Localhost was classified as prevalent resource before the test starts.");
            runTest();
        });
    } else {
        runTest();
    }
</script>
</body>
</html>
