michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: */ michael@0: michael@0: const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; michael@0: michael@0: Cu.import("resource://gre/modules/Promise.jsm"); michael@0: michael@0: const gIsWindows = ("@mozilla.org/windows-registry-key;1" in Cc); michael@0: const gIsOSX = ("nsILocalFileMac" in Ci); michael@0: const gIsLinux = ("@mozilla.org/gnome-gconf-service;1" in Cc) || michael@0: ("@mozilla.org/gio-service;1" in Cc); michael@0: const gDirSvc = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties); michael@0: michael@0: // Finds the test plugin library michael@0: function get_test_plugin(secondplugin=false) { michael@0: var pluginEnum = gDirSvc.get("APluginsDL", Ci.nsISimpleEnumerator); michael@0: while (pluginEnum.hasMoreElements()) { michael@0: let dir = pluginEnum.getNext().QueryInterface(Ci.nsILocalFile); michael@0: let name = get_platform_specific_plugin_name(secondplugin); michael@0: let plugin = dir.clone(); michael@0: plugin.append(name); michael@0: if (plugin.exists()) { michael@0: plugin.normalize(); michael@0: return plugin; michael@0: } michael@0: } michael@0: return null; michael@0: } michael@0: michael@0: // Finds the test nsIPluginTag michael@0: function get_test_plugintag(aName="Test Plug-in") { michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: michael@0: var name = aName || "Test Plug-in"; michael@0: var host = Cc["@mozilla.org/plugin/host;1"]. michael@0: getService(Ci.nsIPluginHost); michael@0: var tags = host.getPluginTags(); michael@0: michael@0: for (var i = 0; i < tags.length; i++) { michael@0: if (tags[i].name == name) michael@0: return tags[i]; michael@0: } michael@0: return null; michael@0: } michael@0: michael@0: // Creates a fake ProfDS directory key, copied from do_get_profile michael@0: function do_get_profile_startup() { michael@0: let env = Components.classes["@mozilla.org/process/environment;1"] michael@0: .getService(Components.interfaces.nsIEnvironment); michael@0: // the python harness sets this in the environment for us michael@0: let profd = env.get("XPCSHELL_TEST_PROFILE_DIR"); michael@0: let file = Components.classes["@mozilla.org/file/local;1"] michael@0: .createInstance(Components.interfaces.nsILocalFile); michael@0: file.initWithPath(profd); michael@0: michael@0: let dirSvc = Components.classes["@mozilla.org/file/directory_service;1"] michael@0: .getService(Components.interfaces.nsIProperties); michael@0: let provider = { michael@0: getFile: function(prop, persistent) { michael@0: persistent.value = true; michael@0: if (prop == "ProfDS") { michael@0: return file.clone(); michael@0: } michael@0: throw Components.results.NS_ERROR_FAILURE; michael@0: }, michael@0: QueryInterface: function(iid) { michael@0: if (iid.equals(Components.interfaces.nsIDirectoryServiceProvider) || michael@0: iid.equals(Components.interfaces.nsISupports)) { michael@0: return this; michael@0: } michael@0: throw Components.results.NS_ERROR_NO_INTERFACE; michael@0: } michael@0: }; michael@0: dirSvc.QueryInterface(Components.interfaces.nsIDirectoryService) michael@0: .registerProvider(provider); michael@0: return file.clone(); michael@0: } michael@0: michael@0: function get_platform_specific_plugin_name(secondplugin=false) { michael@0: if (secondplugin) { michael@0: if (gIsWindows) return "npsecondtest.dll"; michael@0: if (gIsOSX) return "SecondTest.plugin"; michael@0: if (gIsLinux) return "libnpsecondtest.so"; michael@0: } else { michael@0: if (gIsWindows) return "nptest.dll"; michael@0: if (gIsOSX) return "Test.plugin"; michael@0: if (gIsLinux) return "libnptest.so"; michael@0: } michael@0: return null; michael@0: } michael@0: michael@0: function get_platform_specific_plugin_suffix() { michael@0: if (gIsWindows) return ".dll"; michael@0: else if (gIsOSX) return ".plugin"; michael@0: else if (gIsLinux) return ".so"; michael@0: else return null; michael@0: } michael@0: michael@0: function get_test_plugin_no_symlink() { michael@0: let dirSvc = Cc["@mozilla.org/file/directory_service;1"] michael@0: .getService(Ci.nsIProperties); michael@0: let pluginEnum = dirSvc.get("APluginsDL", Ci.nsISimpleEnumerator); michael@0: while (pluginEnum.hasMoreElements()) { michael@0: let dir = pluginEnum.getNext().QueryInterface(Ci.nsILocalFile); michael@0: let plugin = dir.clone(); michael@0: plugin.append(get_platform_specific_plugin_name()); michael@0: if (plugin.exists()) { michael@0: return plugin; michael@0: } michael@0: } michael@0: return null; michael@0: } michael@0: michael@0: let gGlobalScope = this; michael@0: function loadAddonManager() { michael@0: let ns = {}; michael@0: Cu.import("resource://gre/modules/Services.jsm", ns); michael@0: let head = "../../../../toolkit/mozapps/extensions/test/xpcshell/head_addons.js"; michael@0: let file = do_get_file(head); michael@0: let uri = ns.Services.io.newFileURI(file); michael@0: ns.Services.scriptloader.loadSubScript(uri.spec, gGlobalScope); michael@0: createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2"); michael@0: startupManager(); michael@0: } michael@0: michael@0: // Install addon and return a Promise that is michael@0: // resolve with true on success, false otherwise. michael@0: function installAddon(relativePath) { michael@0: let deferred = Promise.defer(); michael@0: let success = () => deferred.resolve(true); michael@0: let fail = () => deferred.resolve(false); michael@0: let listener = { michael@0: onDownloadCancelled: fail, michael@0: onDownloadFailed: fail, michael@0: onInstallCancelled: fail, michael@0: onInstallFailed: fail, michael@0: onInstallEnded: success, michael@0: }; michael@0: michael@0: let installCallback = install => { michael@0: install.addListener(listener); michael@0: install.install(); michael@0: }; michael@0: michael@0: let file = do_get_file(relativePath, false); michael@0: AddonManager.getInstallForFile(file, installCallback, michael@0: "application/x-xpinstall"); michael@0: michael@0: return deferred.promise; michael@0: } michael@0: michael@0: // Uninstall addon and return a Promise that is michael@0: // resolve with true on success, false otherwise. michael@0: function uninstallAddon(id) { michael@0: let deferred = Promise.defer(); michael@0: michael@0: AddonManager.getAddonByID(id, addon => { michael@0: if (!addon) { michael@0: deferred.resolve(false); michael@0: } michael@0: michael@0: let listener = {}; michael@0: let handler = addon => { michael@0: if (addon.id !== id) { michael@0: return; michael@0: } michael@0: michael@0: AddonManager.removeAddonListener(listener); michael@0: deferred.resolve(true); michael@0: }; michael@0: michael@0: listener.onUninstalled = handler; michael@0: listener.onDisabled = handler; michael@0: michael@0: AddonManager.addAddonListener(listener); michael@0: addon.uninstall(); michael@0: }); michael@0: michael@0: return deferred.promise; michael@0: } michael@0: michael@0: // Returns a Promise that is resolved with michael@0: // the corresponding addon or rejected. michael@0: function getAddonByID(id) { michael@0: let deferred = Promise.defer(); michael@0: AddonManager.getAddonByID(id, addon => deferred.resolve(addon)); michael@0: return deferred.promise; michael@0: }