<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test for Partitioned Cookies With and Without User Interaction</title>
    <script src="/js-test-resources/js-test.js"></script>
    <script src="resources/util.js"></script>
</head>
<body>
<script>
    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 fragmentWithReturnUrl = "http://127.0.0.1:8000/resourceLoadStatistics/cookies-with-and-without-user-interaction.html";
    const subPathToGetCookies = "/get-cookies.py?name1=" + firstPartyCookieName;

    function finishTest() {
        setEnableFeature(false, 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 setUserInteractionAndContinue() {
        testRunner.setStatisticsHasHadUserInteraction(thirdPartyOrigin, true, function() {
            if (!testRunner.isStatisticsHasHadUserInteraction(thirdPartyOrigin))
                testFailed("Third party did not get logged for user interaction.");
            runTest();
        });
    }

    function runTest() {
        switch (document.location.hash) {
            case "#step1":
                document.location.href = thirdPartyBaseUrl + subPathToSetFirstPartyCookie + "#" + fragmentWithReturnUrl + "#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";
                testRunner.setStatisticsPrevalentResource(thirdPartyOrigin, true, function() {
                    if (!testRunner.isStatisticsPrevalentResource(thirdPartyOrigin))
                        testFailed("Host did not get set as prevalent resource.");
                    testRunner.statisticsUpdateCookieBlocking(runTest);
                });
                break;
            case "#step4":
                document.location.hash = "step5";
                openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=Should receive no cookies.", setUserInteractionAndContinue);
                break;
            case "#step5":
                openIframe(thirdPartyBaseUrl + subPathToGetCookies + "&message=After user interaction, should still receive no cookies.", finishTest);
                break;
        }
    }

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