Bug 1876888 - Try estimate percentiles as "shortest 5%" instead of "top 95%". r=ahal...
[gecko.git] / testing / mochitest / browser-harness.xhtml
blob1755c25c91ee6f16f817b5ad7a4e2ebb18be69bc
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 src="chrome://mochikit/content/tests/SimpleTest/MozillaLogger.js"/>
13 <script src="chrome://mochikit/content/tests/SimpleTest/LogController.js"/>
14 <script src="chrome://mochikit/content/tests/SimpleTest/TestRunner.js"/>
15 <script src="chrome://mochikit/content/chrome-harness.js"/>
16 <script src="chrome://mochikit/content/manifestLibrary.js" />
17 <script src="chrome://mochikit/content/mochitestListingsUtils.js" />
18 <script src="chrome://mochikit/content/chunkifyTests.js"/>
19 <style xmlns="http://www.w3.org/1999/xhtml"><![CDATA[
20 #results {
21 margin: 5px;
22 background-color: window;
23 user-select: text;
26 #summary {
27 color: white;
28 border: 2px solid black;
31 #summary.success {
32 background-color: #0d0;
35 #summary.failure {
36 background-color: red;
39 #summary.todo {
40 background-color: orange;
43 .info {
44 color: grey;
47 .failed {
48 color: red;
49 font-weight: bold;
52 .testHeader {
53 margin-top: 1em;
56 p {
57 margin: 0.1em;
60 a {
61 color: blue;
62 text-decoration: underline;
64 ]]></style>
65 <script type="application/javascript"><![CDATA[
66 var gConfig;
68 var gDumper = {
69 get fileLogger() {
70 let logger = null;
71 if (gConfig.logFile) {
72 try {
73 logger = new MozillaFileLogger(gConfig.logFile)
74 } catch (ex) {
75 dump("TEST-UNEXPECTED-FAIL | (browser-harness.xhtml) | " +
76 "Error trying to log to " + gConfig.logFile + ": " + ex + "\n");
79 delete this.fileLogger;
80 return this.fileLogger = logger;
82 structuredLogger: TestRunner.structuredLogger,
83 dump(str) {
84 this.structuredLogger.info(str);
86 if (this.fileLogger)
87 this.fileLogger.log(str);
90 done() {
91 if (this.fileLogger)
92 this.fileLogger.close();
96 function TestStart() {
97 gConfig = readConfig();
99 // Update the title for --start-at and --end-at.
100 if (gConfig.startAt || gConfig.endAt)
101 document.getElementById("runTestsButton").label =
102 "Run subset of tests";
104 if (gConfig.autorun)
105 setTimeout(runTests, 0);
108 var gErrorCount = 0;
110 function browserTest(aTestFile) {
111 this.path = aTestFile.url;
112 this.expected = aTestFile.expected;
113 this.https_first_disabled = aTestFile.https_first_disabled || false;
114 this.allow_xul_xbl = aTestFile.allow_xul_xbl || false;
115 this.dumper = gDumper;
116 this.results = [];
117 this.scope = null;
118 this.duration = 0;
119 this.unexpectedTimeouts = 0;
120 this.lastOutputTime = 0;
122 browserTest.prototype = {
123 get passCount() {
124 return this.results.filter(t => !t.info && !t.todo && t.pass).length;
126 get todoCount() {
127 return this.results.filter(t => !t.info && t.todo && t.pass).length;
129 get failCount() {
130 return this.results.filter(t => !t.info && !t.pass).length;
132 get allowedFailureCount() {
133 return this.results.filter(t => t.allowedFailure).length;
136 addResult: function addResult(result) {
137 this.lastOutputTime = Date.now();
138 this.results.push(result);
140 if (result.info) {
141 if (result.msg) {
142 ChromeUtils.addProfilerMarker("TEST-INFO", {category: "Test"},
143 result.msg);
144 this.dumper.structuredLogger.info(result.msg);
146 return;
149 this.dumper.structuredLogger.testStatus(this.path,
150 result.name,
151 result.status,
152 result.expected,
153 result.msg);
154 let markerName = "TEST-";
155 if (result.pass) {
156 markerName += result.todo ? "KNOWN-FAIL" : "PASS";
158 else {
159 markerName += "UNEXPECTED-" + result.status;
161 let markerText = result.name;
162 if (result.msg) {
163 markerText += " - " + result.msg;
165 ChromeUtils.addProfilerMarker(markerName, {category: "Test"}, markerText);
168 setDuration: function setDuration(duration) {
169 this.duration = duration;
172 get htmlLog() {
173 let txtToHTML = Cc["@mozilla.org/txttohtmlconv;1"].
174 getService(Ci.mozITXTToHTMLConv);
175 function _entityEncode(str) {
176 return txtToHTML.scanTXT(str, Ci.mozITXTToHTMLConv.kEntities);
178 var path = _entityEncode(this.path);
179 var html = this.results.map(function (t) {
180 var classname = "result ";
181 var result = "TEST-";
182 if (t.info) {
183 classname = "info";
184 result += "INFO";
186 else if (t.pass) {
187 classname += "passed";
188 if (t.todo)
189 result += "KNOWN-FAIL";
190 else
191 result += "PASS";
193 else {
194 classname += "failed";
195 result += "UNEXPECTED-" + t.status;
197 var message = t.name + (t.msg ? " - " + t.msg : "");
198 var text = result + " | " + path + " | " + _entityEncode(message);
199 if (!t.info && !t.pass) {
200 return '<p class="' + classname + '" id=\"ERROR' + (gErrorCount++) + '">' +
201 text + " <a href=\"javascript:scrollTo('ERROR" + gErrorCount + "')\">NEXT ERROR</a></p>";
203 return '<p class="' + classname + '">' + text + "</p>";
204 }).join("\n");
205 if (this.duration) {
206 html += "<p class=\"info\">TEST-END | " + path + " | finished in " +
207 this.duration + " ms</p>";
209 return html;
213 // Returns an array of browserTest objects for all the selected tests
214 function runTests() {
215 gConfig.baseurl = "chrome://mochitests/content";
216 getTestList(gConfig, loadTestList);
219 function loadTestList(links) {
220 if (!links) {
221 createTester({});
222 return;
225 var fileNames = [];
226 var fileNameRegexp = /browser_.+\.js$/;
227 arrayOfTestFiles(links, fileNames, fileNameRegexp);
229 if (gConfig.startAt || gConfig.endAt) {
230 fileNames = skipTests(fileNames, gConfig.startAt, gConfig.endAt);
233 createTester(fileNames.map(function (f) { return new browserTest(f); }));
236 function setStatus(aStatusString) {
237 document.getElementById("status").value = aStatusString;
240 function createTester(links) {
241 var winType = null;
242 if (gConfig.testRoot == "browser") {
243 const IS_THUNDERBIRD = Services.appinfo.ID == "{3550f703-e582-4d05-9a08-453d09bdfdc6}";
244 winType = IS_THUNDERBIRD ? "mail:3pane" : "navigator:browser";
246 if (!winType) {
247 throw new Error("Unrecognized gConfig.testRoot: " + gConfig.testRoot);
249 var testWin = Services.wm.getMostRecentWindow(winType);
251 setStatus("Running...");
253 // It's possible that the test harness window is not yet focused when this
254 // function runs (in which case testWin is already focused, and focusing it
255 // will be a no-op, and then the test harness window will steal focus later,
256 // which will mess up tests). So wait for the test harness window to be
257 // focused before trying to focus testWin.
258 waitForFocus(() => {
259 // Focus the test window and start tests.
260 waitForFocus(() => {
261 var Tester = new testWin.Tester(links, gDumper.structuredLogger, testsFinished);
262 Tester.start();
263 }, testWin);
264 }, window);
267 function executeSoon(callback) {
268 Services.tm.dispatchToMainThread(callback);
271 function waitForFocus(callback, win) {
272 // If "win" is already focused, just call the callback.
273 if (Services.focus.focusedWindow == win) {
274 executeSoon(callback);
275 return;
278 // Otherwise focus it, and wait for the focus event.
279 win.addEventListener("focus", function listener() {
280 executeSoon(callback);
281 }, { capture: true, once: true});
282 win.focus();
285 function sum(a, b) {
286 return a + b;
289 function getHTMLLogFromTests(aTests) {
290 if (!aTests.length)
291 return "<div id=\"summary\" class=\"failure\">No tests to run." +
292 " Did you pass an invalid --test-path?</div>";
294 var log = "";
296 var passCount = aTests.map(f => f.passCount).reduce(sum);
297 var failCount = aTests.map(f => f.failCount).reduce(sum);
298 var todoCount = aTests.map(f => f.todoCount).reduce(sum);
299 log += "<div id=\"summary\" class=\"";
300 if (failCount != 0) {
301 log += "failure";
302 } else {
303 log += passCount == 0 ? "todo" : "success";
305 log += "\">\n<p>Passed: " + passCount + "</p>\n" +
306 "<p>Failed: " + failCount;
307 if (failCount > 0)
308 log += " <a href=\"javascript:scrollTo('ERROR0')\">NEXT ERROR</a>";
309 log += "</p>\n" +
310 "<p>Todo: " + todoCount + "</p>\n</div>\n<div id=\"log\">\n";
312 return log + aTests.map(function (f) {
313 return "<p class=\"testHeader\">Running " + f.path + "...</p>\n" + f.htmlLog;
314 }).join("\n") + "</div>";
317 function testsFinished(aTests) {
318 if (gConfig.closeWhenDone) {
319 const {AppConstants} = ChromeUtils.importESModule(
320 "resource://gre/modules/AppConstants.sys.mjs"
322 if (
323 !AppConstants.RELEASE_OR_BETA &&
324 !AppConstants.DEBUG &&
325 !AppConstants.MOZ_CODE_COVERAGE &&
326 !AppConstants.ASAN &&
327 !AppConstants.TSAN
329 let filename =
330 Services.profiler.IsActive() &&
331 Services.env.get("MOZ_PROFILER_SHUTDOWN");
332 if (!filename) {
333 Cu.exitIfInAutomation();
335 Services.profiler
336 .dumpProfileToFileAsync(filename)
337 .then(() => Services.profiler.StopProfiler())
338 .then(() => Cu.exitIfInAutomation());
339 } else {
340 Services.startup.quit(Ci.nsIAppStartup.eForceQuit);
342 return;
345 // Focus our window, to display the results
346 window.focus();
348 // UI
349 // eslint-disable-next-line no-unsanitized/property
350 document.getElementById("results").innerHTML = getHTMLLogFromTests(aTests);
351 setStatus("Done.");
354 function scrollTo(id) {
355 var line = document.getElementById(id);
356 if (!line)
357 return;
359 line.scrollIntoView();
361 ]]></script>
362 <button id="runTestsButton" oncommand="runTests();" label="Run All Tests"/>
363 <label id="status"/>
364 <scrollbox flex="1" style="overflow: auto" align="stretch">
365 <div id="results" xmlns="http://www.w3.org/1999/xhtml" flex="1"/>
366 </scrollbox>
367 </window>