<script src="../../../resources/js-test-pre.js"></script>
<style>
body {
    font: 16px Ahem;
}

.bbox {
    position:absolute;
    outline: 5px solid rgba(255, 0, 0, .75);
    z-index: -1;
}

.box {
    width: 400px;
    line-height: 40px;
}

.outer {
    outline: 2px solid green;
}

.inner {
    display: inline-block;
    width: 40px;
    height: 20px;
    outline: 2px solid blue;
}

#test5 {
    -webkit-transform: translate(50px, 100px) rotate(50deg);
}

#console {
    position:absolute;
    left: 500px;
}

#testArea {
    width: 300px;
}
</style>
<div id="console"></div>
<div id="testArea">

<div class="box" id="test1">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div><br><br>

<div class="box" id="test2">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div><br><br>

<div class="box" id="test3">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div><br><br>

<div class="box" id="test4">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div><br><br>

<div class="box" id="test5">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div><br><br>

<div class="box" id="test7">Lorem ipsum<br> dolor sit<br> amet.</div>

</div>
<script>
    if (window.testRunner)
        testRunner.dumpAsText();
    else
        alert("WARNING:\nThis test may show bogus FAILures if not run in DumpRenderTree, due to platform-specific differences in font metrics.");

    function testClientRect(rect, expectedRect)
    {
        shouldBeEqualToString("rect.left.toFixed(3)", expectedRect.left.toFixed(3));
        shouldBeEqualToString("rect.top.toFixed(3)", expectedRect.top.toFixed(3));
        shouldBeEqualToString("rect.width.toFixed(3)", expectedRect.width.toFixed(3));
        shouldBeEqualToString("rect.height.toFixed(3)", expectedRect.height.toFixed(3));
        if (rect.right == Math.round(rect.right))
            shouldBe("rect.right", "rect.left + rect.width");
        else
            shouldBe("Math.abs(rect.left + rect.width - rect.right) < 0.001", "true");
        if (rect.bottom == Math.round(rect.bottom))
            shouldBe("rect.bottom", "rect.top + rect.height");
        else
            shouldBe("Math.abs(rect.top + rect.height - rect.bottom) < 0.001", "true");
        debug("");
    }

    function addBBoxOverClientRect(rect)
    {
        var bbox = document.createElement('div');
        bbox.className = "bbox";
        var style = "";
        style += "left: " + rect.left + "px;";
        style += "top: " + rect.top + "px;";
        style += "width: " + (rect.right - rect.left) + "px;";
        style += "height: " + (rect.bottom - rect.top) + "px;";
        bbox.setAttribute("style", style);
        document.documentElement.appendChild(bbox);
    }

    function show(range)
    {
        if (window.testRunner)
            return;
        addBBoxOverClientRect(range.getBoundingClientRect());
    }

    var expectedResults = [
        /*1*/  { left: 8, top: 8, width: 400, height: 400 },
        /*2*/  { left: 8, top: 452, width: 400, height: 376 },
        /*3*/  { left: 8, top: 1044, width: 400, height: 96 },
        /*4*/  { left: 0, top: 0, width: 0, height: 0 },
        /*5*/  { left: -14.574, top: 1761.947, width: 504.009, height: 535.849 },
        /*6*/  { left: 0, top: 0, width: 0, height: 0 },
        /*7*/  { left: 8, top: 2180, width: 176, height: 96 },
    ];

    // Range over entire element.
    debug("Test 1")
    var range1 = document.createRange();
    range1.selectNode(document.getElementById('test1'));
    show(range1);
    rect = range1.getBoundingClientRect();
    testClientRect(rect, expectedResults[1 - 1]);

    // Range over entire element's contents.
    debug("Test 2")
    var range2 = document.createRange();
    range2.selectNodeContents(document.getElementById('test2'));
    show(range2);
    rect = range2.getBoundingClientRect();
    testClientRect(rect, expectedResults[2 - 1]);

    // Range over subset of element's contents.
    debug("Test 3")
    var range3 = document.createRange();
    range3.setStart(document.getElementById('test3').firstChild, 100);
    range3.setEnd(document.getElementById('test3').lastChild, 150);
    show(range3);
    rect = range3.getBoundingClientRect()
    testClientRect(rect, expectedResults[3 - 1]);

    // Collapsed range.
    debug("Test 4")
    var range4 = document.createRange();
    range4.selectNodeContents(document.getElementById('test4'));
    range4.collapse(true);
    show(range4);

    rect = range4.getBoundingClientRect()
    testClientRect(rect, expectedResults[4 - 1]);

    // Range over transformed elements.
    debug("Test 5")
    var range5 = document.createRange();
    range5.selectNodeContents(document.getElementById('test5'));
    show(range5);
    rect = range5.getBoundingClientRect()
    testClientRect(rect, expectedResults[5 - 1]);

    // Empty range.
    debug("Test 6")
    var range6 = document.createRange();
    rect = range6.getBoundingClientRect()
    testClientRect(rect, expectedResults[6 - 1]);
    
    // Range over subset that contains text and line break elements.
    debug("Test 7")
    var range7 = document.createRange();
    range7.setStart(document.getElementById('test7').firstChild, 2);
    range7.setEnd(document.getElementById('test7').lastChild, 2);
    show(range7);
    rect = range7.getBoundingClientRect()
    testClientRect(rect, expectedResults[7 - 1]);

    if (window.testRunner) {
        var area = document.getElementById('testArea');
        area.parentNode.removeChild(area);
    }
</script>
<script src="../../../resources/js-test-post.js"></script>
