<html> 
<head>
<script>
if (window.testRunner)
     testRunner.dumpEditingCallbacks();
</script>

<script>

function log(str) {
    var li = document.createElement("li");
    li.appendChild(document.createTextNode(str));
    var console = document.getElementById("console");
    console.appendChild(li);
}

function runTest() {
    var elem = document.getElementById("test");
    var selection = window.getSelection();

    var anchorNode = selection.anchorNode;
    var anchorOffset = selection.anchorOffset;
    var focusNode = selection.focusNode;
    var focusOffset = selection.focusOffset;

    var anchorString = "Anchor (" + anchorNode + ", " + anchorOffset + ")";
    var anchorCorrect = anchorNode == elem  && anchorOffset == 1 || anchorNode == elem.firstChild && anchorOffset == elem.firstChild.length;
    if (anchorCorrect)
        log(anchorString + " is correct.");
    else
        throw(anchorString + " is incorrect.");

    var focusString = "Focus (" + focusNode + ", " + focusOffset + ")";
    var focusCorrect = (focusNode == elem || focusNode == elem.firstChild) && focusOffset == 0;
    if (focusCorrect)
        log(focusString + " is correct.");
    else
        throw(focusString + " is incorrect.");
}

function manualTest() {
    try {
        runTest();
    } catch(e) {
        log("Test Failed.  Error was: " + e);
    }
}

function automaticTest() {
    try {
        if (!window.testRunner)
            throw("This test does not run interactively.  It uses the testRunner's eventSender to do mouse clicks.  To run it manually, from right to left, select 'text' in the box below, then click the 'Run Test' button.");
            
        window.testRunner.dumpAsText();
        
        var elem = document.getElementById("test");
    
        // (x1, y) to (x2, y) should select the element.
        // selects the text from right to left.
        x1 = elem.offsetLeft + elem.offsetWidth;
        x2 = elem.offsetLeft;
        y = elem.offsetTop + elem.offsetHeight / 2;
        
        eventSender.mouseMoveTo(x1, y);
        eventSender.mouseDown();
        eventSender.mouseMoveTo(x1, y);
        eventSender.mouseMoveTo(x2, y);
        eventSender.mouseUp();
        
        runTest();
        
    } catch(e) {
        log("Test Failed.  Error was: " + e);
    }
}

</script>
</head>
<body>
<p>This tests the anchorNode, anchorOffset, focusNode and focusOffset properties of the Selection object.  These properties are part of Mozilla's Selection object API, and so their values should be consistent in both browsers.</p>
<input type="button" onclick="manualTest();" value="Run Test">
<div style="border: 1px solid blue;" id="test">text</div>
<ul id="console"></ul>
<script>automaticTest();</script>
</body>
</html>
