michael@0: /* -*- js-indent-level: 2; tab-width: 2; indent-tabs-mode: nil -*- */ michael@0: // Test timeout (seconds) michael@0: var gTimeoutSeconds = 45; michael@0: var gConfig; michael@0: michael@0: if (Cc === undefined) { michael@0: var Cc = Components.classes; michael@0: var Ci = Components.interfaces; michael@0: var Cu = Components.utils; michael@0: } michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Task.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "Services", michael@0: "resource://gre/modules/Services.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "BrowserNewTabPreloader", michael@0: "resource:///modules/BrowserNewTabPreloader.jsm", "BrowserNewTabPreloader"); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "CustomizationTabPreloader", michael@0: "resource:///modules/CustomizationTabPreloader.jsm", "CustomizationTabPreloader"); michael@0: michael@0: const SIMPLETEST_OVERRIDES = michael@0: ["ok", "is", "isnot", "ise", "todo", "todo_is", "todo_isnot", "info", "expectAssertions"]; michael@0: michael@0: window.addEventListener("load", testOnLoad, false); michael@0: michael@0: function testOnLoad() { michael@0: window.removeEventListener("load", testOnLoad, false); michael@0: michael@0: gConfig = readConfig(); michael@0: if (gConfig.testRoot == "browser" || michael@0: gConfig.testRoot == "metro" || michael@0: gConfig.testRoot == "webapprtChrome") { michael@0: // Make sure to launch the test harness for the first opened window only michael@0: var prefs = Services.prefs; michael@0: if (prefs.prefHasUserValue("testing.browserTestHarness.running")) michael@0: return; michael@0: michael@0: prefs.setBoolPref("testing.browserTestHarness.running", true); michael@0: michael@0: if (prefs.prefHasUserValue("testing.browserTestHarness.timeout")) michael@0: gTimeoutSeconds = prefs.getIntPref("testing.browserTestHarness.timeout"); michael@0: michael@0: var sstring = Cc["@mozilla.org/supports-string;1"]. michael@0: createInstance(Ci.nsISupportsString); michael@0: sstring.data = location.search; michael@0: michael@0: Services.ww.openWindow(window, "chrome://mochikit/content/browser-harness.xul", "browserTest", michael@0: "chrome,centerscreen,dialog=no,resizable,titlebar,toolbar=no,width=800,height=600", sstring); michael@0: } else { michael@0: // This code allows us to redirect without requiring specialpowers for chrome and a11y tests. michael@0: let messageHandler = function(m) { michael@0: messageManager.removeMessageListener("chromeEvent", messageHandler); michael@0: var url = m.json.data; michael@0: michael@0: // Window is the [ChromeWindow] for messageManager, so we need content.window michael@0: // Currently chrome tests are run in a content window instead of a ChromeWindow michael@0: var webNav = content.window.QueryInterface(Components.interfaces.nsIInterfaceRequestor) michael@0: .getInterface(Components.interfaces.nsIWebNavigation); michael@0: webNav.loadURI(url, null, null, null, null); michael@0: }; michael@0: michael@0: 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: messageManager.loadFrameScript(listener, true); michael@0: messageManager.addMessageListener("chromeEvent", messageHandler); michael@0: } michael@0: if (gConfig.e10s) { michael@0: e10s_init(); michael@0: } michael@0: } michael@0: michael@0: function Tester(aTests, aDumper, aCallback) { michael@0: this.dumper = aDumper; michael@0: this.tests = aTests; michael@0: this.callback = aCallback; michael@0: this.openedWindows = {}; michael@0: this.openedURLs = {}; michael@0: michael@0: this._scriptLoader = Services.scriptloader; michael@0: this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/EventUtils.js", this.EventUtils); michael@0: var simpleTestScope = {}; michael@0: this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/specialpowersAPI.js", simpleTestScope); michael@0: this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/SpecialPowersObserverAPI.js", simpleTestScope); michael@0: this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/ChromePowers.js", simpleTestScope); michael@0: this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/SimpleTest.js", simpleTestScope); michael@0: this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/MemoryStats.js", simpleTestScope); michael@0: this._scriptLoader.loadSubScript("chrome://mochikit/content/chrome-harness.js", simpleTestScope); michael@0: this.SimpleTest = simpleTestScope.SimpleTest; michael@0: this.MemoryStats = simpleTestScope.MemoryStats; michael@0: this.Task = Task; michael@0: this.Promise = Components.utils.import("resource://gre/modules/Promise.jsm", null).Promise; michael@0: this.Assert = Components.utils.import("resource://testing-common/Assert.jsm", null).Assert; michael@0: michael@0: this.SimpleTestOriginal = {}; michael@0: SIMPLETEST_OVERRIDES.forEach(m => { michael@0: this.SimpleTestOriginal[m] = this.SimpleTest[m]; michael@0: }); michael@0: michael@0: this._uncaughtErrorObserver = function({message, date, fileName, stack, lineNumber}) { michael@0: let text = "Once bug 991040 has landed, THIS ERROR WILL CAUSE A TEST FAILURE.\n" + message; michael@0: let error = text; michael@0: if (fileName || lineNumber) { michael@0: error = { michael@0: fileName: fileName, michael@0: lineNumber: lineNumber, michael@0: message: text, michael@0: toString: function() { michael@0: return text; michael@0: } michael@0: }; michael@0: } michael@0: this.currentTest.addResult( michael@0: new testResult( michael@0: /*success*/ true, michael@0: /*name*/"A promise chain failed to handle a rejection", michael@0: /*error*/error, michael@0: /*known*/true, michael@0: /*stack*/stack)); michael@0: }.bind(this); michael@0: } michael@0: Tester.prototype = { michael@0: EventUtils: {}, michael@0: SimpleTest: {}, michael@0: Task: null, michael@0: Promise: null, michael@0: Assert: null, michael@0: michael@0: repeat: 0, michael@0: runUntilFailure: false, michael@0: checker: null, michael@0: currentTestIndex: -1, michael@0: lastStartTime: null, michael@0: openedWindows: null, michael@0: lastAssertionCount: 0, michael@0: michael@0: get currentTest() { michael@0: return this.tests[this.currentTestIndex]; michael@0: }, michael@0: get done() { michael@0: return this.currentTestIndex == this.tests.length - 1; michael@0: }, michael@0: michael@0: start: function Tester_start() { michael@0: // Check whether this window is ready to run tests. michael@0: if (window.BrowserChromeTest) { michael@0: BrowserChromeTest.runWhenReady(this.actuallyStart.bind(this)); michael@0: return; michael@0: } michael@0: this.actuallyStart(); michael@0: }, michael@0: michael@0: actuallyStart: function Tester_actuallyStart() { michael@0: //if testOnLoad was not called, then gConfig is not defined michael@0: if (!gConfig) michael@0: gConfig = readConfig(); michael@0: michael@0: if (gConfig.runUntilFailure) michael@0: this.runUntilFailure = true; michael@0: michael@0: if (gConfig.repeat) michael@0: this.repeat = gConfig.repeat; michael@0: michael@0: this.dumper.dump("*** Start BrowserChrome Test Results ***\n"); michael@0: Services.console.registerListener(this); michael@0: Services.obs.addObserver(this, "chrome-document-global-created", false); michael@0: Services.obs.addObserver(this, "content-document-global-created", false); michael@0: this._globalProperties = Object.keys(window); michael@0: this._globalPropertyWhitelist = [ michael@0: "navigator", "constructor", "top", michael@0: "Application", michael@0: "__SS_tabsToRestore", "__SSi", michael@0: "webConsoleCommandController", michael@0: ]; michael@0: michael@0: this.Promise.Debugging.clearUncaughtErrorObservers(); michael@0: this.Promise.Debugging.addUncaughtErrorObserver(this._uncaughtErrorObserver); michael@0: michael@0: if (this.tests.length) michael@0: this.nextTest(); michael@0: else michael@0: this.finish(); michael@0: }, michael@0: michael@0: waitForWindowsState: function Tester_waitForWindowsState(aCallback) { michael@0: let timedOut = this.currentTest && this.currentTest.timedOut; michael@0: let baseMsg = timedOut ? "Found a {elt} after previous test timed out" michael@0: : this.currentTest ? "Found an unexpected {elt} at the end of test run" michael@0: : "Found an unexpected {elt}"; michael@0: michael@0: // Remove stale tabs michael@0: if (this.currentTest && window.gBrowser && gBrowser.tabs.length > 1) { michael@0: while (gBrowser.tabs.length > 1) { michael@0: let lastTab = gBrowser.tabContainer.lastChild; michael@0: let msg = baseMsg.replace("{elt}", "tab") + michael@0: ": " + lastTab.linkedBrowser.currentURI.spec; michael@0: this.currentTest.addResult(new testResult(false, msg, "", false)); michael@0: gBrowser.removeTab(lastTab); michael@0: } michael@0: } michael@0: michael@0: // Replace the last tab with a fresh one michael@0: if (window.gBrowser) { michael@0: gBrowser.addTab("about:blank", { skipAnimation: true }); michael@0: gBrowser.removeCurrentTab(); michael@0: gBrowser.stop(); michael@0: } michael@0: michael@0: // Remove stale windows michael@0: this.dumper.dump("TEST-INFO | checking window state\n"); michael@0: let windowsEnum = Services.wm.getEnumerator(null); michael@0: while (windowsEnum.hasMoreElements()) { michael@0: let win = windowsEnum.getNext(); michael@0: if (win != window && !win.closed && michael@0: win.document.documentElement.getAttribute("id") != "browserTestHarness") { michael@0: let type = win.document.documentElement.getAttribute("windowtype"); michael@0: switch (type) { michael@0: case "navigator:browser": michael@0: type = "browser window"; michael@0: break; michael@0: case null: michael@0: type = "unknown window"; michael@0: break; michael@0: } michael@0: let msg = baseMsg.replace("{elt}", type); michael@0: if (this.currentTest) michael@0: this.currentTest.addResult(new testResult(false, msg, "", false)); michael@0: else michael@0: this.dumper.dump("TEST-UNEXPECTED-FAIL | (browser-test.js) | " + msg + "\n"); michael@0: michael@0: win.close(); michael@0: } michael@0: } michael@0: michael@0: // Make sure the window is raised before each test. michael@0: this.SimpleTest.waitForFocus(aCallback); michael@0: }, michael@0: michael@0: finish: function Tester_finish(aSkipSummary) { michael@0: this.Promise.Debugging.flushUncaughtErrors(); michael@0: michael@0: var passCount = this.tests.reduce(function(a, f) a + f.passCount, 0); michael@0: var failCount = this.tests.reduce(function(a, f) a + f.failCount, 0); michael@0: var todoCount = this.tests.reduce(function(a, f) a + f.todoCount, 0); michael@0: michael@0: if (this.repeat > 0) { michael@0: --this.repeat; michael@0: this.currentTestIndex = -1; michael@0: this.nextTest(); michael@0: } michael@0: else{ michael@0: Services.console.unregisterListener(this); michael@0: Services.obs.removeObserver(this, "chrome-document-global-created"); michael@0: Services.obs.removeObserver(this, "content-document-global-created"); michael@0: this.Promise.Debugging.clearUncaughtErrorObservers(); michael@0: this.dumper.dump("\nINFO TEST-START | Shutdown\n"); michael@0: michael@0: if (this.tests.length) { michael@0: this.dumper.dump("Browser Chrome Test Summary\n"); michael@0: michael@0: this.dumper.dump("\tPassed: " + passCount + "\n" + michael@0: "\tFailed: " + failCount + "\n" + michael@0: "\tTodo: " + todoCount + "\n"); michael@0: } else { michael@0: this.dumper.dump("TEST-UNEXPECTED-FAIL | (browser-test.js) | " + michael@0: "No tests to run. Did you pass an invalid --test-path?\n"); michael@0: } michael@0: this.dumper.dump("\n*** End BrowserChrome Test Results ***\n"); michael@0: michael@0: this.dumper.done(); michael@0: michael@0: // Tests complete, notify the callback and return michael@0: this.callback(this.tests); michael@0: this.callback = null; michael@0: this.tests = null; michael@0: this.openedWindows = null; michael@0: } michael@0: }, michael@0: michael@0: haltTests: function Tester_haltTests() { michael@0: // Do not run any further tests michael@0: this.currentTestIndex = this.tests.length - 1; michael@0: this.repeat = 0; michael@0: }, michael@0: michael@0: observe: function Tester_observe(aSubject, aTopic, aData) { michael@0: if (!aTopic) { michael@0: this.onConsoleMessage(aSubject); michael@0: } else if (this.currentTest) { michael@0: this.onDocumentCreated(aSubject); michael@0: } michael@0: }, michael@0: michael@0: onDocumentCreated: function Tester_onDocumentCreated(aWindow) { michael@0: let utils = aWindow.QueryInterface(Ci.nsIInterfaceRequestor) michael@0: .getInterface(Ci.nsIDOMWindowUtils); michael@0: let outerID = utils.outerWindowID; michael@0: let innerID = utils.currentInnerWindowID; michael@0: michael@0: if (!(outerID in this.openedWindows)) { michael@0: this.openedWindows[outerID] = this.currentTest; michael@0: } michael@0: this.openedWindows[innerID] = this.currentTest; michael@0: michael@0: let url = aWindow.location.href || "about:blank"; michael@0: this.openedURLs[outerID] = this.openedURLs[innerID] = url; michael@0: }, michael@0: michael@0: onConsoleMessage: function Tester_onConsoleMessage(aConsoleMessage) { michael@0: // Ignore empty messages. michael@0: if (!aConsoleMessage.message) michael@0: return; michael@0: michael@0: try { michael@0: var msg = "Console message: " + aConsoleMessage.message; michael@0: if (this.currentTest) michael@0: this.currentTest.addResult(new testMessage(msg)); michael@0: else michael@0: this.dumper.dump("TEST-INFO | (browser-test.js) | " + msg.replace(/\n$/, "") + "\n"); michael@0: } catch (ex) { michael@0: // Swallow exception so we don't lead to another error being reported, michael@0: // throwing us into an infinite loop michael@0: } michael@0: }, michael@0: michael@0: nextTest: Task.async(function*() { michael@0: if (this.currentTest) { michael@0: // Run cleanup functions for the current test before moving on to the michael@0: // next one. michael@0: let testScope = this.currentTest.scope; michael@0: while (testScope.__cleanupFunctions.length > 0) { michael@0: let func = testScope.__cleanupFunctions.shift(); michael@0: try { michael@0: yield func.apply(testScope); michael@0: } michael@0: catch (ex) { michael@0: this.currentTest.addResult(new testResult(false, "Cleanup function threw an exception", ex, false)); michael@0: } michael@0: }; michael@0: michael@0: this.Promise.Debugging.flushUncaughtErrors(); michael@0: michael@0: let winUtils = window.QueryInterface(Ci.nsIInterfaceRequestor) michael@0: .getInterface(Ci.nsIDOMWindowUtils); michael@0: if (winUtils.isTestControllingRefreshes) { michael@0: this.currentTest.addResult(new testResult(false, "test left refresh driver under test control", "", false)); michael@0: winUtils.restoreNormalRefresh(); michael@0: } michael@0: michael@0: if (this.SimpleTest.isExpectingUncaughtException()) { michael@0: this.currentTest.addResult(new testResult(false, "expectUncaughtException was called but no uncaught exception was detected!", "", false)); michael@0: } michael@0: michael@0: Object.keys(window).forEach(function (prop) { michael@0: if (parseInt(prop) == prop) { michael@0: // This is a string which when parsed as an integer and then michael@0: // stringified gives the original string. As in, this is in fact a michael@0: // string representation of an integer, so an index into michael@0: // window.frames. Skip those. michael@0: return; michael@0: } michael@0: if (this._globalProperties.indexOf(prop) == -1) { michael@0: this._globalProperties.push(prop); michael@0: if (this._globalPropertyWhitelist.indexOf(prop) == -1) michael@0: this.currentTest.addResult(new testResult(false, "leaked window property: " + prop, "", false)); michael@0: } michael@0: }, this); michael@0: michael@0: // Clear document.popupNode. The test could have set it to a custom value michael@0: // for its own purposes, nulling it out it will go back to the default michael@0: // behavior of returning the last opened popup. michael@0: document.popupNode = null; michael@0: michael@0: // Notify a long running test problem if it didn't end up in a timeout. michael@0: if (this.currentTest.unexpectedTimeouts && !this.currentTest.timedOut) { michael@0: let msg = "This test exceeded the timeout threshold. It should be " + michael@0: "rewritten or split up. If that's not possible, use " + michael@0: "requestLongerTimeout(N), but only as a last resort."; michael@0: this.currentTest.addResult(new testResult(false, msg, "", false)); michael@0: } michael@0: michael@0: // If we're in a debug build, check assertion counts. This code michael@0: // is similar to the code in TestRunner.testUnloaded in michael@0: // TestRunner.js used for all other types of mochitests. michael@0: let debugsvc = Cc["@mozilla.org/xpcom/debug;1"].getService(Ci.nsIDebug2); michael@0: if (debugsvc.isDebugBuild) { michael@0: let newAssertionCount = debugsvc.assertionCount; michael@0: let numAsserts = newAssertionCount - this.lastAssertionCount; michael@0: this.lastAssertionCount = newAssertionCount; michael@0: michael@0: let max = testScope.__expectedMaxAsserts; michael@0: let min = testScope.__expectedMinAsserts; michael@0: if (numAsserts > max) { michael@0: let msg = "Assertion count " + numAsserts + michael@0: " is greater than expected range " + michael@0: min + "-" + max + " assertions."; michael@0: // TEST-UNEXPECTED-FAIL (TEMPORARILY TEST-KNOWN-FAIL) michael@0: //this.currentTest.addResult(new testResult(false, msg, "", false)); michael@0: this.currentTest.addResult(new testResult(true, msg, "", true)); michael@0: } else if (numAsserts < min) { michael@0: let msg = "Assertion count " + numAsserts + michael@0: " is less than expected range " + michael@0: min + "-" + max + " assertions."; michael@0: // TEST-UNEXPECTED-PASS michael@0: this.currentTest.addResult(new testResult(false, msg, "", true)); michael@0: } else if (numAsserts > 0) { michael@0: let msg = "Assertion count " + numAsserts + michael@0: " is within expected range " + michael@0: min + "-" + max + " assertions."; michael@0: // TEST-KNOWN-FAIL michael@0: this.currentTest.addResult(new testResult(true, msg, "", true)); michael@0: } michael@0: } michael@0: michael@0: // Dump memory stats for main thread. michael@0: if (Cc["@mozilla.org/xre/runtime;1"] michael@0: .getService(Ci.nsIXULRuntime) michael@0: .processType == Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT) michael@0: { michael@0: this.MemoryStats.dump((l) => { this.dumper.dump(l + "\n"); }, michael@0: this.currentTestIndex, michael@0: this.currentTest.path, michael@0: gConfig.dumpOutputDirectory, michael@0: gConfig.dumpAboutMemoryAfterTest, michael@0: gConfig.dumpDMDAfterTest); michael@0: } michael@0: michael@0: // Note the test run time michael@0: let time = Date.now() - this.lastStartTime; michael@0: this.dumper.dump("INFO TEST-END | " + this.currentTest.path + " | finished in " + time + "ms\n"); michael@0: this.currentTest.setDuration(time); michael@0: michael@0: if (this.runUntilFailure && this.currentTest.failCount > 0) { michael@0: this.haltTests(); michael@0: } michael@0: michael@0: // Restore original SimpleTest methods to avoid leaks. michael@0: SIMPLETEST_OVERRIDES.forEach(m => { michael@0: this.SimpleTest[m] = this.SimpleTestOriginal[m]; michael@0: }); michael@0: michael@0: testScope.destroy(); michael@0: this.currentTest.scope = null; michael@0: } michael@0: michael@0: // Check the window state for the current test before moving to the next one. michael@0: // This also causes us to check before starting any tests, since nextTest() michael@0: // is invoked to start the tests. michael@0: this.waitForWindowsState((function () { michael@0: if (this.done) { michael@0: // Uninitialize a few things explicitly so that they can clean up michael@0: // frames and browser intentionally kept alive until shutdown to michael@0: // eliminate false positives. michael@0: if (gConfig.testRoot == "browser") { michael@0: // Replace the document currently loaded in the browser's sidebar. michael@0: // This will prevent false positives for tests that were the last michael@0: // to touch the sidebar. They will thus not be blamed for leaking michael@0: // a document. michael@0: let sidebar = document.getElementById("sidebar"); michael@0: sidebar.setAttribute("src", "data:text/html;charset=utf-8,"); michael@0: sidebar.docShell.createAboutBlankContentViewer(null); michael@0: sidebar.setAttribute("src", "about:blank"); michael@0: michael@0: // Do the same for the social sidebar. michael@0: let socialSidebar = document.getElementById("social-sidebar-browser"); michael@0: socialSidebar.setAttribute("src", "data:text/html;charset=utf-8,"); michael@0: socialSidebar.docShell.createAboutBlankContentViewer(null); michael@0: socialSidebar.setAttribute("src", "about:blank"); michael@0: michael@0: // Destroy BackgroundPageThumbs resources. michael@0: let {BackgroundPageThumbs} = michael@0: Cu.import("resource://gre/modules/BackgroundPageThumbs.jsm", {}); michael@0: BackgroundPageThumbs._destroy(); michael@0: michael@0: BrowserNewTabPreloader.uninit(); michael@0: CustomizationTabPreloader.uninit(); michael@0: SocialFlyout.unload(); michael@0: SocialShare.uninit(); michael@0: TabView.uninit(); michael@0: } michael@0: michael@0: // Simulate memory pressure so that we're forced to free more resources michael@0: // and thus get rid of more false leaks like already terminated workers. michael@0: Services.obs.notifyObservers(null, "memory-pressure", "heap-minimize"); michael@0: michael@0: // Schedule GC and CC runs before finishing in order to detect michael@0: // DOM windows leaked by our tests or the tested code. Note that we michael@0: // use a shrinking GC so that the JS engine will discard JIT code and michael@0: // JIT caches more aggressively. michael@0: michael@0: let checkForLeakedGlobalWindows = aCallback => { michael@0: Cu.schedulePreciseShrinkingGC(() => { michael@0: let analyzer = new CCAnalyzer(); michael@0: analyzer.run(() => { michael@0: let results = []; michael@0: for (let obj of analyzer.find("nsGlobalWindow ")) { michael@0: let m = obj.name.match(/^nsGlobalWindow #(\d+)/); michael@0: if (m && m[1] in this.openedWindows) michael@0: results.push({ name: obj.name, url: m[1] }); michael@0: } michael@0: aCallback(results); michael@0: }); michael@0: }); michael@0: }; michael@0: michael@0: let reportLeaks = aResults => { michael@0: for (let result of aResults) { michael@0: let test = this.openedWindows[result.url]; michael@0: let msg = "leaked until shutdown [" + result.name + michael@0: " " + (this.openedURLs[result.url] || "NULL") + "]"; michael@0: test.addResult(new testResult(false, msg, "", false)); michael@0: } michael@0: }; michael@0: michael@0: checkForLeakedGlobalWindows(aResults => { michael@0: if (aResults.length == 0) { michael@0: this.finish(); michael@0: return; michael@0: } michael@0: // After the first check, if there are reported leaked windows, sleep michael@0: // for a while, to allow off-main-thread work to complete and free up michael@0: // main-thread objects. Then check again. michael@0: setTimeout(() => { michael@0: checkForLeakedGlobalWindows(aResults => { michael@0: reportLeaks(aResults); michael@0: this.finish(); michael@0: }); michael@0: }, 1000); michael@0: }); michael@0: michael@0: return; michael@0: } michael@0: michael@0: this.currentTestIndex++; michael@0: this.execTest(); michael@0: }).bind(this)); michael@0: }), michael@0: michael@0: execTest: function Tester_execTest() { michael@0: this.dumper.dump("TEST-START | " + this.currentTest.path + "\n"); michael@0: michael@0: this.SimpleTest.reset(); michael@0: michael@0: // Load the tests into a testscope michael@0: let currentScope = this.currentTest.scope = new testScope(this, this.currentTest); michael@0: let currentTest = this.currentTest; michael@0: michael@0: // Import utils in the test scope. michael@0: this.currentTest.scope.EventUtils = this.EventUtils; michael@0: this.currentTest.scope.SimpleTest = this.SimpleTest; michael@0: this.currentTest.scope.gTestPath = this.currentTest.path; michael@0: this.currentTest.scope.Task = this.Task; michael@0: this.currentTest.scope.Promise = this.Promise; michael@0: // Pass a custom report function for mochitest style reporting. michael@0: this.currentTest.scope.Assert = new this.Assert(function(err, message, stack) { michael@0: let res; michael@0: if (err) { michael@0: res = new testResult(false, err.message, err.stack, false, err.stack); michael@0: } else { michael@0: res = new testResult(true, message, "", false, stack); michael@0: } michael@0: currentTest.addResult(res); michael@0: }); michael@0: michael@0: // Allow Assert.jsm methods to be tacked to the current scope. michael@0: this.currentTest.scope.export_assertions = function() { michael@0: for (let func in this.Assert) { michael@0: this[func] = this.Assert[func].bind(this.Assert); michael@0: } michael@0: }; michael@0: michael@0: // Override SimpleTest methods with ours. michael@0: SIMPLETEST_OVERRIDES.forEach(function(m) { michael@0: this.SimpleTest[m] = this[m]; michael@0: }, this.currentTest.scope); michael@0: michael@0: //load the tools to work with chrome .jar and remote michael@0: try { michael@0: this._scriptLoader.loadSubScript("chrome://mochikit/content/chrome-harness.js", this.currentTest.scope); michael@0: } catch (ex) { /* no chrome-harness tools */ } michael@0: michael@0: // Import head.js script if it exists. michael@0: var currentTestDirPath = michael@0: this.currentTest.path.substr(0, this.currentTest.path.lastIndexOf("/")); michael@0: var headPath = currentTestDirPath + "/head.js"; michael@0: try { michael@0: this._scriptLoader.loadSubScript(headPath, this.currentTest.scope); michael@0: } catch (ex) { michael@0: // Ignore if no head.js exists, but report all other errors. Note this michael@0: // will also ignore an existing head.js attempting to import a missing michael@0: // module - see bug 755558 for why this strategy is preferred anyway. michael@0: if (ex.toString() != 'Error opening input stream (invalid filename?)') { michael@0: this.currentTest.addResult(new testResult(false, "head.js import threw an exception", ex, false)); michael@0: } michael@0: } michael@0: michael@0: // Import the test script. michael@0: try { michael@0: this._scriptLoader.loadSubScript(this.currentTest.path, michael@0: this.currentTest.scope); michael@0: this.Promise.Debugging.flushUncaughtErrors(); michael@0: // Run the test michael@0: this.lastStartTime = Date.now(); michael@0: if (this.currentTest.scope.__tasks) { michael@0: // This test consists of tasks, added via the `add_task()` API. michael@0: if ("test" in this.currentTest.scope) { michael@0: throw "Cannot run both a add_task test and a normal test at the same time."; michael@0: } michael@0: this.Task.spawn(function() { michael@0: let task; michael@0: while ((task = this.__tasks.shift())) { michael@0: this.SimpleTest.info("Entering test " + task.name); michael@0: try { michael@0: yield task(); michael@0: } catch (ex) { michael@0: let isExpected = !!this.SimpleTest.isExpectingUncaughtException(); michael@0: let stack = (typeof ex == "object" && "stack" in ex)?ex.stack:null; michael@0: let name = "Uncaught exception"; michael@0: let result = new testResult(isExpected, name, ex, false, stack); michael@0: currentTest.addResult(result); michael@0: } michael@0: this.Promise.Debugging.flushUncaughtErrors(); michael@0: this.SimpleTest.info("Leaving test " + task.name); michael@0: } michael@0: this.finish(); michael@0: }.bind(currentScope)); michael@0: } else if ("generatorTest" in this.currentTest.scope) { michael@0: if ("test" in this.currentTest.scope) { michael@0: throw "Cannot run both a generator test and a normal test at the same time."; michael@0: } michael@0: michael@0: // This test is a generator. It will not finish immediately. michael@0: this.currentTest.scope.waitForExplicitFinish(); michael@0: var result = this.currentTest.scope.generatorTest(); michael@0: this.currentTest.scope.__generator = result; michael@0: result.next(); michael@0: } else { michael@0: this.currentTest.scope.test(); michael@0: } michael@0: } catch (ex) { michael@0: let isExpected = !!this.SimpleTest.isExpectingUncaughtException(); michael@0: if (!this.SimpleTest.isIgnoringAllUncaughtExceptions()) { michael@0: this.currentTest.addResult(new testResult(isExpected, "Exception thrown", ex, false)); michael@0: this.SimpleTest.expectUncaughtException(false); michael@0: } else { michael@0: this.currentTest.addResult(new testMessage("Exception thrown: " + ex)); michael@0: } michael@0: this.currentTest.scope.finish(); michael@0: } michael@0: michael@0: // If the test ran synchronously, move to the next test, otherwise the test michael@0: // will trigger the next test when it is done. michael@0: if (this.currentTest.scope.__done) { michael@0: this.nextTest(); michael@0: } michael@0: else { michael@0: var self = this; michael@0: this.currentTest.scope.__waitTimer = setTimeout(function timeoutFn() { michael@0: if (--self.currentTest.scope.__timeoutFactor > 0) { michael@0: // We were asked to wait a bit longer. michael@0: self.currentTest.scope.info( michael@0: "Longer timeout required, waiting longer... Remaining timeouts: " + michael@0: self.currentTest.scope.__timeoutFactor); michael@0: self.currentTest.scope.__waitTimer = michael@0: setTimeout(timeoutFn, gTimeoutSeconds * 1000); michael@0: return; michael@0: } michael@0: michael@0: // If the test is taking longer than expected, but it's not hanging, michael@0: // mark the fact, but let the test continue. At the end of the test, michael@0: // if it didn't timeout, we will notify the problem through an error. michael@0: // To figure whether it's an actual hang, compare the time of the last michael@0: // result or message to half of the timeout time. michael@0: // Though, to protect against infinite loops, limit the number of times michael@0: // we allow the test to proceed. michael@0: const MAX_UNEXPECTED_TIMEOUTS = 10; michael@0: if (Date.now() - self.currentTest.lastOutputTime < (gTimeoutSeconds / 2) * 1000 && michael@0: ++self.currentTest.unexpectedTimeouts <= MAX_UNEXPECTED_TIMEOUTS) { michael@0: self.currentTest.scope.__waitTimer = michael@0: setTimeout(timeoutFn, gTimeoutSeconds * 1000); michael@0: return; michael@0: } michael@0: michael@0: self.currentTest.addResult(new testResult(false, "Test timed out", "", false)); michael@0: self.currentTest.timedOut = true; michael@0: self.currentTest.scope.__waitTimer = null; michael@0: self.nextTest(); michael@0: }, gTimeoutSeconds * 1000); michael@0: } michael@0: }, michael@0: michael@0: QueryInterface: function(aIID) { michael@0: if (aIID.equals(Ci.nsIConsoleListener) || michael@0: aIID.equals(Ci.nsISupports)) michael@0: return this; michael@0: michael@0: throw Components.results.NS_ERROR_NO_INTERFACE; michael@0: } michael@0: }; michael@0: michael@0: function testResult(aCondition, aName, aDiag, aIsTodo, aStack) { michael@0: this.msg = aName || ""; michael@0: michael@0: this.info = false; michael@0: this.pass = !!aCondition; michael@0: this.todo = aIsTodo; michael@0: michael@0: if (this.pass) { michael@0: if (aIsTodo) michael@0: this.result = "TEST-KNOWN-FAIL"; michael@0: else michael@0: this.result = "TEST-PASS"; michael@0: } else { michael@0: if (aDiag) { michael@0: if (typeof aDiag == "object" && "fileName" in aDiag) { michael@0: // we have an exception - print filename and linenumber information michael@0: this.msg += " at " + aDiag.fileName + ":" + aDiag.lineNumber; michael@0: } michael@0: this.msg += " - " + aDiag; michael@0: } michael@0: if (aStack) { michael@0: this.msg += "\nStack trace:\n"; michael@0: var frame = aStack; michael@0: while (frame) { michael@0: this.msg += " " + frame + "\n"; michael@0: frame = frame.caller; michael@0: } michael@0: } michael@0: if (aIsTodo) michael@0: this.result = "TEST-UNEXPECTED-PASS"; michael@0: else michael@0: this.result = "TEST-UNEXPECTED-FAIL"; michael@0: michael@0: if (gConfig.debugOnFailure) { michael@0: // You've hit this line because you requested to break into the michael@0: // debugger upon a testcase failure on your test run. michael@0: debugger; michael@0: } michael@0: } michael@0: } michael@0: michael@0: function testMessage(aName) { michael@0: this.msg = aName || ""; michael@0: this.info = true; michael@0: this.result = "TEST-INFO"; michael@0: } michael@0: michael@0: // Need to be careful adding properties to this object, since its properties michael@0: // cannot conflict with global variables used in tests. michael@0: function testScope(aTester, aTest) { michael@0: this.__tester = aTester; michael@0: michael@0: var self = this; michael@0: this.ok = function test_ok(condition, name, diag, stack) { michael@0: aTest.addResult(new testResult(condition, name, diag, false, michael@0: stack ? stack : Components.stack.caller)); michael@0: }; michael@0: this.is = function test_is(a, b, name) { michael@0: self.ok(a == b, name, "Got " + a + ", expected " + b, false, michael@0: Components.stack.caller); michael@0: }; michael@0: this.isnot = function test_isnot(a, b, name) { michael@0: self.ok(a != b, name, "Didn't expect " + a + ", but got it", false, michael@0: Components.stack.caller); michael@0: }; michael@0: this.ise = function test_ise(a, b, name) { michael@0: self.ok(a === b, name, "Got " + a + ", strictly expected " + b, false, michael@0: Components.stack.caller); michael@0: }; michael@0: this.todo = function test_todo(condition, name, diag, stack) { michael@0: aTest.addResult(new testResult(!condition, name, diag, true, michael@0: stack ? stack : Components.stack.caller)); michael@0: }; michael@0: this.todo_is = function test_todo_is(a, b, name) { michael@0: self.todo(a == b, name, "Got " + a + ", expected " + b, michael@0: Components.stack.caller); michael@0: }; michael@0: this.todo_isnot = function test_todo_isnot(a, b, name) { michael@0: self.todo(a != b, name, "Didn't expect " + a + ", but got it", michael@0: Components.stack.caller); michael@0: }; michael@0: this.info = function test_info(name) { michael@0: aTest.addResult(new testMessage(name)); michael@0: }; michael@0: michael@0: this.executeSoon = function test_executeSoon(func) { michael@0: Services.tm.mainThread.dispatch({ michael@0: run: function() { michael@0: func(); michael@0: } michael@0: }, Ci.nsIThread.DISPATCH_NORMAL); michael@0: }; michael@0: michael@0: this.nextStep = function test_nextStep(arg) { michael@0: if (self.__done) { michael@0: aTest.addResult(new testResult(false, "nextStep was called too many times", "", false)); michael@0: return; michael@0: } michael@0: michael@0: if (!self.__generator) { michael@0: aTest.addResult(new testResult(false, "nextStep called with no generator", "", false)); michael@0: self.finish(); michael@0: return; michael@0: } michael@0: michael@0: try { michael@0: self.__generator.send(arg); michael@0: } catch (ex if ex instanceof StopIteration) { michael@0: // StopIteration means test is finished. michael@0: self.finish(); michael@0: } catch (ex) { michael@0: var isExpected = !!self.SimpleTest.isExpectingUncaughtException(); michael@0: if (!self.SimpleTest.isIgnoringAllUncaughtExceptions()) { michael@0: aTest.addResult(new testResult(isExpected, "Exception thrown", ex, false)); michael@0: self.SimpleTest.expectUncaughtException(false); michael@0: } else { michael@0: aTest.addResult(new testMessage("Exception thrown: " + ex)); michael@0: } michael@0: self.finish(); michael@0: } michael@0: }; michael@0: michael@0: this.waitForExplicitFinish = function test_waitForExplicitFinish() { michael@0: self.__done = false; michael@0: }; michael@0: michael@0: this.waitForFocus = function test_waitForFocus(callback, targetWindow, expectBlankPage) { michael@0: self.SimpleTest.waitForFocus(callback, targetWindow, expectBlankPage); michael@0: }; michael@0: michael@0: this.waitForClipboard = function test_waitForClipboard(expected, setup, success, failure, flavor) { michael@0: self.SimpleTest.waitForClipboard(expected, setup, success, failure, flavor); michael@0: }; michael@0: michael@0: this.registerCleanupFunction = function test_registerCleanupFunction(aFunction) { michael@0: self.__cleanupFunctions.push(aFunction); michael@0: }; michael@0: michael@0: this.requestLongerTimeout = function test_requestLongerTimeout(aFactor) { michael@0: self.__timeoutFactor = aFactor; michael@0: }; michael@0: michael@0: this.copyToProfile = function test_copyToProfile(filename) { michael@0: self.SimpleTest.copyToProfile(filename); michael@0: }; michael@0: michael@0: this.expectUncaughtException = function test_expectUncaughtException(aExpecting) { michael@0: self.SimpleTest.expectUncaughtException(aExpecting); michael@0: }; michael@0: michael@0: this.ignoreAllUncaughtExceptions = function test_ignoreAllUncaughtExceptions(aIgnoring) { michael@0: self.SimpleTest.ignoreAllUncaughtExceptions(aIgnoring); michael@0: }; michael@0: michael@0: this.expectAssertions = function test_expectAssertions(aMin, aMax) { michael@0: let min = aMin; michael@0: let max = aMax; michael@0: if (typeof(max) == "undefined") { michael@0: max = min; michael@0: } michael@0: if (typeof(min) != "number" || typeof(max) != "number" || michael@0: min < 0 || max < min) { michael@0: throw "bad parameter to expectAssertions"; michael@0: } michael@0: self.__expectedMinAsserts = min; michael@0: self.__expectedMaxAsserts = max; michael@0: }; michael@0: michael@0: this.finish = function test_finish() { michael@0: self.__done = true; michael@0: if (self.__waitTimer) { michael@0: self.executeSoon(function() { michael@0: if (self.__done && self.__waitTimer) { michael@0: clearTimeout(self.__waitTimer); michael@0: self.__waitTimer = null; michael@0: self.__tester.nextTest(); michael@0: } michael@0: }); michael@0: } michael@0: }; michael@0: } michael@0: testScope.prototype = { michael@0: __done: true, michael@0: __generator: null, michael@0: __tasks: null, michael@0: __waitTimer: null, michael@0: __cleanupFunctions: [], michael@0: __timeoutFactor: 1, michael@0: __expectedMinAsserts: 0, michael@0: __expectedMaxAsserts: 0, michael@0: michael@0: EventUtils: {}, michael@0: SimpleTest: {}, michael@0: Task: null, michael@0: Promise: null, michael@0: Assert: null, michael@0: michael@0: /** michael@0: * Add a test function which is a Task function. michael@0: * michael@0: * Task functions are functions fed into Task.jsm's Task.spawn(). They are michael@0: * generators that emit promises. michael@0: * michael@0: * If an exception is thrown, an assertion fails, or if a rejected michael@0: * promise is yielded, the test function aborts immediately and the test is michael@0: * reported as a failure. Execution continues with the next test function. michael@0: * michael@0: * To trigger premature (but successful) termination of the function, simply michael@0: * return or throw a Task.Result instance. michael@0: * michael@0: * Example usage: michael@0: * michael@0: * add_task(function test() { michael@0: * let result = yield Promise.resolve(true); michael@0: * michael@0: * ok(result); michael@0: * michael@0: * let secondary = yield someFunctionThatReturnsAPromise(result); michael@0: * is(secondary, "expected value"); michael@0: * }); michael@0: * michael@0: * add_task(function test_early_return() { michael@0: * let result = yield somethingThatReturnsAPromise(); michael@0: * michael@0: * if (!result) { michael@0: * // Test is ended immediately, with success. michael@0: * return; michael@0: * } michael@0: * michael@0: * is(result, "foo"); michael@0: * }); michael@0: */ michael@0: add_task: function(aFunction) { michael@0: if (!this.__tasks) { michael@0: this.waitForExplicitFinish(); michael@0: this.__tasks = []; michael@0: } michael@0: this.__tasks.push(aFunction.bind(this)); michael@0: }, michael@0: michael@0: destroy: function test_destroy() { michael@0: for (let prop in this) michael@0: delete this[prop]; michael@0: } michael@0: };