<!doctype html>
<html>
<head>
<script src="../resources/inspector-test.js"></script>
<script>
function test()
{
    let suite = InspectorTest.createAsyncSuite("DOM.DisconnectTreeOnMainResourceChange");

    suite.addTestCase({
        name: "CheckLazyInitializationOfDOMTree",
        description: "Check that DOMTree instances are created lazily.",
        test(resolve, reject) {
            let instances = WI.domManager.activelyListeningObjectsWithPrototype(WI.DOMTree);
            InspectorTest.expectThat(instances.size === 0, "There should not be a DOMTree listening to DOMTreeManager events initially.");
            InspectorTest.log("DOMTree instance count: " + instances.size);
            resolve();
        }
    });

    suite.addTestCase({
        name: "CheckDOMTreeCountAfterUsingGetter",
        description: "Check that the count of connected DOMTrees is correct after initializing them.",
        test(resolve, reject) {
            let mainFrame = WI.networkManager.mainFrame;
            mainFrame.domTree; // Force creation of the root DOM tree.

            let instances = WI.domManager.activelyListeningObjectsWithPrototype(WI.DOMTree);
            InspectorTest.expectThat(instances.size === 1, "There should be a one DOMTree listening to DOMTreeManager events after creation.");
            InspectorTest.log("DOMTree instance count: " + instances.size);

            // Force creation of child DOM trees.
            let childrenLevel1 = Array.from(mainFrame.childFrameCollection);
            childrenLevel1[0].domTree;

            let childrenLevel2 = Array.from(childrenLevel1[0].childFrameCollection);
            childrenLevel2[0].domTree;

            let childrenLevel3 = Array.from(childrenLevel2[0].childFrameCollection);
            childrenLevel3[0].domTree;

            instances = WI.domManager.activelyListeningObjectsWithPrototype(WI.DOMTree);
            InspectorTest.expectThat(instances.size === 4, "There should be four DOMTrees listening to DOMTreeManager events after touching each Frame.");
            InspectorTest.log("DOMTree instance count: " + instances.size);

            resolve();
        }
    });

    suite.addTestCase({
        name: "CheckDOMTreeCountAfterReloading",
        description: "Check that the count of connected DOMTrees is correct after a reload.",
        test(resolve, reject) {
            WI.Frame.awaitEvent(WI.Frame.Event.MainResourceDidChange)
            .then((event) => {
                let instances = WI.domManager.activelyListeningObjectsWithPrototype(WI.DOMTree);
                InspectorTest.expectThat(instances.size === 0, "There should not be any DOMTrees listening to DOMTreeManager events after a main frame navigation.");
                InspectorTest.log("DOMTree instance count: " + instances.size);
            })
            .then(resolve, reject);

            InspectorTest.reloadPage();
        }
    });

    suite.runTestCasesAndFinish();
}
</script>
</head>
<body onload="runTest()">
    <p>Testing that DOMTrees are not retained by DOMTreeManager across main frame navigations.</p>
    <iframe src="resources/nested-frame-2-deep.html"/>
</body>
</html>
