|
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 const { utils: Cu } = Components; |
|
6 const { Services } = Cu.import("resource://gre/modules/Services.jsm", {}); |
|
7 const LoaderModule = Cu.import("resource://gre/modules/commonjs/toolkit/loader.js", {}).Loader; |
|
8 const { console } = Cu.import("resource://gre/modules/devtools/Console.jsm", {}); |
|
9 let { |
|
10 Loader, main, Module, Require, unload |
|
11 } = LoaderModule; |
|
12 |
|
13 let CURRENT_DIR = gTestPath.replace(/\/[^\/]*\.js$/,'/'); |
|
14 let loaders = []; |
|
15 |
|
16 // All tests are asynchronous. |
|
17 waitForExplicitFinish(); |
|
18 |
|
19 let gEnableLogging = Services.prefs.getBoolPref("devtools.debugger.log"); |
|
20 Services.prefs.setBoolPref("devtools.debugger.log", true); |
|
21 |
|
22 registerCleanupFunction(() => { |
|
23 info("finish() was called, cleaning up..."); |
|
24 loaders.forEach(unload); |
|
25 Services.prefs.setBoolPref("devtools.debugger.log", gEnableLogging); |
|
26 }); |
|
27 |
|
28 function makePaths (root) { |
|
29 return { |
|
30 './': CURRENT_DIR, |
|
31 '': 'resource://gre/modules/commonjs/' |
|
32 }; |
|
33 } |
|
34 |
|
35 function makeLoader (options) { |
|
36 let { paths, globals } = options || {}; |
|
37 |
|
38 // We have to have `console` as a global, otherwise |
|
39 // many SDK modules will fail |
|
40 // bug 961252 |
|
41 let globalDefaults = { |
|
42 console: console |
|
43 }; |
|
44 |
|
45 let loader = Loader({ |
|
46 paths: paths || makePaths(), |
|
47 globals: extend({}, globalDefaults, globals) || null, |
|
48 modules: { |
|
49 // Needed because sdk/ modules reference utilities in |
|
50 // `toolkit/loader`, until Bug 961194 is completed |
|
51 'toolkit/loader': LoaderModule |
|
52 }, |
|
53 // We need rootURI due to `sdk/self` (or are using native loader) |
|
54 // which overloads with pseudo modules |
|
55 // bug 961245 |
|
56 rootURI: CURRENT_DIR, |
|
57 // We also need metadata dummy object |
|
58 // bug 961245 |
|
59 metadata: {} |
|
60 }); |
|
61 |
|
62 loaders.push(loader); |
|
63 return loader; |
|
64 } |
|
65 |
|
66 function isUUID (string) { |
|
67 return /^\{[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\}$/.test(string); |
|
68 } |
|
69 |
|
70 function extend (...objs) { |
|
71 if (objs.length === 0 || objs.length === 1) |
|
72 return objs[0] || {}; |
|
73 |
|
74 for (let i = objs.length; i > 1; i--) { |
|
75 for (var prop in objs[i - 1]) |
|
76 objs[0][prop] = objs[i - 1][prop]; |
|
77 } |
|
78 return objs[0]; |
|
79 } |