toolkit/mozapps/extensions/test/xpcshell/test_blocklist_regexp.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* Any copyright is dedicated to the Public Domain.
     2  * http://creativecommons.org/publicdomain/zero/1.0/
     3  */
     5 // Checks that blocklist entries using RegExp work as expected. This only covers
     6 // behavior specific to RegExp entries - general behavior is already tested
     7 // in test_blocklistchange.js.
     9 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
    11 const URI_EXTENSION_BLOCKLIST_DIALOG = "chrome://mozapps/content/extensions/blocklist.xul";
    13 Cu.import("resource://testing-common/httpd.js");
    14 var testserver = new HttpServer();
    15 testserver.start(-1);
    16 gPort = testserver.identity.primaryPort;
    18 // register static files with server and interpolate port numbers in them
    19 mapFile("/data/test_blocklist_regexp_1.xml", testserver);
    21 const profileDir = gProfD.clone();
    22 profileDir.append("extensions");
    24 // Don't need the full interface, attempts to call other methods will just
    25 // throw which is just fine
    26 var WindowWatcher = {
    27   openWindow: function(parent, url, name, features, arguments) {
    28     // Should be called to list the newly blocklisted items
    29     do_check_eq(url, URI_EXTENSION_BLOCKLIST_DIALOG);
    31     // Simulate auto-disabling any softblocks
    32     var list = arguments.wrappedJSObject.list;
    33     list.forEach(function(aItem) {
    34       if (!aItem.blocked)
    35         aItem.disable = true;
    36     });
    38     //run the code after the blocklist is closed
    39     Services.obs.notifyObservers(null, "addon-blocklist-closed", null);
    41   },
    43   QueryInterface: function(iid) {
    44     if (iid.equals(Ci.nsIWindowWatcher)
    45      || iid.equals(Ci.nsISupports))
    46       return this;
    48     throw Cr.NS_ERROR_NO_INTERFACE;
    49   }
    50 };
    52 var WindowWatcherFactory = {
    53   createInstance: function createInstance(outer, iid) {
    54     if (outer != null)
    55       throw Cr.NS_ERROR_NO_AGGREGATION;
    56     return WindowWatcher.QueryInterface(iid);
    57   }
    58 };
    60 var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
    61 registrar.registerFactory(Components.ID("{1dfeb90a-2193-45d5-9cb8-864928b2af55}"),
    62                           "Fake Window Watcher",
    63                           "@mozilla.org/embedcomp/window-watcher;1",
    64                           WindowWatcherFactory);
    67 function load_blocklist(aFile, aCallback) {
    68   Services.obs.addObserver(function() {
    69     Services.obs.removeObserver(arguments.callee, "blocklist-updated");
    71     do_execute_soon(aCallback);
    72   }, "blocklist-updated", false);
    74   Services.prefs.setCharPref("extensions.blocklist.url", "http://localhost:" +
    75                              gPort + "/data/" + aFile);
    76   var blocklist = Cc["@mozilla.org/extensions/blocklist;1"].
    77                   getService(Ci.nsITimerCallback);
    78   blocklist.notify(null);
    79 }
    82 function end_test() {
    83   testserver.stop(do_test_finished);
    84 }
    87 function run_test() {
    88   do_test_pending();
    90   createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1");
    92   writeInstallRDFForExtension({
    93     id: "block1@tests.mozilla.org",
    94     version: "1.0",
    95     name: "RegExp blocked add-on",
    96     targetApplications: [{
    97       id: "xpcshell@tests.mozilla.org",
    98       minVersion: "1",
    99       maxVersion: "3"
   100     }]
   101   }, profileDir);
   103   startupManager();
   105   AddonManager.getAddonsByIDs(["block1@tests.mozilla.org"], function([a1]) {
   106     do_check_eq(a1.blocklistState, Ci.nsIBlocklistService.STATE_NOT_BLOCKED);
   108     run_test_1();
   109   });
   110 }
   112 function run_test_1() {
   113   load_blocklist("test_blocklist_regexp_1.xml", function() {
   114     restartManager();
   116     AddonManager.getAddonsByIDs(["block1@tests.mozilla.org"], function([a1]) {
   117       // Blocklist contains two entries that will match this addon - ensure
   118       // that the first one is applied.
   119       do_check_neq(a1, null);
   120       do_check_eq(a1.blocklistState, Ci.nsIBlocklistService.STATE_SOFTBLOCKED);
   122       end_test();
   123     });
   124   });
   125 }

mercurial