<!DOCTYPE html> <!-- webkit-test-runner [ useFlexibleViewport=true ] -->
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <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>
</head>
<body onload="runTest()">
<script>
    description("Tests that Storage Access API calls work well for removed frames.");
    jsTestIsAsync = true;
    
    function finishTest() {
        setEnableFeature(false, finishJSTest);
    }

    function receiveMessage() {
        requestingiframe.remove();
        testPassed("iframe removed.");
        setTimeout(finishTest, 0.5);
    }

    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();
            }
        );
    }

    const iframeID = "requestingiframe";
    const iframeSource = "http://localhost:8000/storageAccess/resources/request-storage-access-and-immediately-postmessage-iframe.html";

    function runTest() {
        let iframeElement = document.createElement("iframe");
        iframeElement.onload = function() {
            testRunner.statisticsUpdateCookieBlocking(function() {
                activateElement(iframeID);
            });
        };
        iframeElement.id = iframeID;
        iframeElement.src = iframeSource;
        document.body.appendChild(iframeElement);
    }
</script>
</body>
</html>
