<!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 for SameSite=strict enforcement based on top frame unique redirects to.");
    jsTestIsAsync = true;

    const hostUnderTest = "127.0.0.1:8000";
    const statisticsUrl = "http://" + hostUnderTest + "/temp";
    function setMultipleTopFrameUniqueRedirectTo(redirectingParty) {
        testRunner.setStatisticsTopFrameUniqueRedirectTo(redirectingParty, "http://127.0.0.2:8000/temp");
        testRunner.setStatisticsTopFrameUniqueRedirectTo(redirectingParty, "http://127.0.0.3:8000/temp");
        testRunner.setStatisticsTopFrameUniqueRedirectTo(redirectingParty, "http://127.0.0.4:8000/temp");
        testRunner.setStatisticsTopFrameUniqueRedirectTo(redirectingParty, "http://127.0.0.5:8000/temp");
        testRunner.setStatisticsTopFrameUniqueRedirectTo(redirectingParty, "http://127.0.0.6:8000/temp");
        testRunner.setStatisticsTopFrameUniqueRedirectTo(redirectingParty, "http://127.0.0.7:8000/temp");
        testRunner.setStatisticsTopFrameUniqueRedirectTo(redirectingParty, "http://127.0.0.8:8000/temp");
        testRunner.setStatisticsTopFrameUniqueRedirectTo(redirectingParty, "http://127.0.0.9:8000/temp");
        testRunner.setStatisticsTopFrameUniqueRedirectTo(redirectingParty, "http://127.0.0.10:8000/temp");
        testRunner.setStatisticsTopFrameUniqueRedirectTo(redirectingParty, "http://127.0.0.11:8000/temp");
        testRunner.setStatisticsTopFrameUniqueRedirectTo(redirectingParty, "http://127.0.0.12:8000/temp");
    }

    function completeTest() {
        let cookies = internals.getCookies();
        if (cookies.length !== 1) {
            testFailed("Should have exactly one cookie before test but found " + cookies.length + " cookies.");
            setEnableFeature(false, finishJSTest);
            return;
        }

        if (cookies[0].isSameSiteStrict)
            testFailed("Cookie was SameSite=strict after second test.");
        else
            testPassed("Cookie was not SameSite=strict after second test.");

        setEnableFeature(false, finishJSTest);
    }

    function secondStep() {
        let cookies = internals.getCookies();
        if (cookies.length !== 1) {
            testFailed("Should have exactly one cookie before test but found " + cookies.length + " cookies.");
            setEnableFeature(false, finishJSTest);
            return;
        }

        if (cookies[0].isSameSiteStrict)
            testPassed("Cookie was SameSite=strict after first test.");
        else
            testFailed("Cookie was not SameSite=strict after first test.");

        // Set the cookie again to reset its SameSite status.
        document.cookie = "clientSideCookie=1";

        testRunner.installStatisticsDidScanDataRecordsCallback(completeTest);

        testRunner.statisticsProcessStatisticsAndDataRecords();
    }

    function runTest() {
        document.cookie = "clientSideCookie=1";

        testRunner.setStatisticsHasHadUserInteraction(statisticsUrl, true);

        let cookies = internals.getCookies();
        if (cookies.length !== 1) {
            testFailed("Should have exactly one cookie before test but found " + cookies.length + " cookies.");
            finishJSTest();
            return;
        }

        if (cookies[0].isSameSiteStrict)
            testFailed("Cookie was SameSite=strict before test.");
        else
            testPassed("Cookie was not SameSite=strict before test.");

        setMultipleTopFrameUniqueRedirectTo(statisticsUrl);

        testRunner.installStatisticsDidScanDataRecordsCallback(secondStep);

        testRunner.statisticsProcessStatisticsAndDataRecords();
    }

    if (document.location.host === hostUnderTest && window.testRunner && window.internals) {
        setEnableFeature(true, function() {
            runTest();
        });
    }
</script>
</body>
</html>
