toolkit/mozapps/extensions/test/AddonManagerTesting.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/mozapps/extensions/test/AddonManagerTesting.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,105 @@
     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 +// This file is a test-only JSM containing utility methods for
     1.9 +// interacting with the add-ons manager.
    1.10 +
    1.11 +"use strict";
    1.12 +
    1.13 +this.EXPORTED_SYMBOLS = [
    1.14 +  "AddonTestUtils",
    1.15 +];
    1.16 +
    1.17 +const {utils: Cu} = Components;
    1.18 +
    1.19 +Cu.import("resource://gre/modules/Promise.jsm");
    1.20 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.21 +
    1.22 +XPCOMUtils.defineLazyModuleGetter(this, "AddonManager",
    1.23 +                                  "resource://gre/modules/AddonManager.jsm");
    1.24 +
    1.25 +this.AddonTestUtils = {
    1.26 +  /**
    1.27 +   * Uninstall an add-on that is specified by its ID.
    1.28 +   *
    1.29 +   * The returned promise resolves on successful uninstall and rejects
    1.30 +   * if the add-on is not unknown.
    1.31 +   *
    1.32 +   * @return Promise<restartRequired>
    1.33 +   */
    1.34 +  uninstallAddonByID: function (id) {
    1.35 +    let deferred = Promise.defer();
    1.36 +
    1.37 +    AddonManager.getAddonByID(id, (addon) => {
    1.38 +      if (!addon) {
    1.39 +        deferred.reject(new Error("Add-on is not known: " + id));
    1.40 +        return;
    1.41 +      }
    1.42 +
    1.43 +      let listener = {
    1.44 +        onUninstalling: function (addon, needsRestart) {
    1.45 +          if (addon.id != id) {
    1.46 +            return;
    1.47 +          }
    1.48 +
    1.49 +          if (needsRestart) {
    1.50 +            AddonManager.removeAddonListener(listener);
    1.51 +            deferred.resolve(true);
    1.52 +          }
    1.53 +        },
    1.54 +
    1.55 +        onUninstalled: function (addon) {
    1.56 +          if (addon.id != id) {
    1.57 +            return;
    1.58 +          }
    1.59 +
    1.60 +          AddonManager.removeAddonListener(listener);
    1.61 +          deferred.resolve(false);
    1.62 +        },
    1.63 +
    1.64 +        onOperationCancelled: function (addon) {
    1.65 +          if (addon.id != id) {
    1.66 +            return;
    1.67 +          }
    1.68 +
    1.69 +          AddonManager.removeAddonListener(listener);
    1.70 +          deferred.reject(new Error("Uninstall cancelled."));
    1.71 +        },
    1.72 +      };
    1.73 +
    1.74 +      AddonManager.addAddonListener(listener);
    1.75 +      addon.uninstall();
    1.76 +    });
    1.77 +
    1.78 +    return deferred.promise;
    1.79 +  },
    1.80 +
    1.81 +  /**
    1.82 +   * Install an XPI add-on from a URL.
    1.83 +   *
    1.84 +   * @return Promise<addon>
    1.85 +   */
    1.86 +  installXPIFromURL: function (url, hash, name, iconURL, version) {
    1.87 +    let deferred = Promise.defer();
    1.88 +
    1.89 +    AddonManager.getInstallForURL(url, (install) => {
    1.90 +      let fail = () => { deferred.reject(new Error("Add-on install failed.")) };
    1.91 +
    1.92 +      let listener = {
    1.93 +        onDownloadCancelled: fail,
    1.94 +        onDownloadFailed: fail,
    1.95 +        onInstallCancelled: fail,
    1.96 +        onInstallFailed: fail,
    1.97 +        onInstallEnded: function (install, addon) {
    1.98 +          deferred.resolve(addon);
    1.99 +        },
   1.100 +      };
   1.101 +
   1.102 +      install.addListener(listener);
   1.103 +      install.install();
   1.104 +    }, "application/x-xpinstall", hash, name, iconURL, version);
   1.105 +
   1.106 +    return deferred.promise;
   1.107 +  },
   1.108 +};

mercurial