<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
</head>
<body>
<script>
var testCases = [["div0", "div1", "div2"],
                 ["div0", "div2", "div1"],
                 ["div1", "div0", "div2"],
                 ["div1", "div2", "div0"],
                 ["div2", "div0", "div1"],
                 ["div2", "div1", "div0"]];

var rootDiv = document.createElement("div");
document.body.appendChild(rootDiv);
var testHtml = "<div id='div0-parent'><div id='div0'><div id='div0-child'></div></div><div id='div1-parent'><div id='div1'><div id='div1-child'></div></div><div id='div2-parent'><div id='div2'><div id='div2-child'></div></div></div></div></div>";

testCases.forEach(function (test) {
    var divX, divY, divZ;
    debug("=== Initial state ===");
    rootDiv.innerHTML = testHtml;
    divX = document.getElementById(test[0]);
    divY = document.getElementById(test[1]);
    divZ = document.getElementById(test[2]);
    checkParentAndChildAlive(divX, test[0]);
    checkParentAndChildAlive(divY, test[1]);
    checkParentAndChildAlive(divZ, test[2]);

    debug("=== After clearing innerHTML ===");
    rootDiv.innerHTML = testHtml;
    divX = document.getElementById(test[0]);
    divY = document.getElementById(test[1]);
    divZ = document.getElementById(test[2]);
    rootDiv.innerHTML = "";
    checkParentAndChildAlive(divX, test[0]);
    checkParentAndChildAlive(divY, test[1]);
    checkParentAndChildAlive(divZ, test[2]);

    debug("=== After clearing innerHTML and divX ===");
    rootDiv.innerHTML = testHtml;
    divX = document.getElementById(test[0]);
    divY = document.getElementById(test[1]);
    divZ = document.getElementById(test[2]);
    rootDiv.innerHTML = "";
    divX = null;
    gc();
    checkParentAndChildAlive(divY, test[1]);
    checkParentAndChildAlive(divZ, test[2]);

    debug("=== After clearing innerHTML, divX and divY ===");
    rootDiv.innerHTML = testHtml;
    divX = document.getElementById(test[0]);
    divY = document.getElementById(test[1]);
    divZ = document.getElementById(test[2]);
    rootDiv.innerHTML = "";
    divX = null;
    divY = null;
    gc();
    checkParentAndChildAlive(divZ, test[2]);

    debug("=== After clearing innerHTML, divX, divY and divZ ===");
    rootDiv.innerHTML = testHtml;
    divX = document.getElementById(test[0]);
    divY = document.getElementById(test[1]);
    divZ = document.getElementById(test[2]);
    if (window.internals)
        prevNodes = window.internals.numberOfLiveNodes();
    rootDiv.innerHTML = "";
    divX = null;
    divY = null;
    divZ = null;
    gc();
    if (window.internals) {
        // If all the Node objects in testHtml are successfully destructed,
        // at least 9 <div>s objects will be removed.
        // (Actually, since testHtml rendering requires more than 9 Node objects.)
        if (window.internals.numberOfLiveNodes() <= prevNodes - 9)
            testPassed("All <div> objects in a DOM tree are successfully destructed.");
        else
            testFailed("<div> objects in a DOM tree are not destructed.");
    }
});

function checkParentAndChildAlive(div, name) {
    globalDiv = div;
    shouldBeEqualToString('globalDiv.id', name);
    shouldBeEqualToString('globalDiv.parentNode.id', name + "-parent");
    shouldBeEqualToString('globalDiv.firstChild.id', name + "-child");
    globalDiv = null;
    gc();
}

var successfullyParsed = true;
</script>
<script src="../../resources/js-test-post.js"></script>
</body>
</html>
