1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/mozapps/extensions/test/xpcshell/test_bug514327_3.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,166 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +const Cc = Components.classes; 1.9 +const Ci = Components.interfaces; 1.10 +const Cu = Components.utils; 1.11 +const Cr = Components.results; 1.12 + 1.13 +Cu.import("resource://testing-common/httpd.js"); 1.14 + 1.15 +const nsIBLS = Ci.nsIBlocklistService; 1.16 +const URI_EXTENSION_BLOCKLIST_DIALOG = "chrome://mozapps/content/extensions/blocklist.xul"; 1.17 + 1.18 +var gBlocklist = null; 1.19 +var gPrefs = null; 1.20 +var gTestserver = null; 1.21 + 1.22 +var gNextTestPart = null; 1.23 + 1.24 + 1.25 +var PLUGINS = [{ 1.26 + // Tests a plugin whose state goes from not-blocked, to outdated 1.27 + name: "test_bug514327_outdated", 1.28 + version: "5", 1.29 + disabled: false, 1.30 + blocklisted: false 1.31 +}, { 1.32 + // Used to trigger the blocklist dialog, which indicates the blocklist has updated 1.33 + name: "test_bug514327_1", 1.34 + version: "5", 1.35 + disabled: false, 1.36 + blocklisted: false 1.37 +}, { 1.38 + // Used to trigger the blocklist dialog, which indicates the blocklist has updated 1.39 + name: "test_bug514327_2", 1.40 + version: "5", 1.41 + disabled: false, 1.42 + blocklisted: false 1.43 +} ]; 1.44 + 1.45 + 1.46 +// A fake plugin host for the blocklist service to use 1.47 +var PluginHost = { 1.48 + getPluginTags: function(countRef) { 1.49 + countRef.value = PLUGINS.length; 1.50 + return PLUGINS; 1.51 + }, 1.52 + 1.53 + QueryInterface: function(iid) { 1.54 + if (iid.equals(Ci.nsIPluginHost) 1.55 + || iid.equals(Ci.nsISupports)) 1.56 + return this; 1.57 + 1.58 + throw Components.results.NS_ERROR_NO_INTERFACE; 1.59 + } 1.60 +} 1.61 + 1.62 +var PluginHostFactory = { 1.63 + createInstance: function (outer, iid) { 1.64 + if (outer != null) 1.65 + throw Components.results.NS_ERROR_NO_AGGREGATION; 1.66 + return PluginHost.QueryInterface(iid); 1.67 + } 1.68 +}; 1.69 + 1.70 +// Don't need the full interface, attempts to call other methods will just 1.71 +// throw which is just fine 1.72 +var WindowWatcher = { 1.73 + openWindow: function(parent, url, name, features, arguments) { 1.74 + // Should be called to list the newly blocklisted items 1.75 + do_check_eq(url, URI_EXTENSION_BLOCKLIST_DIALOG); 1.76 + // Should only include one item 1.77 + do_check_eq(arguments.wrappedJSObject.list.length, 1); 1.78 + // And that item should be the blocked plugin, not the outdated one 1.79 + var item = arguments.wrappedJSObject.list[0]; 1.80 + do_check_true(item.item instanceof Ci.nsIPluginTag); 1.81 + do_check_neq(item.name, "test_bug514327_outdated"); 1.82 + 1.83 + // Call the next test after the blocklist has finished up 1.84 + do_timeout(0, gNextTestPart); 1.85 + }, 1.86 + 1.87 + QueryInterface: function(iid) { 1.88 + if (iid.equals(Ci.nsIWindowWatcher) 1.89 + || iid.equals(Ci.nsISupports)) 1.90 + return this; 1.91 + 1.92 + throw Cr.NS_ERROR_NO_INTERFACE; 1.93 + } 1.94 +} 1.95 + 1.96 +var WindowWatcherFactory = { 1.97 + createInstance: function createInstance(outer, iid) { 1.98 + if (outer != null) 1.99 + throw Components.results.NS_ERROR_NO_AGGREGATION; 1.100 + return WindowWatcher.QueryInterface(iid); 1.101 + } 1.102 +}; 1.103 + 1.104 +var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); 1.105 +registrar.registerFactory(Components.ID("{721c3e73-969e-474b-a6dc-059fd288c428}"), 1.106 + "Fake Plugin Host", 1.107 + "@mozilla.org/plugin/host;1", PluginHostFactory); 1.108 +registrar.registerFactory(Components.ID("{1dfeb90a-2193-45d5-9cb8-864928b2af55}"), 1.109 + "Fake Window Watcher", 1.110 + "@mozilla.org/embedcomp/window-watcher;1", WindowWatcherFactory); 1.111 + 1.112 + 1.113 +function do_update_blocklist(aDatafile, aNextPart) { 1.114 + gNextTestPart = aNextPart; 1.115 + 1.116 + gPrefs.setCharPref("extensions.blocklist.url", "http://localhost:" + gPort + "/data/" + aDatafile); 1.117 + gBlocklist.QueryInterface(Ci.nsITimerCallback).notify(null); 1.118 +} 1.119 + 1.120 +function run_test() { 1.121 + createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9"); 1.122 + 1.123 + gTestserver = new HttpServer(); 1.124 + gTestserver.registerDirectory("/data/", do_get_file("data")); 1.125 + gTestserver.start(-1); 1.126 + gPort = gTestserver.identity.primaryPort; 1.127 + 1.128 + startupManager(); 1.129 + 1.130 + // initialize the blocklist with no entries 1.131 + copyBlocklistToProfile(do_get_file("data/test_bug514327_3_empty.xml")); 1.132 + 1.133 + gPrefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch); 1.134 + gBlocklist = Cc["@mozilla.org/extensions/blocklist;1"].getService(nsIBLS); 1.135 + 1.136 + // should NOT be marked as outdated by the blocklist 1.137 + do_check_true(gBlocklist.getPluginBlocklistState(PLUGINS[0], "1", "1.9") == nsIBLS.STATE_NOT_BLOCKED); 1.138 + 1.139 + do_test_pending(); 1.140 + 1.141 + // update blocklist with data that marks the plugin as outdated 1.142 + do_update_blocklist("test_bug514327_3_outdated_1.xml", test_part_1); 1.143 +} 1.144 + 1.145 +function test_part_1() { 1.146 + // plugin should now be marked as outdated 1.147 + do_check_true(gBlocklist.getPluginBlocklistState(PLUGINS[0], "1", "1.9") == nsIBLS.STATE_OUTDATED); 1.148 + // and the notifyUser pref should be set to true 1.149 + do_check_true(gPrefs.getBoolPref("plugins.update.notifyUser")); 1.150 + 1.151 + // preternd the user has been notified, reset the pref 1.152 + gPrefs.setBoolPref("plugins.update.notifyUser", false); 1.153 + 1.154 + // update blocklist with data that marks the plugin as outdated 1.155 + do_update_blocklist("test_bug514327_3_outdated_2.xml", test_part_2); 1.156 +} 1.157 + 1.158 +function test_part_2() { 1.159 + // plugin should still be marked as outdated 1.160 + do_check_true(gBlocklist.getPluginBlocklistState(PLUGINS[0], "1", "1.9") == nsIBLS.STATE_OUTDATED); 1.161 + // and the notifyUser pref should NOT be set to true, as the plugin was already outdated 1.162 + do_check_false(gPrefs.getBoolPref("plugins.update.notifyUser")); 1.163 + 1.164 + finish(); 1.165 +} 1.166 + 1.167 +function finish() { 1.168 + gTestserver.stop(do_test_finished); 1.169 +}