1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/mozapps/extensions/test/xpcshell/test_blocklist_regexp.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,125 @@ 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 +// Checks that blocklist entries using RegExp work as expected. This only covers 1.9 +// behavior specific to RegExp entries - general behavior is already tested 1.10 +// in test_blocklistchange.js. 1.11 + 1.12 +const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; 1.13 + 1.14 +const URI_EXTENSION_BLOCKLIST_DIALOG = "chrome://mozapps/content/extensions/blocklist.xul"; 1.15 + 1.16 +Cu.import("resource://testing-common/httpd.js"); 1.17 +var testserver = new HttpServer(); 1.18 +testserver.start(-1); 1.19 +gPort = testserver.identity.primaryPort; 1.20 + 1.21 +// register static files with server and interpolate port numbers in them 1.22 +mapFile("/data/test_blocklist_regexp_1.xml", testserver); 1.23 + 1.24 +const profileDir = gProfD.clone(); 1.25 +profileDir.append("extensions"); 1.26 + 1.27 +// Don't need the full interface, attempts to call other methods will just 1.28 +// throw which is just fine 1.29 +var WindowWatcher = { 1.30 + openWindow: function(parent, url, name, features, arguments) { 1.31 + // Should be called to list the newly blocklisted items 1.32 + do_check_eq(url, URI_EXTENSION_BLOCKLIST_DIALOG); 1.33 + 1.34 + // Simulate auto-disabling any softblocks 1.35 + var list = arguments.wrappedJSObject.list; 1.36 + list.forEach(function(aItem) { 1.37 + if (!aItem.blocked) 1.38 + aItem.disable = true; 1.39 + }); 1.40 + 1.41 + //run the code after the blocklist is closed 1.42 + Services.obs.notifyObservers(null, "addon-blocklist-closed", null); 1.43 + 1.44 + }, 1.45 + 1.46 + QueryInterface: function(iid) { 1.47 + if (iid.equals(Ci.nsIWindowWatcher) 1.48 + || iid.equals(Ci.nsISupports)) 1.49 + return this; 1.50 + 1.51 + throw Cr.NS_ERROR_NO_INTERFACE; 1.52 + } 1.53 +}; 1.54 + 1.55 +var WindowWatcherFactory = { 1.56 + createInstance: function createInstance(outer, iid) { 1.57 + if (outer != null) 1.58 + throw Cr.NS_ERROR_NO_AGGREGATION; 1.59 + return WindowWatcher.QueryInterface(iid); 1.60 + } 1.61 +}; 1.62 + 1.63 +var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); 1.64 +registrar.registerFactory(Components.ID("{1dfeb90a-2193-45d5-9cb8-864928b2af55}"), 1.65 + "Fake Window Watcher", 1.66 + "@mozilla.org/embedcomp/window-watcher;1", 1.67 + WindowWatcherFactory); 1.68 + 1.69 + 1.70 +function load_blocklist(aFile, aCallback) { 1.71 + Services.obs.addObserver(function() { 1.72 + Services.obs.removeObserver(arguments.callee, "blocklist-updated"); 1.73 + 1.74 + do_execute_soon(aCallback); 1.75 + }, "blocklist-updated", false); 1.76 + 1.77 + Services.prefs.setCharPref("extensions.blocklist.url", "http://localhost:" + 1.78 + gPort + "/data/" + aFile); 1.79 + var blocklist = Cc["@mozilla.org/extensions/blocklist;1"]. 1.80 + getService(Ci.nsITimerCallback); 1.81 + blocklist.notify(null); 1.82 +} 1.83 + 1.84 + 1.85 +function end_test() { 1.86 + testserver.stop(do_test_finished); 1.87 +} 1.88 + 1.89 + 1.90 +function run_test() { 1.91 + do_test_pending(); 1.92 + 1.93 + createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1"); 1.94 + 1.95 + writeInstallRDFForExtension({ 1.96 + id: "block1@tests.mozilla.org", 1.97 + version: "1.0", 1.98 + name: "RegExp blocked add-on", 1.99 + targetApplications: [{ 1.100 + id: "xpcshell@tests.mozilla.org", 1.101 + minVersion: "1", 1.102 + maxVersion: "3" 1.103 + }] 1.104 + }, profileDir); 1.105 + 1.106 + startupManager(); 1.107 + 1.108 + AddonManager.getAddonsByIDs(["block1@tests.mozilla.org"], function([a1]) { 1.109 + do_check_eq(a1.blocklistState, Ci.nsIBlocklistService.STATE_NOT_BLOCKED); 1.110 + 1.111 + run_test_1(); 1.112 + }); 1.113 +} 1.114 + 1.115 +function run_test_1() { 1.116 + load_blocklist("test_blocklist_regexp_1.xml", function() { 1.117 + restartManager(); 1.118 + 1.119 + AddonManager.getAddonsByIDs(["block1@tests.mozilla.org"], function([a1]) { 1.120 + // Blocklist contains two entries that will match this addon - ensure 1.121 + // that the first one is applied. 1.122 + do_check_neq(a1, null); 1.123 + do_check_eq(a1.blocklistState, Ci.nsIBlocklistService.STATE_SOFTBLOCKED); 1.124 + 1.125 + end_test(); 1.126 + }); 1.127 + }); 1.128 +}