|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 // a place for miscellaneous social tests |
|
6 |
|
7 let SocialService = Cu.import("resource://gre/modules/SocialService.jsm", {}).SocialService; |
|
8 |
|
9 const URI_EXTENSION_BLOCKLIST_DIALOG = "chrome://mozapps/content/extensions/blocklist.xul"; |
|
10 let blocklistURL = "http://example.com/browser/browser/base/content/test/social/blocklist.xml"; |
|
11 |
|
12 let manifest = { // normal provider |
|
13 name: "provider ok", |
|
14 origin: "https://example.com", |
|
15 sidebarURL: "https://example.com/browser/browser/base/content/test/social/social_sidebar.html", |
|
16 workerURL: "https://example.com/browser/browser/base/content/test/social/social_worker.js", |
|
17 iconURL: "https://example.com/browser/browser/base/content/test/general/moz.png" |
|
18 }; |
|
19 let manifest_bad = { // normal provider |
|
20 name: "provider blocked", |
|
21 origin: "https://test1.example.com", |
|
22 sidebarURL: "https://test1.example.com/browser/browser/base/content/test/social/social_sidebar.html", |
|
23 workerURL: "https://test1.example.com/browser/browser/base/content/test/social/social_worker.js", |
|
24 iconURL: "https://test1.example.com/browser/browser/base/content/test/general/moz.png" |
|
25 }; |
|
26 |
|
27 function test() { |
|
28 waitForExplicitFinish(); |
|
29 // turn on logging for nsBlocklistService.js |
|
30 Services.prefs.setBoolPref("extensions.logging.enabled", true); |
|
31 registerCleanupFunction(function () { |
|
32 Services.prefs.clearUserPref("extensions.logging.enabled"); |
|
33 }); |
|
34 |
|
35 runSocialTests(tests, undefined, undefined, function () { |
|
36 resetBlocklist(finish); //restore to original pref |
|
37 }); |
|
38 } |
|
39 |
|
40 var tests = { |
|
41 testSimpleBlocklist: function(next) { |
|
42 // this really just tests adding and clearing our blocklist for later tests |
|
43 setAndUpdateBlocklist(blocklistURL, function() { |
|
44 ok(Services.blocklist.isAddonBlocklisted(SocialService.createWrapper(manifest_bad)), "blocking 'blocked'"); |
|
45 ok(!Services.blocklist.isAddonBlocklisted(SocialService.createWrapper(manifest)), "not blocking 'good'"); |
|
46 resetBlocklist(function() { |
|
47 ok(!Services.blocklist.isAddonBlocklisted(SocialService.createWrapper(manifest_bad)), "blocklist cleared"); |
|
48 next(); |
|
49 }); |
|
50 }); |
|
51 }, |
|
52 testAddingNonBlockedProvider: function(next) { |
|
53 function finishTest(isgood) { |
|
54 ok(isgood, "adding non-blocked provider ok"); |
|
55 Services.prefs.clearUserPref("social.manifest.good"); |
|
56 resetBlocklist(next); |
|
57 } |
|
58 setManifestPref("social.manifest.good", manifest); |
|
59 setAndUpdateBlocklist(blocklistURL, function() { |
|
60 try { |
|
61 SocialService.addProvider(manifest, function(provider) { |
|
62 try { |
|
63 SocialService.removeProvider(provider.origin, function() { |
|
64 ok(true, "added and removed provider"); |
|
65 finishTest(true); |
|
66 }); |
|
67 } catch(e) { |
|
68 ok(false, "SocialService.removeProvider threw exception: " + e); |
|
69 finishTest(false); |
|
70 } |
|
71 }); |
|
72 } catch(e) { |
|
73 ok(false, "SocialService.addProvider threw exception: " + e); |
|
74 finishTest(false); |
|
75 } |
|
76 }); |
|
77 }, |
|
78 testAddingBlockedProvider: function(next) { |
|
79 function finishTest(good) { |
|
80 ok(good, "Unable to add blocklisted provider"); |
|
81 Services.prefs.clearUserPref("social.manifest.blocked"); |
|
82 resetBlocklist(next); |
|
83 } |
|
84 setManifestPref("social.manifest.blocked", manifest_bad); |
|
85 setAndUpdateBlocklist(blocklistURL, function() { |
|
86 try { |
|
87 SocialService.addProvider(manifest_bad, function(provider) { |
|
88 SocialService.removeProvider(provider.origin, function() { |
|
89 ok(false, "SocialService.addProvider should throw blocklist exception"); |
|
90 finishTest(false); |
|
91 }); |
|
92 }); |
|
93 } catch(e) { |
|
94 ok(true, "SocialService.addProvider should throw blocklist exception: " + e); |
|
95 finishTest(true); |
|
96 } |
|
97 }); |
|
98 }, |
|
99 testInstallingBlockedProvider: function(next) { |
|
100 function finishTest(good) { |
|
101 ok(good, "Unable to add blocklisted provider"); |
|
102 Services.prefs.clearUserPref("social.whitelist"); |
|
103 resetBlocklist(next); |
|
104 } |
|
105 let activationURL = manifest_bad.origin + "/browser/browser/base/content/test/social/social_activate.html" |
|
106 addTab(activationURL, function(tab) { |
|
107 let doc = tab.linkedBrowser.contentDocument; |
|
108 let installFrom = doc.nodePrincipal.origin; |
|
109 // whitelist to avoid the 3rd party install dialog, we only want to test |
|
110 // the blocklist inside installProvider. |
|
111 Services.prefs.setCharPref("social.whitelist", installFrom); |
|
112 setAndUpdateBlocklist(blocklistURL, function() { |
|
113 try { |
|
114 // expecting an exception when attempting to install a hard blocked |
|
115 // provider |
|
116 Social.installProvider(doc, manifest_bad, function(addonManifest) { |
|
117 gBrowser.removeTab(tab); |
|
118 finishTest(false); |
|
119 }); |
|
120 } catch(e) { |
|
121 gBrowser.removeTab(tab); |
|
122 finishTest(true); |
|
123 } |
|
124 }); |
|
125 }); |
|
126 }, |
|
127 testBlockingExistingProvider: function(next) { |
|
128 let listener = { |
|
129 _window: null, |
|
130 onOpenWindow: function(aXULWindow) { |
|
131 Services.wm.removeListener(this); |
|
132 this._window = aXULWindow; |
|
133 let domwindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor) |
|
134 .getInterface(Ci.nsIDOMWindow); |
|
135 |
|
136 domwindow.addEventListener("load", function _load() { |
|
137 domwindow.removeEventListener("load", _load, false); |
|
138 |
|
139 domwindow.addEventListener("unload", function _unload() { |
|
140 domwindow.removeEventListener("unload", _unload, false); |
|
141 info("blocklist window was closed"); |
|
142 Services.wm.removeListener(listener); |
|
143 next(); |
|
144 }, false); |
|
145 |
|
146 is(domwindow.document.location.href, URI_EXTENSION_BLOCKLIST_DIALOG, "dialog opened and focused"); |
|
147 // wait until after load to cancel so the dialog has initalized. we |
|
148 // don't want to accept here since that restarts the browser. |
|
149 executeSoon(() => { |
|
150 let cancelButton = domwindow.document.documentElement.getButton("cancel"); |
|
151 info("***** hit the cancel button\n"); |
|
152 cancelButton.doCommand(); |
|
153 }); |
|
154 }, false); |
|
155 }, |
|
156 onCloseWindow: function(aXULWindow) { }, |
|
157 onWindowTitleChange: function(aXULWindow, aNewTitle) { } |
|
158 }; |
|
159 |
|
160 Services.wm.addListener(listener); |
|
161 |
|
162 setManifestPref("social.manifest.blocked", manifest_bad); |
|
163 try { |
|
164 SocialService.addProvider(manifest_bad, function(provider) { |
|
165 // the act of blocking should cause a 'provider-disabled' notification |
|
166 // from SocialService. |
|
167 SocialService.registerProviderListener(function providerListener(topic, origin, providers) { |
|
168 if (topic != "provider-disabled") |
|
169 return; |
|
170 SocialService.unregisterProviderListener(providerListener); |
|
171 is(origin, provider.origin, "provider disabled"); |
|
172 SocialService.getProvider(provider.origin, function(p) { |
|
173 ok(p == null, "blocklisted provider disabled"); |
|
174 Services.prefs.clearUserPref("social.manifest.blocked"); |
|
175 resetBlocklist(); |
|
176 }); |
|
177 }); |
|
178 // no callback - the act of updating should cause the listener above |
|
179 // to fire. |
|
180 setAndUpdateBlocklist(blocklistURL); |
|
181 }); |
|
182 } catch(e) { |
|
183 ok(false, "unable to add provider " + e); |
|
184 next(); |
|
185 } |
|
186 } |
|
187 } |