<!DOCTYPE html>
<html>
<head>
    <script src="/js-test-resources/js-test.js"></script>
    <script src="/resourceLoadStatistics/resources/util.js"></script>
    <script>
        description("Tests document.hasStorageAccess() for a 3rd-party without cookies under general third-party cookie blocking.");
        jsTestIsAsync = true;

        const firstPartyUrl = "http://127.0.0.1:8000";
        const thirdPartyUrl = "http://localhost:8000";

        window.addEventListener("message", receiveMessage, false);

        function receiveMessage(event) {
            if (event.origin === thirdPartyUrl) {
                if (event.data.indexOf("PASS") !== -1)
                    testPassed(event.data.replace("PASS ", ""));
                else
                    testFailed(event.data.replace("FAIL ", ""));
            } else
                testFailed("Received a message from an unexpected origin: " + event.origin);
            if (document.location.hash === "#lastStep") {
                testRunner.setStatisticsShouldBlockThirdPartyCookies(false, function() {
                    setEnableFeature(false, finishJSTest);
                });
            } else
                runTests();
        }

        function runTests() {
            switch (document.location.hash) {
                case "":
                    setEnableFeature(true, function () {
                        if (testRunner.isStatisticsPrevalentResource(thirdPartyUrl))
                            testFailed("Third-party started out as prevalent resource.");
                        if (testRunner.isStatisticsHasHadUserInteraction(thirdPartyUrl))
                            testFailed("Third-party started out logged for user interaction.");
                        if (testRunner.isStatisticsHasHadUserInteraction(firstPartyUrl))
                            testFailed("First-party started out logged for user interaction.");
                        document.location.hash = "step1";
                        runTests();
                    });
                    break;

                case "#step1":
                    document.location.hash = "step2";
                    // No general third-party cookie blocking, no user interaction, not prevalent.
                    let iframeElement = document.createElement("iframe");
                    iframeElement.src = thirdPartyUrl + "/storageAccess/resources/has-storage-access-iframe.html#policyShouldNotGrantAccess";
                    document.body.appendChild(iframeElement);
                    break;
                case "#step2":
                    document.location.hash = "step3";
                    // No general third-party cookie blocking, third-party user interaction, not prevalent.
                    testRunner.setStatisticsHasHadUserInteraction(thirdPartyUrl, true, function() {
                        if (!testRunner.isStatisticsHasHadUserInteraction(thirdPartyUrl))
                            testFailed("Third-party did not get logged for user interaction.");
                        let iframeElement = document.createElement("iframe");
                        iframeElement.src = thirdPartyUrl + "/storageAccess/resources/has-storage-access-iframe.html#policyShouldNotGrantAccess";
                        document.body.appendChild(iframeElement);
                    });
                    break;
                case "#step3":
                    document.location.hash = "step4";
                    // No general third-party cookie blocking, first- and third-party user interaction, not prevalent.
                    testRunner.setStatisticsHasHadUserInteraction(firstPartyUrl, true, function() {
                        if (!testRunner.isStatisticsHasHadUserInteraction(firstPartyUrl))
                            testFailed("First-party did not get logged for user interaction.");
                        let iframeElement = document.createElement("iframe");
                        iframeElement.src = thirdPartyUrl + "/storageAccess/resources/has-storage-access-iframe.html#policyShouldNotGrantAccess";
                        document.body.appendChild(iframeElement);
                    });
                    break;
                case "#step4":
                    document.location.hash = "step5";
                    // General third-party cookie blocking, first- and third-party user interaction, not prevalent.
                    testRunner.setStatisticsShouldBlockThirdPartyCookies(true, function() {
                        let iframeElement = document.createElement("iframe");
                        iframeElement.src = thirdPartyUrl + "/storageAccess/resources/has-storage-access-iframe.html#policyShouldNotGrantAccess";
                        document.body.appendChild(iframeElement);
                    });
                    break;
                case "#step5":
                    document.location.hash = "lastStep";
                    // General third-party cookie blocking, first- and third-party user interaction, prevalent.
                    testRunner.setStatisticsPrevalentResource(thirdPartyUrl, true, function() {
                        if (!testRunner.isStatisticsPrevalentResource(thirdPartyUrl))
                            testFailed("Third-party not set as prevalent resource.");
                        testRunner.statisticsUpdateCookieBlocking(function() {
                            let iframeElement = document.createElement("iframe");
                            iframeElement.src = thirdPartyUrl + "/storageAccess/resources/has-storage-access-iframe.html#policyShouldNotGrantAccess";
                            document.body.appendChild(iframeElement);
                        });
                    });
                    break;
            }
        }
    </script>
</head>
<body onload="runTests()">
</body>
</html>
