<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <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-origin iframe from a prevalent domain that is granted storage access and then navigates itself cross-site does not have storage access.");
        jsTestIsAsync = true;

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

        function finishTest() {
            // Reset access scope.
            internals.settings.setStorageAccessAPIPerPageScopeEnabled(true);
            setEnableFeature(false, finishJSTest);
        }

        const expectedPassMessages = 2;
        var passMessagesReceived = 0;
        function receiveMessage(event) {
            if (event.origin === "http://localhost:8000") {
                if (event.data.indexOf("document.cookie") === 0) {
                    testPassed(event.data);
                } else if (event.data.indexOf("PASS") !== -1) {
                    testPassed(event.data.replace("PASS ", ""));
                    passMessagesReceived++;
                    if (passMessagesReceived >= expectedPassMessages)
                        finishTest();
                } else {
                    testFailed(event.data);
                    finishTest();
                }
            } else {
                testFailed("Received a message from an unexpected origin: " + event.origin);
                finishTest();
            }
        }

        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() {
            activateElement("TheIframeThatRequestsStorageAccess");
        }

        const hostUnderTest = "localhost:8000";
        const statisticsUrl = "http://" + hostUnderTest;
        if (document.location.hash !== "#firstPartyCookieSet") {
            setEnableFeature(true, function() {
                document.location.href = statisticsUrl + "/storageAccess/resources/set-cookie.py?name=firstPartyCookie&value=value#http://127.0.0.1:8000/storageAccess/request-and-grant-access-then-navigate-cross-site-should-not-have-access.html#firstPartyCookieSet";
            });
        } else {
            // Set per-frame access since clearing access on cross-site navigation of the iframe requires per-frame access.
            internals.settings.setStorageAccessAPIPerPageScopeEnabled(false);
            testRunner.setStatisticsPrevalentResource(statisticsUrl, true, function() {
                if (!testRunner.isStatisticsPrevalentResource(statisticsUrl))
                    testFailed("Host did not get set as prevalent resource.");
                testRunner.setStatisticsHasHadUserInteraction(statisticsUrl, true, function() {
                    if (!testRunner.isStatisticsHasHadUserInteraction(statisticsUrl))
                        testFailed("Host did not get logged for user interaction.");
                    testRunner.statisticsUpdateCookieBlocking(function() {
                        let iframeElement = document.createElement("iframe");
                        iframeElement.setAttribute("sandbox", "allow-storage-access-by-user-activation allow-scripts allow-same-origin allow-modals");
                        iframeElement.onload = runTest;
                        iframeElement.id = "TheIframeThatRequestsStorageAccess";
                        iframeElement.src = "http://localhost:8000/storageAccess/resources/self-navigating-frame-after-granted-access.html#userShouldGrantAccess,userShouldBeConsulted,policyShouldGrantAccess,crossSiteNavigation";
                        document.body.appendChild(iframeElement);
                    });
                });
            });
        }
    </script>
</body>
</html>
