testing/mochitest/browser-harness.xul
author Doug Turner <dougt@meer.net>
Tue, 15 Jul 2008 16:37:48 -0700
changeset 15958 46b8c45ac8775fd5a53fc76728141b3b8b708841
parent 6373 986307d758e9ed190bafe6fdbe03512e44352585
child 15961 b2d1c3093a84bac8fc6a08d7c9b6df6e358003ec
permissions -rw-r--r--
Inital geolocation implementation. bug=437948, r/sr=jst

<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<!-- ***** BEGIN LICENSE BLOCK *****
   - Version: MPL 1.1/GPL 2.0/LGPL 2.1
   -
   - The contents of this file are subject to the Mozilla Public License Version
   - 1.1 (the "License"); you may not use this file except in compliance with
   - the License. You may obtain a copy of the License at
   - http://www.mozilla.org/MPL/
   -
   - Software distributed under the License is distributed on an "AS IS" basis,
   - WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
   - for the specific language governing rights and limitations under the
   - License.
   -
   - The Original Code is Browser Test Harness.
   -
   - The Initial Developer of the Original Code is
   - Mozilla Corporation.
   -
   - Portions created by the Initial Developer are Copyright (C) 2007
   - the Initial Developer. All Rights Reserved.
   -
   - Contributor(s):
   -      Gavin Sharp <gavin@gavinsharp.com> (original author)
   -
   - Alternatively, the contents of this file may be used under the terms of
   - either the GNU General Public License Version 2 or later (the "GPL"), or
   - the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
   - in which case the provisions of the GPL or the LGPL are applicable instead
   - of those above. If you wish to allow use of your version of this file only
   - under the terms of either the GPL or the LGPL, and not to allow others to
   - use your version of this file under the terms of the MPL, indicate your
   - decision by deleting the provisions above and replace them with the notice
   - and other provisions required by the LGPL or the GPL. If you do not delete
   - the provisions above, a recipient may use your version of this file under
   - the terms of any one of the MPL, the GPL or the LGPL.
   -
   - ***** END LICENSE BLOCK ***** -->

