Bug 1777486 - Part 9: Do not use XPCOMUtils in tests for JSM loader. r=kmag

Differential Revision: https://phabricator.services.mozilla.com/D151221
This commit is contained in:
Tooru Fujisawa 2022-07-11 15:09:16 +00:00
parent 6a043ab687
commit 71bd403ecf
2 changed files with 26 additions and 26 deletions

View file

@ -24,19 +24,19 @@ SimpleTest.waitForExplicitFinish();
const Cu = SpecialPowers.Cu;
function doTest() {
msg = "XPCOMUtils should be imported on window";
msg = "AppConstants should be imported on window";
try {
Cu.import("resource://gre/modules/XPCOMUtils.jsm", window);
ok(XPCOMUtils, msg);
Cu.import("resource://gre/modules/AppConstants.jsm", window);
ok(AppConstants, msg);
} catch (ex) {
ok(false, msg + " : " + ex);
}
msg = "XPCOMUtils should be imported on myObj";
msg = "AppConstants should be imported on myObj";
try {
var myObj = {};
Cu.import("resource://gre/modules/XPCOMUtils.jsm", myObj);
ok(myObj.XPCOMUtils, msg);
Cu.import("resource://gre/modules/AppConstants.jsm", myObj);
ok(myObj.AppConstants, msg);
} catch (ex) {
ok(false, msg + " : " + ex);
}

View file

@ -2,48 +2,48 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var XPCOMUtils;
var AppConstants;
function run_test() {
var scope = {};
var exports = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm", scope);
Assert.equal(typeof(scope.XPCOMUtils), "object");
Assert.equal(typeof(scope.XPCOMUtils.defineLazyGetter), "function");
var exports = ChromeUtils.import("resource://gre/modules/AppConstants.jsm", scope);
Assert.equal(typeof(scope.AppConstants), "object");
Assert.equal(typeof(scope.AppConstants.isPlatformAndVersionAtLeast), "function");
equal(scope.XPCOMUtils, exports.XPCOMUtils);
deepEqual(Object.keys(scope), ["XPCOMUtils"]);
deepEqual(Object.keys(exports), ["XPCOMUtils"]);
equal(scope.AppConstants, exports.AppConstants);
deepEqual(Object.keys(scope), ["AppConstants"]);
deepEqual(Object.keys(exports), ["AppConstants"]);
exports = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
equal(scope.XPCOMUtils, exports.XPCOMUtils);
deepEqual(Object.keys(exports), ["XPCOMUtils"]);
exports = ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
equal(scope.AppConstants, exports.AppConstants);
deepEqual(Object.keys(exports), ["AppConstants"]);
// access module's global object directly without importing any
// symbols
Assert.throws(
() => ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm", null),
() => ChromeUtils.import("resource://gre/modules/AppConstants.jsm", null),
TypeError
);
// import symbols to our global object
Assert.equal(typeof(Cu.import), "function");
({XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm"));
Assert.equal(typeof(XPCOMUtils), "object");
Assert.equal(typeof(XPCOMUtils.defineLazyGetter), "function");
({AppConstants} = ChromeUtils.import("resource://gre/modules/AppConstants.jsm"));
Assert.equal(typeof(AppConstants), "object");
Assert.equal(typeof(AppConstants.isPlatformAndVersionAtLeast), "function");
// try on a new object
var scope2 = {};
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm", scope2);
Assert.equal(typeof(scope2.XPCOMUtils), "object");
Assert.equal(typeof(scope2.XPCOMUtils.defineLazyGetter), "function");
ChromeUtils.import("resource://gre/modules/AppConstants.jsm", scope2);
Assert.equal(typeof(scope2.AppConstants), "object");
Assert.equal(typeof(scope2.AppConstants.isPlatformAndVersionAtLeast), "function");
Assert.ok(scope2.XPCOMUtils == scope.XPCOMUtils);
Assert.ok(scope2.AppConstants == scope.AppConstants);
// try on a new object using the resolved URL
var res = Cc["@mozilla.org/network/protocol;1?name=resource"]
.getService(Ci.nsIResProtocolHandler);
var resURI = Cc["@mozilla.org/network/io-service;1"]
.getService(Ci.nsIIOService)
.newURI("resource://gre/modules/XPCOMUtils.jsm");
.newURI("resource://gre/modules/AppConstants.jsm");
dump("resURI: " + resURI + "\n");
var filePath = res.resolveURI(resURI);
var scope3 = {};
@ -55,7 +55,7 @@ function run_test() {
// make sure we throw when the second arg is bogus
var didThrow = false;
try {
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm", "wrong");
ChromeUtils.import("resource://gre/modules/AppConstants.jsm", "wrong");
} catch (ex) {
print("exception (expected): " + ex);
didThrow = true;