Bug 1412282 - Allow calling Assert.jsm methods directly from other test-only modules. r=mikedeboer
This adds a global instance that can be used by invoking assertion methods directly on the imported Assert object. The test suites set the global reporter function to the one for the currently running test.
MozReview-Commit-ID: 8dksVc9o7r
--- a/testing/mochitest/browser-test.js
+++ b/testing/mochitest/browser-test.js
@@ -725,19 +725,18 @@ Tester.prototype = {
stack: err.stack,
allowFailure: currentTest.allowFailure,
} : {
name: message,
pass: true,
stack,
allowFailure: currentTest.allowFailure,
}));
- });
+ }, true);
- this.PromiseTestUtils.Assert = this.currentTest.scope.Assert;
this.ContentTask.setTestScope(currentScope);
// Allow Assert.jsm methods to be tacked to the current scope.
this.currentTest.scope.export_assertions = function() {
for (let func in this.Assert) {
this[func] = this.Assert[func].bind(this.Assert);
}
};
--- a/testing/modules/Assert.jsm
+++ b/testing/modules/Assert.jsm
@@ -20,25 +20,35 @@ Components.utils.import("resource://gre/
Components.utils.import("resource://gre/modules/ObjectUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Promise",
"resource://gre/modules/Promise.jsm");
/**
* 1. The assert module provides functions that throw AssertionError's when
* particular conditions are not met.
*
- * To use the module you'll need to instantiate it first, which allows consumers
+ * To use the module you may instantiate it first, which allows consumers
* to override certain behavior on the newly obtained instance. For examples,
* see the javadoc comments for the `report` member function.
+ *
+ * The isDefault argument is used by test suites to set reporterFunc as the
+ * default used by the global instance, which is called for example by other
+ * test-only modules. This is false when the reporter is set by content scripts,
+ * because they may still run in the parent process.
*/
-var Assert = this.Assert = function(reporterFunc) {
+var Assert = this.Assert = function(reporterFunc, isDefault) {
if (reporterFunc)
this.setReporter(reporterFunc);
+ if (isDefault)
+ Assert.setReporter(reporterFunc);
};
+// This allows using the Assert object as an additional global instance.
+Object.setPrototypeOf(Assert, Assert.prototype);
+
function instanceOf(object, type) {
return Object.prototype.toString.call(object) == "[object " + type + "]";
}
function replacer(key, value) {
if (value === undefined) {
return "" + value;
}
--- a/testing/xpcshell/head.js
+++ b/testing/xpcshell/head.js
@@ -46,17 +46,17 @@ Components.utils.importGlobalProperties(
var AssertCls = Components.utils.import("resource://testing-common/Assert.jsm", null).Assert;
// Pass a custom report function for xpcshell-test style reporting.
var Assert = new AssertCls(function(err, message, stack) {
if (err) {
do_report_result(false, err.message, err.stack);
} else {
do_report_result(true, message, stack);
}
-});
+}, true);
var _add_params = function(params) {
if (typeof _XPCSHELL_PROCESS != "undefined") {
params.xpcshell_process = _XPCSHELL_PROCESS;
}
};
var _dumpLog = function(raw_msg) {
@@ -505,17 +505,16 @@ function _execute_test() {
_register_protocol_handlers();
// Override idle service by default.
// Call do_get_idle() to restore the factory and get the service.
_fakeIdleService.activate();
_PromiseTestUtils.init();
- _PromiseTestUtils.Assert = Assert;
let coverageCollector = null;
if (typeof _JSCOV_DIR === "string") {
let _CoverageCollector = Components.utils.import("resource://testing-common/CoverageUtils.jsm", {}).CoverageCollector;
coverageCollector = new _CoverageCollector(_JSCOV_DIR);
}
// _HEAD_FILES is dynamically defined by <runxpcshelltests.py>.
--- a/toolkit/components/passwordmgr/test/LoginTestUtils.jsm
+++ b/toolkit/components/passwordmgr/test/LoginTestUtils.jsm
@@ -11,30 +11,24 @@ this.EXPORTED_SYMBOLS = [
"LoginTestUtils",
];
const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+Cu.import("resource://testing-common/Assert.jsm");
Cu.import("resource://testing-common/TestUtils.jsm");
const LoginInfo =
Components.Constructor("@mozilla.org/login-manager/loginInfo;1",
"nsILoginInfo", "init");
-// For now, we need consumers to provide a reference to Assert.jsm.
-var Assert = null;
-
this.LoginTestUtils = {
- set Assert(assert) {
- Assert = assert; // eslint-disable-line no-native-reassign
- },
-
/**
* Forces the storage module to save all data, and the Login Manager service
* to replace the storage module with a newly initialized instance.
*/
async reloadData() {
Services.obs.notifyObservers(null, "passwordmgr-storage-replace");
await TestUtils.topicObserved("passwordmgr-storage-replace-complete");
},
--- a/toolkit/components/passwordmgr/test/unit/head.js
+++ b/toolkit/components/passwordmgr/test/unit/head.js
@@ -7,33 +7,30 @@
// Globals
let { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/LoginRecipes.jsm");
Cu.import("resource://gre/modules/LoginHelper.jsm");
+Cu.import("resource://testing-common/LoginTestUtils.jsm");
Cu.import("resource://testing-common/MockDocument.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "DownloadPaths",
"resource://gre/modules/DownloadPaths.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
"resource://gre/modules/FileUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "OS",
"resource://gre/modules/osfile.jsm");
const LoginInfo =
Components.Constructor("@mozilla.org/login-manager/loginInfo;1",
"nsILoginInfo", "init");
-// Import LoginTestUtils.jsm as LoginTestUtils.
-XPCOMUtils.defineLazyModuleGetter(this, "LoginTestUtils",
- "resource://testing-common/LoginTestUtils.jsm");
-LoginTestUtils.Assert = Assert;
const TestData = LoginTestUtils.testData;
const newPropertyBag = LoginHelper.newPropertyBag;
/**
* All the tests are implemented with add_task, this starts them automatically.
*/
function run_test()
{
--- a/toolkit/modules/tests/modules/PromiseTestUtils.jsm
+++ b/toolkit/modules/tests/modules/PromiseTestUtils.jsm
@@ -1,35 +1,30 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
/*
* Detects and reports unhandled rejections during test runs. Test harnesses
* will fail tests in this case, unless the test whitelists itself.
*/
-// For this test we allow the global Assert to be modified.
-/* global Assert:true */
-
"use strict";
this.EXPORTED_SYMBOLS = [
"PromiseTestUtils",
];
const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
Cu.import("resource://gre/modules/Services.jsm", this);
+Cu.import("resource://testing-common/Assert.jsm", this);
// Keep "JSMPromise" separate so "Promise" still refers to DOM Promises.
let JSMPromise = Cu.import("resource://gre/modules/Promise.jsm", {}).Promise;
-// For now, we need test harnesses to provide a reference to Assert.jsm.
-let Assert = null;
-
this.PromiseTestUtils = {
/**
* Array of objects containing the details of the Promise rejections that are
* currently left uncaught. This includes DOM Promise and Promise.jsm. When
* rejections in DOM Promises are consumed, they are removed from this list.
*
* The objects contain at least the following properties:
* {
@@ -131,23 +126,16 @@ this.PromiseTestUtils = {
* Called by tests that have been whitelisted, disables the observers in this
* module. For new tests where uncaught rejections are expected, you should
* use the more granular expectUncaughtRejection function instead.
*/
thisTestLeaksUncaughtRejectionsAndShouldBeFixed() {
this.uninit();
},
- /**
- * Sets or updates the Assert object instance to be used for error reporting.
- */
- set Assert(assert) {
- Assert = assert;
- },
-
// UncaughtRejectionObserver
onLeftUncaught(promise) {
let message = "(Unable to convert rejection reason to string.)";
try {
let reason = PromiseDebugging.getState(promise).reason;
if (reason === this._ensureDOMPromiseRejectionsProcessedReason) {
// Ignore the special promise for ensureDOMPromiseRejectionsProcessed.
return;
--- a/uriloader/exthandler/tests/HandlerServiceTestUtils.jsm
+++ b/uriloader/exthandler/tests/HandlerServiceTestUtils.jsm
@@ -11,32 +11,26 @@ this.EXPORTED_SYMBOLS = [
"HandlerServiceTestUtils",
];
const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
Cu.import("resource://gre/modules/AppConstants.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+Cu.import("resource://testing-common/Assert.jsm");
XPCOMUtils.defineLazyServiceGetter(this, "gExternalProtocolService",
"@mozilla.org/uriloader/external-protocol-service;1",
"nsIExternalProtocolService");
XPCOMUtils.defineLazyServiceGetter(this, "gMIMEService",
"@mozilla.org/mime;1",
"nsIMIMEService");
-// For now, we need consumers to provide a reference to Assert.jsm.
-var Assert = null;
-
this.HandlerServiceTestUtils = {
- set Assert(assert) {
- Assert = assert; // eslint-disable-line no-native-reassign
- },
-
/**
* This has to be initialized to the nsIHandlerService instance under testing.
*
* When support for migration from the RDF to the JSON back-end is removed,
* this can be replaced by a lazy getter for the default implementation.
*/
handlerService: null,
--- a/uriloader/exthandler/tests/unit/head.js
+++ b/uriloader/exthandler/tests/unit/head.js
@@ -22,18 +22,16 @@ Cu.import("resource://testing-common/Tes
XPCOMUtils.defineLazyServiceGetter(this, "gHandlerServiceJSON",
"@mozilla.org/uriloader/handler-service;1",
"nsIHandlerService");
XPCOMUtils.defineLazyServiceGetter(this, "gHandlerServiceRDF",
"@mozilla.org/uriloader/handler-service-rdf;1",
"nsIHandlerService");
-HandlerServiceTestUtils.Assert = Assert;
-
do_get_profile();
let jsonPath = OS.Path.join(OS.Constants.Path.profileDir, "handlers.json");
let rdfFile = FileUtils.getFile("ProfD", ["mimeTypes.rdf"]);
/**
* Unloads the nsIHandlerService data store, so the back-end file can be
* accessed or modified, and the new data will be loaded at the next access.