addon-sdk/test/head.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/test/head.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,79 @@
     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 +const { utils: Cu } = Components;
     1.9 +const { Services } = Cu.import("resource://gre/modules/Services.jsm", {});
    1.10 +const LoaderModule = Cu.import("resource://gre/modules/commonjs/toolkit/loader.js", {}).Loader;
    1.11 +const { console } = Cu.import("resource://gre/modules/devtools/Console.jsm", {});
    1.12 +let {
    1.13 +  Loader, main, Module, Require, unload
    1.14 +} = LoaderModule;
    1.15 +
    1.16 +let CURRENT_DIR = gTestPath.replace(/\/[^\/]*\.js$/,'/');
    1.17 +let loaders = [];
    1.18 +
    1.19 +// All tests are asynchronous.
    1.20 +waitForExplicitFinish();
    1.21 +
    1.22 +let gEnableLogging = Services.prefs.getBoolPref("devtools.debugger.log");
    1.23 +Services.prefs.setBoolPref("devtools.debugger.log", true);
    1.24 +
    1.25 +registerCleanupFunction(() => {
    1.26 +  info("finish() was called, cleaning up...");
    1.27 +  loaders.forEach(unload);
    1.28 +  Services.prefs.setBoolPref("devtools.debugger.log", gEnableLogging);
    1.29 +});
    1.30 +
    1.31 +function makePaths (root) {
    1.32 +  return {
    1.33 +    './': CURRENT_DIR,
    1.34 +    '': 'resource://gre/modules/commonjs/'
    1.35 +  };
    1.36 +}
    1.37 +
    1.38 +function makeLoader (options) {
    1.39 +  let { paths, globals } = options || {};
    1.40 +
    1.41 +  // We have to have `console` as a global, otherwise
    1.42 +  // many SDK modules will fail
    1.43 +  // bug 961252
    1.44 +  let globalDefaults = {
    1.45 +    console: console
    1.46 +  };
    1.47 +
    1.48 +  let loader = Loader({
    1.49 +    paths: paths || makePaths(),
    1.50 +    globals: extend({}, globalDefaults, globals) || null,
    1.51 +    modules: {
    1.52 +      // Needed because sdk/ modules reference utilities in
    1.53 +      // `toolkit/loader`, until Bug 961194 is completed
    1.54 +      'toolkit/loader': LoaderModule
    1.55 +    },
    1.56 +    // We need rootURI due to `sdk/self` (or are using native loader)
    1.57 +    // which overloads with pseudo modules
    1.58 +    // bug 961245
    1.59 +    rootURI: CURRENT_DIR,
    1.60 +    // We also need metadata dummy object
    1.61 +    // bug 961245
    1.62 +    metadata: {}
    1.63 +  });
    1.64 +
    1.65 +  loaders.push(loader);
    1.66 +  return loader;
    1.67 +}
    1.68 +
    1.69 +function isUUID (string) {
    1.70 +  return /^\{[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\}$/.test(string);
    1.71 +}
    1.72 +
    1.73 +function extend (...objs) {
    1.74 +  if (objs.length === 0 || objs.length === 1)
    1.75 +    return objs[0] || {};
    1.76 +
    1.77 +  for (let i = objs.length; i > 1; i--) {
    1.78 +    for (var prop in objs[i - 1])
    1.79 +      objs[0][prop] = objs[i - 1][prop];
    1.80 +  }
    1.81 +  return objs[0];
    1.82 +}

mercurial