<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="../../resources/js-test-pre.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
description("Test to make sure left and right arrows keys behave correctly in RTL content.")

var testContainer = document.createElement("div");
testContainer.contentEditable = true;
testContainer.style.padding = "2px";
testContainer.dir = "rtl";
testContainer.innerText = 'מקור השם עברית';
document.body.insertBefore(testContainer, document.body.firstChild);

var selection = window.getSelection();

function dragSelection()
{
    var text = testContainer.firstChild;
    selection.setBaseAndExtent(text, 0, text, 0);
    var middleY = testContainer.offsetTop + (testContainer.offsetHeight / 2);
    eventSender.dragMode = false;
    eventSender.mouseMoveTo(testContainer.offsetLeft, middleY);
    eventSender.leapForward(200);
    eventSender.mouseDown();
    eventSender.leapForward(200);
    for (var i = 0; i <= 10; i++) {
      eventSender.mouseMoveTo(testContainer.offsetLeft + (testContainer.offsetWidth * (i /10)) - 2, middleY);
      eventSender.leapForward(200);
    }
    eventSender.mouseUp();
    eventSender.leapForward(1000);
}

function setSelection()
{
    var text = testContainer.firstChild;

    selection.setBaseAndExtent(text, 5, text , 8);
}

function testSelectionChange(setupFunction, mod, dir, startDelta, endDelta, testName)
{
    setupFunction();
    var range = window.getSelection().getRangeAt(0);
    var initialStart = range.startOffset;
    var initialEnd = range.endOffset;
    
    selection.modify(mod, dir, "character");
    range = window.getSelection().getRangeAt(0);
    var start = range.startOffset;
    var end = range.endOffset;
    
    if (initialStart + startDelta != start)
        testFailed(testName + ": Selection start is " + start + " but should be " + (startDelta + initialStart));
    else if (initialEnd + endDelta != end)
        testFailed(testName + ": Selection end is " + end + " but should be " + (endDelta + initialEnd));
    else
        testPassed(testName);
}

function runTests()
{
    testSelectionChange(setSelection, "move", "right", 0, -3, "Move selection right");
    testSelectionChange(setSelection, "move", "left", 3, 0, "Move selection left");
    if (window.testRunner && window.internals) {
        testRunner.dumpAsText();
        internals.settings.setEditingBehavior("mac");
        testSelectionChange(dragSelection, "extend", "right", 0, 0, "Extend dragged selection right (Mac)");
        testSelectionChange(dragSelection, "extend", "left", 0, 0, "Extend dragged selection left (Mac)");
        internals.settings.setEditingBehavior("windows");
        testSelectionChange(dragSelection, "extend", "right", 0, 0, "Extend dragged selection right (Win)");
        testSelectionChange(dragSelection, "extend", "left", 1, 0, "Extend dragged selection left (Win)");
        internals.settings.setEditingBehavior("unix");
        testSelectionChange(dragSelection, "extend", "right", 0, 0, "Extend dragged selection right (Unix)");
        testSelectionChange(dragSelection, "extend", "left", 1, 0, "Extend dragged selection left (Unix)");
    }
}

runTests();
document.body.removeChild(testContainer);
</script>
<script src="../../resources/js-test-post.js"></script>
</body>
</html>
