1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/mozapps/extensions/test/xpcshell/test_pluginchange.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,290 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ 1.6 + */ 1.7 + 1.8 +const LIST_UPDATED_TOPIC = "plugins-list-updated"; 1.9 + 1.10 +// We need to use the same algorithm for generating IDs for plugins 1.11 +var { getIDHashForString } = Components.utils.import("resource://gre/modules/addons/PluginProvider.jsm"); 1.12 + 1.13 +function PluginTag(name, description) { 1.14 + this.name = name; 1.15 + this.description = description; 1.16 +} 1.17 + 1.18 +PluginTag.prototype = { 1.19 + name: null, 1.20 + description: null, 1.21 + version: "1.0", 1.22 + filename: null, 1.23 + fullpath: null, 1.24 + disabled: false, 1.25 + blocklisted: false, 1.26 + clicktoplay: false, 1.27 + 1.28 + mimeTypes: [], 1.29 + 1.30 + getMimeTypes: function(count) { 1.31 + count.value = this.mimeTypes.length; 1.32 + return this.mimeTypes; 1.33 + } 1.34 +}; 1.35 + 1.36 +PLUGINS = [ 1.37 + // A standalone plugin 1.38 + new PluginTag("Java", "A mock Java plugin"), 1.39 + 1.40 + // A plugin made up of two plugin files 1.41 + new PluginTag("Flash", "A mock Flash plugin"), 1.42 + new PluginTag("Flash", "A mock Flash plugin") 1.43 +]; 1.44 + 1.45 +gPluginHost = { 1.46 + // nsIPluginHost 1.47 + getPluginTags: function(count) { 1.48 + count.value = PLUGINS.length; 1.49 + return PLUGINS; 1.50 + }, 1.51 + 1.52 + QueryInterface: XPCOMUtils.generateQI([AM_Ci.nsIPluginHost]) 1.53 +}; 1.54 + 1.55 +var PluginHostFactory = { 1.56 + createInstance: function (outer, iid) { 1.57 + if (outer != null) 1.58 + throw Components.results.NS_ERROR_NO_AGGREGATION; 1.59 + return gPluginHost.QueryInterface(iid); 1.60 + } 1.61 +}; 1.62 + 1.63 +var registrar = Components.manager.QueryInterface(AM_Ci.nsIComponentRegistrar); 1.64 +registrar.registerFactory(Components.ID("{aa6f9fef-cbe2-4d55-a2fa-dcf5482068b9}"), "PluginHost", 1.65 + "@mozilla.org/plugin/host;1", PluginHostFactory); 1.66 + 1.67 +// This verifies that when the list of plugins changes the add-ons manager 1.68 +// correctly updates 1.69 +function run_test() { 1.70 + do_test_pending(); 1.71 + createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2"); 1.72 + 1.73 + startupManager(); 1.74 + AddonManager.addAddonListener(AddonListener); 1.75 + AddonManager.addInstallListener(InstallListener); 1.76 + 1.77 + run_test_1(); 1.78 +} 1.79 + 1.80 +function end_test() { 1.81 + do_execute_soon(do_test_finished); 1.82 +} 1.83 + 1.84 +function sortAddons(addons) { 1.85 + addons.sort(function(a, b) { 1.86 + return a.name.localeCompare(b.name); 1.87 + }); 1.88 +} 1.89 + 1.90 +// Basic check that the mock object works 1.91 +function run_test_1() { 1.92 + AddonManager.getAddonsByTypes(["plugin"], function(addons) { 1.93 + sortAddons(addons); 1.94 + 1.95 + do_check_eq(addons.length, 2); 1.96 + 1.97 + do_check_eq(addons[0].name, "Flash"); 1.98 + do_check_false(addons[0].userDisabled); 1.99 + do_check_eq(addons[1].name, "Java"); 1.100 + do_check_false(addons[1].userDisabled); 1.101 + 1.102 + run_test_2(); 1.103 + }); 1.104 +} 1.105 + 1.106 +// No change to the list should not trigger any events or changes in the API 1.107 +function run_test_2() { 1.108 + // Reorder the list a bit 1.109 + let tag = PLUGINS[0]; 1.110 + PLUGINS[0] = PLUGINS[2]; 1.111 + PLUGINS[2] = PLUGINS[1]; 1.112 + PLUGINS[1] = tag; 1.113 + 1.114 + Services.obs.notifyObservers(null, LIST_UPDATED_TOPIC, null); 1.115 + 1.116 + AddonManager.getAddonsByTypes(["plugin"], function(addons) { 1.117 + sortAddons(addons); 1.118 + 1.119 + do_check_eq(addons.length, 2); 1.120 + 1.121 + do_check_eq(addons[0].name, "Flash"); 1.122 + do_check_false(addons[0].userDisabled); 1.123 + do_check_eq(addons[1].name, "Java"); 1.124 + do_check_false(addons[1].userDisabled); 1.125 + 1.126 + run_test_3(); 1.127 + }); 1.128 +} 1.129 + 1.130 +// Tests that a newly detected plugin shows up in the API and sends out events 1.131 +function run_test_3() { 1.132 + let tag = new PluginTag("Quicktime", "A mock Quicktime plugin"); 1.133 + PLUGINS.push(tag); 1.134 + let id = getIDHashForString(tag.name + tag.description); 1.135 + 1.136 + let test_params = {}; 1.137 + test_params[id] = [ 1.138 + ["onInstalling", false], 1.139 + "onInstalled" 1.140 + ]; 1.141 + 1.142 + prepare_test(test_params, [ 1.143 + "onExternalInstall" 1.144 + ]); 1.145 + 1.146 + Services.obs.notifyObservers(null, LIST_UPDATED_TOPIC, null); 1.147 + 1.148 + ensure_test_completed(); 1.149 + 1.150 + AddonManager.getAddonsByTypes(["plugin"], function(addons) { 1.151 + sortAddons(addons); 1.152 + 1.153 + do_check_eq(addons.length, 3); 1.154 + 1.155 + do_check_eq(addons[0].name, "Flash"); 1.156 + do_check_false(addons[0].userDisabled); 1.157 + do_check_eq(addons[1].name, "Java"); 1.158 + do_check_false(addons[1].userDisabled); 1.159 + do_check_eq(addons[2].name, "Quicktime"); 1.160 + do_check_false(addons[2].userDisabled); 1.161 + 1.162 + run_test_4(); 1.163 + }); 1.164 +} 1.165 + 1.166 +// Tests that a removed plugin disappears from in the API and sends out events 1.167 +function run_test_4() { 1.168 + let tag = PLUGINS.splice(1, 1)[0]; 1.169 + let id = getIDHashForString(tag.name + tag.description); 1.170 + 1.171 + let test_params = {}; 1.172 + test_params[id] = [ 1.173 + ["onUninstalling", false], 1.174 + "onUninstalled" 1.175 + ]; 1.176 + 1.177 + prepare_test(test_params); 1.178 + 1.179 + Services.obs.notifyObservers(null, LIST_UPDATED_TOPIC, null); 1.180 + 1.181 + ensure_test_completed(); 1.182 + 1.183 + AddonManager.getAddonsByTypes(["plugin"], function(addons) { 1.184 + sortAddons(addons); 1.185 + 1.186 + do_check_eq(addons.length, 2); 1.187 + 1.188 + do_check_eq(addons[0].name, "Flash"); 1.189 + do_check_false(addons[0].userDisabled); 1.190 + do_check_eq(addons[1].name, "Quicktime"); 1.191 + do_check_false(addons[1].userDisabled); 1.192 + 1.193 + run_test_5(); 1.194 + }); 1.195 +} 1.196 + 1.197 +// Removing part of the flash plugin should have no effect 1.198 +function run_test_5() { 1.199 + PLUGINS.splice(0, 1); 1.200 + 1.201 + Services.obs.notifyObservers(null, LIST_UPDATED_TOPIC, null); 1.202 + 1.203 + ensure_test_completed(); 1.204 + 1.205 + AddonManager.getAddonsByTypes(["plugin"], function(addons) { 1.206 + sortAddons(addons); 1.207 + 1.208 + do_check_eq(addons.length, 2); 1.209 + 1.210 + do_check_eq(addons[0].name, "Flash"); 1.211 + do_check_false(addons[0].userDisabled); 1.212 + do_check_eq(addons[1].name, "Quicktime"); 1.213 + do_check_false(addons[1].userDisabled); 1.214 + 1.215 + run_test_6(); 1.216 + }); 1.217 +} 1.218 + 1.219 +// Replacing flash should be detected 1.220 +function run_test_6() { 1.221 + let oldTag = PLUGINS.splice(0, 1)[0]; 1.222 + let newTag = new PluginTag("Flash 2", "A new crash-free Flash!"); 1.223 + newTag.disabled = true; 1.224 + PLUGINS.push(newTag); 1.225 + 1.226 + let test_params = {}; 1.227 + test_params[getIDHashForString(oldTag.name + oldTag.description)] = [ 1.228 + ["onUninstalling", false], 1.229 + "onUninstalled" 1.230 + ]; 1.231 + test_params[getIDHashForString(newTag.name + newTag.description)] = [ 1.232 + ["onInstalling", false], 1.233 + "onInstalled" 1.234 + ]; 1.235 + 1.236 + prepare_test(test_params, [ 1.237 + "onExternalInstall" 1.238 + ]); 1.239 + 1.240 + Services.obs.notifyObservers(null, LIST_UPDATED_TOPIC, null); 1.241 + 1.242 + ensure_test_completed(); 1.243 + 1.244 + AddonManager.getAddonsByTypes(["plugin"], function(addons) { 1.245 + sortAddons(addons); 1.246 + 1.247 + do_check_eq(addons.length, 2); 1.248 + 1.249 + do_check_eq(addons[0].name, "Flash 2"); 1.250 + do_check_true(addons[0].userDisabled); 1.251 + do_check_eq(addons[1].name, "Quicktime"); 1.252 + do_check_false(addons[1].userDisabled); 1.253 + 1.254 + run_test_7(); 1.255 + }); 1.256 +} 1.257 + 1.258 +// If new tags are detected and the disabled state changes then we should send 1.259 +// out appropriate notifications 1.260 +function run_test_7() { 1.261 + PLUGINS[0] = new PluginTag("Quicktime", "A mock Quicktime plugin"); 1.262 + PLUGINS[0].disabled = true; 1.263 + PLUGINS[1] = new PluginTag("Flash 2", "A new crash-free Flash!"); 1.264 + 1.265 + let test_params = {}; 1.266 + test_params[getIDHashForString(PLUGINS[0].name + PLUGINS[0].description)] = [ 1.267 + ["onDisabling", false], 1.268 + "onDisabled" 1.269 + ]; 1.270 + test_params[getIDHashForString(PLUGINS[1].name + PLUGINS[1].description)] = [ 1.271 + ["onEnabling", false], 1.272 + "onEnabled" 1.273 + ]; 1.274 + 1.275 + prepare_test(test_params); 1.276 + 1.277 + Services.obs.notifyObservers(null, LIST_UPDATED_TOPIC, null); 1.278 + 1.279 + ensure_test_completed(); 1.280 + 1.281 + AddonManager.getAddonsByTypes(["plugin"], function(addons) { 1.282 + sortAddons(addons); 1.283 + 1.284 + do_check_eq(addons.length, 2); 1.285 + 1.286 + do_check_eq(addons[0].name, "Flash 2"); 1.287 + do_check_false(addons[0].userDisabled); 1.288 + do_check_eq(addons[1].name, "Quicktime"); 1.289 + do_check_true(addons[1].userDisabled); 1.290 + 1.291 + end_test(); 1.292 + }); 1.293 +}