|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 * http://creativecommons.org/publicdomain/zero/1.0/ |
|
3 */ |
|
4 |
|
5 const LIST_UPDATED_TOPIC = "plugins-list-updated"; |
|
6 |
|
7 // We need to use the same algorithm for generating IDs for plugins |
|
8 var { getIDHashForString } = Components.utils.import("resource://gre/modules/addons/PluginProvider.jsm"); |
|
9 |
|
10 function PluginTag(name, description) { |
|
11 this.name = name; |
|
12 this.description = description; |
|
13 } |
|
14 |
|
15 PluginTag.prototype = { |
|
16 name: null, |
|
17 description: null, |
|
18 version: "1.0", |
|
19 filename: null, |
|
20 fullpath: null, |
|
21 disabled: false, |
|
22 blocklisted: false, |
|
23 clicktoplay: false, |
|
24 |
|
25 mimeTypes: [], |
|
26 |
|
27 getMimeTypes: function(count) { |
|
28 count.value = this.mimeTypes.length; |
|
29 return this.mimeTypes; |
|
30 } |
|
31 }; |
|
32 |
|
33 PLUGINS = [ |
|
34 // A standalone plugin |
|
35 new PluginTag("Java", "A mock Java plugin"), |
|
36 |
|
37 // A plugin made up of two plugin files |
|
38 new PluginTag("Flash", "A mock Flash plugin"), |
|
39 new PluginTag("Flash", "A mock Flash plugin") |
|
40 ]; |
|
41 |
|
42 gPluginHost = { |
|
43 // nsIPluginHost |
|
44 getPluginTags: function(count) { |
|
45 count.value = PLUGINS.length; |
|
46 return PLUGINS; |
|
47 }, |
|
48 |
|
49 QueryInterface: XPCOMUtils.generateQI([AM_Ci.nsIPluginHost]) |
|
50 }; |
|
51 |
|
52 var PluginHostFactory = { |
|
53 createInstance: function (outer, iid) { |
|
54 if (outer != null) |
|
55 throw Components.results.NS_ERROR_NO_AGGREGATION; |
|
56 return gPluginHost.QueryInterface(iid); |
|
57 } |
|
58 }; |
|
59 |
|
60 var registrar = Components.manager.QueryInterface(AM_Ci.nsIComponentRegistrar); |
|
61 registrar.registerFactory(Components.ID("{aa6f9fef-cbe2-4d55-a2fa-dcf5482068b9}"), "PluginHost", |
|
62 "@mozilla.org/plugin/host;1", PluginHostFactory); |
|
63 |
|
64 // This verifies that when the list of plugins changes the add-ons manager |
|
65 // correctly updates |
|
66 function run_test() { |
|
67 do_test_pending(); |
|
68 createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2"); |
|
69 |
|
70 startupManager(); |
|
71 AddonManager.addAddonListener(AddonListener); |
|
72 AddonManager.addInstallListener(InstallListener); |
|
73 |
|
74 run_test_1(); |
|
75 } |
|
76 |
|
77 function end_test() { |
|
78 do_execute_soon(do_test_finished); |
|
79 } |
|
80 |
|
81 function sortAddons(addons) { |
|
82 addons.sort(function(a, b) { |
|
83 return a.name.localeCompare(b.name); |
|
84 }); |
|
85 } |
|
86 |
|
87 // Basic check that the mock object works |
|
88 function run_test_1() { |
|
89 AddonManager.getAddonsByTypes(["plugin"], function(addons) { |
|
90 sortAddons(addons); |
|
91 |
|
92 do_check_eq(addons.length, 2); |
|
93 |
|
94 do_check_eq(addons[0].name, "Flash"); |
|
95 do_check_false(addons[0].userDisabled); |
|
96 do_check_eq(addons[1].name, "Java"); |
|
97 do_check_false(addons[1].userDisabled); |
|
98 |
|
99 run_test_2(); |
|
100 }); |
|
101 } |
|
102 |
|
103 // No change to the list should not trigger any events or changes in the API |
|
104 function run_test_2() { |
|
105 // Reorder the list a bit |
|
106 let tag = PLUGINS[0]; |
|
107 PLUGINS[0] = PLUGINS[2]; |
|
108 PLUGINS[2] = PLUGINS[1]; |
|
109 PLUGINS[1] = tag; |
|
110 |
|
111 Services.obs.notifyObservers(null, LIST_UPDATED_TOPIC, null); |
|
112 |
|
113 AddonManager.getAddonsByTypes(["plugin"], function(addons) { |
|
114 sortAddons(addons); |
|
115 |
|
116 do_check_eq(addons.length, 2); |
|
117 |
|
118 do_check_eq(addons[0].name, "Flash"); |
|
119 do_check_false(addons[0].userDisabled); |
|
120 do_check_eq(addons[1].name, "Java"); |
|
121 do_check_false(addons[1].userDisabled); |
|
122 |
|
123 run_test_3(); |
|
124 }); |
|
125 } |
|
126 |
|
127 // Tests that a newly detected plugin shows up in the API and sends out events |
|
128 function run_test_3() { |
|
129 let tag = new PluginTag("Quicktime", "A mock Quicktime plugin"); |
|
130 PLUGINS.push(tag); |
|
131 let id = getIDHashForString(tag.name + tag.description); |
|
132 |
|
133 let test_params = {}; |
|
134 test_params[id] = [ |
|
135 ["onInstalling", false], |
|
136 "onInstalled" |
|
137 ]; |
|
138 |
|
139 prepare_test(test_params, [ |
|
140 "onExternalInstall" |
|
141 ]); |
|
142 |
|
143 Services.obs.notifyObservers(null, LIST_UPDATED_TOPIC, null); |
|
144 |
|
145 ensure_test_completed(); |
|
146 |
|
147 AddonManager.getAddonsByTypes(["plugin"], function(addons) { |
|
148 sortAddons(addons); |
|
149 |
|
150 do_check_eq(addons.length, 3); |
|
151 |
|
152 do_check_eq(addons[0].name, "Flash"); |
|
153 do_check_false(addons[0].userDisabled); |
|
154 do_check_eq(addons[1].name, "Java"); |
|
155 do_check_false(addons[1].userDisabled); |
|
156 do_check_eq(addons[2].name, "Quicktime"); |
|
157 do_check_false(addons[2].userDisabled); |
|
158 |
|
159 run_test_4(); |
|
160 }); |
|
161 } |
|
162 |
|
163 // Tests that a removed plugin disappears from in the API and sends out events |
|
164 function run_test_4() { |
|
165 let tag = PLUGINS.splice(1, 1)[0]; |
|
166 let id = getIDHashForString(tag.name + tag.description); |
|
167 |
|
168 let test_params = {}; |
|
169 test_params[id] = [ |
|
170 ["onUninstalling", false], |
|
171 "onUninstalled" |
|
172 ]; |
|
173 |
|
174 prepare_test(test_params); |
|
175 |
|
176 Services.obs.notifyObservers(null, LIST_UPDATED_TOPIC, null); |
|
177 |
|
178 ensure_test_completed(); |
|
179 |
|
180 AddonManager.getAddonsByTypes(["plugin"], function(addons) { |
|
181 sortAddons(addons); |
|
182 |
|
183 do_check_eq(addons.length, 2); |
|
184 |
|
185 do_check_eq(addons[0].name, "Flash"); |
|
186 do_check_false(addons[0].userDisabled); |
|
187 do_check_eq(addons[1].name, "Quicktime"); |
|
188 do_check_false(addons[1].userDisabled); |
|
189 |
|
190 run_test_5(); |
|
191 }); |
|
192 } |
|
193 |
|
194 // Removing part of the flash plugin should have no effect |
|
195 function run_test_5() { |
|
196 PLUGINS.splice(0, 1); |
|
197 |
|
198 Services.obs.notifyObservers(null, LIST_UPDATED_TOPIC, null); |
|
199 |
|
200 ensure_test_completed(); |
|
201 |
|
202 AddonManager.getAddonsByTypes(["plugin"], function(addons) { |
|
203 sortAddons(addons); |
|
204 |
|
205 do_check_eq(addons.length, 2); |
|
206 |
|
207 do_check_eq(addons[0].name, "Flash"); |
|
208 do_check_false(addons[0].userDisabled); |
|
209 do_check_eq(addons[1].name, "Quicktime"); |
|
210 do_check_false(addons[1].userDisabled); |
|
211 |
|
212 run_test_6(); |
|
213 }); |
|
214 } |
|
215 |
|
216 // Replacing flash should be detected |
|
217 function run_test_6() { |
|
218 let oldTag = PLUGINS.splice(0, 1)[0]; |
|
219 let newTag = new PluginTag("Flash 2", "A new crash-free Flash!"); |
|
220 newTag.disabled = true; |
|
221 PLUGINS.push(newTag); |
|
222 |
|
223 let test_params = {}; |
|
224 test_params[getIDHashForString(oldTag.name + oldTag.description)] = [ |
|
225 ["onUninstalling", false], |
|
226 "onUninstalled" |
|
227 ]; |
|
228 test_params[getIDHashForString(newTag.name + newTag.description)] = [ |
|
229 ["onInstalling", false], |
|
230 "onInstalled" |
|
231 ]; |
|
232 |
|
233 prepare_test(test_params, [ |
|
234 "onExternalInstall" |
|
235 ]); |
|
236 |
|
237 Services.obs.notifyObservers(null, LIST_UPDATED_TOPIC, null); |
|
238 |
|
239 ensure_test_completed(); |
|
240 |
|
241 AddonManager.getAddonsByTypes(["plugin"], function(addons) { |
|
242 sortAddons(addons); |
|
243 |
|
244 do_check_eq(addons.length, 2); |
|
245 |
|
246 do_check_eq(addons[0].name, "Flash 2"); |
|
247 do_check_true(addons[0].userDisabled); |
|
248 do_check_eq(addons[1].name, "Quicktime"); |
|
249 do_check_false(addons[1].userDisabled); |
|
250 |
|
251 run_test_7(); |
|
252 }); |
|
253 } |
|
254 |
|
255 // If new tags are detected and the disabled state changes then we should send |
|
256 // out appropriate notifications |
|
257 function run_test_7() { |
|
258 PLUGINS[0] = new PluginTag("Quicktime", "A mock Quicktime plugin"); |
|
259 PLUGINS[0].disabled = true; |
|
260 PLUGINS[1] = new PluginTag("Flash 2", "A new crash-free Flash!"); |
|
261 |
|
262 let test_params = {}; |
|
263 test_params[getIDHashForString(PLUGINS[0].name + PLUGINS[0].description)] = [ |
|
264 ["onDisabling", false], |
|
265 "onDisabled" |
|
266 ]; |
|
267 test_params[getIDHashForString(PLUGINS[1].name + PLUGINS[1].description)] = [ |
|
268 ["onEnabling", false], |
|
269 "onEnabled" |
|
270 ]; |
|
271 |
|
272 prepare_test(test_params); |
|
273 |
|
274 Services.obs.notifyObservers(null, LIST_UPDATED_TOPIC, null); |
|
275 |
|
276 ensure_test_completed(); |
|
277 |
|
278 AddonManager.getAddonsByTypes(["plugin"], function(addons) { |
|
279 sortAddons(addons); |
|
280 |
|
281 do_check_eq(addons.length, 2); |
|
282 |
|
283 do_check_eq(addons[0].name, "Flash 2"); |
|
284 do_check_false(addons[0].userDisabled); |
|
285 do_check_eq(addons[1].name, "Quicktime"); |
|
286 do_check_true(addons[1].userDisabled); |
|
287 |
|
288 end_test(); |
|
289 }); |
|
290 } |