Esempio n. 1
0
exports.loader = function(testModule, subjectPath, modules) {
  const loaderOptions = require('@loader/options');

  // Get a handle on the original resolver created by default in Loader.
  const realResolve = loaderOptions.isNative ?
      (id, requirer) => Loader.nodeResolve(id, requirer, {rootURI: loaderOptions.rootURI}) :
      Loader.resolve;

  // Custom resolver that substitutes mock modules, but only when the test
  // subject module is asking for them.
  const mockResolve = (id, requirer) => {
    let resolvedPath = realResolve(id, requirer);
    // Only trigger mock module substitution for the test subject
    if (requirer === subjectPath) {
      // If the real resolver came up with no mapping, use the original ID
      if (!resolvedPath) { resolvedPath = id; }
      // If the path is in our set of mock substitutions, use the prefix.
      if (resolvedPath in modules) {
        resolvedPath = 'mock:' + resolvedPath;
        // Announce the substitution if debug is on.
        if (debug) {
          console.log('Substituting mock', requirer, '<=', id);  // eslint-disable-line no-console
        }
      }
    }
    return resolvedPath;
  };

  // Prefix all the supplied mock modules for conditional injection
  const mockModules = {};
  Object.keys(modules).forEach(path => {
    mockModules['mock:' + path] = modules[path];
  });

  // Build and return the mock module loader
  const loader = Loader(override(  // eslint-disable-line new-cap
    loaderOptions,
    {modules: mockModules, resolve: mockResolve}
  ));

  const mockLoader = Object.create(loader, descriptor({
    require: Require(loader, testModule),  // eslint-disable-line new-cap
    unload: reason => unload(loader, reason)
  }));

  ensure(mockLoader);

  return mockLoader;
};
Esempio n. 2
0
function startup(reason, options) {
  // Try accessing hidden window to guess if we are running during firefox
  // startup, so that we should wait for session restore event before
  // running the addon
  let initialized = false;
  try {
    appShellService.hiddenDOMWindow;
    initialized = true;
  }
  catch(e) {}
  if (reason === 'startup' || !initialized) {
    return wait(reason, options);
  }

  // Inject globals ASAP in order to have console API working ASAP
  Object.defineProperties(options.loader.globals, descriptor(globals));

  // NOTE: Module is intentionally required only now because it relies
  // on existence of hidden window, which does not exists until startup.
  let { ready } = require('../addon/window');
  // Load localization manifest and .properties files.
  // Run the addon even in case of error (best effort approach)
  require('../l10n/loader').
    load(rootURI).
    then(null, function failure(error) {
      console.info("Error while loading localization: " + error.message);
    }).
    then(function onLocalizationReady(data) {
      // Exports data to a pseudo module so that api-utils/l10n/core
      // can get access to it
      definePseudo(options.loader, '@l10n/data', data ? data : null);
      return ready;
    }).then(function() {
      run(options);
    }).then(null, console.exception);
    return void 0; // otherwise we raise a warning, see bug 910304
}