<?xml version="1.0" ?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>

<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
   <tree flex="20" id="t" ref="urn:data:row" datasources="rdf:null" seltype="single">
     <treecols> 
      <treecol flex="1" id="id" label="id" sort="rdf:http://dummy/rdf#id" />
      <splitter class="tree-splitter"/>
      <treecol flex="1" id="title" label="title" sort="rdf:http://dummy/rdf#title" sortActive="true" sortDirection="ascending" /><splitter class="tree-splitter"/>
     </treecols>
     <template>
       <treechildren>
         <treeitem uri="rdf:*" seltype="single">
           <treerow >
             <treecell label="rdf:http://dummy/rdf#id"/>
             <treecell label="rdf:http://dummy/rdf#title"/>
           </treerow>
         </treeitem>
       </treechildren>
     </template>
   </tree>
   <tree flex="20" id="tc" ref="urn:data:row" datasources="rdf:null" seltype="single" flags="dont-build-content">
     <treecols> 
      <treecol flex="1" id="idc" label="id" sort="rdf:http://dummy/rdf#id" />
      <splitter class="tree-splitter"/>
      <treecol flex="1" id="titlec" label="title" sort="rdf:http://dummy/rdf#title" sortActive="true" sortDirection="ascending" /><splitter class="tree-splitter"/>
     </treecols>
     <template>
       <treechildren>
         <treeitem uri="rdf:*" seltype="single">
           <treerow >
             <treecell label="rdf:http://dummy/rdf#id"/>
             <treecell label="rdf:http://dummy/rdf#title"/>
           </treerow>
         </treeitem>
       </treechildren>
     </template>
   </tree>

<body xmlns="http://www.w3.org/1999/xhtml" style="height: 300px; overflow: auto;"/>

<script type="application/javascript" src="/MochiKit/packed.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>      

<script type="application/x-javascript">
<![CDATA[

var buildCount = 0;

SimpleTest.waitForExplicitFinish();

var TemplateBuilderListener = {
  willRebuild: function(aBuilder) {
  },

  didRebuild: function(aBuilder) {
  ++buildCount;
    var remove = false;
    if (buildCount == 2) {
      remove =true;
      setTimeout(nextDataSource, 0);
    } else if (buildCount == 4) {
      remove = true;
      setTimeout(continueTest, 0);
    }
    if (remove) {
      var tree = document.getElementById('t');
      var treec = document.getElementById('tc');
      tree.builder.removeListener(TemplateBuilderListener);
      treec.builder.removeListener(TemplateBuilderListener);
    }
  },

  QueryInterface: function (aIID)
  {
    if (!aIID.equals(Components.interfaces.nsIXULBuilderListener) &&
        !aIID.equals(Components.interfaces.nsISupports))
      throw Components.results.NS_ERROR_NO_INTERFACE;
    return this;
  }
};

function runTest() {
  var tree = document.getElementById('t');
  var treec = document.getElementById('tc');

  try {
    netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
    var rdfService = Components.classes["@mozilla.org/rdf/rdf-service;1"].
                       getService(Components.interfaces.nsIRDFService);

    var s1 = window.location.href.replace(/test_bug441785.xul/, "bug441785-1.rdf");
    var ds1 = rdfService.GetDataSourceBlocking(s1);

    var s2 = window.location.href.replace(/test_bug441785.xul/, "bug441785-2.rdf");
    var ds2 = rdfService.GetDataSourceBlocking(s2);
  } catch (ex) { }

  tree.builder.addListener(TemplateBuilderListener);
  treec.builder.addListener(TemplateBuilderListener);
  tree.setAttribute('datasources', 'bug441785-1.rdf');
  treec.setAttribute('datasources', 'bug441785-1.rdf');
}

var oldtreefirstrow, oldtreecfirstrow;

function nextDataSource()
{
  var tree = document.getElementById('t');
  var treec = document.getElementById('tc');
  tree.treeBoxObject.scrollToRow(10);
  treec.treeBoxObject.scrollToRow(10);

  is(tree.treeBoxObject.getFirstVisibleRow(), 10, "first tree row count datasource 1");
  is(treec.treeBoxObject.getFirstVisibleRow(), 10, "second tree row count datasource 1");

  tree.builder.addListener(TemplateBuilderListener);
  treec.builder.addListener(TemplateBuilderListener);
  tree.setAttribute('datasources', 'bug441785-2.rdf');
  treec.setAttribute('datasources', 'bug441785-2.rdf');
}

function continueTest() {
  var tree = document.getElementById('t');
  var treec = document.getElementById('tc');

  is(tree.treeBoxObject.getFirstVisibleRow(), 0, "first tree row count datasource 2");
  is(treec.treeBoxObject.getFirstVisibleRow(), 0, "second tree row count datasource 2");

  try {
    window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
            .getInterface(Components.interfaces.nsIDOMWindowUtils)
            .garbageCollect();
  } 
  catch (e) { }

  // Hit the bug, crash 
  // (not exactly the same kind of crash as 441785, but from the same cause)
  tree.parentNode.removeChild(tree);
  treec.parentNode.removeChild(treec);

  SimpleTest.finish();
}

window.addEventListener("load", runTest, false);

]]>
</script>
</window>
