<!DOCTYPE html> <!-- webkit-test-runner [ useFlexibleViewport=true ] -->
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="../../../resources/js-test.js"></script>
    <script src="../../../resources/ui-helper.js"></script>
    <style>
        input {
            font-size: 20px;
        }
    </style>
</head>
<body>
    <div><input id="create-new-page"></input></div>
    <div><input id="normal-input"></input></div>
</body>
<script>
    jsTestIsAsync = true;
    createNewPageInput = document.getElementById("create-new-page");
    normalInput = document.getElementById("normal-input");

    async function activateAndWaitForInputSessionWithNewPageHandler(element)
    {
        const x = element.offsetLeft + element.offsetWidth / 2;
        const y = element.offsetTop + element.offsetHeight / 2;
        await new Promise(resolve => {
            testRunner.runUIScript(`
                (function() {
                    uiController.willCreateNewPageCallback = () => {
                        uiController.removeViewFromWindow();
                        uiController.addViewToWindow();
                        uiController.becomeFirstResponder();
                    };
                    uiController.didShowKeyboardCallback = () => uiController.uiScriptComplete();
                    uiController.singleTapAtPoint(${x}, ${y}, function() { });
                })()`, resolve);
        });
    }

    createNewPageInput.addEventListener("focus", () => {
        w = open("about:blank");
        w.close();
    }, { "once" : true });

    addEventListener("load", async () => {
        description("This test verifies that unparenting, reparenting and then making the web view first responder while immediately after focusing an input field does not either cause a crash, or a hang when subsequently attempting to interact with the page. To test manually, tap the top field, dismiss the keyboard, and then tap the bottom field. The test passes if the UI process does not crash or hang.");

        if (!window.testRunner)
            return;

        await activateAndWaitForInputSessionWithNewPageHandler(createNewPageInput);
        createNewPageInput.blur();
        await UIHelper.waitForKeyboardToHide();

        await UIHelper.activateElementAndWaitForInputSession(normalInput);
        shouldBe("document.activeElement", "normalInput");
        normalInput.blur();
        await UIHelper.waitForKeyboardToHide();

        finishJSTest();
    });
</script>
</html>
