|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 // This file is a test-only JSM containing utility methods for |
|
6 // interacting with the add-ons manager. |
|
7 |
|
8 "use strict"; |
|
9 |
|
10 this.EXPORTED_SYMBOLS = [ |
|
11 "AddonTestUtils", |
|
12 ]; |
|
13 |
|
14 const {utils: Cu} = Components; |
|
15 |
|
16 Cu.import("resource://gre/modules/Promise.jsm"); |
|
17 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
18 |
|
19 XPCOMUtils.defineLazyModuleGetter(this, "AddonManager", |
|
20 "resource://gre/modules/AddonManager.jsm"); |
|
21 |
|
22 this.AddonTestUtils = { |
|
23 /** |
|
24 * Uninstall an add-on that is specified by its ID. |
|
25 * |
|
26 * The returned promise resolves on successful uninstall and rejects |
|
27 * if the add-on is not unknown. |
|
28 * |
|
29 * @return Promise<restartRequired> |
|
30 */ |
|
31 uninstallAddonByID: function (id) { |
|
32 let deferred = Promise.defer(); |
|
33 |
|
34 AddonManager.getAddonByID(id, (addon) => { |
|
35 if (!addon) { |
|
36 deferred.reject(new Error("Add-on is not known: " + id)); |
|
37 return; |
|
38 } |
|
39 |
|
40 let listener = { |
|
41 onUninstalling: function (addon, needsRestart) { |
|
42 if (addon.id != id) { |
|
43 return; |
|
44 } |
|
45 |
|
46 if (needsRestart) { |
|
47 AddonManager.removeAddonListener(listener); |
|
48 deferred.resolve(true); |
|
49 } |
|
50 }, |
|
51 |
|
52 onUninstalled: function (addon) { |
|
53 if (addon.id != id) { |
|
54 return; |
|
55 } |
|
56 |
|
57 AddonManager.removeAddonListener(listener); |
|
58 deferred.resolve(false); |
|
59 }, |
|
60 |
|
61 onOperationCancelled: function (addon) { |
|
62 if (addon.id != id) { |
|
63 return; |
|
64 } |
|
65 |
|
66 AddonManager.removeAddonListener(listener); |
|
67 deferred.reject(new Error("Uninstall cancelled.")); |
|
68 }, |
|
69 }; |
|
70 |
|
71 AddonManager.addAddonListener(listener); |
|
72 addon.uninstall(); |
|
73 }); |
|
74 |
|
75 return deferred.promise; |
|
76 }, |
|
77 |
|
78 /** |
|
79 * Install an XPI add-on from a URL. |
|
80 * |
|
81 * @return Promise<addon> |
|
82 */ |
|
83 installXPIFromURL: function (url, hash, name, iconURL, version) { |
|
84 let deferred = Promise.defer(); |
|
85 |
|
86 AddonManager.getInstallForURL(url, (install) => { |
|
87 let fail = () => { deferred.reject(new Error("Add-on install failed.")) }; |
|
88 |
|
89 let listener = { |
|
90 onDownloadCancelled: fail, |
|
91 onDownloadFailed: fail, |
|
92 onInstallCancelled: fail, |
|
93 onInstallFailed: fail, |
|
94 onInstallEnded: function (install, addon) { |
|
95 deferred.resolve(addon); |
|
96 }, |
|
97 }; |
|
98 |
|
99 install.addListener(listener); |
|
100 install.install(); |
|
101 }, "application/x-xpinstall", hash, name, iconURL, version); |
|
102 |
|
103 return deferred.promise; |
|
104 }, |
|
105 }; |