exports.testDispatchTrivialEvent = function(test) {
  var file = require("file");
  var url = require("url");
  var mydir = file.dirname(url.toFilename(__url__));
  var indexUrl = url.fromFilename(file.join(mydir, "test-tab-events.html"));

  var tracker = tabBrowser.whenContentLoaded(
    function(window) {
      if (window.location == indexUrl) {
        function checkGotFoo() {
          test.assertEqual(window.wrappedJSObject.gotFoo, true,
                           "foo event should have been dispatched");
          window.close();
          timer.setTimeout(function() {
                             tracker.unload();
                             test.done();
                           }, ARB_TIMEOUT);
        }

        test.assertEqual(window.wrappedJSObject.gotFoo, false,
                         "foo event should not have been dispatched yet");

        timer.setTimeout(function() {
                           tabEvents.dispatchTrivialEvent("foo");
                           timer.setTimeout(checkGotFoo, 10);
                         }, 10);
      }
    });

  tabBrowser.addTab(indexUrl);
  test.waitUntilDone(5000);
};
exports.testBasic = function(test) {
  var manager = require("json-channel").createManager("boop");
  var file = require("file");
  var url = require("url");
  var mydir = file.dirname(url.toFilename(__url__));
  var indexUrl = url.fromFilename(file.join(mydir, "test-json-channel.html"));

  var tracker = tabBrowser.whenContentLoaded(
    function(window) {
      if (window.location == indexUrl) {
        var foo = window.document.getElementById("foo");
        var channel = manager.addChannel(window);
        channel.whenMessaged(
          function(data) {
            test.assertEqual(data, "HAI2u!",
                             "chrome channel.whenMessaged() works.");
            channel.send("O YEA!");
            timer.setTimeout(
              function() {
                test.assertEqual(foo.textContent,
                                 'got "O YEA!"',
                                 "content channel.whenMessaged works.");
                test.assertEqual(manager.channels.length,
                                 1,
                                 "channel count is correct");
                window.close();
                timer.setTimeout(function() {
                                   test.assertEqual(
                                     manager.channels.length,
                                     0,
                                     "channel closed on window close"
                                   );
                                   manager.unload();
                                   tracker.unload();
                                   test.done();
                                 }, ARB_TIMEOUT);
              },
              ARB_TIMEOUT);
            return {msg: 'gotcha'};
          });
      }
    });

  tabBrowser.addTab(indexUrl);
  test.waitUntilDone(5000);
};
exports.GM_openInTab = function GM_openInTab(url) {
  tabBrowser.addTab(url);
}