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