testing/mochitest/browser-harness.xul

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

     1 <?xml version="1.0"?>
     2 <?xml-stylesheet href="chrome://global/skin" type="text/css"?>
     3 <!-- This Source Code Form is subject to the terms of the Mozilla Public
     4    - License, v. 2.0. If a copy of the MPL was not distributed with this
     5    - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
     7 <window id="browserTestHarness"
     8         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
     9         onload="TestStart();"
    10         title="Browser chrome tests"
    11         width="1024">
    12   <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/MozillaLogger.js"/>
    13   <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/LogController.js"/>
    14   <script type="application/javascript" src="chrome://mochikit/content/chrome-harness.js"/>
    15   <script type="application/javascript" src="chrome://mochikit/content/manifestLibrary.js" />
    16   <script type="application/javascript" src="chrome://mochikit/content/chunkifyTests.js"/>
    17   <style xmlns="http://www.w3.org/1999/xhtml"><![CDATA[
    18     #results {
    19       margin: 5px;
    20       background-color: window;
    21       -moz-user-select: text;
    22     }
    24     #summary {
    25       color: white;
    26       border: 2px solid black;
    27     }
    29     #summary.success {
    30       background-color: #0d0;
    31     }
    33     #summary.failure {
    34       background-color: red;
    35     }
    37     #summary.todo {
    38       background-color: orange;
    39     }
    41     .info {
    42       color: grey;
    43     }
    45     .failed {
    46       color: red;
    47       font-weight: bold;
    48     }
    50     .testHeader {
    51       margin-top: 1em;
    52     }
    54     p {
    55       margin: 0.1em;
    56     }
    58     a {
    59       color: blue;
    60       text-decoration: underline;
    61     }
    62   ]]></style>
    63   <script type="application/javascript;version=1.7"><![CDATA[
    64     if (Cc === undefined) {
    65       var Cc = Components.classes;
    66       var Ci = Components.interfaces;
    67     }
    69     var gConfig;
    71     var gDumper = {
    72       get fileLogger() {
    73         let logger = null;
    74         if (gConfig.logFile) {
    75           try {
    76             logger = new MozillaFileLogger(gConfig.logFile)
    77           } catch (ex) {
    78             dump("TEST-UNEXPECTED-FAIL | (browser-harness.xul) | " +
    79                  "Error trying to log to " + gConfig.logFile + ": " + ex + "\n");
    80           }
    81         }
    82         delete this.fileLogger;
    83         return this.fileLogger = logger;
    84       },
    86       dump: function (str) {
    87         dump(str);
    89         if (this.fileLogger)
    90           this.fileLogger._foStream.write(str, str.length);
    91       },
    93       done: function () {
    94         if (this.fileLogger)
    95           this.fileLogger.close();
    96       }
    97     }
    99     function TestStart() {
   100       gConfig = readConfig();
   102       // If MochiTest was started with the --test-path flag specifying a subset
   103       // of tests to run, put that path in the label of the "Run Tests" button
   104       // so the tester knows which tests will run when they press that button.
   105       if (gConfig.testPath)
   106         document.getElementById("runTestsButton").label =
   107           "Run " + gConfig.testPath + " tests";
   109       // Similarly, update the title for --start-at and --end-at.
   110       if (gConfig.startAt || gConfig.endAt)
   111         document.getElementById("runTestsButton").label =
   112           "Run subset of tests";
   114       if (gConfig.autorun)
   115         setTimeout(runTests, 0);
   116     }
   118     var gErrorCount = 0;
   120     function browserTest(aTestFile) {
   121       this.path = aTestFile;
   122       this.dumper = gDumper;
   123       this.results = [];
   124       this.scope = null;
   125       this.duration = 0;
   126       this.unexpectedTimeouts = 0;
   127       this.lastOutputTime = 0;
   128     }
   129     browserTest.prototype = {
   130       get passCount() {
   131         return this.results.filter(function (t) !t.info && !t.todo && t.pass).length;
   132       },
   133       get todoCount() {
   134         return this.results.filter(function (t) !t.info && t.todo && t.pass).length;
   135       },
   136       get failCount() {
   137         return this.results.filter(function (t) !t.info && !t.pass).length;
   138       },
   140       addResult: function addResult(result) {
   141         this.lastOutputTime = Date.now();
   142         this.results.push(result);
   144         this.dumper.dump(result.result + " | " + this.path + " | " + result.msg + "\n");
   145       },
   147       setDuration: function setDuration(duration) {
   148         this.duration = duration;
   149       },
   151       get htmlLog() {
   152         let txtToHTML = Cc["@mozilla.org/txttohtmlconv;1"].
   153                         getService(Ci.mozITXTToHTMLConv);
   154         function _entityEncode(str) {
   155           return txtToHTML.scanTXT(str, Ci.mozITXTToHTMLConv.kEntities);
   156         }
   157         var path = _entityEncode(this.path);
   158         var html = this.results.map(function (t) {
   159           var classname = t.info ? "info" : "result " + (t.pass ? "passed" : "failed");
   160           var text = t.result + " | " + path + " | " + _entityEncode(t.msg);
   161           if (!t.info && !t.pass) {
   162             return '<p class="' + classname + '" id=\"ERROR' + (gErrorCount++) + '">' +
   163                    text + " <a href=\"javascript:scrollTo('ERROR" + gErrorCount + "')\">NEXT ERROR</a></p>";
   164           }
   165           return '<p class="' + classname + '">' + text + "</p>";
   166         }).join("\n");
   167         if (this.duration) {
   168           html += "<p class=\"info\">TEST-END | " + path + " | finished in " +
   169                   this.duration + " ms</p>";
   170         }
   171         return html;
   172       }
   173     };
   175     // Returns an array of browserTest objects for all the selected tests
   176     function runTests() {
   177       gConfig.baseurl = "chrome://mochitests/content";
   178       getTestList(gConfig, loadTestList);
   179     }
   181     function loadTestList(links) {
   182       if (!links) {
   183         createTester({});
   184         return;
   185       }
   187       // load server.js in so we can share template functions
   188       var scriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"].
   189                            getService(Ci.mozIJSSubScriptLoader);
   190       var srvScope = {};
   191       scriptLoader.loadSubScript('chrome://mochikit/content/server.js',
   192                                  srvScope);
   194       var fileNames = [];
   195       var fileNameRegexp = /browser_.+\.js$/;
   196       srvScope.arrayOfTestFiles(links, fileNames, fileNameRegexp);
   198       if (gConfig.startAt || gConfig.endAt) {
   199         fileNames = skipTests(fileNames, gConfig.startAt, gConfig.endAt);
   200       }
   202       if (gConfig.totalChunks && gConfig.thisChunk) {
   203         fileNames = chunkifyTests(fileNames, gConfig.totalChunks,
   204                                   gConfig.thisChunk, gConfig.chunkByDir);
   205       }
   207       createTester(fileNames.map(function (f) { return new browserTest(f); }));
   208     }
   210     function setStatus(aStatusString) {
   211       document.getElementById("status").value = aStatusString;
   212     }
   214     function createTester(links) {
   215       var windowMediator = Cc['@mozilla.org/appshell/window-mediator;1'].
   216                              getService(Ci.nsIWindowMediator);
   217       var winType = gConfig.testRoot == "browser" ? "navigator:browser" :
   218                     gConfig.testRoot == "metro" ? "navigator:browser" :
   219                     gConfig.testRoot == "webapprtChrome" ? "webapprt:webapp" :
   220                     null;
   221       if (!winType) {
   222         throw new Error("Unrecognized gConfig.testRoot: " + gConfig.testRoot);
   223       }
   224       var testWin = windowMediator.getMostRecentWindow(winType);
   226       setStatus("Running...");
   227       testWin.focus();
   228       var Tester = new testWin.Tester(links, gDumper, testsFinished);
   229       Tester.start();
   230     }
   232     function sum(a, b) {
   233       return a + b;
   234     }
   236     function getHTMLLogFromTests(aTests) {
   237       if (!aTests.length)
   238         return "<div id=\"summary\" class=\"failure\">No tests to run." +
   239                  " Did you pass an invalid --test-path?</div>";
   241       var log = "";
   243       var passCount = aTests.map(function (f) f.passCount).reduce(sum);
   244       var failCount = aTests.map(function (f) f.failCount).reduce(sum);
   245       var todoCount = aTests.map(function (f) f.todoCount).reduce(sum);
   246       log += "<div id=\"summary\" class=\"";
   247       log += failCount != 0 ? "failure" :
   248                passCount == 0 ? "todo" : "success";
   249       log += "\">\n<p>Passed: " + passCount + "</p>\n" +
   250              "<p>Failed: " + failCount;
   251       if (failCount > 0)
   252         log += " <a href=\"javascript:scrollTo('ERROR0')\">NEXT ERROR</a>";
   253       log += "</p>\n" +
   254              "<p>Todo: " + todoCount + "</p>\n</div>\n<div id=\"log\">\n";
   256       return log + aTests.map(function (f) {
   257                                 return "<p class=\"testHeader\">Running " + f.path + "...</p>\n" + f.htmlLog;
   258                               }).join("\n") + "</div>";
   259     }
   261     function testsFinished(aTests) {
   262       // Focus our window, to display the results
   263       window.focus();
   265       if (gConfig.closeWhenDone) {
   266         let appStartup = Cc["@mozilla.org/toolkit/app-startup;1"].getService(Ci.nsIAppStartup);
   267         appStartup.quit(Ci.nsIAppStartup.eForceQuit);
   268         return;
   269       }
   271       // UI
   272       document.getElementById("results").innerHTML = getHTMLLogFromTests(aTests);
   273       setStatus("Done.");
   274     }
   276     function scrollTo(id) {
   277       var line = document.getElementById(id);
   278       if (!line)
   279         return;
   281       var boxObject = document.getElementById("results").parentNode.boxObject;
   282       boxObject.QueryInterface(Components.interfaces.nsIScrollBoxObject);
   283       boxObject.scrollToElement(line);
   284     }
   285   ]]></script>
   286   <button id="runTestsButton" oncommand="runTests();" label="Run All Tests"/>
   287   <label id="status"/>
   288   <scrollbox flex="1" style="overflow: auto" align="stretch">
   289     <div id="results" xmlns="http://www.w3.org/1999/xhtml" flex="1"/>
   290   </scrollbox>
   291 </window>

mercurial