File: browser-harness.xul

package info (click to toggle)
firefox-esr 68.10.0esr-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,143,932 kB
  • sloc: cpp: 5,227,879; javascript: 4,315,531; ansic: 2,467,042; python: 794,975; java: 349,993; asm: 232,034; xml: 228,320; sh: 82,008; lisp: 41,202; makefile: 22,347; perl: 15,555; objc: 5,277; cs: 4,725; yacc: 1,778; ada: 1,681; pascal: 1,673; lex: 1,417; exp: 527; php: 436; ruby: 225; awk: 162; sed: 53; csh: 44
file content (339 lines) | stat: -rw-r--r-- 10,930 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

<window id="browserTestHarness"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        onload="TestStart();"
        title="Browser chrome tests"
        width="1024">
  <script src="chrome://mochikit/content/tests/SimpleTest/MozillaLogger.js"/>
  <script src="chrome://mochikit/content/tests/SimpleTest/LogController.js"/>
  <script src="chrome://mochikit/content/tests/SimpleTest/StructuredLog.jsm"/>
  <script src="chrome://mochikit/content/tests/SimpleTest/TestRunner.js"/>
  <script src="chrome://mochikit/content/chrome-harness.js"/>
  <script src="chrome://mochikit/content/manifestLibrary.js" />
  <script src="chrome://mochikit/content/chunkifyTests.js"/>
  <style xmlns="http://www.w3.org/1999/xhtml"><![CDATA[
    #results {
      margin: 5px;
      background-color: window;
      -moz-user-select: text;
    }

    #summary {
      color: white;
      border: 2px solid black;
    }

    #summary.success {
      background-color: #0d0;
    }

    #summary.failure {
      background-color: red;
    }

    #summary.todo {
      background-color: orange;
    }

    .info {
      color: grey;
    }

    .failed {
      color: red;
      font-weight: bold;
    }

    .testHeader {
      margin-top: 1em;
    }

    p {
      margin: 0.1em;
    }

    a {
      color: blue;
      text-decoration: underline;
    }
  ]]></style>
  <script type="application/javascript"><![CDATA[
    var gConfig;

    var gDumper = {
      get fileLogger() {
        let logger = null;
        if (gConfig.logFile) {
          try {
            logger = new MozillaFileLogger(gConfig.logFile)
          } catch (ex) {
            dump("TEST-UNEXPECTED-FAIL | (browser-harness.xul) | " +
                 "Error trying to log to " + gConfig.logFile + ": " + ex + "\n");
          }
        }
        delete this.fileLogger;
        return this.fileLogger = logger;
      },
      structuredLogger: TestRunner.structuredLogger,
      dump: function (str) {
        this.structuredLogger.info(str);

        if (this.fileLogger)
          this.fileLogger.log(str);
      },

      done: function () {
        if (this.fileLogger)
          this.fileLogger.close();
      }
    }

    function TestStart() {
      gConfig = readConfig();

      // Update the title for --start-at and --end-at.
      if (gConfig.startAt || gConfig.endAt)
        document.getElementById("runTestsButton").label =
          "Run subset of tests";

      if (gConfig.autorun)
        setTimeout(runTests, 0);
    }

    var gErrorCount = 0;

    function browserTest(aTestFile) {
      this.path = aTestFile['url'];
      this.expected = aTestFile['expected'];
      this.usesUnsafeCPOWs = aTestFile['uses-unsafe-cpows'] || false;
      this.dumper = gDumper;
      this.results = [];
      this.scope = null;
      this.duration = 0;
      this.unexpectedTimeouts = 0;
      this.lastOutputTime = 0;
    }
    browserTest.prototype = {
      get passCount() {
        return this.results.filter(t => !t.info && !t.todo && t.pass).length;
      },
      get todoCount() {
        return this.results.filter(t => !t.info && t.todo && t.pass).length;
      },
      get failCount() {
        return this.results.filter(t => !t.info && !t.pass).length;
      },
      get allowedFailureCount() {
        return this.results.filter(t => t.allowedFailure).length;
      },

      addResult: function addResult(result) {
        this.lastOutputTime = Date.now();
        this.results.push(result);

        if (result.info) {
          if (result.msg) {
            this.dumper.structuredLogger.info(result.msg);
          }
          return;
        }

        this.dumper.structuredLogger.testStatus(this.path,
                                                result.name,
                                                result.status,
                                                result.expected,
                                                result.msg);
      },

      setDuration: function setDuration(duration) {
        this.duration = duration;
      },

      get htmlLog() {
        let txtToHTML = Cc["@mozilla.org/txttohtmlconv;1"].
                        getService(Ci.mozITXTToHTMLConv);
        function _entityEncode(str) {
          return txtToHTML.scanTXT(str, Ci.mozITXTToHTMLConv.kEntities);
        }
        var path = _entityEncode(this.path);
        var html = this.results.map(function (t) {
          var classname = "result ";
          var result = "TEST-";
          if (t.info) {
            classname = "info";
            result += "INFO";
          }
          else if (t.pass) {
            classname += "passed";
            if (t.todo)
              result += "KNOWN-FAIL";
            else
              result += "PASS";
          }
          else {
            classname += "failed";
            result += "UNEXPECTED-" + t.status;
          }
          var message = t.name + (t.msg ? " - " + t.msg : "");
          var text = result + " | " + path + " | " + _entityEncode(message);
          if (!t.info && !t.pass) {
            return '<p class="' + classname + '" id=\"ERROR' + (gErrorCount++) + '">' +
                   text + " <a href=\"javascript:scrollTo('ERROR" + gErrorCount + "')\">NEXT ERROR</a></p>";
          }
          return '<p class="' + classname + '">' + text + "</p>";
        }).join("\n");
        if (this.duration) {
          html += "<p class=\"info\">TEST-END | " + path + " | finished in " +
                  this.duration + " ms</p>";
        }
        return html;
      }
    };

    // Returns an array of browserTest objects for all the selected tests
    function runTests() {
      gConfig.baseurl = "chrome://mochitests/content";
      getTestList(gConfig, loadTestList);
    }

    function loadTestList(links) {
      if (!links) {
        createTester({});
        return;
      }

      // 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 fileNames = [];
      var fileNameRegexp = /browser_.+\.js$/;
      srvScope.arrayOfTestFiles(links, fileNames, fileNameRegexp);

      if (gConfig.startAt || gConfig.endAt) {
        fileNames = skipTests(fileNames, gConfig.startAt, gConfig.endAt);
      }

      createTester(fileNames.map(function (f) { return new browserTest(f); }));
    }

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

    function createTester(links) {
      var windowMediator = Cc['@mozilla.org/appshell/window-mediator;1'].
                             getService(Ci.nsIWindowMediator);
      var winType = null;
      if (gConfig.testRoot == "browser") {
        const IS_THUNDERBIRD = Services.appinfo.ID == "{3550f703-e582-4d05-9a08-453d09bdfdc6}";
        winType = IS_THUNDERBIRD ? "mail:3pane" : "navigator:browser";
      }
      if (!winType) {
        throw new Error("Unrecognized gConfig.testRoot: " + gConfig.testRoot);
      }
      var testWin = windowMediator.getMostRecentWindow(winType);

      setStatus("Running...");

      // It's possible that the test harness window is not yet focused when this
      // function runs (in which case testWin is already focused, and focusing it
      // will be a no-op, and then the test harness window will steal focus later,
      // which will mess up tests). So wait for the test harness window to be
      // focused before trying to focus testWin.
      waitForFocus(() => {
        // Focus the test window and start tests.
        waitForFocus(() => {
          var Tester = new testWin.Tester(links, gDumper.structuredLogger, testsFinished);
          Tester.start();
        }, testWin);
      }, window);
    }

    function executeSoon(callback) {
      let tm = Cc["@mozilla.org/thread-manager;1"].getService(Ci.nsIThreadManager);
      tm.dispatchToMainThread(callback);
    }

    function waitForFocus(callback, win) {
      // If "win" is already focused, just call the callback.
      let fm = Cc["@mozilla.org/focus-manager;1"].getService(Ci.nsIFocusManager);
      if (fm.focusedWindow == win) {
        executeSoon(callback);
        return;
      }

      // Otherwise focus it, and wait for the focus event.
      win.addEventListener("focus", function listener() {
        win.removeEventListener("focus", listener, true);
        executeSoon(callback);
      }, true);
      win.focus();
    }

    function sum(a, b) {
      return a + b;
    }

    function getHTMLLogFromTests(aTests) {
      if (!aTests.length)
        return "<div id=\"summary\" class=\"failure\">No tests to run." +
                 " Did you pass an invalid --test-path?</div>";

      var log = "";

      var passCount = aTests.map(f => f.passCount).reduce(sum);
      var failCount = aTests.map(f => f.failCount).reduce(sum);
      var todoCount = aTests.map(f => f.todoCount).reduce(sum);
      log += "<div id=\"summary\" class=\"";
      log += failCount != 0 ? "failure" :
               passCount == 0 ? "todo" : "success";
      log += "\">\n<p>Passed: " + passCount + "</p>\n" +
             "<p>Failed: " + failCount;
      if (failCount > 0)
        log += " <a href=\"javascript:scrollTo('ERROR0')\">NEXT ERROR</a>";
      log += "</p>\n" +
             "<p>Todo: " + todoCount + "</p>\n</div>\n<div id=\"log\">\n";

      return log + aTests.map(function (f) {
                                return "<p class=\"testHeader\">Running " + f.path + "...</p>\n" + f.htmlLog;
                              }).join("\n") + "</div>";
    }

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

      if (gConfig.closeWhenDone) {
        let appStartup = Cc["@mozilla.org/toolkit/app-startup;1"].getService(Ci.nsIAppStartup);
        appStartup.quit(Ci.nsIAppStartup.eForceQuit);
        return;
      }

      // UI
      document.getElementById("results").innerHTML = getHTMLLogFromTests(aTests);
      setStatus("Done.");
    }

    function scrollTo(id) {
      var line = document.getElementById(id);
      if (!line)
        return;

      line.scrollIntoView();
    }
  ]]></script>
  <button id="runTestsButton" oncommand="runTests();" label="Run All Tests"/>
  <label id="status"/>
  <scrollbox flex="1" style="overflow: auto" align="stretch">
    <div id="results" xmlns="http://www.w3.org/1999/xhtml" flex="1"/>
  </scrollbox>
</window>