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: // This file is a test-only JSM containing utility methods for michael@0: // interacting with the add-ons manager. michael@0: michael@0: "use strict"; michael@0: michael@0: this.EXPORTED_SYMBOLS = [ michael@0: "AddonTestUtils", michael@0: ]; michael@0: michael@0: const {utils: Cu} = Components; michael@0: michael@0: Cu.import("resource://gre/modules/Promise.jsm"); michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "AddonManager", michael@0: "resource://gre/modules/AddonManager.jsm"); michael@0: michael@0: this.AddonTestUtils = { michael@0: /** michael@0: * Uninstall an add-on that is specified by its ID. michael@0: * michael@0: * The returned promise resolves on successful uninstall and rejects michael@0: * if the add-on is not unknown. michael@0: * michael@0: * @return Promise michael@0: */ michael@0: uninstallAddonByID: function (id) { michael@0: let deferred = Promise.defer(); michael@0: michael@0: AddonManager.getAddonByID(id, (addon) => { michael@0: if (!addon) { michael@0: deferred.reject(new Error("Add-on is not known: " + id)); michael@0: return; michael@0: } michael@0: michael@0: let listener = { michael@0: onUninstalling: function (addon, needsRestart) { michael@0: if (addon.id != id) { michael@0: return; michael@0: } michael@0: michael@0: if (needsRestart) { michael@0: AddonManager.removeAddonListener(listener); michael@0: deferred.resolve(true); michael@0: } michael@0: }, michael@0: michael@0: onUninstalled: function (addon) { michael@0: if (addon.id != id) { michael@0: return; michael@0: } michael@0: michael@0: AddonManager.removeAddonListener(listener); michael@0: deferred.resolve(false); michael@0: }, michael@0: michael@0: onOperationCancelled: function (addon) { michael@0: if (addon.id != id) { michael@0: return; michael@0: } michael@0: michael@0: AddonManager.removeAddonListener(listener); michael@0: deferred.reject(new Error("Uninstall cancelled.")); michael@0: }, michael@0: }; 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: /** michael@0: * Install an XPI add-on from a URL. michael@0: * michael@0: * @return Promise michael@0: */ michael@0: installXPIFromURL: function (url, hash, name, iconURL, version) { michael@0: let deferred = Promise.defer(); michael@0: michael@0: AddonManager.getInstallForURL(url, (install) => { michael@0: let fail = () => { deferred.reject(new Error("Add-on install failed.")) }; michael@0: michael@0: let listener = { michael@0: onDownloadCancelled: fail, michael@0: onDownloadFailed: fail, michael@0: onInstallCancelled: fail, michael@0: onInstallFailed: fail, michael@0: onInstallEnded: function (install, addon) { michael@0: deferred.resolve(addon); michael@0: }, michael@0: }; michael@0: michael@0: install.addListener(listener); michael@0: install.install(); michael@0: }, "application/x-xpinstall", hash, name, iconURL, version); michael@0: michael@0: return deferred.promise; michael@0: }, michael@0: };