michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ michael@0: */ michael@0: michael@0: // Tests resetting of preferences in blocklist entry when an add-on is blocked. michael@0: // See bug 802434. michael@0: michael@0: const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; michael@0: michael@0: const URI_EXTENSION_BLOCKLIST_DIALOG = "chrome://mozapps/content/extensions/blocklist.xul"; michael@0: michael@0: XPCOMUtils.defineLazyGetter(this, "gPref", function bls_gPref() { michael@0: return Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService). michael@0: QueryInterface(Ci.nsIPrefBranch); michael@0: }); michael@0: michael@0: Cu.import("resource://testing-common/httpd.js"); michael@0: var testserver = new HttpServer(); michael@0: testserver.start(-1); michael@0: gPort = testserver.identity.primaryPort; michael@0: michael@0: // register static files with server and interpolate port numbers in them michael@0: mapFile("/data/test_blocklist_prefs_1.xml", testserver); michael@0: michael@0: const profileDir = gProfD.clone(); michael@0: profileDir.append("extensions"); michael@0: michael@0: // A window watcher to handle the blocklist UI. michael@0: // Don't need the full interface, attempts to call other methods will just michael@0: // throw which is just fine michael@0: var WindowWatcher = { michael@0: openWindow: function(parent, url, name, features, arguments) { michael@0: // Should be called to list the newly blocklisted items michael@0: do_check_eq(url, URI_EXTENSION_BLOCKLIST_DIALOG); michael@0: michael@0: // Simulate auto-disabling any softblocks michael@0: var list = arguments.wrappedJSObject.list; michael@0: list.forEach(function(aItem) { michael@0: if (!aItem.blocked) michael@0: aItem.disable = true; michael@0: }); michael@0: michael@0: //run the code after the blocklist is closed michael@0: Services.obs.notifyObservers(null, "addon-blocklist-closed", null); michael@0: michael@0: }, michael@0: michael@0: QueryInterface: function(iid) { michael@0: if (iid.equals(Ci.nsIWindowWatcher) michael@0: || iid.equals(Ci.nsISupports)) michael@0: return this; michael@0: michael@0: throw Cr.NS_ERROR_NO_INTERFACE; michael@0: } michael@0: }; michael@0: michael@0: var WindowWatcherFactory = { michael@0: createInstance: function createInstance(outer, iid) { michael@0: if (outer != null) michael@0: throw Cr.NS_ERROR_NO_AGGREGATION; michael@0: return WindowWatcher.QueryInterface(iid); michael@0: } michael@0: }; michael@0: michael@0: var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); michael@0: registrar.registerFactory(Components.ID("{1dfeb90a-2193-45d5-9cb8-864928b2af55}"), michael@0: "Fake Window Watcher", michael@0: "@mozilla.org/embedcomp/window-watcher;1", michael@0: WindowWatcherFactory); michael@0: michael@0: function load_blocklist(aFile, aCallback) { michael@0: Services.obs.addObserver(function() { michael@0: Services.obs.removeObserver(arguments.callee, "blocklist-updated"); michael@0: michael@0: do_execute_soon(aCallback); michael@0: }, "blocklist-updated", false); michael@0: michael@0: Services.prefs.setCharPref("extensions.blocklist.url", "http://localhost:" + michael@0: gPort + "/data/" + aFile); michael@0: var blocklist = Cc["@mozilla.org/extensions/blocklist;1"]. michael@0: getService(Ci.nsITimerCallback); michael@0: blocklist.notify(null); michael@0: } michael@0: michael@0: function end_test() { michael@0: testserver.stop(do_test_finished); michael@0: } michael@0: michael@0: function run_test() { michael@0: do_test_pending(); michael@0: michael@0: createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1"); michael@0: michael@0: // Add 2 extensions michael@0: writeInstallRDFForExtension({ michael@0: id: "block1@tests.mozilla.org", michael@0: version: "1.0", michael@0: name: "Blocked add-on-1 with to-be-reset prefs", michael@0: targetApplications: [{ michael@0: id: "xpcshell@tests.mozilla.org", michael@0: minVersion: "1", michael@0: maxVersion: "3" michael@0: }] michael@0: }, profileDir); michael@0: michael@0: writeInstallRDFForExtension({ michael@0: id: "block2@tests.mozilla.org", michael@0: version: "1.0", michael@0: name: "Blocked add-on-2 with to-be-reset prefs", michael@0: targetApplications: [{ michael@0: id: "xpcshell@tests.mozilla.org", michael@0: minVersion: "1", michael@0: maxVersion: "3" michael@0: }] michael@0: }, profileDir); michael@0: michael@0: // Pre-set the preferences that we expect to get reset. michael@0: gPref.setIntPref("test.blocklist.pref1", 15); michael@0: gPref.setIntPref("test.blocklist.pref2", 15); michael@0: gPref.setBoolPref("test.blocklist.pref3", true); michael@0: gPref.setBoolPref("test.blocklist.pref4", true); michael@0: michael@0: startupManager(); michael@0: michael@0: // Before blocklist is loaded. michael@0: AddonManager.getAddonsByIDs(["block1@tests.mozilla.org", michael@0: "block2@tests.mozilla.org"], function([a1, a2]) { michael@0: do_check_eq(a1.blocklistState, Ci.nsIBlocklistService.STATE_NOT_BLOCKED); michael@0: do_check_eq(a2.blocklistState, Ci.nsIBlocklistService.STATE_NOT_BLOCKED); michael@0: michael@0: do_check_eq(gPref.getIntPref("test.blocklist.pref1"), 15); michael@0: do_check_eq(gPref.getIntPref("test.blocklist.pref2"), 15); michael@0: do_check_eq(gPref.getBoolPref("test.blocklist.pref3"), true); michael@0: do_check_eq(gPref.getBoolPref("test.blocklist.pref4"), true); michael@0: run_test_1(); michael@0: }); michael@0: } michael@0: michael@0: function run_test_1() { michael@0: load_blocklist("test_blocklist_prefs_1.xml", function() { michael@0: restartManager(); michael@0: michael@0: // Blocklist changes should have applied and the prefs must be reset. michael@0: AddonManager.getAddonsByIDs(["block1@tests.mozilla.org", michael@0: "block2@tests.mozilla.org"], function([a1, a2]) { michael@0: do_check_neq(a1, null); michael@0: do_check_eq(a1.blocklistState, Ci.nsIBlocklistService.STATE_SOFTBLOCKED); michael@0: do_check_neq(a2, null); michael@0: do_check_eq(a2.blocklistState, Ci.nsIBlocklistService.STATE_BLOCKED); michael@0: michael@0: // All these prefs must be reset to defaults. michael@0: do_check_eq(gPref.prefHasUserValue("test.blocklist.pref1"), false); michael@0: do_check_eq(gPref.prefHasUserValue("test.blocklist.pref2"), false); michael@0: do_check_eq(gPref.prefHasUserValue("test.blocklist.pref3"), false); michael@0: do_check_eq(gPref.prefHasUserValue("test.blocklist.pref4"), false); michael@0: end_test(); michael@0: }); michael@0: }); michael@0: }