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

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

#editor {
    width: 100%;
    height: 100px;
    border: 1px dashed silver;
    text-align: center;
}
</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>
    copy.addEventListener('click', () => {
        getSelection().selectAllChildren(copy);
        document.execCommand('Copy');
        getSelection().removeAllRanges();
    });
    </script>"></iframe>
<div id="description"></div>
<div id="console"></div>
<script>
jsTestIsAsync = true;

const editor = document.getElementById("editor");

description("Verifies that no menu is shown when the page programmatically triggers paste on a timer after user interaction. To test manually, click or tap the text on the bottom to copy, and then click or tap the editable area above to trigger two programmatic pastes with the menu. Check that permissions for the first programmatic paste do not affect the second programmatic paste, since it is performed on a zero-delay timer.");

async function waitForAndTapPasteMenuTwice() {
    return new Promise(resolve => {
        testRunner.runUIScript(`
            (() => {
                doneCount = 0;
                function incrementProgress() {
                    if (++doneCount === 5)
                        uiController.uiScriptComplete();
                }

                uiController.didHideMenuCallback = incrementProgress;
                uiController.didShowMenuCallback = tapPasteMenuAction;

                function tapPasteMenuAction() {
                    uiController.chooseMenuAction("Paste", incrementProgress);
                }

                uiController.activateAtPoint(160, 50, incrementProgress);
            })()`, resolve);
    });
}

function paste() {
    getSelection().setPosition(editor);
    shouldBe("document.execCommand('Paste')", "true");
    shouldBeEqualToString("editor.textContent", "Click here to copy");
    editor.textContent = "";
    getSelection().removeAllRanges(editor);
}

editor.addEventListener("click", event => {
    event.preventDefault();
    paste();
    setTimeout(paste, 0);
});

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

    await UIHelper.activateAt(160, 125);
    await waitForAndTapPasteMenuTwice();
    finishJSTest();
});
</script>
</body>
</html>
