<!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 classification based on top frame redirect collusion.");
    jsTestIsAsync = true;

    const hostUnderTest = "127.0.0.1:8000";
    const statisticsUrl = "http://" + hostUnderTest + "/temp";
    const topFrameOrigin1 = "http://127.0.0.2:8000/temp";
    const topFrameOrigin2 = "http://127.0.0.3:8000/temp";
    const topFrameOrigin3 = "http://127.0.0.4:8000/temp";
    const topFrameOrigin4 = "http://127.0.0.5:8000/temp";
    const topFrameOrigin5 = "http://localhost:8000/";

    function runTest() {
        testRunner.setStatisticsTopFrameUniqueRedirectFrom(statisticsUrl, topFrameOrigin1);
        testRunner.setStatisticsTopFrameUniqueRedirectFrom(topFrameOrigin1, topFrameOrigin2);
        testRunner.setStatisticsTopFrameUniqueRedirectFrom(topFrameOrigin2, topFrameOrigin3);
        testRunner.setStatisticsTopFrameUniqueRedirectFrom(topFrameOrigin3, topFrameOrigin4);

        testRunner.setStatisticsPrevalentResource(statisticsUrl, true, function() {
            if (!testRunner.isStatisticsPrevalentResource(statisticsUrl))
                testFailed("Host explicity set did not get set as prevalent resource.");
            if (testRunner.isStatisticsPrevalentResource(topFrameOrigin1))
                testPassed("Colluding host 1 got set as prevalent resource.");
            if (testRunner.isStatisticsVeryPrevalentResource(topFrameOrigin1))
                testFailed("Colluding host 1 got classified as very prevalent resource.");
            if (testRunner.isStatisticsPrevalentResource(topFrameOrigin2))
                testPassed("Colluding host 2 got set as prevalent resource.");
            if (testRunner.isStatisticsVeryPrevalentResource(topFrameOrigin2))
                testFailed("Colluding host 2 got classified as very prevalent resource.");
            if (testRunner.isStatisticsPrevalentResource(topFrameOrigin3))
                testPassed("Colluding host 3 got set as prevalent resource.");
            if (testRunner.isStatisticsVeryPrevalentResource(topFrameOrigin3))
                testFailed("Colluding host 3 got classified as very prevalent resource.");
            if (testRunner.isStatisticsPrevalentResource(topFrameOrigin4))
                testPassed("Colluding host 4 got set as prevalent resource.");
            if (testRunner.isStatisticsVeryPrevalentResource(topFrameOrigin4))
                testFailed("Colluding host 4 got classified as very prevalent resource.");
            if (testRunner.isStatisticsPrevalentResource(topFrameOrigin5))
                testPassed("Colluding localhost got set as prevalent resource after actual navigational redirect.");
            if (testRunner.isStatisticsVeryPrevalentResource(topFrameOrigin5))
                testFailed("Colluding localhost got classified as very prevalent resource.");

            setEnableFeature(false, finishJSTest);
        });
    }

    if (document.location.hash === "" && window.testRunner && window.internals) {
        setEnableFeature(true, function() {
            testRunner.setStatisticsPrevalentResource(statisticsUrl, false, function() {
                if (testRunner.isStatisticsPrevalentResource(statisticsUrl))
                    testFailed("Host did not get set as non-prevalent resource.");

                document.location.href = topFrameOrigin5 + "resourceLoadStatistics/resources/redirect.py?redirectTo=http://127.0.0.1:8000/resourceLoadStatistics/classify-as-prevalent-based-on-top-frame-redirect-collusion.html#continue";
            });
        });
    } else {
        testRunner.statisticsNotifyObserver();
        runTest();
    }
</script>
</body>
</html>
