1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/mozapps/extensions/test/xpcshell/test_duplicateplugins.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,184 @@ 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 Ci = Components.interfaces; 1.9 + 1.10 +// This verifies that duplicate plugins are coalesced and maintain their ID 1.11 +// across restarts. 1.12 + 1.13 +var PLUGINS = [{ 1.14 + name: "Duplicate Plugin 1", 1.15 + description: "A duplicate plugin", 1.16 + version: "1", 1.17 + blocklisted: false, 1.18 + enabledState: Ci.nsIPluginTag.STATE_ENABLED, 1.19 + get disabled() this.enabledState == Ci.nsIPluginTag.STATE_DISABLED, 1.20 + filename: "/home/mozilla/.plugins/dupplugin1.so" 1.21 +}, { 1.22 + name: "Duplicate Plugin 1", 1.23 + description: "A duplicate plugin", 1.24 + version: "1", 1.25 + blocklisted: false, 1.26 + enabledState: Ci.nsIPluginTag.STATE_ENABLED, 1.27 + get disabled() this.enabledState == Ci.nsIPluginTag.STATE_DISABLED, 1.28 + filename: "", 1.29 + filename: "/usr/lib/plugins/dupplugin1.so" 1.30 +}, { 1.31 + name: "Duplicate Plugin 2", 1.32 + description: "Another duplicate plugin", 1.33 + version: "1", 1.34 + blocklisted: false, 1.35 + enabledState: Ci.nsIPluginTag.STATE_ENABLED, 1.36 + get disabled() this.enabledState == Ci.nsIPluginTag.STATE_DISABLED, 1.37 + filename: "/home/mozilla/.plugins/dupplugin2.so" 1.38 +}, { 1.39 + name: "Duplicate Plugin 2", 1.40 + description: "Another duplicate plugin", 1.41 + version: "1", 1.42 + blocklisted: false, 1.43 + enabledState: Ci.nsIPluginTag.STATE_ENABLED, 1.44 + get disabled() this.enabledState == Ci.nsIPluginTag.STATE_DISABLED, 1.45 + filename: "", 1.46 + filename: "/usr/lib/plugins/dupplugin2.so" 1.47 +}, { 1.48 + name: "Non-duplicate Plugin", // 3 1.49 + description: "Not a duplicate plugin", 1.50 + version: "1", 1.51 + blocklisted: false, 1.52 + enabledState: Ci.nsIPluginTag.STATE_ENABLED, 1.53 + get disabled() this.enabledState == Ci.nsIPluginTag.STATE_DISABLED, 1.54 + filename: "/home/mozilla/.plugins/dupplugin3.so" 1.55 +}, { 1.56 + name: "Non-duplicate Plugin", // 4 1.57 + description: "Not a duplicate because the descriptions are different", 1.58 + version: "1", 1.59 + blocklisted: false, 1.60 + enabledState: Ci.nsIPluginTag.STATE_ENABLED, 1.61 + get disabled() this.enabledState == Ci.nsIPluginTag.STATE_DISABLED, 1.62 + filename: "", 1.63 + filename: "/usr/lib/plugins/dupplugin4.so" 1.64 +}, { 1.65 + name: "Another Non-duplicate Plugin", // 5 1.66 + description: "Not a duplicate plugin", 1.67 + version: "1", 1.68 + blocklisted: false, 1.69 + enabledState: Ci.nsIPluginTag.STATE_ENABLED, 1.70 + get disabled() this.enabledState == Ci.nsIPluginTag.STATE_DISABLED, 1.71 + filename: "/home/mozilla/.plugins/dupplugin5.so" 1.72 +}]; 1.73 + 1.74 +// A fake plugin host to return the plugins defined above 1.75 +var PluginHost = { 1.76 + getPluginTags: function(countRef) { 1.77 + countRef.value = PLUGINS.length; 1.78 + return PLUGINS; 1.79 + }, 1.80 + 1.81 + QueryInterface: function(iid) { 1.82 + if (iid.equals(Components.interfaces.nsIPluginHost) 1.83 + || iid.equals(Components.interfaces.nsISupports)) 1.84 + return this; 1.85 + 1.86 + throw Components.results.NS_ERROR_NO_INTERFACE; 1.87 + } 1.88 +} 1.89 + 1.90 +var PluginHostFactory = { 1.91 + createInstance: function (outer, iid) { 1.92 + if (outer != null) 1.93 + throw Components.results.NS_ERROR_NO_AGGREGATION; 1.94 + return PluginHost.QueryInterface(iid); 1.95 + } 1.96 +}; 1.97 + 1.98 +var registrar = Components.manager.QueryInterface(Components.interfaces.nsIComponentRegistrar); 1.99 +registrar.registerFactory(Components.ID("{721c3e73-969e-474b-a6dc-059fd288c428}"), 1.100 + "Fake Plugin Host", 1.101 + "@mozilla.org/plugin/host;1", PluginHostFactory); 1.102 + 1.103 +var gPluginIDs = [null, null, null, null, null]; 1.104 + 1.105 +function run_test() { 1.106 + do_test_pending(); 1.107 + createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2"); 1.108 + 1.109 + startupManager(); 1.110 + 1.111 + run_test_1(); 1.112 +} 1.113 + 1.114 +function found_plugin(aNum, aId) { 1.115 + if (gPluginIDs[aNum]) 1.116 + do_throw("Found duplicate of plugin " + aNum); 1.117 + gPluginIDs[aNum] = aId; 1.118 +} 1.119 + 1.120 +// Test that the plugins were coalesced and all appear in the returned list 1.121 +function run_test_1() { 1.122 + AddonManager.getAddonsByTypes(["plugin"], function(aAddons) { 1.123 + do_check_eq(aAddons.length, 5); 1.124 + aAddons.forEach(function(aAddon) { 1.125 + if (aAddon.name == "Duplicate Plugin 1") { 1.126 + found_plugin(0, aAddon.id); 1.127 + do_check_eq(aAddon.description, "A duplicate plugin"); 1.128 + } 1.129 + else if (aAddon.name == "Duplicate Plugin 2") { 1.130 + found_plugin(1, aAddon.id); 1.131 + do_check_eq(aAddon.description, "Another duplicate plugin"); 1.132 + } 1.133 + else if (aAddon.name == "Another Non-duplicate Plugin") { 1.134 + found_plugin(5, aAddon.id); 1.135 + do_check_eq(aAddon.description, "Not a duplicate plugin"); 1.136 + } 1.137 + else if (aAddon.name == "Non-duplicate Plugin") { 1.138 + if (aAddon.description == "Not a duplicate plugin") 1.139 + found_plugin(3, aAddon.id); 1.140 + else if (aAddon.description == "Not a duplicate because the descriptions are different") 1.141 + found_plugin(4, aAddon.id); 1.142 + else 1.143 + do_throw("Found unexpected plugin with description " + aAddon.description); 1.144 + } 1.145 + else { 1.146 + do_throw("Found unexpected plugin " + aAddon.name); 1.147 + } 1.148 + }); 1.149 + 1.150 + run_test_2(); 1.151 + }); 1.152 +} 1.153 + 1.154 +// Test that disabling a coalesced plugin disables all its tags 1.155 +function run_test_2() { 1.156 + AddonManager.getAddonByID(gPluginIDs[0], function(p) { 1.157 + do_check_false(p.userDisabled); 1.158 + p.userDisabled = true; 1.159 + do_check_true(PLUGINS[0].disabled); 1.160 + do_check_true(PLUGINS[1].disabled); 1.161 + 1.162 + do_execute_soon(run_test_3); 1.163 + }); 1.164 +} 1.165 + 1.166 +// Test that IDs persist across restart 1.167 +function run_test_3() { 1.168 + restartManager(); 1.169 + 1.170 + AddonManager.getAddonByID(gPluginIDs[0], callback_soon(function(p) { 1.171 + do_check_neq(p, null); 1.172 + do_check_eq(p.name, "Duplicate Plugin 1"); 1.173 + do_check_eq(p.description, "A duplicate plugin"); 1.174 + 1.175 + // Reorder the plugins and restart again 1.176 + [PLUGINS[0], PLUGINS[1]] = [PLUGINS[1], PLUGINS[0]]; 1.177 + restartManager(); 1.178 + 1.179 + AddonManager.getAddonByID(gPluginIDs[0], function(p) { 1.180 + do_check_neq(p, null); 1.181 + do_check_eq(p.name, "Duplicate Plugin 1"); 1.182 + do_check_eq(p.description, "A duplicate plugin"); 1.183 + 1.184 + do_execute_soon(do_test_finished); 1.185 + }); 1.186 + })); 1.187 +}