Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/
3 */
5 const LIST_UPDATED_TOPIC = "plugins-list-updated";
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");
10 function PluginTag(name, description) {
11 this.name = name;
12 this.description = description;
13 }
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,
25 mimeTypes: [],
27 getMimeTypes: function(count) {
28 count.value = this.mimeTypes.length;
29 return this.mimeTypes;
30 }
31 };
33 PLUGINS = [
34 // A standalone plugin
35 new PluginTag("Java", "A mock Java plugin"),
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 ];
42 gPluginHost = {
43 // nsIPluginHost
44 getPluginTags: function(count) {
45 count.value = PLUGINS.length;
46 return PLUGINS;
47 },
49 QueryInterface: XPCOMUtils.generateQI([AM_Ci.nsIPluginHost])
50 };
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 };
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);
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");
70 startupManager();
71 AddonManager.addAddonListener(AddonListener);
72 AddonManager.addInstallListener(InstallListener);
74 run_test_1();
75 }
77 function end_test() {
78 do_execute_soon(do_test_finished);
79 }
81 function sortAddons(addons) {
82 addons.sort(function(a, b) {
83 return a.name.localeCompare(b.name);
84 });
85 }
87 // Basic check that the mock object works
88 function run_test_1() {
89 AddonManager.getAddonsByTypes(["plugin"], function(addons) {
90 sortAddons(addons);
92 do_check_eq(addons.length, 2);
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);
99 run_test_2();
100 });
101 }
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;
111 Services.obs.notifyObservers(null, LIST_UPDATED_TOPIC, null);
113 AddonManager.getAddonsByTypes(["plugin"], function(addons) {
114 sortAddons(addons);
116 do_check_eq(addons.length, 2);
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);
123 run_test_3();
124 });
125 }
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);
133 let test_params = {};
134 test_params[id] = [
135 ["onInstalling", false],
136 "onInstalled"
137 ];
139 prepare_test(test_params, [
140 "onExternalInstall"
141 ]);
143 Services.obs.notifyObservers(null, LIST_UPDATED_TOPIC, null);
145 ensure_test_completed();
147 AddonManager.getAddonsByTypes(["plugin"], function(addons) {
148 sortAddons(addons);
150 do_check_eq(addons.length, 3);
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);
159 run_test_4();
160 });
161 }
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);
168 let test_params = {};
169 test_params[id] = [
170 ["onUninstalling", false],
171 "onUninstalled"
172 ];
174 prepare_test(test_params);
176 Services.obs.notifyObservers(null, LIST_UPDATED_TOPIC, null);
178 ensure_test_completed();
180 AddonManager.getAddonsByTypes(["plugin"], function(addons) {
181 sortAddons(addons);
183 do_check_eq(addons.length, 2);
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);
190 run_test_5();
191 });
192 }
194 // Removing part of the flash plugin should have no effect
195 function run_test_5() {
196 PLUGINS.splice(0, 1);
198 Services.obs.notifyObservers(null, LIST_UPDATED_TOPIC, null);
200 ensure_test_completed();
202 AddonManager.getAddonsByTypes(["plugin"], function(addons) {
203 sortAddons(addons);
205 do_check_eq(addons.length, 2);
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);
212 run_test_6();
213 });
214 }
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);
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 ];
233 prepare_test(test_params, [
234 "onExternalInstall"
235 ]);
237 Services.obs.notifyObservers(null, LIST_UPDATED_TOPIC, null);
239 ensure_test_completed();
241 AddonManager.getAddonsByTypes(["plugin"], function(addons) {
242 sortAddons(addons);
244 do_check_eq(addons.length, 2);
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);
251 run_test_7();
252 });
253 }
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!");
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 ];
272 prepare_test(test_params);
274 Services.obs.notifyObservers(null, LIST_UPDATED_TOPIC, null);
276 ensure_test_completed();
278 AddonManager.getAddonsByTypes(["plugin"], function(addons) {
279 sortAddons(addons);
281 do_check_eq(addons.length, 2);
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);
288 end_test();
289 });
290 }