<!-- webkit-test-runner [ useEphemeralSession=true ] -->
<!DOCTYPE html>
<html>
<head>
    <script src="/js-test-resources/js-test.js"></script>
    <script src="/js-test-resources/ui-helper.js"></script>
    <script src="/resourceLoadStatistics/resources/util.js"></script>
    <script>
        description("Tests that a cross-site iframe can not request storage access under general third-party cookie blocking if it has not received user interaction as first-party (ephemeral).");
        jsTestIsAsync = true;

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

        function finishTest() {
            testRunner.setStatisticsShouldBlockThirdPartyCookies(false, function() {
                setEnableFeature(false, finishJSTest);
            });
        }

        function receiveMessage(event) {
            if (event.origin === "http://localhost:8000") {
                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);
            finishTest();
        }

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

        function activateElement(elementId) {
            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 () {
                    if (window.eventSender)
                        eventSender.keyDown("escape");
                    else {
                        testFailed("No eventSender.");
                        finishTest();
                    }
                },
                function () {
                    testFailed("Promise rejected.");
                    finishTest();
                }
            );
        }

        function runTest() {
            if (document.location.hash !== "#cookieSet") {
                setEnableFeature(true, function() {
                    testRunner.setStatisticsShouldBlockThirdPartyCookies(true, function() {
                        document.location.href = thirdPartyUrl + "/storageAccess/resources/set-cookie.py?name=firstPartyCookie&value=value#" + firstPartyUrl + "/storageAccess/deny-due-to-no-interaction-under-general-third-party-cookie-blocking-ephemeral.html#cookieSet";
                    });
                });
            } else {
                let iframeElement = document.createElement("iframe");
                iframeElement.onload = function() {
                    activateElement("TheIframeThatRequestsStorageAccess");
                };
                iframeElement.id = "TheIframeThatRequestsStorageAccess";
                iframeElement.src = "http://localhost:8000/storageAccess/resources/request-storage-access-iframe.html#userShouldGrantAccess,userShouldNotBeConsulted,policyShouldNotGrantAccess,isNotSameOriginIframe";
                document.body.appendChild(iframeElement);
            }
        }
    </script>
</head>
<body onload="runTest()">
</body>
</html>