<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="/js-test-resources/ui-helper.js"></script>
    <script src="resources/util.js"></script>
</head>
<body>
<div id="description">Tests that cookie blocking is applied to ping loads.</div>
<a id="targetLink" href="#" ping="http://localhost:8000/contentextensions/resources/save-ping.py?test=ping-to-prevalent-resource">The link</a>
<script>
    if (testRunner)
        testRunner.waitUntilDone();

    const partitionHost = "127.0.0.1:8000";
    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 returnUrl = "http://" + partitionHost + "/resourceLoadStatistics/ping-to-prevalent-resource.html";
    const subPathToGetCookies = "/get-cookies.py?name1=" + firstPartyCookieName;

    function activateElement(elementID, callback) {
        var element = document.getElementById(elementID);
        var centerX = element.offsetLeft + element.offsetWidth / 2;
        var centerY = element.offsetTop + element.offsetHeight / 2;
        UIHelper.activateAt(centerX, centerY).then(
            function () {
                callback();
            },
            function () {
                testRunner.notifyDone();
            }
        );
    }

    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":
                // Check that the cookie gets sent for localhost under 127.0.0.1 since localhost is not prevalent.
                document.location.hash = "step3";
                openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=Should receive first-party cookie.", runTest);
                break;
            case "#step3":
                // Set localhost as prevalent to put it in the blocking category.
                testRunner.setStatisticsPrevalentResource(thirdPartyOrigin, true, function() {
                    testRunner.statisticsUpdateCookieBlocking(runTest);
                    document.location.hash = "step4";
                });
                break;
            case "#step4":
                // Check that no cookie gets sent for localhost under 127.0.0.1 since localhost's cookies are blocked.
                document.location.hash = "step5";
                openIframe(thirdPartyBaseUrl + subPathToGetCookies +  "&message=Should receive no cookies.", runTest);
                break;
            case "#step5":
                // Click the link to trigger the ping.
                activateElement("targetLink", function() {
                    openIframe(thirdPartyOrigin + "/contentextensions/resources/get-ping-data.py?test=ping-to-prevalent-resource", function() {
                        document.body.removeChild(document.getElementById("targetLink"));
                        setEnableFeature(false, function() {
                            fetch("/contentextensions/resources/delete-ping.py?test=ping-to-prevalent-resource").then(()=>{ testRunner.notifyDone(); });
                        });
                    });
                });
                break;
        }
    }

    if (document.location.hash === "") {
        if (testRunner) {
            setEnableFeature(true, function () {
                testRunner.dumpChildFramesAsText();
                document.location.hash = "step1";
                runTest();
            });
        }
    } else {
        runTest();
    }
</script>
</body>
</html>
