<?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 title="Right to Left UI Test"
        onload="runTest()"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

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

  <body  xmlns="http://www.w3.org/1999/xhtml">
    <p id="display"></p>
    <div id="content" style="display: none">
    </div>
    <pre id="test">
    </pre>
  </body>

  <iframe id="subframe" width="100" height="100" onload="frameLoaded();"/>

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

      /* This code is mostly copied from chrome/test/unit/head_crtestutils.js */
      const NS_CHROME_MANIFESTS_FILE_LIST = "ChromeML";

      const Cc = Components.classes;
      const Ci = Components.interfaces;
      const Cr = Components.results;

      function ArrayEnumerator(array)
      {
        this.array = array;
      }

      ArrayEnumerator.prototype = {
        pos: 0,
        
        hasMoreElements: function() {
          return this.pos < this.array.length;
        },
        
        getNext: function() {
          if (this.pos < this.array.length)
            return this.array[this.pos++];
          throw Cr.NS_ERROR_FAILURE;
        },

        QueryInterface: function(iid) {
          if (iid.equals(Ci.nsISimpleEnumerator)
           || iid.equals(Ci.nsISupports))
            return this;

          throw Cr.NS_ERROR_NO_INTERFACE;
        }
      };

      function ChromeProvider(manifests)
      {
        this._manifests = manifests;
      }

      ChromeProvider.prototype = {
        getFile: function(prop, persistent) {
          throw Cr.NS_ERROR_FAILURE;
        },

        getFiles: function(prop) {
          if (prop == NS_CHROME_MANIFESTS_FILE_LIST) {
            return new ArrayEnumerator(this._manifests);
          }
          throw Cr.NS_ERROR_FAILURE;
        },

        QueryInterface: function(iid) {
          if (iid.equals(Ci.nsIDirectoryServiceProvider)
           || iid.equals(Ci.nsIDirectoryServiceProvider2)
           || iid.equals(Ci.nsISupports))
            return this;

          throw Cr.NS_ERROR_NO_INTERFACE;
        }
      };

      let dirSvc = Cc["@mozilla.org/file/directory_service;1"].
                   getService(Ci.nsIDirectoryService);
      function registerManifests(manifests)
      {
        let provider = new ChromeProvider(manifests);
        dirSvc.registerProvider(provider);
        return provider;
      }

      SimpleTest.waitForExplicitFinish();

      // Disable XUL cache temporarily
      let prefs = Cc["@mozilla.org/preferences-service;1"].
                  getService(Ci.nsIPrefBranch);
      const XUL_CACHE_PREF = "nglayout.debug.disable_xul_cache";
      const UI_DIRECTION_PREF = "intl.uidirection.ar";
      prefs.setBoolPref(XUL_CACHE_PREF, true);
      prefs.setCharPref(UI_DIRECTION_PREF, "rtl");

      // Register our manifest
      let cr = Cc["@mozilla.org/chrome/chrome-registry;1"].
               getService(Ci.nsIXULChromeRegistry);
      let manifests = [];
      let currentManifests = dirSvc.QueryInterface(Ci.nsIProperties)
                                   .get(NS_CHROME_MANIFESTS_FILE_LIST,
                                        Ci.nsISimpleEnumerator);
      while (currentManifests.hasMoreElements())
        manifests.push(currentManifests.getNext());

      let ioService = Cc["@mozilla.org/network/io-service;1"].
                        getService(Ci.nsIIOService);

      let uri = ioService.newURI("chrome://mochikit/content/chrome/toolkit/content/tests/chrome/rtltest/", null, null);
      uri = cr.convertChromeURL(uri);
      let newChromePath = uri.QueryInterface(Ci.nsIFileURL).file;
      manifests.push(newChromePath);
      let provider = registerManifests(manifests);
      cr.checkForNewChrome();
      cr.refreshSkins();

      function runTest()
      {
        var subframe = document.getElementById("subframe");
        subframe.setAttribute("src", "chrome://ltrtest/content/dirtest.xul");
      }

      function frameLoaded()
      {
        var subframe = document.getElementById("subframe");
        var subwin = subframe.contentWindow;
        var subdoc = subframe.contentDocument;
        var url = String(subwin.location);
        if (url.indexOf("chrome://ltrtest") == 0) {
          is(subwin.getComputedStyle(subdoc.getElementById("hbox"), "").backgroundColor,
             "rgb(255, 255, 0)", "left to right with :-moz-locale-dir(ltr)");
          is(subwin.getComputedStyle(subdoc.getElementById("vbox"), "").backgroundColor,
             "rgb(255, 255, 255)", "left to right with :-moz-locale-dir(rtl)");

          is(subwin.getComputedStyle(subdoc.documentElement, "").direction, "ltr",
             "left to right direction");

          subdoc.documentElement.setAttribute("localedir", "rtl");

          is(subwin.getComputedStyle(subdoc.getElementById("hbox"), "").backgroundColor,
             "rgb(255, 255, 255)", "left to right with :-moz-locale-dir(ltr) and localedir='rtl'");
          is(subwin.getComputedStyle(subdoc.getElementById("vbox"), "").backgroundColor,
             "rgb(0, 128, 0)", "left to right with :-moz-locale-dir(rtl) and localedir='rtl'");
          is(subwin.getComputedStyle(subdoc.documentElement, "").direction, "rtl",
             "left to right direction with localedir='rtl'");

          subdoc.documentElement.removeAttribute("localedir");

          is(subwin.getComputedStyle(subdoc.getElementById("hbox"), "").backgroundColor,
             "rgb(255, 255, 0)", "left to right with :-moz-locale-dir(ltr) and localedir removed");
          is(subwin.getComputedStyle(subdoc.getElementById("vbox"), "").backgroundColor,
             "rgb(255, 255, 255)", "left to right with :-moz-locale-dir(rtl) and localedir removed");
          is(subwin.getComputedStyle(subdoc.documentElement, "").direction, "ltr",
             "left to right direction with localedir removed");

          subframe.setAttribute("src", "chrome://rtltest/content/dirtest.xul");
        }
        else if (url.indexOf("chrome://rtltest") == 0) {
          is(subwin.getComputedStyle(subdoc.getElementById("hbox"), "").backgroundColor,
             "rgb(255, 255, 255)", "right to left with :-moz-locale-dir(ltr)");
          is(subwin.getComputedStyle(subdoc.getElementById("vbox"), "").backgroundColor,
             "rgb(0, 128, 0)", "right to left with :-moz-locale-dir(rtl)");
          is(subwin.getComputedStyle(subdoc.documentElement, "").direction, "rtl",
             "right to left direction");

          subdoc.documentElement.setAttribute("localedir", "ltr");

          is(subwin.getComputedStyle(subdoc.getElementById("hbox"), "").backgroundColor,
             "rgb(255, 255, 0)", "right to left with :-moz-locale-dir(ltr) and localedir='ltr'");
          is(subwin.getComputedStyle(subdoc.getElementById("vbox"), "").backgroundColor,
             "rgb(255, 255, 255)", "right to left with :-moz-locale-dir(rtl) and localedir='ltr'");
          is(subwin.getComputedStyle(subdoc.documentElement, "").direction, "ltr",
             "right to left direction with localedir='ltr'");

          subdoc.documentElement.removeAttribute("localedir");

          prefs.setCharPref(UI_DIRECTION_PREF, "");
          is(subwin.getComputedStyle(subdoc.documentElement, "").direction, "ltr",
             "left to right direction with no preference set");
          prefs.setCharPref(UI_DIRECTION_PREF + "-QA", "rtl");
          is(subwin.getComputedStyle(subdoc.documentElement, "").direction, "rtl",
             "right to left direction with more specific preference set");
          prefs.setCharPref(UI_DIRECTION_PREF, "ltr");
          is(subwin.getComputedStyle(subdoc.documentElement, "").direction, "rtl",
             "right to left direction with less specific and more specific preference set");
          prefs.setCharPref(UI_DIRECTION_PREF, "rtl");
          prefs.setCharPref(UI_DIRECTION_PREF + "-QA", "ltr");
          is(subwin.getComputedStyle(subdoc.documentElement, "").direction, "ltr",
             "left to right direction specific preference overrides");
          if (prefs.prefHasUserValue(UI_DIRECTION_PREF + "-QA"))
            prefs.clearUserPref(UI_DIRECTION_PREF + "-QA");

          dirSvc.unregisterProvider(provider);
          cr.checkForNewChrome();
          cr.refreshSkins();
          if (prefs.prefHasUserValue(XUL_CACHE_PREF))
            prefs.clearUserPref(XUL_CACHE_PREF);
          if (prefs.prefHasUserValue(UI_DIRECTION_PREF))
            prefs.clearUserPref(UI_DIRECTION_PREF);

          SimpleTest.finish();
        }
      }
    ]]>
  </script>

</window>