<window id="browserTestHarness"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        onload="TestStart();"
        title="Browser chrome tests">
  <script src="chrome://mochikit/content/tests/SimpleTest/MozillaFileLogger.js"/>
  <script src="chrome://mochikit/content/tests/SimpleTest/quit.js"/>
  <script type="application/javascript;version=1.7"><![CDATA[
    var gConfig;
    function TestStart() {
      gConfig = readConfig();

      // If MochiTest was started with the --test-path flag specifying a subset
      // of tests to run, put that path in the label of the "Run Tests" button
      // so the tester knows which tests will run when they press that button.
      if (gConfig.testPath)
        document.getElementById("runTestsButton").label =
          "Run " + gConfig.testPath + " Tests";

      if (gConfig.autoRun)
        setTimeout(runAllTests, 0);
    }

    function readConfig() {
      var fileLocator = Cc["@mozilla.org/file/directory_service;1"].
                        getService(Ci.nsIProperties);
      var configFile = fileLocator.get("ProfD", Ci.nsIFile);
      configFile.append("testConfig.js");

      if (!configFile.exists())
        return;

      var fileInStream = Cc["@mozilla.org/network/file-input-stream;1"].
                         createInstance(Ci.nsIFileInputStream);
      var sstream = Cc["@mozilla.org/scriptableinputstream;1"].
                    createInstance(Ci.nsIScriptableInputStream);
      fileInStream.init(configFile, -1, 0, 0);
      sstream.init(fileInStream);

      var config = "";
      var str = sstream.read(4096);
      while (str.length > 0) {
        config += str;
        str = sstream.read(4096);
      }

      sstream.close();
      fileInStream.close();

      return eval(config);
    }

    function getChromeDir() {
      const Cc = Components.classes; const Ci = Components.interfaces;

      /** Find our chrome dir **/
      var ios = Cc["@mozilla.org/network/io-service;1"].
                  getService(Ci.nsIIOService);
      var chromeURI = ios.newURI("chrome://mochikit/content/",
                                 null, null);
      var resolvedURI = Cc["@mozilla.org/chrome/chrome-registry;1"].
                        getService(Ci.nsIChromeRegistry).
                        convertChromeURL(chromeURI);
      var fileHandler = Cc["@mozilla.org/network/protocol;1?name=file"].
                        getService(Ci.nsIFileProtocolHandler);
      var chromeDir = fileHandler.getFileFromURLSpec(resolvedURI.spec);

      return chromeDir.parent.QueryInterface(Ci.nsILocalFile);
    }

    function browserTestFile(aTestFile) {
      this.path = aTestFile;
      this.tests = [];
      this.scope = null;
    }
    browserTestFile.prototype = {
      get passCount() {
        return this.tests.filter(function (t) !t.todo && t.pass).length;
      },
      get todoCount() {
        return this.tests.filter(function (t) t.todo && t.pass).length;
      },
      get failCount() {
        return this.tests.filter(function (t) !t.pass).length;
      },
      get log() {
        var path = this.path;
        return this.tests.map(function (t) {
                                if (!t.pass)
                                  return t.msg + " - " + path;
                                return t.msg;
                              }).join("\n");
      }
    };

    // Returns an array of chrome:// URLs to all the test files
    function listTests() {
      const Cc = Components.classes; const Ci = Components.interfaces;

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

      var testsDir = getChromeDir();
      testsDir.appendRelativePath("browser");
      if (gConfig.testPath) {
        var testsDirURI = ioSvc.newFileURI(testsDir);
        testsDir = ioSvc.newURI(gConfig.testPath, null, testsDirURI)
                        .QueryInterface(Ci.nsIFileURL).file;
      }

      /** load server.js in so we can share template functions **/
      var scriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"].
                         getService(Ci.mozIJSSubScriptLoader);
      var srvScope = {};
      scriptLoader.loadSubScript("chrome://mochikit/content/server.js", srvScope);

      var requestPath = "chrome://mochikit/content/browser";
      if (gConfig.testPath)
        requestPath += "/" + gConfig.testPath;

      var [links, count] = srvScope.list(requestPath, testsDir, true);
      var fileNames = [];
      srvScope.arrayOfTestFiles(links, fileNames, /browser_.+\.js$/);

      return fileNames.map(function (f) new browserTestFile(f));;
    }

    function setStatus(aStatusString) {
      document.getElementById("status").value = aStatusString;
    }

    function runAllTests() {
      var windowMediator = Cc['@mozilla.org/appshell/window-mediator;1'].
                             getService(Ci.nsIWindowMediator);
      var testWin = windowMediator.getMostRecentWindow("navigator:browser");

      setStatus("Running...");
      testWin.focus();

      var Tester = new testWin.Tester(listTests(), testsFinished);
      Tester.start();
    }

    function getLogFromTests(aTests) {
      if (!aTests.length)
        return "PASS - No tests to run";

      var log = aTests.map(function (f) {
                             var output = f.path + "\n";
                             if (f.log)
                               output += f.log + "\n";
                             return output;
                           }).join("");
      log += "\nBrowser Chrome Test Summary\n";
      function sum(a, b){ return a + b; }
      var passCount = aTests.map(function (f) f.passCount).reduce(sum);
      var failCount = aTests.map(function (f) f.failCount).reduce(sum);
      var todoCount = aTests.map(function (f) f.todoCount).reduce(sum);
      log += "\tPass: " + passCount + "\n\tFail: " + failCount + "\n\tTodo: " + todoCount + "\n";

      return log;
    }

    function testsFinished(aTests) {
      // Focus our window, to display the results
      window.focus();

      var start = "*** Start BrowserChrome Test Results ***\n";
      var end = "\n*** End BrowserChrome Test Results ***\n";
      var output = start + getLogFromTests(aTests) + end;

      // Output to stdout
      dump(output);

      // Output to file
      if (gConfig.logPath) {
        MozillaFileLogger.init(gConfig.logPath);
        MozillaFileLogger._foStream.write(output, output.length);
        MozillaFileLogger.close();
      }

      if (gConfig.closeWhenDone) {
        goQuitApplication();
        return;
      }

      // UI
      document.getElementById("results").value = output;
      setStatus("Done.");
    }
  ]]></script>
  <button id="runTestsButton" onclick="runAllTests();" label="Run All Tests"/>
  <label id="status"/>
  <textbox flex="1" multiline="true" id="results"/>
</window>