testing/mochitest/browser-test.js

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

michael@0 1 /* -*- js-indent-level: 2; tab-width: 2; indent-tabs-mode: nil -*- */
michael@0 2 // Test timeout (seconds)
michael@0 3 var gTimeoutSeconds = 45;
michael@0 4 var gConfig;
michael@0 5
michael@0 6 if (Cc === undefined) {
michael@0 7 var Cc = Components.classes;
michael@0 8 var Ci = Components.interfaces;
michael@0 9 var Cu = Components.utils;
michael@0 10 }
michael@0 11
michael@0 12 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 13 Cu.import("resource://gre/modules/Task.jsm");
michael@0 14
michael@0 15 XPCOMUtils.defineLazyModuleGetter(this, "Services",
michael@0 16 "resource://gre/modules/Services.jsm");
michael@0 17
michael@0 18 XPCOMUtils.defineLazyModuleGetter(this, "BrowserNewTabPreloader",
michael@0 19 "resource:///modules/BrowserNewTabPreloader.jsm", "BrowserNewTabPreloader");
michael@0 20
michael@0 21 XPCOMUtils.defineLazyModuleGetter(this, "CustomizationTabPreloader",
michael@0 22 "resource:///modules/CustomizationTabPreloader.jsm", "CustomizationTabPreloader");
michael@0 23
michael@0 24 const SIMPLETEST_OVERRIDES =
michael@0 25 ["ok", "is", "isnot", "ise", "todo", "todo_is", "todo_isnot", "info", "expectAssertions"];
michael@0 26
michael@0 27 window.addEventListener("load", testOnLoad, false);
michael@0 28
michael@0 29 function testOnLoad() {
michael@0 30 window.removeEventListener("load", testOnLoad, false);
michael@0 31
michael@0 32 gConfig = readConfig();
michael@0 33 if (gConfig.testRoot == "browser" ||
michael@0 34 gConfig.testRoot == "metro" ||
michael@0 35 gConfig.testRoot == "webapprtChrome") {
michael@0 36 // Make sure to launch the test harness for the first opened window only
michael@0 37 var prefs = Services.prefs;
michael@0 38 if (prefs.prefHasUserValue("testing.browserTestHarness.running"))
michael@0 39 return;
michael@0 40
michael@0 41 prefs.setBoolPref("testing.browserTestHarness.running", true);
michael@0 42
michael@0 43 if (prefs.prefHasUserValue("testing.browserTestHarness.timeout"))
michael@0 44 gTimeoutSeconds = prefs.getIntPref("testing.browserTestHarness.timeout");
michael@0 45
michael@0 46 var sstring = Cc["@mozilla.org/supports-string;1"].
michael@0 47 createInstance(Ci.nsISupportsString);
michael@0 48 sstring.data = location.search;
michael@0 49
michael@0 50 Services.ww.openWindow(window, "chrome://mochikit/content/browser-harness.xul", "browserTest",
michael@0 51 "chrome,centerscreen,dialog=no,resizable,titlebar,toolbar=no,width=800,height=600", sstring);
michael@0 52 } else {
michael@0 53 // This code allows us to redirect without requiring specialpowers for chrome and a11y tests.
michael@0 54 let messageHandler = function(m) {
michael@0 55 messageManager.removeMessageListener("chromeEvent", messageHandler);
michael@0 56 var url = m.json.data;
michael@0 57
michael@0 58 // Window is the [ChromeWindow] for messageManager, so we need content.window
michael@0 59 // Currently chrome tests are run in a content window instead of a ChromeWindow
michael@0 60 var webNav = content.window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
michael@0 61 .getInterface(Components.interfaces.nsIWebNavigation);
michael@0 62 webNav.loadURI(url, null, null, null, null);
michael@0 63 };
michael@0 64
michael@0 65 var listener = 'data:,function doLoad(e) { var data=e.detail&&e.detail.data;removeEventListener("contentEvent", function (e) { doLoad(e); }, false, true);sendAsyncMessage("chromeEvent", {"data":data}); };addEventListener("contentEvent", function (e) { doLoad(e); }, false, true);';
michael@0 66 messageManager.loadFrameScript(listener, true);
michael@0 67 messageManager.addMessageListener("chromeEvent", messageHandler);
michael@0 68 }
michael@0 69 if (gConfig.e10s) {
michael@0 70 e10s_init();
michael@0 71 }
michael@0 72 }
michael@0 73
michael@0 74 function Tester(aTests, aDumper, aCallback) {
michael@0 75 this.dumper = aDumper;
michael@0 76 this.tests = aTests;
michael@0 77 this.callback = aCallback;
michael@0 78 this.openedWindows = {};
michael@0 79 this.openedURLs = {};
michael@0 80
michael@0 81 this._scriptLoader = Services.scriptloader;
michael@0 82 this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/EventUtils.js", this.EventUtils);
michael@0 83 var simpleTestScope = {};
michael@0 84 this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/specialpowersAPI.js", simpleTestScope);
michael@0 85 this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/SpecialPowersObserverAPI.js", simpleTestScope);
michael@0 86 this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/ChromePowers.js", simpleTestScope);
michael@0 87 this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/SimpleTest.js", simpleTestScope);
michael@0 88 this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/MemoryStats.js", simpleTestScope);
michael@0 89 this._scriptLoader.loadSubScript("chrome://mochikit/content/chrome-harness.js", simpleTestScope);
michael@0 90 this.SimpleTest = simpleTestScope.SimpleTest;
michael@0 91 this.MemoryStats = simpleTestScope.MemoryStats;
michael@0 92 this.Task = Task;
michael@0 93 this.Promise = Components.utils.import("resource://gre/modules/Promise.jsm", null).Promise;
michael@0 94 this.Assert = Components.utils.import("resource://testing-common/Assert.jsm", null).Assert;
michael@0 95
michael@0 96 this.SimpleTestOriginal = {};
michael@0 97 SIMPLETEST_OVERRIDES.forEach(m => {
michael@0 98 this.SimpleTestOriginal[m] = this.SimpleTest[m];
michael@0 99 });
michael@0 100
michael@0 101 this._uncaughtErrorObserver = function({message, date, fileName, stack, lineNumber}) {
michael@0 102 let text = "Once bug 991040 has landed, THIS ERROR WILL CAUSE A TEST FAILURE.\n" + message;
michael@0 103 let error = text;
michael@0 104 if (fileName || lineNumber) {
michael@0 105 error = {
michael@0 106 fileName: fileName,
michael@0 107 lineNumber: lineNumber,
michael@0 108 message: text,
michael@0 109 toString: function() {
michael@0 110 return text;
michael@0 111 }
michael@0 112 };
michael@0 113 }
michael@0 114 this.currentTest.addResult(
michael@0 115 new testResult(
michael@0 116 /*success*/ true,
michael@0 117 /*name*/"A promise chain failed to handle a rejection",
michael@0 118 /*error*/error,
michael@0 119 /*known*/true,
michael@0 120 /*stack*/stack));
michael@0 121 }.bind(this);
michael@0 122 }
michael@0 123 Tester.prototype = {
michael@0 124 EventUtils: {},
michael@0 125 SimpleTest: {},
michael@0 126 Task: null,
michael@0 127 Promise: null,
michael@0 128 Assert: null,
michael@0 129
michael@0 130 repeat: 0,
michael@0 131 runUntilFailure: false,
michael@0 132 checker: null,
michael@0 133 currentTestIndex: -1,
michael@0 134 lastStartTime: null,
michael@0 135 openedWindows: null,
michael@0 136 lastAssertionCount: 0,
michael@0 137
michael@0 138 get currentTest() {
michael@0 139 return this.tests[this.currentTestIndex];
michael@0 140 },
michael@0 141 get done() {
michael@0 142 return this.currentTestIndex == this.tests.length - 1;
michael@0 143 },
michael@0 144
michael@0 145 start: function Tester_start() {
michael@0 146 // Check whether this window is ready to run tests.
michael@0 147 if (window.BrowserChromeTest) {
michael@0 148 BrowserChromeTest.runWhenReady(this.actuallyStart.bind(this));
michael@0 149 return;
michael@0 150 }
michael@0 151 this.actuallyStart();
michael@0 152 },
michael@0 153
michael@0 154 actuallyStart: function Tester_actuallyStart() {
michael@0 155 //if testOnLoad was not called, then gConfig is not defined
michael@0 156 if (!gConfig)
michael@0 157 gConfig = readConfig();
michael@0 158
michael@0 159 if (gConfig.runUntilFailure)
michael@0 160 this.runUntilFailure = true;
michael@0 161
michael@0 162 if (gConfig.repeat)
michael@0 163 this.repeat = gConfig.repeat;
michael@0 164
michael@0 165 this.dumper.dump("*** Start BrowserChrome Test Results ***\n");
michael@0 166 Services.console.registerListener(this);
michael@0 167 Services.obs.addObserver(this, "chrome-document-global-created", false);
michael@0 168 Services.obs.addObserver(this, "content-document-global-created", false);
michael@0 169 this._globalProperties = Object.keys(window);
michael@0 170 this._globalPropertyWhitelist = [
michael@0 171 "navigator", "constructor", "top",
michael@0 172 "Application",
michael@0 173 "__SS_tabsToRestore", "__SSi",
michael@0 174 "webConsoleCommandController",
michael@0 175 ];
michael@0 176
michael@0 177 this.Promise.Debugging.clearUncaughtErrorObservers();
michael@0 178 this.Promise.Debugging.addUncaughtErrorObserver(this._uncaughtErrorObserver);
michael@0 179
michael@0 180 if (this.tests.length)
michael@0 181 this.nextTest();
michael@0 182 else
michael@0 183 this.finish();
michael@0 184 },
michael@0 185
michael@0 186 waitForWindowsState: function Tester_waitForWindowsState(aCallback) {
michael@0 187 let timedOut = this.currentTest && this.currentTest.timedOut;
michael@0 188 let baseMsg = timedOut ? "Found a {elt} after previous test timed out"
michael@0 189 : this.currentTest ? "Found an unexpected {elt} at the end of test run"
michael@0 190 : "Found an unexpected {elt}";
michael@0 191
michael@0 192 // Remove stale tabs
michael@0 193 if (this.currentTest && window.gBrowser && gBrowser.tabs.length > 1) {
michael@0 194 while (gBrowser.tabs.length > 1) {
michael@0 195 let lastTab = gBrowser.tabContainer.lastChild;
michael@0 196 let msg = baseMsg.replace("{elt}", "tab") +
michael@0 197 ": " + lastTab.linkedBrowser.currentURI.spec;
michael@0 198 this.currentTest.addResult(new testResult(false, msg, "", false));
michael@0 199 gBrowser.removeTab(lastTab);
michael@0 200 }
michael@0 201 }
michael@0 202
michael@0 203 // Replace the last tab with a fresh one
michael@0 204 if (window.gBrowser) {
michael@0 205 gBrowser.addTab("about:blank", { skipAnimation: true });
michael@0 206 gBrowser.removeCurrentTab();
michael@0 207 gBrowser.stop();
michael@0 208 }
michael@0 209
michael@0 210 // Remove stale windows
michael@0 211 this.dumper.dump("TEST-INFO | checking window state\n");
michael@0 212 let windowsEnum = Services.wm.getEnumerator(null);
michael@0 213 while (windowsEnum.hasMoreElements()) {
michael@0 214 let win = windowsEnum.getNext();
michael@0 215 if (win != window && !win.closed &&
michael@0 216 win.document.documentElement.getAttribute("id") != "browserTestHarness") {
michael@0 217 let type = win.document.documentElement.getAttribute("windowtype");
michael@0 218 switch (type) {
michael@0 219 case "navigator:browser":
michael@0 220 type = "browser window";
michael@0 221 break;
michael@0 222 case null:
michael@0 223 type = "unknown window";
michael@0 224 break;
michael@0 225 }
michael@0 226 let msg = baseMsg.replace("{elt}", type);
michael@0 227 if (this.currentTest)
michael@0 228 this.currentTest.addResult(new testResult(false, msg, "", false));
michael@0 229 else
michael@0 230 this.dumper.dump("TEST-UNEXPECTED-FAIL | (browser-test.js) | " + msg + "\n");
michael@0 231
michael@0 232 win.close();
michael@0 233 }
michael@0 234 }
michael@0 235
michael@0 236 // Make sure the window is raised before each test.
michael@0 237 this.SimpleTest.waitForFocus(aCallback);
michael@0 238 },
michael@0 239
michael@0 240 finish: function Tester_finish(aSkipSummary) {
michael@0 241 this.Promise.Debugging.flushUncaughtErrors();
michael@0 242
michael@0 243 var passCount = this.tests.reduce(function(a, f) a + f.passCount, 0);
michael@0 244 var failCount = this.tests.reduce(function(a, f) a + f.failCount, 0);
michael@0 245 var todoCount = this.tests.reduce(function(a, f) a + f.todoCount, 0);
michael@0 246
michael@0 247 if (this.repeat > 0) {
michael@0 248 --this.repeat;
michael@0 249 this.currentTestIndex = -1;
michael@0 250 this.nextTest();
michael@0 251 }
michael@0 252 else{
michael@0 253 Services.console.unregisterListener(this);
michael@0 254 Services.obs.removeObserver(this, "chrome-document-global-created");
michael@0 255 Services.obs.removeObserver(this, "content-document-global-created");
michael@0 256 this.Promise.Debugging.clearUncaughtErrorObservers();
michael@0 257 this.dumper.dump("\nINFO TEST-START | Shutdown\n");
michael@0 258
michael@0 259 if (this.tests.length) {
michael@0 260 this.dumper.dump("Browser Chrome Test Summary\n");
michael@0 261
michael@0 262 this.dumper.dump("\tPassed: " + passCount + "\n" +
michael@0 263 "\tFailed: " + failCount + "\n" +
michael@0 264 "\tTodo: " + todoCount + "\n");
michael@0 265 } else {
michael@0 266 this.dumper.dump("TEST-UNEXPECTED-FAIL | (browser-test.js) | " +
michael@0 267 "No tests to run. Did you pass an invalid --test-path?\n");
michael@0 268 }
michael@0 269 this.dumper.dump("\n*** End BrowserChrome Test Results ***\n");
michael@0 270
michael@0 271 this.dumper.done();
michael@0 272
michael@0 273 // Tests complete, notify the callback and return
michael@0 274 this.callback(this.tests);
michael@0 275 this.callback = null;
michael@0 276 this.tests = null;
michael@0 277 this.openedWindows = null;
michael@0 278 }
michael@0 279 },
michael@0 280
michael@0 281 haltTests: function Tester_haltTests() {
michael@0 282 // Do not run any further tests
michael@0 283 this.currentTestIndex = this.tests.length - 1;
michael@0 284 this.repeat = 0;
michael@0 285 },
michael@0 286
michael@0 287 observe: function Tester_observe(aSubject, aTopic, aData) {
michael@0 288 if (!aTopic) {
michael@0 289 this.onConsoleMessage(aSubject);
michael@0 290 } else if (this.currentTest) {
michael@0 291 this.onDocumentCreated(aSubject);
michael@0 292 }
michael@0 293 },
michael@0 294
michael@0 295 onDocumentCreated: function Tester_onDocumentCreated(aWindow) {
michael@0 296 let utils = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 297 .getInterface(Ci.nsIDOMWindowUtils);
michael@0 298 let outerID = utils.outerWindowID;
michael@0 299 let innerID = utils.currentInnerWindowID;
michael@0 300
michael@0 301 if (!(outerID in this.openedWindows)) {
michael@0 302 this.openedWindows[outerID] = this.currentTest;
michael@0 303 }
michael@0 304 this.openedWindows[innerID] = this.currentTest;
michael@0 305
michael@0 306 let url = aWindow.location.href || "about:blank";
michael@0 307 this.openedURLs[outerID] = this.openedURLs[innerID] = url;
michael@0 308 },
michael@0 309
michael@0 310 onConsoleMessage: function Tester_onConsoleMessage(aConsoleMessage) {
michael@0 311 // Ignore empty messages.
michael@0 312 if (!aConsoleMessage.message)
michael@0 313 return;
michael@0 314
michael@0 315 try {
michael@0 316 var msg = "Console message: " + aConsoleMessage.message;
michael@0 317 if (this.currentTest)
michael@0 318 this.currentTest.addResult(new testMessage(msg));
michael@0 319 else
michael@0 320 this.dumper.dump("TEST-INFO | (browser-test.js) | " + msg.replace(/\n$/, "") + "\n");
michael@0 321 } catch (ex) {
michael@0 322 // Swallow exception so we don't lead to another error being reported,
michael@0 323 // throwing us into an infinite loop
michael@0 324 }
michael@0 325 },
michael@0 326
michael@0 327 nextTest: Task.async(function*() {
michael@0 328 if (this.currentTest) {
michael@0 329 // Run cleanup functions for the current test before moving on to the
michael@0 330 // next one.
michael@0 331 let testScope = this.currentTest.scope;
michael@0 332 while (testScope.__cleanupFunctions.length > 0) {
michael@0 333 let func = testScope.__cleanupFunctions.shift();
michael@0 334 try {
michael@0 335 yield func.apply(testScope);
michael@0 336 }
michael@0 337 catch (ex) {
michael@0 338 this.currentTest.addResult(new testResult(false, "Cleanup function threw an exception", ex, false));
michael@0 339 }
michael@0 340 };
michael@0 341
michael@0 342 this.Promise.Debugging.flushUncaughtErrors();
michael@0 343
michael@0 344 let winUtils = window.QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 345 .getInterface(Ci.nsIDOMWindowUtils);
michael@0 346 if (winUtils.isTestControllingRefreshes) {
michael@0 347 this.currentTest.addResult(new testResult(false, "test left refresh driver under test control", "", false));
michael@0 348 winUtils.restoreNormalRefresh();
michael@0 349 }
michael@0 350
michael@0 351 if (this.SimpleTest.isExpectingUncaughtException()) {
michael@0 352 this.currentTest.addResult(new testResult(false, "expectUncaughtException was called but no uncaught exception was detected!", "", false));
michael@0 353 }
michael@0 354
michael@0 355 Object.keys(window).forEach(function (prop) {
michael@0 356 if (parseInt(prop) == prop) {
michael@0 357 // This is a string which when parsed as an integer and then
michael@0 358 // stringified gives the original string. As in, this is in fact a
michael@0 359 // string representation of an integer, so an index into
michael@0 360 // window.frames. Skip those.
michael@0 361 return;
michael@0 362 }
michael@0 363 if (this._globalProperties.indexOf(prop) == -1) {
michael@0 364 this._globalProperties.push(prop);
michael@0 365 if (this._globalPropertyWhitelist.indexOf(prop) == -1)
michael@0 366 this.currentTest.addResult(new testResult(false, "leaked window property: " + prop, "", false));
michael@0 367 }
michael@0 368 }, this);
michael@0 369
michael@0 370 // Clear document.popupNode. The test could have set it to a custom value
michael@0 371 // for its own purposes, nulling it out it will go back to the default
michael@0 372 // behavior of returning the last opened popup.
michael@0 373 document.popupNode = null;
michael@0 374
michael@0 375 // Notify a long running test problem if it didn't end up in a timeout.
michael@0 376 if (this.currentTest.unexpectedTimeouts && !this.currentTest.timedOut) {
michael@0 377 let msg = "This test exceeded the timeout threshold. It should be " +
michael@0 378 "rewritten or split up. If that's not possible, use " +
michael@0 379 "requestLongerTimeout(N), but only as a last resort.";
michael@0 380 this.currentTest.addResult(new testResult(false, msg, "", false));
michael@0 381 }
michael@0 382
michael@0 383 // If we're in a debug build, check assertion counts. This code
michael@0 384 // is similar to the code in TestRunner.testUnloaded in
michael@0 385 // TestRunner.js used for all other types of mochitests.
michael@0 386 let debugsvc = Cc["@mozilla.org/xpcom/debug;1"].getService(Ci.nsIDebug2);
michael@0 387 if (debugsvc.isDebugBuild) {
michael@0 388 let newAssertionCount = debugsvc.assertionCount;
michael@0 389 let numAsserts = newAssertionCount - this.lastAssertionCount;
michael@0 390 this.lastAssertionCount = newAssertionCount;
michael@0 391
michael@0 392 let max = testScope.__expectedMaxAsserts;
michael@0 393 let min = testScope.__expectedMinAsserts;
michael@0 394 if (numAsserts > max) {
michael@0 395 let msg = "Assertion count " + numAsserts +
michael@0 396 " is greater than expected range " +
michael@0 397 min + "-" + max + " assertions.";
michael@0 398 // TEST-UNEXPECTED-FAIL (TEMPORARILY TEST-KNOWN-FAIL)
michael@0 399 //this.currentTest.addResult(new testResult(false, msg, "", false));
michael@0 400 this.currentTest.addResult(new testResult(true, msg, "", true));
michael@0 401 } else if (numAsserts < min) {
michael@0 402 let msg = "Assertion count " + numAsserts +
michael@0 403 " is less than expected range " +
michael@0 404 min + "-" + max + " assertions.";
michael@0 405 // TEST-UNEXPECTED-PASS
michael@0 406 this.currentTest.addResult(new testResult(false, msg, "", true));
michael@0 407 } else if (numAsserts > 0) {
michael@0 408 let msg = "Assertion count " + numAsserts +
michael@0 409 " is within expected range " +
michael@0 410 min + "-" + max + " assertions.";
michael@0 411 // TEST-KNOWN-FAIL
michael@0 412 this.currentTest.addResult(new testResult(true, msg, "", true));
michael@0 413 }
michael@0 414 }
michael@0 415
michael@0 416 // Dump memory stats for main thread.
michael@0 417 if (Cc["@mozilla.org/xre/runtime;1"]
michael@0 418 .getService(Ci.nsIXULRuntime)
michael@0 419 .processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT)
michael@0 420 {
michael@0 421 this.MemoryStats.dump((l) => { this.dumper.dump(l + "\n"); },
michael@0 422 this.currentTestIndex,
michael@0 423 this.currentTest.path,
michael@0 424 gConfig.dumpOutputDirectory,
michael@0 425 gConfig.dumpAboutMemoryAfterTest,
michael@0 426 gConfig.dumpDMDAfterTest);
michael@0 427 }
michael@0 428
michael@0 429 // Note the test run time
michael@0 430 let time = Date.now() - this.lastStartTime;
michael@0 431 this.dumper.dump("INFO TEST-END | " + this.currentTest.path + " | finished in " + time + "ms\n");
michael@0 432 this.currentTest.setDuration(time);
michael@0 433
michael@0 434 if (this.runUntilFailure && this.currentTest.failCount > 0) {
michael@0 435 this.haltTests();
michael@0 436 }
michael@0 437
michael@0 438 // Restore original SimpleTest methods to avoid leaks.
michael@0 439 SIMPLETEST_OVERRIDES.forEach(m => {
michael@0 440 this.SimpleTest[m] = this.SimpleTestOriginal[m];
michael@0 441 });
michael@0 442
michael@0 443 testScope.destroy();
michael@0 444 this.currentTest.scope = null;
michael@0 445 }
michael@0 446
michael@0 447 // Check the window state for the current test before moving to the next one.
michael@0 448 // This also causes us to check before starting any tests, since nextTest()
michael@0 449 // is invoked to start the tests.
michael@0 450 this.waitForWindowsState((function () {
michael@0 451 if (this.done) {
michael@0 452 // Uninitialize a few things explicitly so that they can clean up
michael@0 453 // frames and browser intentionally kept alive until shutdown to
michael@0 454 // eliminate false positives.
michael@0 455 if (gConfig.testRoot == "browser") {
michael@0 456 // Replace the document currently loaded in the browser's sidebar.
michael@0 457 // This will prevent false positives for tests that were the last
michael@0 458 // to touch the sidebar. They will thus not be blamed for leaking
michael@0 459 // a document.
michael@0 460 let sidebar = document.getElementById("sidebar");
michael@0 461 sidebar.setAttribute("src", "data:text/html;charset=utf-8,");
michael@0 462 sidebar.docShell.createAboutBlankContentViewer(null);
michael@0 463 sidebar.setAttribute("src", "about:blank");
michael@0 464
michael@0 465 // Do the same for the social sidebar.
michael@0 466 let socialSidebar = document.getElementById("social-sidebar-browser");
michael@0 467 socialSidebar.setAttribute("src", "data:text/html;charset=utf-8,");
michael@0 468 socialSidebar.docShell.createAboutBlankContentViewer(null);
michael@0 469 socialSidebar.setAttribute("src", "about:blank");
michael@0 470
michael@0 471 // Destroy BackgroundPageThumbs resources.
michael@0 472 let {BackgroundPageThumbs} =
michael@0 473 Cu.import("resource://gre/modules/BackgroundPageThumbs.jsm", {});
michael@0 474 BackgroundPageThumbs._destroy();
michael@0 475
michael@0 476 BrowserNewTabPreloader.uninit();
michael@0 477 CustomizationTabPreloader.uninit();
michael@0 478 SocialFlyout.unload();
michael@0 479 SocialShare.uninit();
michael@0 480 TabView.uninit();
michael@0 481 }
michael@0 482
michael@0 483 // Simulate memory pressure so that we're forced to free more resources
michael@0 484 // and thus get rid of more false leaks like already terminated workers.
michael@0 485 Services.obs.notifyObservers(null, "memory-pressure", "heap-minimize");
michael@0 486
michael@0 487 // Schedule GC and CC runs before finishing in order to detect
michael@0 488 // DOM windows leaked by our tests or the tested code. Note that we
michael@0 489 // use a shrinking GC so that the JS engine will discard JIT code and
michael@0 490 // JIT caches more aggressively.
michael@0 491
michael@0 492 let checkForLeakedGlobalWindows = aCallback => {
michael@0 493 Cu.schedulePreciseShrinkingGC(() => {
michael@0 494 let analyzer = new CCAnalyzer();
michael@0 495 analyzer.run(() => {
michael@0 496 let results = [];
michael@0 497 for (let obj of analyzer.find("nsGlobalWindow ")) {
michael@0 498 let m = obj.name.match(/^nsGlobalWindow #(\d+)/);
michael@0 499 if (m && m[1] in this.openedWindows)
michael@0 500 results.push({ name: obj.name, url: m[1] });
michael@0 501 }
michael@0 502 aCallback(results);
michael@0 503 });
michael@0 504 });
michael@0 505 };
michael@0 506
michael@0 507 let reportLeaks = aResults => {
michael@0 508 for (let result of aResults) {
michael@0 509 let test = this.openedWindows[result.url];
michael@0 510 let msg = "leaked until shutdown [" + result.name +
michael@0 511 " " + (this.openedURLs[result.url] || "NULL") + "]";
michael@0 512 test.addResult(new testResult(false, msg, "", false));
michael@0 513 }
michael@0 514 };
michael@0 515
michael@0 516 checkForLeakedGlobalWindows(aResults => {
michael@0 517 if (aResults.length == 0) {
michael@0 518 this.finish();
michael@0 519 return;
michael@0 520 }
michael@0 521 // After the first check, if there are reported leaked windows, sleep
michael@0 522 // for a while, to allow off-main-thread work to complete and free up
michael@0 523 // main-thread objects. Then check again.
michael@0 524 setTimeout(() => {
michael@0 525 checkForLeakedGlobalWindows(aResults => {
michael@0 526 reportLeaks(aResults);
michael@0 527 this.finish();
michael@0 528 });
michael@0 529 }, 1000);
michael@0 530 });
michael@0 531
michael@0 532 return;
michael@0 533 }
michael@0 534
michael@0 535 this.currentTestIndex++;
michael@0 536 this.execTest();
michael@0 537 }).bind(this));
michael@0 538 }),
michael@0 539
michael@0 540 execTest: function Tester_execTest() {
michael@0 541 this.dumper.dump("TEST-START | " + this.currentTest.path + "\n");
michael@0 542
michael@0 543 this.SimpleTest.reset();
michael@0 544
michael@0 545 // Load the tests into a testscope
michael@0 546 let currentScope = this.currentTest.scope = new testScope(this, this.currentTest);
michael@0 547 let currentTest = this.currentTest;
michael@0 548
michael@0 549 // Import utils in the test scope.
michael@0 550 this.currentTest.scope.EventUtils = this.EventUtils;
michael@0 551 this.currentTest.scope.SimpleTest = this.SimpleTest;
michael@0 552 this.currentTest.scope.gTestPath = this.currentTest.path;
michael@0 553 this.currentTest.scope.Task = this.Task;
michael@0 554 this.currentTest.scope.Promise = this.Promise;
michael@0 555 // Pass a custom report function for mochitest style reporting.
michael@0 556 this.currentTest.scope.Assert = new this.Assert(function(err, message, stack) {
michael@0 557 let res;
michael@0 558 if (err) {
michael@0 559 res = new testResult(false, err.message, err.stack, false, err.stack);
michael@0 560 } else {
michael@0 561 res = new testResult(true, message, "", false, stack);
michael@0 562 }
michael@0 563 currentTest.addResult(res);
michael@0 564 });
michael@0 565
michael@0 566 // Allow Assert.jsm methods to be tacked to the current scope.
michael@0 567 this.currentTest.scope.export_assertions = function() {
michael@0 568 for (let func in this.Assert) {
michael@0 569 this[func] = this.Assert[func].bind(this.Assert);
michael@0 570 }
michael@0 571 };
michael@0 572
michael@0 573 // Override SimpleTest methods with ours.
michael@0 574 SIMPLETEST_OVERRIDES.forEach(function(m) {
michael@0 575 this.SimpleTest[m] = this[m];
michael@0 576 }, this.currentTest.scope);
michael@0 577
michael@0 578 //load the tools to work with chrome .jar and remote
michael@0 579 try {
michael@0 580 this._scriptLoader.loadSubScript("chrome://mochikit/content/chrome-harness.js", this.currentTest.scope);
michael@0 581 } catch (ex) { /* no chrome-harness tools */ }
michael@0 582
michael@0 583 // Import head.js script if it exists.
michael@0 584 var currentTestDirPath =
michael@0 585 this.currentTest.path.substr(0, this.currentTest.path.lastIndexOf("/"));
michael@0 586 var headPath = currentTestDirPath + "/head.js";
michael@0 587 try {
michael@0 588 this._scriptLoader.loadSubScript(headPath, this.currentTest.scope);
michael@0 589 } catch (ex) {
michael@0 590 // Ignore if no head.js exists, but report all other errors. Note this
michael@0 591 // will also ignore an existing head.js attempting to import a missing
michael@0 592 // module - see bug 755558 for why this strategy is preferred anyway.
michael@0 593 if (ex.toString() != 'Error opening input stream (invalid filename?)') {
michael@0 594 this.currentTest.addResult(new testResult(false, "head.js import threw an exception", ex, false));
michael@0 595 }
michael@0 596 }
michael@0 597
michael@0 598 // Import the test script.
michael@0 599 try {
michael@0 600 this._scriptLoader.loadSubScript(this.currentTest.path,
michael@0 601 this.currentTest.scope);
michael@0 602 this.Promise.Debugging.flushUncaughtErrors();
michael@0 603 // Run the test
michael@0 604 this.lastStartTime = Date.now();
michael@0 605 if (this.currentTest.scope.__tasks) {
michael@0 606 // This test consists of tasks, added via the `add_task()` API.
michael@0 607 if ("test" in this.currentTest.scope) {
michael@0 608 throw "Cannot run both a add_task test and a normal test at the same time.";
michael@0 609 }
michael@0 610 this.Task.spawn(function() {
michael@0 611 let task;
michael@0 612 while ((task = this.__tasks.shift())) {
michael@0 613 this.SimpleTest.info("Entering test " + task.name);
michael@0 614 try {
michael@0 615 yield task();
michael@0 616 } catch (ex) {
michael@0 617 let isExpected = !!this.SimpleTest.isExpectingUncaughtException();
michael@0 618 let stack = (typeof ex == "object" && "stack" in ex)?ex.stack:null;
michael@0 619 let name = "Uncaught exception";
michael@0 620 let result = new testResult(isExpected, name, ex, false, stack);
michael@0 621 currentTest.addResult(result);
michael@0 622 }
michael@0 623 this.Promise.Debugging.flushUncaughtErrors();
michael@0 624 this.SimpleTest.info("Leaving test " + task.name);
michael@0 625 }
michael@0 626 this.finish();
michael@0 627 }.bind(currentScope));
michael@0 628 } else if ("generatorTest" in this.currentTest.scope) {
michael@0 629 if ("test" in this.currentTest.scope) {
michael@0 630 throw "Cannot run both a generator test and a normal test at the same time.";
michael@0 631 }
michael@0 632
michael@0 633 // This test is a generator. It will not finish immediately.
michael@0 634 this.currentTest.scope.waitForExplicitFinish();
michael@0 635 var result = this.currentTest.scope.generatorTest();
michael@0 636 this.currentTest.scope.__generator = result;
michael@0 637 result.next();
michael@0 638 } else {
michael@0 639 this.currentTest.scope.test();
michael@0 640 }
michael@0 641 } catch (ex) {
michael@0 642 let isExpected = !!this.SimpleTest.isExpectingUncaughtException();
michael@0 643 if (!this.SimpleTest.isIgnoringAllUncaughtExceptions()) {
michael@0 644 this.currentTest.addResult(new testResult(isExpected, "Exception thrown", ex, false));
michael@0 645 this.SimpleTest.expectUncaughtException(false);
michael@0 646 } else {
michael@0 647 this.currentTest.addResult(new testMessage("Exception thrown: " + ex));
michael@0 648 }
michael@0 649 this.currentTest.scope.finish();
michael@0 650 }
michael@0 651
michael@0 652 // If the test ran synchronously, move to the next test, otherwise the test
michael@0 653 // will trigger the next test when it is done.
michael@0 654 if (this.currentTest.scope.__done) {
michael@0 655 this.nextTest();
michael@0 656 }
michael@0 657 else {
michael@0 658 var self = this;
michael@0 659 this.currentTest.scope.__waitTimer = setTimeout(function timeoutFn() {
michael@0 660 if (--self.currentTest.scope.__timeoutFactor > 0) {
michael@0 661 // We were asked to wait a bit longer.
michael@0 662 self.currentTest.scope.info(
michael@0 663 "Longer timeout required, waiting longer... Remaining timeouts: " +
michael@0 664 self.currentTest.scope.__timeoutFactor);
michael@0 665 self.currentTest.scope.__waitTimer =
michael@0 666 setTimeout(timeoutFn, gTimeoutSeconds * 1000);
michael@0 667 return;
michael@0 668 }
michael@0 669
michael@0 670 // If the test is taking longer than expected, but it's not hanging,
michael@0 671 // mark the fact, but let the test continue. At the end of the test,
michael@0 672 // if it didn't timeout, we will notify the problem through an error.
michael@0 673 // To figure whether it's an actual hang, compare the time of the last
michael@0 674 // result or message to half of the timeout time.
michael@0 675 // Though, to protect against infinite loops, limit the number of times
michael@0 676 // we allow the test to proceed.
michael@0 677 const MAX_UNEXPECTED_TIMEOUTS = 10;
michael@0 678 if (Date.now() - self.currentTest.lastOutputTime < (gTimeoutSeconds / 2) * 1000 &&
michael@0 679 ++self.currentTest.unexpectedTimeouts <= MAX_UNEXPECTED_TIMEOUTS) {
michael@0 680 self.currentTest.scope.__waitTimer =
michael@0 681 setTimeout(timeoutFn, gTimeoutSeconds * 1000);
michael@0 682 return;
michael@0 683 }
michael@0 684
michael@0 685 self.currentTest.addResult(new testResult(false, "Test timed out", "", false));
michael@0 686 self.currentTest.timedOut = true;
michael@0 687 self.currentTest.scope.__waitTimer = null;
michael@0 688 self.nextTest();
michael@0 689 }, gTimeoutSeconds * 1000);
michael@0 690 }
michael@0 691 },
michael@0 692
michael@0 693 QueryInterface: function(aIID) {
michael@0 694 if (aIID.equals(Ci.nsIConsoleListener) ||
michael@0 695 aIID.equals(Ci.nsISupports))
michael@0 696 return this;
michael@0 697
michael@0 698 throw Components.results.NS_ERROR_NO_INTERFACE;
michael@0 699 }
michael@0 700 };
michael@0 701
michael@0 702 function testResult(aCondition, aName, aDiag, aIsTodo, aStack) {
michael@0 703 this.msg = aName || "";
michael@0 704
michael@0 705 this.info = false;
michael@0 706 this.pass = !!aCondition;
michael@0 707 this.todo = aIsTodo;
michael@0 708
michael@0 709 if (this.pass) {
michael@0 710 if (aIsTodo)
michael@0 711 this.result = "TEST-KNOWN-FAIL";
michael@0 712 else
michael@0 713 this.result = "TEST-PASS";
michael@0 714 } else {
michael@0 715 if (aDiag) {
michael@0 716 if (typeof aDiag == "object" && "fileName" in aDiag) {
michael@0 717 // we have an exception - print filename and linenumber information
michael@0 718 this.msg += " at " + aDiag.fileName + ":" + aDiag.lineNumber;
michael@0 719 }
michael@0 720 this.msg += " - " + aDiag;
michael@0 721 }
michael@0 722 if (aStack) {
michael@0 723 this.msg += "\nStack trace:\n";
michael@0 724 var frame = aStack;
michael@0 725 while (frame) {
michael@0 726 this.msg += " " + frame + "\n";
michael@0 727 frame = frame.caller;
michael@0 728 }
michael@0 729 }
michael@0 730 if (aIsTodo)
michael@0 731 this.result = "TEST-UNEXPECTED-PASS";
michael@0 732 else
michael@0 733 this.result = "TEST-UNEXPECTED-FAIL";
michael@0 734
michael@0 735 if (gConfig.debugOnFailure) {
michael@0 736 // You've hit this line because you requested to break into the
michael@0 737 // debugger upon a testcase failure on your test run.
michael@0 738 debugger;
michael@0 739 }
michael@0 740 }
michael@0 741 }
michael@0 742
michael@0 743 function testMessage(aName) {
michael@0 744 this.msg = aName || "";
michael@0 745 this.info = true;
michael@0 746 this.result = "TEST-INFO";
michael@0 747 }
michael@0 748
michael@0 749 // Need to be careful adding properties to this object, since its properties
michael@0 750 // cannot conflict with global variables used in tests.
michael@0 751 function testScope(aTester, aTest) {
michael@0 752 this.__tester = aTester;
michael@0 753
michael@0 754 var self = this;
michael@0 755 this.ok = function test_ok(condition, name, diag, stack) {
michael@0 756 aTest.addResult(new testResult(condition, name, diag, false,
michael@0 757 stack ? stack : Components.stack.caller));
michael@0 758 };
michael@0 759 this.is = function test_is(a, b, name) {
michael@0 760 self.ok(a == b, name, "Got " + a + ", expected " + b, false,
michael@0 761 Components.stack.caller);
michael@0 762 };
michael@0 763 this.isnot = function test_isnot(a, b, name) {
michael@0 764 self.ok(a != b, name, "Didn't expect " + a + ", but got it", false,
michael@0 765 Components.stack.caller);
michael@0 766 };
michael@0 767 this.ise = function test_ise(a, b, name) {
michael@0 768 self.ok(a === b, name, "Got " + a + ", strictly expected " + b, false,
michael@0 769 Components.stack.caller);
michael@0 770 };
michael@0 771 this.todo = function test_todo(condition, name, diag, stack) {
michael@0 772 aTest.addResult(new testResult(!condition, name, diag, true,
michael@0 773 stack ? stack : Components.stack.caller));
michael@0 774 };
michael@0 775 this.todo_is = function test_todo_is(a, b, name) {
michael@0 776 self.todo(a == b, name, "Got " + a + ", expected " + b,
michael@0 777 Components.stack.caller);
michael@0 778 };
michael@0 779 this.todo_isnot = function test_todo_isnot(a, b, name) {
michael@0 780 self.todo(a != b, name, "Didn't expect " + a + ", but got it",
michael@0 781 Components.stack.caller);
michael@0 782 };
michael@0 783 this.info = function test_info(name) {
michael@0 784 aTest.addResult(new testMessage(name));
michael@0 785 };
michael@0 786
michael@0 787 this.executeSoon = function test_executeSoon(func) {
michael@0 788 Services.tm.mainThread.dispatch({
michael@0 789 run: function() {
michael@0 790 func();
michael@0 791 }
michael@0 792 }, Ci.nsIThread.DISPATCH_NORMAL);
michael@0 793 };
michael@0 794
michael@0 795 this.nextStep = function test_nextStep(arg) {
michael@0 796 if (self.__done) {
michael@0 797 aTest.addResult(new testResult(false, "nextStep was called too many times", "", false));
michael@0 798 return;
michael@0 799 }
michael@0 800
michael@0 801 if (!self.__generator) {
michael@0 802 aTest.addResult(new testResult(false, "nextStep called with no generator", "", false));
michael@0 803 self.finish();
michael@0 804 return;
michael@0 805 }
michael@0 806
michael@0 807 try {
michael@0 808 self.__generator.send(arg);
michael@0 809 } catch (ex if ex instanceof StopIteration) {
michael@0 810 // StopIteration means test is finished.
michael@0 811 self.finish();
michael@0 812 } catch (ex) {
michael@0 813 var isExpected = !!self.SimpleTest.isExpectingUncaughtException();
michael@0 814 if (!self.SimpleTest.isIgnoringAllUncaughtExceptions()) {
michael@0 815 aTest.addResult(new testResult(isExpected, "Exception thrown", ex, false));
michael@0 816 self.SimpleTest.expectUncaughtException(false);
michael@0 817 } else {
michael@0 818 aTest.addResult(new testMessage("Exception thrown: " + ex));
michael@0 819 }
michael@0 820 self.finish();
michael@0 821 }
michael@0 822 };
michael@0 823
michael@0 824 this.waitForExplicitFinish = function test_waitForExplicitFinish() {
michael@0 825 self.__done = false;
michael@0 826 };
michael@0 827
michael@0 828 this.waitForFocus = function test_waitForFocus(callback, targetWindow, expectBlankPage) {
michael@0 829 self.SimpleTest.waitForFocus(callback, targetWindow, expectBlankPage);
michael@0 830 };
michael@0 831
michael@0 832 this.waitForClipboard = function test_waitForClipboard(expected, setup, success, failure, flavor) {
michael@0 833 self.SimpleTest.waitForClipboard(expected, setup, success, failure, flavor);
michael@0 834 };
michael@0 835
michael@0 836 this.registerCleanupFunction = function test_registerCleanupFunction(aFunction) {
michael@0 837 self.__cleanupFunctions.push(aFunction);
michael@0 838 };
michael@0 839
michael@0 840 this.requestLongerTimeout = function test_requestLongerTimeout(aFactor) {
michael@0 841 self.__timeoutFactor = aFactor;
michael@0 842 };
michael@0 843
michael@0 844 this.copyToProfile = function test_copyToProfile(filename) {
michael@0 845 self.SimpleTest.copyToProfile(filename);
michael@0 846 };
michael@0 847
michael@0 848 this.expectUncaughtException = function test_expectUncaughtException(aExpecting) {
michael@0 849 self.SimpleTest.expectUncaughtException(aExpecting);
michael@0 850 };
michael@0 851
michael@0 852 this.ignoreAllUncaughtExceptions = function test_ignoreAllUncaughtExceptions(aIgnoring) {
michael@0 853 self.SimpleTest.ignoreAllUncaughtExceptions(aIgnoring);
michael@0 854 };
michael@0 855
michael@0 856 this.expectAssertions = function test_expectAssertions(aMin, aMax) {
michael@0 857 let min = aMin;
michael@0 858 let max = aMax;
michael@0 859 if (typeof(max) == "undefined") {
michael@0 860 max = min;
michael@0 861 }
michael@0 862 if (typeof(min) != "number" || typeof(max) != "number" ||
michael@0 863 min < 0 || max < min) {
michael@0 864 throw "bad parameter to expectAssertions";
michael@0 865 }
michael@0 866 self.__expectedMinAsserts = min;
michael@0 867 self.__expectedMaxAsserts = max;
michael@0 868 };
michael@0 869
michael@0 870 this.finish = function test_finish() {
michael@0 871 self.__done = true;
michael@0 872 if (self.__waitTimer) {
michael@0 873 self.executeSoon(function() {
michael@0 874 if (self.__done && self.__waitTimer) {
michael@0 875 clearTimeout(self.__waitTimer);
michael@0 876 self.__waitTimer = null;
michael@0 877 self.__tester.nextTest();
michael@0 878 }
michael@0 879 });
michael@0 880 }
michael@0 881 };
michael@0 882 }
michael@0 883 testScope.prototype = {
michael@0 884 __done: true,
michael@0 885 __generator: null,
michael@0 886 __tasks: null,
michael@0 887 __waitTimer: null,
michael@0 888 __cleanupFunctions: [],
michael@0 889 __timeoutFactor: 1,
michael@0 890 __expectedMinAsserts: 0,
michael@0 891 __expectedMaxAsserts: 0,
michael@0 892
michael@0 893 EventUtils: {},
michael@0 894 SimpleTest: {},
michael@0 895 Task: null,
michael@0 896 Promise: null,
michael@0 897 Assert: null,
michael@0 898
michael@0 899 /**
michael@0 900 * Add a test function which is a Task function.
michael@0 901 *
michael@0 902 * Task functions are functions fed into Task.jsm's Task.spawn(). They are
michael@0 903 * generators that emit promises.
michael@0 904 *
michael@0 905 * If an exception is thrown, an assertion fails, or if a rejected
michael@0 906 * promise is yielded, the test function aborts immediately and the test is
michael@0 907 * reported as a failure. Execution continues with the next test function.
michael@0 908 *
michael@0 909 * To trigger premature (but successful) termination of the function, simply
michael@0 910 * return or throw a Task.Result instance.
michael@0 911 *
michael@0 912 * Example usage:
michael@0 913 *
michael@0 914 * add_task(function test() {
michael@0 915 * let result = yield Promise.resolve(true);
michael@0 916 *
michael@0 917 * ok(result);
michael@0 918 *
michael@0 919 * let secondary = yield someFunctionThatReturnsAPromise(result);
michael@0 920 * is(secondary, "expected value");
michael@0 921 * });
michael@0 922 *
michael@0 923 * add_task(function test_early_return() {
michael@0 924 * let result = yield somethingThatReturnsAPromise();
michael@0 925 *
michael@0 926 * if (!result) {
michael@0 927 * // Test is ended immediately, with success.
michael@0 928 * return;
michael@0 929 * }
michael@0 930 *
michael@0 931 * is(result, "foo");
michael@0 932 * });
michael@0 933 */
michael@0 934 add_task: function(aFunction) {
michael@0 935 if (!this.__tasks) {
michael@0 936 this.waitForExplicitFinish();
michael@0 937 this.__tasks = [];
michael@0 938 }
michael@0 939 this.__tasks.push(aFunction.bind(this));
michael@0 940 },
michael@0 941
michael@0 942 destroy: function test_destroy() {
michael@0 943 for (let prop in this)
michael@0 944 delete this[prop];
michael@0 945 }
michael@0 946 };

mercurial