michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: function run_test() { michael@0: var scope = {}; michael@0: Components.utils.import("resource://gre/modules/XPCOMUtils.jsm", scope); michael@0: do_check_eq(typeof(scope.XPCOMUtils), "object"); michael@0: do_check_eq(typeof(scope.XPCOMUtils.generateNSGetFactory), "function"); michael@0: michael@0: // access module's global object directly without importing any michael@0: // symbols michael@0: var module = Components.utils.import("resource://gre/modules/XPCOMUtils.jsm", michael@0: null); michael@0: do_check_eq(typeof(XPCOMUtils), "undefined"); michael@0: do_check_eq(typeof(module), "object"); michael@0: do_check_eq(typeof(module.XPCOMUtils), "object"); michael@0: do_check_eq(typeof(module.XPCOMUtils.generateNSGetFactory), "function"); michael@0: do_check_true(scope.XPCOMUtils == module.XPCOMUtils); michael@0: michael@0: // import symbols to our global object michael@0: do_check_eq(typeof(Components.utils.import), "function"); michael@0: Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: do_check_eq(typeof(XPCOMUtils), "object"); michael@0: do_check_eq(typeof(XPCOMUtils.generateNSGetFactory), "function"); michael@0: michael@0: // try on a new object michael@0: var scope2 = {}; michael@0: Components.utils.import("resource://gre/modules/XPCOMUtils.jsm", scope2); michael@0: do_check_eq(typeof(scope2.XPCOMUtils), "object"); michael@0: do_check_eq(typeof(scope2.XPCOMUtils.generateNSGetFactory), "function"); michael@0: michael@0: do_check_true(scope2.XPCOMUtils == scope.XPCOMUtils); michael@0: michael@0: // try on a new object using the resolved URL michael@0: var res = Components.classes["@mozilla.org/network/protocol;1?name=resource"] michael@0: .getService(Components.interfaces.nsIResProtocolHandler); michael@0: var resURI = res.newURI("resource://gre/modules/XPCOMUtils.jsm", null, null); michael@0: dump("resURI: " + resURI + "\n"); michael@0: var filePath = res.resolveURI(resURI); michael@0: var scope3 = {}; michael@0: Components.utils.import(filePath, scope3); michael@0: do_check_eq(typeof(scope3.XPCOMUtils), "object"); michael@0: do_check_eq(typeof(scope3.XPCOMUtils.generateNSGetFactory), "function"); michael@0: michael@0: do_check_true(scope3.XPCOMUtils == scope.XPCOMUtils); michael@0: michael@0: // make sure we throw when the second arg is bogus michael@0: var didThrow = false; michael@0: try { michael@0: Components.utils.import("resource://gre/modules/XPCOMUtils.jsm", "wrong"); michael@0: } catch (ex) { michael@0: print("exception (expected): " + ex); michael@0: didThrow = true; michael@0: } michael@0: do_check_true(didThrow); michael@0: michael@0: // try to create a component michael@0: do_load_manifest("component_import.manifest"); michael@0: const contractID = "@mozilla.org/tests/module-importer;"; michael@0: do_check_true((contractID + "1") in Components.classes); michael@0: var foo = Components.classes[contractID + "1"] michael@0: .createInstance(Components.interfaces.nsIClassInfo); michael@0: do_check_true(Boolean(foo)); michael@0: do_check_true(foo.contractID == contractID + "1"); michael@0: // XXX the following check succeeds only if the test component wasn't michael@0: // already registered. Need to figure out a way to force registration michael@0: // (to manually force it, delete compreg.dat before running the test) michael@0: // do_check_true(foo.wrappedJSObject.postRegisterCalled); michael@0: michael@0: // Call getInterfaces to test line numbers in JS components. But as long as michael@0: // we're doing that, why not test what it returns too? michael@0: // Kind of odd that this is not returning an array containing the michael@0: // number... Or for that matter not returning an array containing an object? michael@0: var interfaces = foo.getInterfaces({}); michael@0: do_check_eq(interfaces, Components.interfaces.nsIClassInfo.number); michael@0: michael@0: // try to create a component by CID michael@0: const cid = "{6b933fe6-6eba-4622-ac86-e4f654f1b474}"; michael@0: do_check_true(cid in Components.classesByID); michael@0: foo = Components.classesByID[cid] michael@0: .createInstance(Components.interfaces.nsIClassInfo); michael@0: do_check_true(foo.contractID == contractID + "1"); michael@0: michael@0: // try to create another component which doesn't directly implement QI michael@0: do_check_true((contractID + "2") in Components.classes); michael@0: var bar = Components.classes[contractID + "2"] michael@0: .createInstance(Components.interfaces.nsIClassInfo); michael@0: do_check_true(Boolean(bar)); michael@0: do_check_true(bar.contractID == contractID + "2"); michael@0: }