<!DOCTYPE html> 
<html> 
<body> 
<p id="description">This test ensures that when editable text is selected by a drag that ends outside of the editable region, selection is extended to the either end of the text. See the <a href="https://bugs.webkit.org/show_bug.cgi?id=52986">bug 52986</a> for more details.</p> 
<div id="test" contenteditable style="width: 200px; margin: 20px; border: solid 1px; font-size: x-large;">hello world</div> 
<script> 
 
function selectTextByDrag(div, endX, endY) {
    window.getSelection().removeAllRanges();
 
    eventSender.mouseMoveTo(div.offsetLeft + (div.offsetWidth / 2) , div.offsetTop + (div.offsetHeight / 2));
    eventSender.mouseDown();
 
    eventSender.leapForward(200);
 
    // Without this, drag doesn't start selection.
    eventSender.mouseMoveTo(div.offsetLeft + (div.offsetWidth / 3) , div.offsetTop + (div.offsetHeight / 3));
    eventSender.mouseDown();
 
    eventSender.leapForward(200);
 
    eventSender.mouseMoveTo(endX, endY);
    eventSender.mouseUp();
}
 
var log = '';
 
function assertEqual(actual, expected) {
    if (actual == expected)
        log += 'PASS';
    else
        log += 'FAIL - expected ' + expected + ' but got ' + actual;
    log += '\n';
}
 
if (!window.testRunner || !window.eventSender)
    document.writeln("This test requires eventSender since the bug does not reproduce reliably when tested manually.");
else {
    testRunner.dumpAsText();
    var description = document.getElementById('description');
    description.style.display = 'none';
 
    var div = document.getElementById('test');
 
    log += 'Dragging to the left: ';
    selectTextByDrag(div, div.offsetLeft - 10, div.offsetTop - 10);
    assertEqual(window.getSelection().getRangeAt(0).startOffset, 0);
 
    log += 'Dragging to the left: ';
    selectTextByDrag(div, div.offsetLeft + 300, div.offsetTop + 100);
    assertEqual(window.getSelection().getRangeAt(0).endOffset, div.innerText.length);
 
    // Having the log in DOM will interfere with the test so append it at last.
    var console = document.createElement('pre');
    document.body.appendChild(console);
    console.innerHTML = log;
}
 
</script> 
</body> 
</html> 
