dom/plugins/test/unit/head_plugins.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/plugins/test/unit/head_plugins.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,191 @@
     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 +
     1.9 +const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
    1.10 +
    1.11 +Cu.import("resource://gre/modules/Promise.jsm");
    1.12 +
    1.13 +const gIsWindows = ("@mozilla.org/windows-registry-key;1" in Cc);
    1.14 +const gIsOSX = ("nsILocalFileMac" in Ci);
    1.15 +const gIsLinux = ("@mozilla.org/gnome-gconf-service;1" in Cc) ||
    1.16 +  ("@mozilla.org/gio-service;1" in Cc);
    1.17 +const gDirSvc = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
    1.18 +
    1.19 +// Finds the test plugin library
    1.20 +function get_test_plugin(secondplugin=false) {
    1.21 +  var pluginEnum = gDirSvc.get("APluginsDL", Ci.nsISimpleEnumerator);
    1.22 +  while (pluginEnum.hasMoreElements()) {
    1.23 +    let dir = pluginEnum.getNext().QueryInterface(Ci.nsILocalFile);
    1.24 +    let name = get_platform_specific_plugin_name(secondplugin);
    1.25 +    let plugin = dir.clone();
    1.26 +    plugin.append(name);
    1.27 +    if (plugin.exists()) {
    1.28 +      plugin.normalize();
    1.29 +      return plugin;
    1.30 +    }
    1.31 +  }
    1.32 +  return null;
    1.33 +}
    1.34 +
    1.35 +// Finds the test nsIPluginTag
    1.36 +function get_test_plugintag(aName="Test Plug-in") {
    1.37 +  const Cc = Components.classes;
    1.38 +  const Ci = Components.interfaces;
    1.39 +
    1.40 +  var name = aName || "Test Plug-in";
    1.41 +  var host = Cc["@mozilla.org/plugin/host;1"].
    1.42 +             getService(Ci.nsIPluginHost);
    1.43 +  var tags = host.getPluginTags();
    1.44 +
    1.45 +  for (var i = 0; i < tags.length; i++) {
    1.46 +    if (tags[i].name == name)
    1.47 +      return tags[i];
    1.48 +  }
    1.49 +  return null;
    1.50 +}
    1.51 +
    1.52 +// Creates a fake ProfDS directory key, copied from do_get_profile
    1.53 +function do_get_profile_startup() {
    1.54 +  let env = Components.classes["@mozilla.org/process/environment;1"]
    1.55 +                      .getService(Components.interfaces.nsIEnvironment);
    1.56 +  // the python harness sets this in the environment for us
    1.57 +  let profd = env.get("XPCSHELL_TEST_PROFILE_DIR");
    1.58 +  let file = Components.classes["@mozilla.org/file/local;1"]
    1.59 +                       .createInstance(Components.interfaces.nsILocalFile);
    1.60 +  file.initWithPath(profd);
    1.61 +
    1.62 +  let dirSvc = Components.classes["@mozilla.org/file/directory_service;1"]
    1.63 +                         .getService(Components.interfaces.nsIProperties);
    1.64 +  let provider = {
    1.65 +    getFile: function(prop, persistent) {
    1.66 +      persistent.value = true;
    1.67 +      if (prop == "ProfDS") {
    1.68 +        return file.clone();
    1.69 +      }
    1.70 +      throw Components.results.NS_ERROR_FAILURE;
    1.71 +    },
    1.72 +    QueryInterface: function(iid) {
    1.73 +      if (iid.equals(Components.interfaces.nsIDirectoryServiceProvider) ||
    1.74 +          iid.equals(Components.interfaces.nsISupports)) {
    1.75 +        return this;
    1.76 +      }
    1.77 +      throw Components.results.NS_ERROR_NO_INTERFACE;
    1.78 +    }
    1.79 +  };
    1.80 +  dirSvc.QueryInterface(Components.interfaces.nsIDirectoryService)
    1.81 +        .registerProvider(provider);
    1.82 +  return file.clone();
    1.83 +}
    1.84 +
    1.85 +function get_platform_specific_plugin_name(secondplugin=false) {
    1.86 +  if (secondplugin) {
    1.87 +    if (gIsWindows) return "npsecondtest.dll";
    1.88 +    if (gIsOSX) return "SecondTest.plugin";
    1.89 +    if (gIsLinux) return "libnpsecondtest.so";
    1.90 +  } else {
    1.91 +    if (gIsWindows) return "nptest.dll";
    1.92 +    if (gIsOSX) return "Test.plugin";
    1.93 +    if (gIsLinux) return "libnptest.so";
    1.94 +  }
    1.95 +  return null;
    1.96 +}
    1.97 +
    1.98 +function get_platform_specific_plugin_suffix() {
    1.99 +  if (gIsWindows) return ".dll";
   1.100 +  else if (gIsOSX) return ".plugin";
   1.101 +  else if (gIsLinux) return ".so";
   1.102 +  else return null;
   1.103 +}
   1.104 +
   1.105 +function get_test_plugin_no_symlink() {
   1.106 +  let dirSvc = Cc["@mozilla.org/file/directory_service;1"]
   1.107 +                .getService(Ci.nsIProperties);
   1.108 +  let pluginEnum = dirSvc.get("APluginsDL", Ci.nsISimpleEnumerator);
   1.109 +  while (pluginEnum.hasMoreElements()) {
   1.110 +    let dir = pluginEnum.getNext().QueryInterface(Ci.nsILocalFile);
   1.111 +    let plugin = dir.clone();
   1.112 +    plugin.append(get_platform_specific_plugin_name());
   1.113 +    if (plugin.exists()) {
   1.114 +      return plugin;
   1.115 +    }
   1.116 +  }
   1.117 +  return null;
   1.118 +}
   1.119 +
   1.120 +let gGlobalScope = this;
   1.121 +function loadAddonManager() {
   1.122 +  let ns = {};
   1.123 +  Cu.import("resource://gre/modules/Services.jsm", ns);
   1.124 +  let head = "../../../../toolkit/mozapps/extensions/test/xpcshell/head_addons.js";
   1.125 +  let file = do_get_file(head);
   1.126 +  let uri = ns.Services.io.newFileURI(file);
   1.127 +  ns.Services.scriptloader.loadSubScript(uri.spec, gGlobalScope);
   1.128 +  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2");
   1.129 +  startupManager();
   1.130 +}
   1.131 +
   1.132 +// Install addon and return a Promise<boolean> that is
   1.133 +// resolve with true on success, false otherwise.
   1.134 +function installAddon(relativePath) {
   1.135 +  let deferred = Promise.defer();
   1.136 +  let success = () => deferred.resolve(true);
   1.137 +  let fail = () => deferred.resolve(false);
   1.138 +  let listener = {
   1.139 +    onDownloadCancelled: fail,
   1.140 +    onDownloadFailed: fail,
   1.141 +    onInstallCancelled: fail,
   1.142 +    onInstallFailed: fail,
   1.143 +    onInstallEnded: success,
   1.144 +  };
   1.145 +
   1.146 +  let installCallback = install => {
   1.147 +    install.addListener(listener);
   1.148 +    install.install();
   1.149 +  };
   1.150 +
   1.151 +  let file = do_get_file(relativePath, false);
   1.152 +  AddonManager.getInstallForFile(file, installCallback,
   1.153 +                                 "application/x-xpinstall");
   1.154 +
   1.155 +  return deferred.promise;
   1.156 +}
   1.157 +
   1.158 +// Uninstall addon and return a Promise<boolean> that is
   1.159 +// resolve with true on success, false otherwise.
   1.160 +function uninstallAddon(id) {
   1.161 +  let deferred = Promise.defer();
   1.162 +
   1.163 +  AddonManager.getAddonByID(id, addon => {
   1.164 +    if (!addon) {
   1.165 +      deferred.resolve(false);
   1.166 +    }
   1.167 +
   1.168 +    let listener = {};
   1.169 +    let handler = addon => {
   1.170 +      if (addon.id !== id) {
   1.171 +        return;
   1.172 +      }
   1.173 +
   1.174 +      AddonManager.removeAddonListener(listener);
   1.175 +      deferred.resolve(true);
   1.176 +    };
   1.177 +
   1.178 +    listener.onUninstalled = handler;
   1.179 +    listener.onDisabled = handler;
   1.180 +
   1.181 +    AddonManager.addAddonListener(listener);
   1.182 +    addon.uninstall();
   1.183 +  });
   1.184 +
   1.185 +  return deferred.promise;
   1.186 +}
   1.187 +
   1.188 +// Returns a Promise<Addon> that is resolved with
   1.189 +// the corresponding addon or rejected.
   1.190 +function getAddonByID(id) {
   1.191 +  let deferred = Promise.defer();
   1.192 +  AddonManager.getAddonByID(id, addon => deferred.resolve(addon));
   1.193 +  return deferred.promise;
   1.194 +}

mercurial