|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ |
|
3 */ |
|
4 |
|
5 // This verifies that add-ons URIs can be mapped to add-on IDs |
|
6 // |
|
7 Components.utils.import("resource://gre/modules/Services.jsm"); |
|
8 |
|
9 // Enable loading extensions from the user scopes |
|
10 Services.prefs.setIntPref("extensions.enabledScopes", |
|
11 AddonManager.SCOPE_PROFILE + AddonManager.SCOPE_USER); |
|
12 |
|
13 createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2"); |
|
14 |
|
15 const profileDir = gProfD.clone(); |
|
16 profileDir.append("extensions"); |
|
17 const userExtDir = gProfD.clone(); |
|
18 userExtDir.append("extensions2"); |
|
19 userExtDir.append(gAppInfo.ID); |
|
20 registerDirectory("XREUSysExt", userExtDir.parent); |
|
21 |
|
22 function TestProvider(result) { |
|
23 this.result = result; |
|
24 } |
|
25 TestProvider.prototype = { |
|
26 uri: Services.io.newURI("hellow://world", null, null), |
|
27 id: "valid@id", |
|
28 startup: function() {}, |
|
29 shutdown: function() {}, |
|
30 mapURIToAddonID: function(aURI) { |
|
31 if (aURI.spec === this.uri.spec) { |
|
32 return this.id; |
|
33 } |
|
34 throw Components.Exception("Not mapped", result); |
|
35 } |
|
36 }; |
|
37 |
|
38 function TestProviderNoMap() {} |
|
39 TestProviderNoMap.prototype = { |
|
40 startup: function() {}, |
|
41 shutdown: function() {} |
|
42 }; |
|
43 |
|
44 function check_mapping(uri, id) { |
|
45 do_check_eq(AddonManager.mapURIToAddonID(uri), id); |
|
46 let svc = Components.classes["@mozilla.org/addons/integration;1"]. |
|
47 getService(Components.interfaces.amIAddonManager); |
|
48 let val = {}; |
|
49 do_check_true(svc.mapURIToAddonID(uri, val)); |
|
50 do_check_eq(val.value, id); |
|
51 } |
|
52 |
|
53 function resetPrefs() { |
|
54 Services.prefs.setIntPref("bootstraptest.active_version", -1); |
|
55 } |
|
56 |
|
57 function waitForPref(aPref, aCallback) { |
|
58 function prefChanged() { |
|
59 Services.prefs.removeObserver(aPref, prefChanged); |
|
60 aCallback(); |
|
61 } |
|
62 Services.prefs.addObserver(aPref, prefChanged, false); |
|
63 } |
|
64 |
|
65 function getActiveVersion() { |
|
66 return Services.prefs.getIntPref("bootstraptest.active_version"); |
|
67 } |
|
68 |
|
69 function run_test() { |
|
70 do_test_pending(); |
|
71 |
|
72 resetPrefs(); |
|
73 |
|
74 run_test_early(); |
|
75 } |
|
76 |
|
77 function run_test_early() { |
|
78 startupManager(); |
|
79 |
|
80 installAllFiles([do_get_addon("test_chromemanifest_1")], function() { |
|
81 restartManager(); |
|
82 AddonManager.getAddonByID("addon1@tests.mozilla.org", function(addon) { |
|
83 let uri = addon.getResourceURI("."); |
|
84 let id = addon.id; |
|
85 check_mapping(uri, id); |
|
86 |
|
87 shutdownManager(); |
|
88 |
|
89 // Force an early call, to check that mappings will get correctly |
|
90 // initialized when the manager actually starts up. |
|
91 // See bug 957089 |
|
92 |
|
93 // First force-initialize the XPIProvider. |
|
94 let s = Components.utils.import( |
|
95 "resource://gre/modules/addons/XPIProvider.jsm", {}); |
|
96 |
|
97 // Make the early API call. |
|
98 do_check_null(s.XPIProvider.mapURIToAddonID(uri)); |
|
99 do_check_null(AddonManager.mapURIToAddonID(uri)); |
|
100 |
|
101 // Actually start up the manager. |
|
102 startupManager(false); |
|
103 |
|
104 // Check that the mapping is there now. |
|
105 check_mapping(uri, id); |
|
106 do_check_eq(s.XPIProvider.mapURIToAddonID(uri), id); |
|
107 |
|
108 run_test_nomapping(); |
|
109 }); |
|
110 }); |
|
111 } |
|
112 |
|
113 function run_test_nomapping() { |
|
114 do_check_eq(AddonManager.mapURIToAddonID(TestProvider.prototype.uri), null); |
|
115 try { |
|
116 let svc = Components.classes["@mozilla.org/addons/integration;1"]. |
|
117 getService(Components.interfaces.amIAddonManager); |
|
118 let val = {}; |
|
119 do_check_false(svc.mapURIToAddonID(TestProvider.prototype.uri, val)); |
|
120 } |
|
121 catch (ex) { |
|
122 do_throw(ex); |
|
123 } |
|
124 |
|
125 run_test_1(); |
|
126 } |
|
127 |
|
128 |
|
129 // Tests that add-on URIs are mappable after an install |
|
130 function run_test_1() { |
|
131 prepare_test({ }, [ |
|
132 "onNewInstall" |
|
133 ]); |
|
134 |
|
135 AddonManager.getInstallForFile(do_get_addon("test_bootstrap1_1"), function(install) { |
|
136 ensure_test_completed(); |
|
137 |
|
138 let addon = install.addon; |
|
139 prepare_test({ |
|
140 "bootstrap1@tests.mozilla.org": [ |
|
141 ["onInstalling", false], |
|
142 "onInstalled" |
|
143 ] |
|
144 }, [ |
|
145 "onInstallStarted", |
|
146 "onInstallEnded", |
|
147 ], function() { |
|
148 let uri = addon.getResourceURI("."); |
|
149 check_mapping(uri, addon.id); |
|
150 |
|
151 waitForPref("bootstraptest.active_version", function() { |
|
152 run_test_2(uri); |
|
153 }); |
|
154 }); |
|
155 install.install(); |
|
156 }); |
|
157 } |
|
158 |
|
159 // Tests that add-on URIs are still mappable, even after the add-on gets |
|
160 // disabled in-session. |
|
161 function run_test_2(uri) { |
|
162 AddonManager.getAddonByID("bootstrap1@tests.mozilla.org", function(b1) { |
|
163 prepare_test({ |
|
164 "bootstrap1@tests.mozilla.org": [ |
|
165 ["onDisabling", false], |
|
166 "onDisabled" |
|
167 ] |
|
168 }); |
|
169 |
|
170 b1.userDisabled = true; |
|
171 ensure_test_completed(); |
|
172 |
|
173 AddonManager.getAddonByID("bootstrap1@tests.mozilla.org", function(newb1) { |
|
174 do_check_true(newb1.userDisabled); |
|
175 check_mapping(uri, newb1.id); |
|
176 |
|
177 do_execute_soon(() => run_test_3(uri)); |
|
178 }); |
|
179 }); |
|
180 } |
|
181 |
|
182 // Tests that add-on URIs aren't mappable if the add-on was never started in a |
|
183 // session |
|
184 function run_test_3(uri) { |
|
185 restartManager(); |
|
186 |
|
187 do_check_eq(AddonManager.mapURIToAddonID(uri), null); |
|
188 |
|
189 run_test_4(); |
|
190 } |
|
191 |
|
192 // Tests that add-on URIs are mappable after a restart + reenable |
|
193 function run_test_4() { |
|
194 restartManager(); |
|
195 |
|
196 AddonManager.getAddonByID("bootstrap1@tests.mozilla.org", function(b1) { |
|
197 prepare_test({ |
|
198 "bootstrap1@tests.mozilla.org": [ |
|
199 ["onEnabling", false], |
|
200 "onEnabled" |
|
201 ] |
|
202 }); |
|
203 |
|
204 b1.userDisabled = false; |
|
205 ensure_test_completed(); |
|
206 |
|
207 AddonManager.getAddonByID("bootstrap1@tests.mozilla.org", function(newb1) { |
|
208 let uri = newb1.getResourceURI("."); |
|
209 check_mapping(uri, newb1.id); |
|
210 |
|
211 do_execute_soon(run_test_5); |
|
212 }); |
|
213 }); |
|
214 } |
|
215 |
|
216 // Tests that add-on URIs are mappable after a restart |
|
217 function run_test_5() { |
|
218 restartManager(); |
|
219 |
|
220 AddonManager.getAddonByID("bootstrap1@tests.mozilla.org", function(b1) { |
|
221 let uri = b1.getResourceURI("."); |
|
222 check_mapping(uri, b1.id); |
|
223 |
|
224 do_execute_soon(run_test_invalidarg); |
|
225 }); |
|
226 } |
|
227 |
|
228 // Tests that the AddonManager will bail when mapURIToAddonID is called with an |
|
229 // invalid argument |
|
230 function run_test_invalidarg() { |
|
231 restartManager(); |
|
232 |
|
233 let tests = [undefined, |
|
234 null, |
|
235 1, |
|
236 "string", |
|
237 "chrome://global/content/", |
|
238 function() {} |
|
239 ]; |
|
240 for (var test of tests) { |
|
241 try { |
|
242 AddonManager.mapURIToAddonID(test); |
|
243 throw new Error("Shouldn't be able to map the URI in question"); |
|
244 } |
|
245 catch (ex if ex.result) { |
|
246 do_check_eq(ex.result, Components.results.NS_ERROR_INVALID_ARG); |
|
247 } |
|
248 catch (ex) { |
|
249 do_throw(ex); |
|
250 } |
|
251 } |
|
252 |
|
253 run_test_provider(); |
|
254 } |
|
255 |
|
256 // Tests that custom providers are correctly handled |
|
257 function run_test_provider() { |
|
258 restartManager(); |
|
259 |
|
260 const provider = new TestProvider(Components.results.NS_ERROR_NOT_AVAILABLE); |
|
261 AddonManagerPrivate.registerProvider(provider); |
|
262 |
|
263 check_mapping(provider.uri, provider.id); |
|
264 |
|
265 let u2 = provider.uri.clone(); |
|
266 u2.path = "notmapped"; |
|
267 do_check_eq(AddonManager.mapURIToAddonID(u2), null); |
|
268 |
|
269 AddonManagerPrivate.unregisterProvider(provider); |
|
270 |
|
271 run_test_provider_nomap(); |
|
272 } |
|
273 |
|
274 // Tests that custom providers are correctly handled, even not implementing |
|
275 // mapURIToAddonID |
|
276 function run_test_provider_nomap() { |
|
277 restartManager(); |
|
278 |
|
279 const provider = new TestProviderNoMap(); |
|
280 AddonManagerPrivate.registerProvider(provider); |
|
281 |
|
282 do_check_eq(AddonManager.mapURIToAddonID(TestProvider.prototype.uri), null); |
|
283 |
|
284 AddonManagerPrivate.unregisterProvider(provider); |
|
285 |
|
286 do_test_finished(); |
|
287 } |
|
288 |
|
289 |