<p>This tests for a bug where extending a selection created with the mouse would blow it away before extending it.</p>
<div id="div" contenteditable="true">There <span id="select" style="color:green">should be</span> six characters selected in this sentence on Mac and four characters selected on Win/Linux.</div>
<ul id="console"></ul>
<script>
function log(str)
{
    console = document.getElementById("console");
    li = document.createElement("li");
    text = document.createTextNode(str);
    console.appendChild(li);
    li.appendChild(text);
}

function runTest(platform, expectedText)
{
    internals.settings.setEditingBehavior(platform);
    
    var target = document.getElementById('select');
    var startX = target.offsetLeft + target.offsetWidth;
    var y = target.offsetTop + target.offsetHeight / 2;
    var endX = target.offsetLeft;

    // Select all the green text using the mouse.
    // Use a word-granularity selection to avoid eventSender bug.
    // Using just a single-click would not act is if the initial
    // mouseDown was at (endX, y), resulting in a collapsed selection.
    eventSender.mouseMoveTo(startX, y);
    eventSender.mouseDown();
    eventSender.mouseUp();
    eventSender.mouseDown();
    eventSender.mouseMoveTo(endX, y);
    eventSender.mouseUp();
    // Extending this 5 character selection will select 6 characters on mac,
    // but shrink the selection on win/linux.
    testRunner.execCommand("MoveForwardAndModifySelection");
    // Extending it in this way flips the anchor and the focus on Mac.
    var selectedText = window.getSelection().toString();
    if (selectedText != expectedText)
        log("Failure: Selected text was \"" + selectedText + "\" and should be \"" + expectedText + "\"");
    else
        log("Success");
}

if (window.testRunner && window.internals) {
    testRunner.dumpAsText()
    runTest('mac', 'should be s');
    runTest('windows', 'hould be ');
} else {
    log("Failure: To test manually, select the green text " +
        "starting at the end via double-click (i.e. word granularity), then shift+right-arrow. " +
        "On Win/Linux you should see 'hould be ' selected. On Mac, you should see 'should be s' selected." +
        "There's currently a bug that selects the space " +
        "after 'should be' during the initial selection (https://bugs.webkit.org/show_bug.cgi?id=36256). " +
        "Once that's fixed, it the final selected text should be 'should be ' on Mac and 'hould be' on Win/Linux.")
}
</script>
