<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>

<!-- Firefox tabbrowser -->
<?xml-stylesheet href="chrome://browser/content/browser.css"
                 type="text/css"?>
<!-- SeaMonkey tabbrowser -->
<?xml-stylesheet href="chrome://navigator/content/navigator.css"
                 type="text/css"?>

<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

  <script type="application/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>

  <script type="application/javascript">
  <![CDATA[
    ////////////////////////////////////////////////////////////////////////////
    // SimpleTest stuffs

    var gOpenerWnd = window.opener.wrappedJSObject;

    function ok(aCond, aMsg) {
      gOpenerWnd.SimpleTest.ok(aCond, aMsg);
    }

    function is(aExpected, aActual, aMsg) {
      gOpenerWnd.SimpleTest.is(aExpected, aActual, aMsg);
    }

    function testStates(aAccOrElmOrID, aState, aExtraState, aAbsentState,
                        aAbsentExtraState)
    {
      gOpenerWnd.testStates(aAccOrElmOrID, aState, aExtraState, aAbsentState,
                            aAbsentExtraState);
    }

    var invokerChecker = gOpenerWnd.invokerChecker;

    const STATE_BUSY = gOpenerWnd.STATE_BUSY;
    const EVENT_DOCUMENT_LOAD_COMPLETE =
      gOpenerWnd.EVENT_DOCUMENT_LOAD_COMPLETE;
    const EVENT_DOCUMENT_RELOAD = gOpenerWnd.EVENT_DOCUMENT_RELOAD;
    const EVENT_DOCUMENT_LOAD_STOPPED =
      gOpenerWnd.EVENT_DOCUMENT_LOAD_STOPPED;
    const EVENT_REORDER = gOpenerWnd.EVENT_REORDER;
    const EVENT_STATE_CHANGE = gOpenerWnd.EVENT_STATE_CHANGE;
    const nsIAccessibleStateChangeEvent =
      gOpenerWnd.nsIAccessibleStateChangeEvent;

    ////////////////////////////////////////////////////////////////////////////
    // Hacks to make xul:tabbrowser work.

    var handleDroppedLink = null; // needed for tabbrowser usage

    Components.utils.import("resource://gre/modules/Services.jsm");
    var XULBrowserWindow = {
      isBusy: false,
      setOverLink: function (link, b) {
      }
    };

    gFindBarInitialized = false;

    ////////////////////////////////////////////////////////////////////////////
    // Helpers.

    function getContainer()
    {
      var idx = gTabBrowser.tabContainer.selectedIndex;
      return gTabBrowser.getBrowserAtIndex(idx);
    }

    function getDocument()
    {
      return getContainer().contentDocument;
    }

    ////////////////////////////////////////////////////////////////////////////
    // Invoker checkers.
    function stateBusyChecker(aIsEnabled)
    {
      this.type = EVENT_STATE_CHANGE;
      this.__defineGetter__("target", getDocument);

      this.check = function stateBusyChecker_check(aEvent)
      {
        var event = null;
        try {
          var event = aEvent.QueryInterface(nsIAccessibleStateChangeEvent);
        } catch (e) {
          ok(false, "State change event was expected");
        }

        if (!event)
          return;

        is(event.state, STATE_BUSY, "Wrong state of statechange event.");
        is(event.isEnabled(), aIsEnabled,
           "Wrong value of state of statechange event");

        testStates(event.accessible, (aIsEnabled ? STATE_BUSY : 0), 0,
                   (aIsEnabled ? 0 : STATE_BUSY), 0);
      }
    }

    function documentReloadChecker(aIsFromUserInput)
    {
      this.type = EVENT_DOCUMENT_RELOAD;
      this.__defineGetter__("target", getDocument);

      this.check = function documentReloadChecker_check(aEvent)
      {
        is(aEvent.isFromUserInput, aIsFromUserInput,
           "Wrong value of isFromUserInput");
      }
    }

    ////////////////////////////////////////////////////////////////////////////
    // Invokers.

    /**
     * Load URI.
     */
    function loadURIInvoker(aURI)
    {
      this.invoke = function loadURIInvoker_invoke()
      {
        gTabBrowser.loadURI(aURI);
      }

      this.eventSeq = [
        // We don't expect state change event for busy true since things happen
        // quickly and it's coalesced.
        new invokerChecker(EVENT_REORDER, getContainer),
        new invokerChecker(EVENT_DOCUMENT_LOAD_COMPLETE, getDocument),
        new stateBusyChecker(false)
      ];

      this.getID = function loadURIInvoker_getID()
      {
        return "load uri " + aURI;
      }
    }

    /**
     * Click reload page button.
     */
    function clickReloadBtnInvoker()
    {
      this.invoke = function clickReloadBtnInvoker_invoke()
      {
        synthesizeMouse(document.getElementById("reloadbtn"), 5, 5, {});
      }

      this.eventSeq = [
        new documentReloadChecker(true),
        new invokerChecker(EVENT_REORDER, getContainer),
        new invokerChecker(EVENT_DOCUMENT_LOAD_COMPLETE, getDocument),
        new stateBusyChecker(false)
      ];

      this.getID = function reloadInvoker_getID()
      {
        return "click reload page button";
      }
    }

    /**
     * Reload the page.
     */
    function reloadInvoker()
    {
      this.invoke = function reloadInvoker_invoke()
      {
        gTabBrowser.reload();
      }

      this.eventSeq = [
        new documentReloadChecker(false),
        new invokerChecker(EVENT_REORDER, getContainer),
        new invokerChecker(EVENT_DOCUMENT_LOAD_COMPLETE, getDocument),
        new stateBusyChecker(false)
      ];

      this.getID = function reloadInvoker_getID()
      {
        return "reload page";
      }
    }

    /**
     * Load wrong URI what results in error page loading.
     */
    function loadErrorPageInvoker()
    {
      this.invoke = function loadErrorPageInvoker_invoke()
      {
        gTabBrowser.loadURI("www.wronguri.wronguri");
      }

      this.eventSeq = [
        // We don't expect state change for busy true, load stopped events since
        // things happen quickly and it's coalesced.
        new invokerChecker(EVENT_REORDER, getContainer),
        new invokerChecker(EVENT_DOCUMENT_LOAD_COMPLETE, getDocument),
        new stateBusyChecker(false)
      ];

      this.getID = function loadErrorPageInvoker_getID()
      {
        return "load error page";
      }
    }

    ////////////////////////////////////////////////////////////////////////////
    // Tests

    var gQueue = null;

    const Ci = Components.interfaces;

    var gTabBrowser = null;
    function doTest()
    {
      gTabBrowser = document.getElementById("content");

      gQueue = new gOpenerWnd.eventQueue();
      gQueue.push(new loadURIInvoker("about:robots"));
      gQueue.push(new clickReloadBtnInvoker());
      gQueue.push(new loadURIInvoker("about:mozilla"));
      gQueue.push(new reloadInvoker());
      gQueue.push(new loadErrorPageInvoker());
      gQueue.onFinish = function() { window.close(); }
      gQueue.invoke();
    }

    gOpenerWnd.addA11yLoadEvent(doTest);
  ]]>
  </script>

  <!-- Hack to make xul:tabbrowser work -->
  <menubar>
    <menu label="menu">
      <menupopup>
        <menuitem label="close window hook" id="menu_closeWindow"/>
        <menuitem label="close hook" id="menu_close"/>
      </menupopup>
    </menu>
  </menubar>

  <button id="reloadbtn" label="reload page"
          oncommand="gTabBrowser.reload();"/>

  <toolbar>
    <tabs id="tabbrowser-tabs" class="tabbrowser-tabs"
          tabbrowser="content"
          flex="1"
          setfocus="false">
      <tab class="tabbrowser-tab" selected="true"/>
    </tabs>
  </toolbar>
  <tabbrowser id="content"
              type="content-primary"
              tabcontainer="tabbrowser-tabs"
              flex="1"/>

</window>
