toolkit/mozapps/extensions/test/xpcshell/test_blocklist_prefs.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.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 * http://creativecommons.org/publicdomain/zero/1.0/
michael@0 3 */
michael@0 4
michael@0 5 // Tests resetting of preferences in blocklist entry when an add-on is blocked.
michael@0 6 // See bug 802434.
michael@0 7
michael@0 8 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
michael@0 9
michael@0 10 const URI_EXTENSION_BLOCKLIST_DIALOG = "chrome://mozapps/content/extensions/blocklist.xul";
michael@0 11
michael@0 12 XPCOMUtils.defineLazyGetter(this, "gPref", function bls_gPref() {
michael@0 13 return Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService).
michael@0 14 QueryInterface(Ci.nsIPrefBranch);
michael@0 15 });
michael@0 16
michael@0 17 Cu.import("resource://testing-common/httpd.js");
michael@0 18 var testserver = new HttpServer();
michael@0 19 testserver.start(-1);
michael@0 20 gPort = testserver.identity.primaryPort;
michael@0 21
michael@0 22 // register static files with server and interpolate port numbers in them
michael@0 23 mapFile("/data/test_blocklist_prefs_1.xml", testserver);
michael@0 24
michael@0 25 const profileDir = gProfD.clone();
michael@0 26 profileDir.append("extensions");
michael@0 27
michael@0 28 // A window watcher to handle the blocklist UI.
michael@0 29 // Don't need the full interface, attempts to call other methods will just
michael@0 30 // throw which is just fine
michael@0 31 var WindowWatcher = {
michael@0 32 openWindow: function(parent, url, name, features, arguments) {
michael@0 33 // Should be called to list the newly blocklisted items
michael@0 34 do_check_eq(url, URI_EXTENSION_BLOCKLIST_DIALOG);
michael@0 35
michael@0 36 // Simulate auto-disabling any softblocks
michael@0 37 var list = arguments.wrappedJSObject.list;
michael@0 38 list.forEach(function(aItem) {
michael@0 39 if (!aItem.blocked)
michael@0 40 aItem.disable = true;
michael@0 41 });
michael@0 42
michael@0 43 //run the code after the blocklist is closed
michael@0 44 Services.obs.notifyObservers(null, "addon-blocklist-closed", null);
michael@0 45
michael@0 46 },
michael@0 47
michael@0 48 QueryInterface: function(iid) {
michael@0 49 if (iid.equals(Ci.nsIWindowWatcher)
michael@0 50 || iid.equals(Ci.nsISupports))
michael@0 51 return this;
michael@0 52
michael@0 53 throw Cr.NS_ERROR_NO_INTERFACE;
michael@0 54 }
michael@0 55 };
michael@0 56
michael@0 57 var WindowWatcherFactory = {
michael@0 58 createInstance: function createInstance(outer, iid) {
michael@0 59 if (outer != null)
michael@0 60 throw Cr.NS_ERROR_NO_AGGREGATION;
michael@0 61 return WindowWatcher.QueryInterface(iid);
michael@0 62 }
michael@0 63 };
michael@0 64
michael@0 65 var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
michael@0 66 registrar.registerFactory(Components.ID("{1dfeb90a-2193-45d5-9cb8-864928b2af55}"),
michael@0 67 "Fake Window Watcher",
michael@0 68 "@mozilla.org/embedcomp/window-watcher;1",
michael@0 69 WindowWatcherFactory);
michael@0 70
michael@0 71 function load_blocklist(aFile, aCallback) {
michael@0 72 Services.obs.addObserver(function() {
michael@0 73 Services.obs.removeObserver(arguments.callee, "blocklist-updated");
michael@0 74
michael@0 75 do_execute_soon(aCallback);
michael@0 76 }, "blocklist-updated", false);
michael@0 77
michael@0 78 Services.prefs.setCharPref("extensions.blocklist.url", "http://localhost:" +
michael@0 79 gPort + "/data/" + aFile);
michael@0 80 var blocklist = Cc["@mozilla.org/extensions/blocklist;1"].
michael@0 81 getService(Ci.nsITimerCallback);
michael@0 82 blocklist.notify(null);
michael@0 83 }
michael@0 84
michael@0 85 function end_test() {
michael@0 86 testserver.stop(do_test_finished);
michael@0 87 }
michael@0 88
michael@0 89 function run_test() {
michael@0 90 do_test_pending();
michael@0 91
michael@0 92 createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1");
michael@0 93
michael@0 94 // Add 2 extensions
michael@0 95 writeInstallRDFForExtension({
michael@0 96 id: "block1@tests.mozilla.org",
michael@0 97 version: "1.0",
michael@0 98 name: "Blocked add-on-1 with to-be-reset prefs",
michael@0 99 targetApplications: [{
michael@0 100 id: "xpcshell@tests.mozilla.org",
michael@0 101 minVersion: "1",
michael@0 102 maxVersion: "3"
michael@0 103 }]
michael@0 104 }, profileDir);
michael@0 105
michael@0 106 writeInstallRDFForExtension({
michael@0 107 id: "block2@tests.mozilla.org",
michael@0 108 version: "1.0",
michael@0 109 name: "Blocked add-on-2 with to-be-reset prefs",
michael@0 110 targetApplications: [{
michael@0 111 id: "xpcshell@tests.mozilla.org",
michael@0 112 minVersion: "1",
michael@0 113 maxVersion: "3"
michael@0 114 }]
michael@0 115 }, profileDir);
michael@0 116
michael@0 117 // Pre-set the preferences that we expect to get reset.
michael@0 118 gPref.setIntPref("test.blocklist.pref1", 15);
michael@0 119 gPref.setIntPref("test.blocklist.pref2", 15);
michael@0 120 gPref.setBoolPref("test.blocklist.pref3", true);
michael@0 121 gPref.setBoolPref("test.blocklist.pref4", true);
michael@0 122
michael@0 123 startupManager();
michael@0 124
michael@0 125 // Before blocklist is loaded.
michael@0 126 AddonManager.getAddonsByIDs(["block1@tests.mozilla.org",
michael@0 127 "block2@tests.mozilla.org"], function([a1, a2]) {
michael@0 128 do_check_eq(a1.blocklistState, Ci.nsIBlocklistService.STATE_NOT_BLOCKED);
michael@0 129 do_check_eq(a2.blocklistState, Ci.nsIBlocklistService.STATE_NOT_BLOCKED);
michael@0 130
michael@0 131 do_check_eq(gPref.getIntPref("test.blocklist.pref1"), 15);
michael@0 132 do_check_eq(gPref.getIntPref("test.blocklist.pref2"), 15);
michael@0 133 do_check_eq(gPref.getBoolPref("test.blocklist.pref3"), true);
michael@0 134 do_check_eq(gPref.getBoolPref("test.blocklist.pref4"), true);
michael@0 135 run_test_1();
michael@0 136 });
michael@0 137 }
michael@0 138
michael@0 139 function run_test_1() {
michael@0 140 load_blocklist("test_blocklist_prefs_1.xml", function() {
michael@0 141 restartManager();
michael@0 142
michael@0 143 // Blocklist changes should have applied and the prefs must be reset.
michael@0 144 AddonManager.getAddonsByIDs(["block1@tests.mozilla.org",
michael@0 145 "block2@tests.mozilla.org"], function([a1, a2]) {
michael@0 146 do_check_neq(a1, null);
michael@0 147 do_check_eq(a1.blocklistState, Ci.nsIBlocklistService.STATE_SOFTBLOCKED);
michael@0 148 do_check_neq(a2, null);
michael@0 149 do_check_eq(a2.blocklistState, Ci.nsIBlocklistService.STATE_BLOCKED);
michael@0 150
michael@0 151 // All these prefs must be reset to defaults.
michael@0 152 do_check_eq(gPref.prefHasUserValue("test.blocklist.pref1"), false);
michael@0 153 do_check_eq(gPref.prefHasUserValue("test.blocklist.pref2"), false);
michael@0 154 do_check_eq(gPref.prefHasUserValue("test.blocklist.pref3"), false);
michael@0 155 do_check_eq(gPref.prefHasUserValue("test.blocklist.pref4"), false);
michael@0 156 end_test();
michael@0 157 });
michael@0 158 });
michael@0 159 }

mercurial