<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        width="800" height="600" orient="vertical">

  <script type="application/javascript">

    var cpow_tests = {
      resolve: function(obj, keys) {
        alert("resolving");
        keys = keys.split(" ");
        for (var i = 0; i != keys.length; ++i)
          alert("has " + keys[i] + "? " + (keys[i] in obj));
        alert("done resolving");
      },
      iterate: function(obj) {
        alert("iterating");
        for (var k in obj)
          alert("key: " + k);
        alert("done iterating");
      },
      navigate: function(child) {
        alert("navigating");
        child.location = prompt("Where to?");
        setTimeout(function() {
          alert(child.location.href);
        }, 2000);
      },
      construct: function(foo) {
        alert(new foo.ctor("answer", 42).answer);
        alert(new foo.fakector("answer", 42).answer);
      },
      indirect_eval: function(child) {
        alert(child.eval("location.href"));
        alert(new child.Function("x", "return x+1")(42));
      },
      funcalls: function(foo) {
        var fn = foo.f;
        alert(foo.f(2));
        alert(fn.call.call(fn, foo, 3));
      },
      equality: function(child) {
        var foo = child.foo,
            self = foo.self;
        alert("foo == self? " + (foo == self));
        alert("foo === self? " + (foo === self));
      },
      exceptions: function(child) {
        var ball = "ball";
        try {
          child.foo.pitch(ball);
          alert("shouldn't reach this point");
        } catch (x) {
          alert("ball === x? " + (ball === x));
        }
      }
    }

    function getCPOW() {
      if (!getCPOW.cpow) {
        var page = document.getElementById("page"),
            owner = page.QueryInterface(Components.interfaces.nsIFrameLoaderOwner);
        getCPOW.cpow = owner.crossProcessObjectWrapper;
        alert("got fresh CPOW");
      }
      return getCPOW.cpow;
    }
    
    function test_cpow() {
      var child = getCPOW();
      cpow_tests.construct(child.foo);
      cpow_tests.resolve(child.location, "href hostname");
      cpow_tests.iterate(child.location);
      cpow_tests.iterate(child.foo);
      cpow_tests.funcalls(child.foo);
      cpow_tests.navigate(child);
      cpow_tests.equality(child);
      cpow_tests.exceptions(child);
      setTimeout(function() {
        alert("going back");
        child.history.back();
      }, 3000);
    }

    function show_location() {
      var child = getCPOW();
      child.location += "#fragment";
      alert(child.location.href);
      alert(child.document.documentURI);
    }
  </script>

  <toolbar id="controls">
    <toolbarbutton onclick="test_cpow()" label="Run tests."/>
    <toolbarbutton onclick="show_location()" label="Show location."/>
  </toolbar>

  <browser remote="true" width="200" height="200"
           type="content"
           src="child.html"
           id="page" />

</window>
