|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 function run_test() { |
|
6 var scope = {}; |
|
7 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm", scope); |
|
8 do_check_eq(typeof(scope.XPCOMUtils), "object"); |
|
9 do_check_eq(typeof(scope.XPCOMUtils.generateNSGetFactory), "function"); |
|
10 |
|
11 // access module's global object directly without importing any |
|
12 // symbols |
|
13 var module = Components.utils.import("resource://gre/modules/XPCOMUtils.jsm", |
|
14 null); |
|
15 do_check_eq(typeof(XPCOMUtils), "undefined"); |
|
16 do_check_eq(typeof(module), "object"); |
|
17 do_check_eq(typeof(module.XPCOMUtils), "object"); |
|
18 do_check_eq(typeof(module.XPCOMUtils.generateNSGetFactory), "function"); |
|
19 do_check_true(scope.XPCOMUtils == module.XPCOMUtils); |
|
20 |
|
21 // import symbols to our global object |
|
22 do_check_eq(typeof(Components.utils.import), "function"); |
|
23 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
24 do_check_eq(typeof(XPCOMUtils), "object"); |
|
25 do_check_eq(typeof(XPCOMUtils.generateNSGetFactory), "function"); |
|
26 |
|
27 // try on a new object |
|
28 var scope2 = {}; |
|
29 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm", scope2); |
|
30 do_check_eq(typeof(scope2.XPCOMUtils), "object"); |
|
31 do_check_eq(typeof(scope2.XPCOMUtils.generateNSGetFactory), "function"); |
|
32 |
|
33 do_check_true(scope2.XPCOMUtils == scope.XPCOMUtils); |
|
34 |
|
35 // try on a new object using the resolved URL |
|
36 var res = Components.classes["@mozilla.org/network/protocol;1?name=resource"] |
|
37 .getService(Components.interfaces.nsIResProtocolHandler); |
|
38 var resURI = res.newURI("resource://gre/modules/XPCOMUtils.jsm", null, null); |
|
39 dump("resURI: " + resURI + "\n"); |
|
40 var filePath = res.resolveURI(resURI); |
|
41 var scope3 = {}; |
|
42 Components.utils.import(filePath, scope3); |
|
43 do_check_eq(typeof(scope3.XPCOMUtils), "object"); |
|
44 do_check_eq(typeof(scope3.XPCOMUtils.generateNSGetFactory), "function"); |
|
45 |
|
46 do_check_true(scope3.XPCOMUtils == scope.XPCOMUtils); |
|
47 |
|
48 // make sure we throw when the second arg is bogus |
|
49 var didThrow = false; |
|
50 try { |
|
51 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm", "wrong"); |
|
52 } catch (ex) { |
|
53 print("exception (expected): " + ex); |
|
54 didThrow = true; |
|
55 } |
|
56 do_check_true(didThrow); |
|
57 |
|
58 // try to create a component |
|
59 do_load_manifest("component_import.manifest"); |
|
60 const contractID = "@mozilla.org/tests/module-importer;"; |
|
61 do_check_true((contractID + "1") in Components.classes); |
|
62 var foo = Components.classes[contractID + "1"] |
|
63 .createInstance(Components.interfaces.nsIClassInfo); |
|
64 do_check_true(Boolean(foo)); |
|
65 do_check_true(foo.contractID == contractID + "1"); |
|
66 // XXX the following check succeeds only if the test component wasn't |
|
67 // already registered. Need to figure out a way to force registration |
|
68 // (to manually force it, delete compreg.dat before running the test) |
|
69 // do_check_true(foo.wrappedJSObject.postRegisterCalled); |
|
70 |
|
71 // Call getInterfaces to test line numbers in JS components. But as long as |
|
72 // we're doing that, why not test what it returns too? |
|
73 // Kind of odd that this is not returning an array containing the |
|
74 // number... Or for that matter not returning an array containing an object? |
|
75 var interfaces = foo.getInterfaces({}); |
|
76 do_check_eq(interfaces, Components.interfaces.nsIClassInfo.number); |
|
77 |
|
78 // try to create a component by CID |
|
79 const cid = "{6b933fe6-6eba-4622-ac86-e4f654f1b474}"; |
|
80 do_check_true(cid in Components.classesByID); |
|
81 foo = Components.classesByID[cid] |
|
82 .createInstance(Components.interfaces.nsIClassInfo); |
|
83 do_check_true(foo.contractID == contractID + "1"); |
|
84 |
|
85 // try to create another component which doesn't directly implement QI |
|
86 do_check_true((contractID + "2") in Components.classes); |
|
87 var bar = Components.classes[contractID + "2"] |
|
88 .createInstance(Components.interfaces.nsIClassInfo); |
|
89 do_check_true(Boolean(bar)); |
|
90 do_check_true(bar.contractID == contractID + "2"); |
|
91 } |