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