Bug 1735858 [wpt PR 31247] - App history: make it mostly nonfunctional for opaque...
[gecko.git] / testing / mochitest / browser-harness.xhtml
blob177a9b6423356b487e4e6dde0dc094ff5338b977
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/StructuredLog.jsm"/>
15 <script src="chrome://mochikit/content/tests/SimpleTest/TestRunner.js"/>
16 <script src="chrome://mochikit/content/chrome-harness.js"/>
17 <script src="chrome://mochikit/content/manifestLibrary.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: function (str) {
84 this.structuredLogger.info(str);
86 if (this.fileLogger)
87 this.fileLogger.log(str);
90 done: function () {
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.dumper = gDumper;
115 this.results = [];
116 this.scope = null;
117 this.duration = 0;
118 this.unexpectedTimeouts = 0;
119 this.lastOutputTime = 0;
121 browserTest.prototype = {
122 get passCount() {
123 return this.results.filter(t => !t.info && !t.todo && t.pass).length;
125 get todoCount() {
126 return this.results.filter(t => !t.info && t.todo && t.pass).length;
128 get failCount() {
129 return this.results.filter(t => !t.info && !t.pass).length;
131 get allowedFailureCount() {
132 return this.results.filter(t => t.allowedFailure).length;
135 addResult: function addResult(result) {
136 this.lastOutputTime = Date.now();
137 this.results.push(result);
139 if (result.info) {
140 if (result.msg) {
141 ChromeUtils.addProfilerMarker("TEST-INFO", {category: "Test"},
142 result.msg);
143 this.dumper.structuredLogger.info(result.msg);
145 return;
148 this.dumper.structuredLogger.testStatus(this.path,
149 result.name,
150 result.status,
151 result.expected,
152 result.msg);
153 let markerName = "TEST-";
154 if (result.pass) {
155 markerName += result.todo ? "KNOWN-FAIL" : "PASS";
157 else {
158 markerName += "UNEXPECTED-" + result.status;
160 let markerText = result.name;
161 if (result.msg) {
162 markerText += " - " + result.msg;
164 ChromeUtils.addProfilerMarker(markerName, {category: "Test"}, markerText);
167 setDuration: function setDuration(duration) {
168 this.duration = duration;
171 get htmlLog() {
172 let txtToHTML = Cc["@mozilla.org/txttohtmlconv;1"].
173 getService(Ci.mozITXTToHTMLConv);
174 function _entityEncode(str) {
175 return txtToHTML.scanTXT(str, Ci.mozITXTToHTMLConv.kEntities);
177 var path = _entityEncode(this.path);
178 var html = this.results.map(function (t) {
179 var classname = "result ";
180 var result = "TEST-";
181 if (t.info) {
182 classname = "info";
183 result += "INFO";
185 else if (t.pass) {
186 classname += "passed";
187 if (t.todo)
188 result += "KNOWN-FAIL";
189 else
190 result += "PASS";
192 else {
193 classname += "failed";
194 result += "UNEXPECTED-" + t.status;
196 var message = t.name + (t.msg ? " - " + t.msg : "");
197 var text = result + " | " + path + " | " + _entityEncode(message);
198 if (!t.info && !t.pass) {
199 return '<p class="' + classname + '" id=\"ERROR' + (gErrorCount++) + '">' +
200 text + " <a href=\"javascript:scrollTo('ERROR" + gErrorCount + "')\">NEXT ERROR</a></p>";
202 return '<p class="' + classname + '">' + text + "</p>";
203 }).join("\n");
204 if (this.duration) {
205 html += "<p class=\"info\">TEST-END | " + path + " | finished in " +
206 this.duration + " ms</p>";
208 return html;
212 // Returns an array of browserTest objects for all the selected tests
213 function runTests() {
214 gConfig.baseurl = "chrome://mochitests/content";
215 getTestList(gConfig, loadTestList);
218 function loadTestList(links) {
219 if (!links) {
220 createTester({});
221 return;
224 // load server.js in so we can share template functions
225 var scriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"].
226 getService(Ci.mozIJSSubScriptLoader);
227 var srvScope = {};
228 scriptLoader.loadSubScript('chrome://mochikit/content/server.js',
229 srvScope);
231 var fileNames = [];
232 var fileNameRegexp = /browser_.+\.js$/;
233 srvScope.arrayOfTestFiles(links, fileNames, fileNameRegexp);
235 if (gConfig.startAt || gConfig.endAt) {
236 fileNames = skipTests(fileNames, gConfig.startAt, gConfig.endAt);
239 createTester(fileNames.map(function (f) { return new browserTest(f); }));
242 function setStatus(aStatusString) {
243 document.getElementById("status").value = aStatusString;
246 function createTester(links) {
247 var windowMediator = Cc['@mozilla.org/appshell/window-mediator;1'].
248 getService(Ci.nsIWindowMediator);
249 var winType = null;
250 if (gConfig.testRoot == "browser") {
251 const IS_THUNDERBIRD = Services.appinfo.ID == "{3550f703-e582-4d05-9a08-453d09bdfdc6}";
252 winType = IS_THUNDERBIRD ? "mail:3pane" : "navigator:browser";
254 if (!winType) {
255 throw new Error("Unrecognized gConfig.testRoot: " + gConfig.testRoot);
257 var testWin = windowMediator.getMostRecentWindow(winType);
259 setStatus("Running...");
261 // It's possible that the test harness window is not yet focused when this
262 // function runs (in which case testWin is already focused, and focusing it
263 // will be a no-op, and then the test harness window will steal focus later,
264 // which will mess up tests). So wait for the test harness window to be
265 // focused before trying to focus testWin.
266 waitForFocus(() => {
267 // Focus the test window and start tests.
268 waitForFocus(() => {
269 var Tester = new testWin.Tester(links, gDumper.structuredLogger, testsFinished);
270 Tester.start();
271 }, testWin);
272 }, window);
275 function executeSoon(callback) {
276 let tm = Cc["@mozilla.org/thread-manager;1"].getService(Ci.nsIThreadManager);
277 tm.dispatchToMainThread(callback);
280 function waitForFocus(callback, win) {
281 // If "win" is already focused, just call the callback.
282 let fm = Cc["@mozilla.org/focus-manager;1"].getService(Ci.nsIFocusManager);
283 if (fm.focusedWindow == win) {
284 executeSoon(callback);
285 return;
288 // Otherwise focus it, and wait for the focus event.
289 win.addEventListener("focus", function listener() {
290 win.removeEventListener("focus", listener, true);
291 executeSoon(callback);
292 }, true);
293 win.focus();
296 function sum(a, b) {
297 return a + b;
300 function getHTMLLogFromTests(aTests) {
301 if (!aTests.length)
302 return "<div id=\"summary\" class=\"failure\">No tests to run." +
303 " Did you pass an invalid --test-path?</div>";
305 var log = "";
307 var passCount = aTests.map(f => f.passCount).reduce(sum);
308 var failCount = aTests.map(f => f.failCount).reduce(sum);
309 var todoCount = aTests.map(f => f.todoCount).reduce(sum);
310 log += "<div id=\"summary\" class=\"";
311 log += failCount != 0 ? "failure" :
312 passCount == 0 ? "todo" : "success";
313 log += "\">\n<p>Passed: " + passCount + "</p>\n" +
314 "<p>Failed: " + failCount;
315 if (failCount > 0)
316 log += " <a href=\"javascript:scrollTo('ERROR0')\">NEXT ERROR</a>";
317 log += "</p>\n" +
318 "<p>Todo: " + todoCount + "</p>\n</div>\n<div id=\"log\">\n";
320 return log + aTests.map(function (f) {
321 return "<p class=\"testHeader\">Running " + f.path + "...</p>\n" + f.htmlLog;
322 }).join("\n") + "</div>";
325 function testsFinished(aTests) {
326 if (gConfig.closeWhenDone) {
327 const {AppConstants} = ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
328 if (
329 !AppConstants.RELEASE_OR_BETA &&
330 !AppConstants.DEBUG &&
331 !AppConstants.MOZ_CODE_COVERAGE &&
332 !AppConstants.ASAN &&
333 !AppConstants.TSAN
335 Cu.exitIfInAutomation();
336 } else {
337 let appStartup = Cc["@mozilla.org/toolkit/app-startup;1"].getService(Ci.nsIAppStartup);
338 appStartup.quit(Ci.nsIAppStartup.eForceQuit);
340 return;
343 // Focus our window, to display the results
344 window.focus();
346 // UI
347 document.getElementById("results").innerHTML = getHTMLLogFromTests(aTests);
348 setStatus("Done.");
351 function scrollTo(id) {
352 var line = document.getElementById(id);
353 if (!line)
354 return;
356 line.scrollIntoView();
358 ]]></script>
359 <button id="runTestsButton" oncommand="runTests();" label="Run All Tests"/>
360 <label id="status"/>
361 <scrollbox flex="1" style="overflow: auto" align="stretch">
362 <div id="results" xmlns="http://www.w3.org/1999/xhtml" flex="1"/>
363 </scrollbox>
364 </window>