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

#copy {
    width: 100%;
    height: 100%;
    border: 1px dashed black;
}

#editor {
    width: 100%;
    height: 200px;
    border: 1px dashed tomato;
    text-align: center;
}

iframe {
    width: 100%;
    height: 200px;
}
</style>
</head>
<body>
<div id="editor" contenteditable></div>
<iframe id="copy" src="data:text/html,<div id='copy' style='font-size: 40px; text-align: center;'>Click here to copy</div>
    <script>
    document.body.addEventListener('mousedown', event => {
        getSelection().selectAllChildren(copy);
        document.execCommand('Copy');
        getSelection().removeAllRanges();
        event.preventDefault();
    });
    </script>"></iframe>
<textarea></textarea>
<div id="description"></div>
<div id="console"></div>
<script>
jsTestIsAsync = true;

description("Verifies that a promise scheduled right before a programmatically triggered paste does not resolve during the paste. To test manually, tap the frame to copy some text, and then tap the editable area to paste. The promise should run after the paste event handler.");

textarea = document.querySelector("textarea");
frame = document.querySelector("iframe");
editor = document.getElementById("editor");
progress = 0;

function checkDone() {
    if (++progress == 3)
        finishJSTest();
}

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

    await UIHelper.activateElement(frame);
    await UIHelper.activateElement(editor);
    await UIHelper.activateElement(editor);
    checkDone();
});

editor.addEventListener("mousedown", event => {
    editor.focus();
    Promise.resolve().then(() => textarea.focus());
    document.execCommand("Paste");
    event.preventDefault();
}, { "once": true });

editor.addEventListener("paste", () => {
    testPassed("Handled paste.");
    checkDone();
});

editor.addEventListener("focus", () => testPassed("Focused editor."), { "once": true });
textarea.addEventListener("focus", () => {
    testPassed("Focused textarea.");
    checkDone();
}, { "once": true });
</script>
</body>
</html>
