<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="/js-test-resources/js-test.js"></script>
    <script src="resources/util.js"></script>
</head>
<body>
<script>
    description("Tests that all third-party cookies are blocked.");
    jsTestIsAsync = true;

    const partitionHost = "127.0.0.1:8000";
    const partitionOrigin = "http://" + partitionHost;
    const thirdPartyOrigin = "http://localhost:8000";
    const resourcePath = "/resourceLoadStatistics/resources";
    const thirdPartyBaseUrl = thirdPartyOrigin + resourcePath;
    const firstPartyCookieName = "firstPartyCookie";
    const subPathToSetFirstPartyCookie = "/set-cookie.py?name=" + firstPartyCookieName + "&value=value";
    const partitionedCookieName = "partitionedCookie";
    const subPathToSetPartitionedCookie = "/set-cookie.py?name=" + partitionedCookieName + "&value=value";
    const returnUrl = partitionOrigin + "/resourceLoadStatistics/third-party-cookie-blocking.html";
    const subPathToGetCookies = "/get-cookies.py?name1=" + firstPartyCookieName + "&name2=" + partitionedCookieName;

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

    function runTest() {
        switch (document.location.hash) {
            case "#step1":
                // Set first-party cookie for localhost.
                document.location.href = thirdPartyBaseUrl + subPathToSetFirstPartyCookie + "#" + returnUrl + "#step2";
                break;
            case "#step2":
                document.location.hash = "step3";
                // Check that the cookie is blocked for localhost under 127.0.0.1 even though localhost is not prevalent.
                openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=Before user interaction, should receive no cookies.", runTest);
                break;
            case "#step3":
                document.location.hash = "step4";
                // Produce user interaction for the first-party.
                testRunner.setStatisticsHasHadUserInteraction(partitionOrigin, true, function() {
                    // Check that the cookie is still blocked for localhost under 127.0.0.1.
                    openIframe(thirdPartyBaseUrl + subPathToGetCookies +  "&message=After user interaction, should receive no cookies.", runTest);
                });
                break;
            case "#step4":
                testRunner.setStatisticsShouldBlockThirdPartyCookies(false, function() {
                    setEnableFeature(false, finishJSTest);
                });
                break;
        }
    }

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