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

.container {
    width: 320px;
    height: 320px;
    border: solid 2px silver;
    box-sizing: border-box;
}

#editor {
    opacity: 0.001;
    width: inherit;
    height: inherit;
    font-size: 24px;
}
</style>
</head>
<body>
<div class="container">
    <div id="editor" contenteditable>
        Here's to the crazy ones, the misfits, the rebels, the troublemakers, the round pegs in the square holes.
        The ones who see things differently. They're not fond of rules. You can quote them, disagree with them, glorify or vilify them, but the only thing you can't do is ignore them because they change things.
    </div>
</div>
<p>Verifies that selection UI is suppressed when the editable root is transparent. To manually test, focus the box above and verify that:</p>
<ul>
    <li>The caret is not shown.</li>
    <li>Selection highlights are not shown.</li>
    <li>The selection cannot be changed via gesture.</li>
</ul>
<div>Caret rect after focus: <span id="caret-rect"></span></div>
<div>Selection rects after selecting all: <span id="selection-rects"></span></div>
<div>Selection start grabber rect after selecting all: <span id="start-grabber-rect"></span></div>
<div>Selection end grabber rect after selecting all: <span id="end-grabber-rect"></span></div>
<div>Selection before tap: <span id="selection-before"></span></div>
<div>Selection after tap: <span id="selection-after"></span></div>
<script>
function rectToString(rect) {
    return `(left = ${Math.round(rect.left)}, top = ${Math.round(rect.top)}, width = ${Math.round(rect.width)}, height = ${Math.round(rect.height)})`;
}

function selectionToString() {
    const selection = getSelection();
    if (!selection.rangeCount)
        return "(no selection)";

    const range = selection.getRangeAt(0);
    return `(${range.startContainer}#${range.startOffset}, ${range.endContainer}#${range.endOffset})`;
}

(async () => {
    if (!window.testRunner)
        return;

    testRunner.dumpAsText();
    testRunner.waitUntilDone();

    console.log("1. Waiting for keyboard to appear");
    await UIHelper.activateAndWaitForInputSessionAt(160, 160);
    document.querySelector("#caret-rect").textContent = rectToString(await UIHelper.getUICaretViewRect());

    console.log("2. Selecting all");
    document.execCommand("selectAll");
    document.querySelector("#selection-rects").textContent = (await UIHelper.getUISelectionViewRects()).map(rectToString).join(", ");
    document.querySelector("#start-grabber-rect").textContent = rectToString(await UIHelper.getSelectionStartGrabberViewRect());
    document.querySelector("#end-grabber-rect").textContent = rectToString(await UIHelper.getSelectionEndGrabberViewRect());

    console.log("3. Collapsing selection");
    getSelection().collapseToEnd();
    document.querySelector("#selection-before").textContent = selectionToString();

    console.log("4. Tapping page");
    await UIHelper.tapAt(32, 32);
    document.querySelector("#selection-after").textContent = selectionToString();

    console.log("5. Blurring focused element and waiting for keyboard to dismiss");
    document.activeElement.blur();
    await UIHelper.waitForKeyboardToHide();

    testRunner.notifyDone();
})();
</script>
</body>
</html>
