<!DOCTYPE html>
<html>
<head>
    <script src="../../../resources/ui-helper.js"></script>
    <script src="../../../resources/js-test.js"></script>
</head>
<body>
<input list="foobar" />
<datalist id="foobar">
    <select>
        <option>Foo</option>
    </select>
</datalist>
<br>
</body>
<script>
jsTestIsAsync = true;

addEventListener("load", async function() {
    description("This test verifies that we don't crash when selecting a datalist suggestion, if the suggestions list is reloaded during the click (after mousedown and before mouseup). To manually run the test, click 'Foo' in the datalist dropdown. The application should not crash.");
    if (!window.testRunner)
        return;

    await UIHelper.activateDataListSuggestion(0);
    document.querySelectorAll("input, select").forEach(element => element.remove());
    testPassed("Did not crash.");
    finishJSTest();
});

(async function() {
    while (true) {
        const input = document.querySelector("input");
        if (!input)
            return;

        input.focus();
        document.execCommand("InsertText", true, "");
        await new Promise(requestAnimationFrame);
    }
})();
</script>
</html>
