1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/xpconnect/tests/unit/test_import.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,91 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +function run_test() { 1.9 + var scope = {}; 1.10 + Components.utils.import("resource://gre/modules/XPCOMUtils.jsm", scope); 1.11 + do_check_eq(typeof(scope.XPCOMUtils), "object"); 1.12 + do_check_eq(typeof(scope.XPCOMUtils.generateNSGetFactory), "function"); 1.13 + 1.14 + // access module's global object directly without importing any 1.15 + // symbols 1.16 + var module = Components.utils.import("resource://gre/modules/XPCOMUtils.jsm", 1.17 + null); 1.18 + do_check_eq(typeof(XPCOMUtils), "undefined"); 1.19 + do_check_eq(typeof(module), "object"); 1.20 + do_check_eq(typeof(module.XPCOMUtils), "object"); 1.21 + do_check_eq(typeof(module.XPCOMUtils.generateNSGetFactory), "function"); 1.22 + do_check_true(scope.XPCOMUtils == module.XPCOMUtils); 1.23 + 1.24 + // import symbols to our global object 1.25 + do_check_eq(typeof(Components.utils.import), "function"); 1.26 + Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); 1.27 + do_check_eq(typeof(XPCOMUtils), "object"); 1.28 + do_check_eq(typeof(XPCOMUtils.generateNSGetFactory), "function"); 1.29 + 1.30 + // try on a new object 1.31 + var scope2 = {}; 1.32 + Components.utils.import("resource://gre/modules/XPCOMUtils.jsm", scope2); 1.33 + do_check_eq(typeof(scope2.XPCOMUtils), "object"); 1.34 + do_check_eq(typeof(scope2.XPCOMUtils.generateNSGetFactory), "function"); 1.35 + 1.36 + do_check_true(scope2.XPCOMUtils == scope.XPCOMUtils); 1.37 + 1.38 + // try on a new object using the resolved URL 1.39 + var res = Components.classes["@mozilla.org/network/protocol;1?name=resource"] 1.40 + .getService(Components.interfaces.nsIResProtocolHandler); 1.41 + var resURI = res.newURI("resource://gre/modules/XPCOMUtils.jsm", null, null); 1.42 + dump("resURI: " + resURI + "\n"); 1.43 + var filePath = res.resolveURI(resURI); 1.44 + var scope3 = {}; 1.45 + Components.utils.import(filePath, scope3); 1.46 + do_check_eq(typeof(scope3.XPCOMUtils), "object"); 1.47 + do_check_eq(typeof(scope3.XPCOMUtils.generateNSGetFactory), "function"); 1.48 + 1.49 + do_check_true(scope3.XPCOMUtils == scope.XPCOMUtils); 1.50 + 1.51 + // make sure we throw when the second arg is bogus 1.52 + var didThrow = false; 1.53 + try { 1.54 + Components.utils.import("resource://gre/modules/XPCOMUtils.jsm", "wrong"); 1.55 + } catch (ex) { 1.56 + print("exception (expected): " + ex); 1.57 + didThrow = true; 1.58 + } 1.59 + do_check_true(didThrow); 1.60 + 1.61 + // try to create a component 1.62 + do_load_manifest("component_import.manifest"); 1.63 + const contractID = "@mozilla.org/tests/module-importer;"; 1.64 + do_check_true((contractID + "1") in Components.classes); 1.65 + var foo = Components.classes[contractID + "1"] 1.66 + .createInstance(Components.interfaces.nsIClassInfo); 1.67 + do_check_true(Boolean(foo)); 1.68 + do_check_true(foo.contractID == contractID + "1"); 1.69 + // XXX the following check succeeds only if the test component wasn't 1.70 + // already registered. Need to figure out a way to force registration 1.71 + // (to manually force it, delete compreg.dat before running the test) 1.72 + // do_check_true(foo.wrappedJSObject.postRegisterCalled); 1.73 + 1.74 + // Call getInterfaces to test line numbers in JS components. But as long as 1.75 + // we're doing that, why not test what it returns too? 1.76 + // Kind of odd that this is not returning an array containing the 1.77 + // number... Or for that matter not returning an array containing an object? 1.78 + var interfaces = foo.getInterfaces({}); 1.79 + do_check_eq(interfaces, Components.interfaces.nsIClassInfo.number); 1.80 + 1.81 + // try to create a component by CID 1.82 + const cid = "{6b933fe6-6eba-4622-ac86-e4f654f1b474}"; 1.83 + do_check_true(cid in Components.classesByID); 1.84 + foo = Components.classesByID[cid] 1.85 + .createInstance(Components.interfaces.nsIClassInfo); 1.86 + do_check_true(foo.contractID == contractID + "1"); 1.87 + 1.88 + // try to create another component which doesn't directly implement QI 1.89 + do_check_true((contractID + "2") in Components.classes); 1.90 + var bar = Components.classes[contractID + "2"] 1.91 + .createInstance(Components.interfaces.nsIClassInfo); 1.92 + do_check_true(Boolean(bar)); 1.93 + do_check_true(bar.contractID == contractID + "2"); 1.94 +}