<html>
<body>
<div id="test"><span id="span1">foo<span id="span2">bar</span></span><span id="span3">baz</span></div>
<div id="console"></div>
<script>
var s = window.getSelection();
var testDiv = document.getElementById("test");
var span1 = document.getElementById("span1");
var span2 = document.getElementById("span2");
var span3 = document.getElementById("span3");

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

function shouldBe(expr, expected) {
    var actual = eval(expr);
    if (actual != expected)
        log("Failure: " + expr + " should be " + expected + ", was " + actual + ".");
    else
        log("Success: " + expr + " is " + expected + ".");
}

function shouldThrow(expr) {
    try {
        eval(expr);
        log("Failure: " + expr + " didn't raise an exception" + ".");
    } catch (ex) {
        log("Success: " + expr + " raised " + ex + ".");
    }
}

if (window.testRunner)
    testRunner.dumpAsText();

var r = document.createRange();


r.setStart(span2.firstChild, 1);
r.setEnd(span2.firstChild, 1);
s.addRange(r);

s.extend(span2.firstChild, 3);
shouldBe('window.getSelection()', 'ar');

shouldThrow('s.extend(span2.firstChild, 4)');

shouldThrow('s.extend(span2.firstChild, -1)');

s.extend(span2.firstChild, 0);
shouldBe('window.getSelection()', 'b');

s.extend(span3, 1);
shouldBe('window.getSelection()', 'arbaz');

shouldThrow('s.extend()');
shouldThrow('s.extend(null, 0)');

</script>
</body>
</html>
