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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/mozapps/extensions/test/xpcshell/test_blocklist_prefs.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,159 @@
     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 +// Tests resetting of preferences in blocklist entry when an add-on is blocked.
     1.9 +// See bug 802434.
    1.10 +
    1.11 +const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
    1.12 +
    1.13 +const URI_EXTENSION_BLOCKLIST_DIALOG = "chrome://mozapps/content/extensions/blocklist.xul";
    1.14 +
    1.15 +XPCOMUtils.defineLazyGetter(this, "gPref", function bls_gPref() {
    1.16 +  return Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService).
    1.17 +         QueryInterface(Ci.nsIPrefBranch);
    1.18 +});
    1.19 +
    1.20 +Cu.import("resource://testing-common/httpd.js");
    1.21 +var testserver = new HttpServer();
    1.22 +testserver.start(-1);
    1.23 +gPort = testserver.identity.primaryPort;
    1.24 +
    1.25 +// register static files with server and interpolate port numbers in them
    1.26 +mapFile("/data/test_blocklist_prefs_1.xml", testserver);
    1.27 +
    1.28 +const profileDir = gProfD.clone();
    1.29 +profileDir.append("extensions");
    1.30 +
    1.31 +// A window watcher to handle the blocklist UI.
    1.32 +// Don't need the full interface, attempts to call other methods will just
    1.33 +// throw which is just fine
    1.34 +var WindowWatcher = {
    1.35 +  openWindow: function(parent, url, name, features, arguments) {
    1.36 +    // Should be called to list the newly blocklisted items
    1.37 +    do_check_eq(url, URI_EXTENSION_BLOCKLIST_DIALOG);
    1.38 +
    1.39 +    // Simulate auto-disabling any softblocks
    1.40 +    var list = arguments.wrappedJSObject.list;
    1.41 +    list.forEach(function(aItem) {
    1.42 +      if (!aItem.blocked)
    1.43 +        aItem.disable = true;
    1.44 +    });
    1.45 +
    1.46 +    //run the code after the blocklist is closed
    1.47 +    Services.obs.notifyObservers(null, "addon-blocklist-closed", null);
    1.48 +
    1.49 +  },
    1.50 +
    1.51 +  QueryInterface: function(iid) {
    1.52 +    if (iid.equals(Ci.nsIWindowWatcher)
    1.53 +     || iid.equals(Ci.nsISupports))
    1.54 +      return this;
    1.55 +
    1.56 +    throw Cr.NS_ERROR_NO_INTERFACE;
    1.57 +  }
    1.58 +};
    1.59 +
    1.60 +var WindowWatcherFactory = {
    1.61 +  createInstance: function createInstance(outer, iid) {
    1.62 +    if (outer != null)
    1.63 +      throw Cr.NS_ERROR_NO_AGGREGATION;
    1.64 +    return WindowWatcher.QueryInterface(iid);
    1.65 +  }
    1.66 +};
    1.67 +
    1.68 +var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
    1.69 +registrar.registerFactory(Components.ID("{1dfeb90a-2193-45d5-9cb8-864928b2af55}"),
    1.70 +                          "Fake Window Watcher",
    1.71 +                          "@mozilla.org/embedcomp/window-watcher;1",
    1.72 +                          WindowWatcherFactory);
    1.73 +
    1.74 +function load_blocklist(aFile, aCallback) {
    1.75 +  Services.obs.addObserver(function() {
    1.76 +    Services.obs.removeObserver(arguments.callee, "blocklist-updated");
    1.77 +
    1.78 +    do_execute_soon(aCallback);
    1.79 +  }, "blocklist-updated", false);
    1.80 +
    1.81 +  Services.prefs.setCharPref("extensions.blocklist.url", "http://localhost:" +
    1.82 +                             gPort + "/data/" + aFile);
    1.83 +  var blocklist = Cc["@mozilla.org/extensions/blocklist;1"].
    1.84 +                  getService(Ci.nsITimerCallback);
    1.85 +  blocklist.notify(null);
    1.86 +}
    1.87 +
    1.88 +function end_test() {
    1.89 +  testserver.stop(do_test_finished);
    1.90 +}
    1.91 +
    1.92 +function run_test() {
    1.93 +  do_test_pending();
    1.94 +
    1.95 +  createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1");
    1.96 +
    1.97 +  // Add 2 extensions
    1.98 +  writeInstallRDFForExtension({
    1.99 +    id: "block1@tests.mozilla.org",
   1.100 +    version: "1.0",
   1.101 +    name: "Blocked add-on-1 with to-be-reset prefs",
   1.102 +    targetApplications: [{
   1.103 +      id: "xpcshell@tests.mozilla.org",
   1.104 +      minVersion: "1",
   1.105 +      maxVersion: "3"
   1.106 +    }]
   1.107 +  }, profileDir);
   1.108 +
   1.109 +  writeInstallRDFForExtension({
   1.110 +    id: "block2@tests.mozilla.org",
   1.111 +    version: "1.0",
   1.112 +    name: "Blocked add-on-2 with to-be-reset prefs",
   1.113 +    targetApplications: [{
   1.114 +      id: "xpcshell@tests.mozilla.org",
   1.115 +      minVersion: "1",
   1.116 +      maxVersion: "3"
   1.117 +    }]
   1.118 +  }, profileDir);
   1.119 +
   1.120 +  // Pre-set the preferences that we expect to get reset.
   1.121 +  gPref.setIntPref("test.blocklist.pref1", 15);
   1.122 +  gPref.setIntPref("test.blocklist.pref2", 15);
   1.123 +  gPref.setBoolPref("test.blocklist.pref3", true);
   1.124 +  gPref.setBoolPref("test.blocklist.pref4", true);
   1.125 +
   1.126 +  startupManager();
   1.127 +
   1.128 +  // Before blocklist is loaded.
   1.129 +  AddonManager.getAddonsByIDs(["block1@tests.mozilla.org",
   1.130 +                               "block2@tests.mozilla.org"], function([a1, a2]) {
   1.131 +    do_check_eq(a1.blocklistState, Ci.nsIBlocklistService.STATE_NOT_BLOCKED);
   1.132 +    do_check_eq(a2.blocklistState, Ci.nsIBlocklistService.STATE_NOT_BLOCKED);
   1.133 +
   1.134 +    do_check_eq(gPref.getIntPref("test.blocklist.pref1"), 15);
   1.135 +    do_check_eq(gPref.getIntPref("test.blocklist.pref2"), 15);
   1.136 +    do_check_eq(gPref.getBoolPref("test.blocklist.pref3"), true);
   1.137 +    do_check_eq(gPref.getBoolPref("test.blocklist.pref4"), true);
   1.138 +    run_test_1();
   1.139 +  });
   1.140 +}
   1.141 +
   1.142 +function run_test_1() {
   1.143 +  load_blocklist("test_blocklist_prefs_1.xml", function() {
   1.144 +    restartManager();
   1.145 +
   1.146 +    // Blocklist changes should have applied and the prefs must be reset.
   1.147 +    AddonManager.getAddonsByIDs(["block1@tests.mozilla.org",
   1.148 +                                 "block2@tests.mozilla.org"], function([a1, a2]) {
   1.149 +      do_check_neq(a1, null);
   1.150 +      do_check_eq(a1.blocklistState, Ci.nsIBlocklistService.STATE_SOFTBLOCKED);
   1.151 +      do_check_neq(a2, null);
   1.152 +      do_check_eq(a2.blocklistState, Ci.nsIBlocklistService.STATE_BLOCKED);
   1.153 +
   1.154 +      // All these prefs must be reset to defaults.
   1.155 +      do_check_eq(gPref.prefHasUserValue("test.blocklist.pref1"), false);
   1.156 +      do_check_eq(gPref.prefHasUserValue("test.blocklist.pref2"), false);
   1.157 +      do_check_eq(gPref.prefHasUserValue("test.blocklist.pref3"), false);
   1.158 +      do_check_eq(gPref.prefHasUserValue("test.blocklist.pref4"), false);
   1.159 +      end_test();
   1.160 +    });
   1.161 +  });
   1.162 +}

mercurial