
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, user-scalable=no">
    <script src="../../resources/js-test.js"></script>
    <script src="../../resources/ui-helper.js"></script>
    <style>
        body {
            margin: 0;
        }

        input, button {
            width: 100%;
            height: 100px;
            display: block;
        }
    </style>
</head>
<body>
<input type="text" id="field"></input>
<button value="Insert text" id="button"></button>
<div id="description"></div>
<div id="console"></div>
</body>
<script>
    description("Verifies that programmatically changing text field values with user interaction invokes the -didChangeInTextField"
        + " injected bundle method. This test requires WebKitTestRunner.");

    buttonClicked = false;
    keyPressed = false;

    if (window.testRunner) {
        jsTestIsAsync = true;
        testRunner.installTextDidChangeInTextFieldCallback(() => debug("Text did change in text field."));
    }

    field.addEventListener("keypress", event => {
        field.value += event.key;
        event.preventDefault();
        keyPressed = true;
    });

    button.addEventListener("click", event => {
        field.focus();
        field.value += "b";
        buttonClicked = true;
    });

    addEventListener("load", async () => {
        if (!window.testRunner)
            return;

        await UIHelper.activateAndWaitForInputSessionAt(100, 50);

        debug("1. Changing text field value with user typing gesture:");
        await UIHelper.typeCharacter("a");
        await new Promise(resolve => shouldBecomeEqual("keyPressed", "true", resolve));
        debug(`The value of the text field is: "${field.value}"`);
        debug("");

        debug("2. Changing text field value with non-typing user gesture:");
        await UIHelper.activateAt(100, 150);
        await new Promise(resolve => shouldBecomeEqual("buttonClicked", "true", resolve));
        debug(`The value of the text field is: "${field.value}"`);
        debug("");

        debug("3. Changing text field value without user gesture:");
        field.value += "c";
        debug(`The value of the text field is: "${field.value}"`);

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