|
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 let SocialService = Cu.import("resource://gre/modules/SocialService.jsm", {}).SocialService; |
|
6 |
|
7 let tabsToRemove = []; |
|
8 |
|
9 function postTestCleanup(callback) { |
|
10 // any tabs opened by the test. |
|
11 for (let tab of tabsToRemove) |
|
12 gBrowser.removeTab(tab); |
|
13 tabsToRemove = []; |
|
14 // theses tests use the notification panel but don't bother waiting for it |
|
15 // to fully open - the end result is that the panel might stay open |
|
16 //SocialUI.activationPanel.hidePopup(); |
|
17 |
|
18 Services.prefs.clearUserPref("social.whitelist"); |
|
19 |
|
20 // all providers may have had their manifests added. |
|
21 for (let manifest of gProviders) |
|
22 Services.prefs.clearUserPref("social.manifest." + manifest.origin); |
|
23 |
|
24 // all the providers may have been added. |
|
25 let providers = gProviders.slice(0) |
|
26 function removeProviders() { |
|
27 if (providers.length < 1) { |
|
28 executeSoon(function() { |
|
29 is(Social.providers.length, 0, "all providers removed"); |
|
30 callback(); |
|
31 }); |
|
32 return; |
|
33 } |
|
34 |
|
35 let provider = providers.pop(); |
|
36 try { |
|
37 SocialService.removeProvider(provider.origin, removeProviders); |
|
38 } catch(ex) { |
|
39 removeProviders(); |
|
40 } |
|
41 } |
|
42 removeProviders(); |
|
43 } |
|
44 |
|
45 function addBuiltinManifest(manifest) { |
|
46 let prefname = getManifestPrefname(manifest); |
|
47 setBuiltinManifestPref(prefname, manifest); |
|
48 return prefname; |
|
49 } |
|
50 |
|
51 function addTab(url, callback) { |
|
52 let tab = gBrowser.selectedTab = gBrowser.addTab(url, {skipAnimation: true}); |
|
53 tab.linkedBrowser.addEventListener("load", function tabLoad(event) { |
|
54 tab.linkedBrowser.removeEventListener("load", tabLoad, true); |
|
55 tabsToRemove.push(tab); |
|
56 executeSoon(function() {callback(tab)}); |
|
57 }, true); |
|
58 } |
|
59 |
|
60 function sendActivationEvent(tab, callback, nullManifest) { |
|
61 // hack Social.lastEventReceived so we don't hit the "too many events" check. |
|
62 Social.lastEventReceived = 0; |
|
63 let doc = tab.linkedBrowser.contentDocument; |
|
64 // if our test has a frame, use it |
|
65 if (doc.defaultView.frames[0]) |
|
66 doc = doc.defaultView.frames[0].document; |
|
67 let button = doc.getElementById(nullManifest ? "activation-old" : "activation"); |
|
68 EventUtils.synthesizeMouseAtCenter(button, {}, doc.defaultView); |
|
69 executeSoon(callback); |
|
70 } |
|
71 |
|
72 function activateProvider(domain, callback, nullManifest) { |
|
73 let activationURL = domain+"/browser/browser/base/content/test/social/social_activate.html" |
|
74 addTab(activationURL, function(tab) { |
|
75 sendActivationEvent(tab, callback, nullManifest); |
|
76 }); |
|
77 } |
|
78 |
|
79 function activateIFrameProvider(domain, callback) { |
|
80 let activationURL = domain+"/browser/browser/base/content/test/social/social_activate_iframe.html" |
|
81 addTab(activationURL, function(tab) { |
|
82 sendActivationEvent(tab, callback, false); |
|
83 }); |
|
84 } |
|
85 |
|
86 function waitForProviderLoad(cb) { |
|
87 Services.obs.addObserver(function providerSet(subject, topic, data) { |
|
88 Services.obs.removeObserver(providerSet, "social:provider-enabled"); |
|
89 info("social:provider-enabled observer was notified"); |
|
90 waitForCondition(function() { |
|
91 let sbrowser = document.getElementById("social-sidebar-browser"); |
|
92 let provider = SocialSidebar.provider; |
|
93 let postActivation = provider && gBrowser.contentDocument.location.href == provider.origin + "/browser/browser/base/content/test/social/social_postActivation.html"; |
|
94 |
|
95 return provider && |
|
96 provider.profile && |
|
97 provider.profile.displayName && |
|
98 postActivation && |
|
99 sbrowser.docShellIsActive; |
|
100 }, function() { |
|
101 // executeSoon to let the browser UI observers run first |
|
102 executeSoon(cb); |
|
103 }, |
|
104 "waitForProviderLoad: provider profile was not set"); |
|
105 }, "social:provider-enabled", false); |
|
106 } |
|
107 |
|
108 |
|
109 function getAddonItemInList(aId, aList) { |
|
110 var item = aList.firstChild; |
|
111 while (item) { |
|
112 if ("mAddon" in item && item.mAddon.id == aId) { |
|
113 aList.ensureElementIsVisible(item); |
|
114 return item; |
|
115 } |
|
116 item = item.nextSibling; |
|
117 } |
|
118 return null; |
|
119 } |
|
120 |
|
121 function clickAddonRemoveButton(tab, aCallback) { |
|
122 AddonManager.getAddonsByTypes(["service"], function(aAddons) { |
|
123 let addon = aAddons[0]; |
|
124 |
|
125 let doc = tab.linkedBrowser.contentDocument; |
|
126 let list = doc.getElementById("addon-list"); |
|
127 |
|
128 let item = getAddonItemInList(addon.id, list); |
|
129 isnot(item, null, "Should have found the add-on in the list"); |
|
130 |
|
131 var button = doc.getAnonymousElementByAttribute(item, "anonid", "remove-btn"); |
|
132 isnot(button, null, "Should have a remove button"); |
|
133 ok(!button.disabled, "Button should not be disabled"); |
|
134 |
|
135 EventUtils.synthesizeMouseAtCenter(button, { }, doc.defaultView); |
|
136 |
|
137 // Force XBL to apply |
|
138 item.clientTop; |
|
139 |
|
140 is(item.getAttribute("pending"), "uninstall", "Add-on should be uninstalling"); |
|
141 |
|
142 executeSoon(function() { aCallback(addon); }); |
|
143 }); |
|
144 } |
|
145 |
|
146 function activateOneProvider(manifest, finishActivation, aCallback) { |
|
147 let panel = document.getElementById("servicesInstall-notification"); |
|
148 PopupNotifications.panel.addEventListener("popupshown", function onpopupshown() { |
|
149 PopupNotifications.panel.removeEventListener("popupshown", onpopupshown); |
|
150 info("servicesInstall-notification panel opened"); |
|
151 if (finishActivation) |
|
152 panel.button.click(); |
|
153 else |
|
154 panel.closebutton.click(); |
|
155 }); |
|
156 |
|
157 activateProvider(manifest.origin, function() { |
|
158 if (!finishActivation) { |
|
159 ok(panel.hidden, "activation panel is not showing"); |
|
160 executeSoon(aCallback); |
|
161 } else { |
|
162 waitForProviderLoad(function() { |
|
163 is(SocialSidebar.provider.origin, manifest.origin, "new provider is active"); |
|
164 ok(SocialSidebar.opened, "sidebar is open"); |
|
165 checkSocialUI(); |
|
166 executeSoon(aCallback); |
|
167 }); |
|
168 } |
|
169 }); |
|
170 } |
|
171 |
|
172 let gTestDomains = ["https://example.com", "https://test1.example.com", "https://test2.example.com"]; |
|
173 let gProviders = [ |
|
174 { |
|
175 name: "provider 1", |
|
176 origin: "https://example.com", |
|
177 sidebarURL: "https://example.com/browser/browser/base/content/test/social/social_sidebar.html?provider1", |
|
178 workerURL: "https://example.com/browser/browser/base/content/test/social/social_worker.js#no-profile,no-recommend", |
|
179 iconURL: "chrome://branding/content/icon48.png" |
|
180 }, |
|
181 { |
|
182 name: "provider 2", |
|
183 origin: "https://test1.example.com", |
|
184 sidebarURL: "https://test1.example.com/browser/browser/base/content/test/social/social_sidebar.html?provider2", |
|
185 workerURL: "https://test1.example.com/browser/browser/base/content/test/social/social_worker.js#no-profile,no-recommend", |
|
186 iconURL: "chrome://branding/content/icon64.png" |
|
187 }, |
|
188 { |
|
189 name: "provider 3", |
|
190 origin: "https://test2.example.com", |
|
191 sidebarURL: "https://test2.example.com/browser/browser/base/content/test/social/social_sidebar.html?provider2", |
|
192 workerURL: "https://test2.example.com/browser/browser/base/content/test/social/social_worker.js#no-profile,no-recommend", |
|
193 iconURL: "chrome://branding/content/about-logo.png" |
|
194 } |
|
195 ]; |
|
196 |
|
197 |
|
198 function test() { |
|
199 waitForExplicitFinish(); |
|
200 runSocialTests(tests, undefined, postTestCleanup); |
|
201 } |
|
202 |
|
203 var tests = { |
|
204 testActivationWrongOrigin: function(next) { |
|
205 // At this stage none of our providers exist, so we expect failure. |
|
206 Services.prefs.setBoolPref("social.remote-install.enabled", false); |
|
207 activateProvider(gTestDomains[0], function() { |
|
208 is(SocialUI.enabled, false, "SocialUI is not enabled"); |
|
209 let panel = document.getElementById("servicesInstall-notification"); |
|
210 ok(panel.hidden, "activation panel still hidden"); |
|
211 checkSocialUI(); |
|
212 Services.prefs.clearUserPref("social.remote-install.enabled"); |
|
213 next(); |
|
214 }); |
|
215 }, |
|
216 |
|
217 testIFrameActivation: function(next) { |
|
218 Services.prefs.setCharPref("social.whitelist", gTestDomains.join(",")); |
|
219 activateIFrameProvider(gTestDomains[0], function() { |
|
220 is(SocialUI.enabled, false, "SocialUI is not enabled"); |
|
221 ok(!SocialSidebar.provider, "provider is not installed"); |
|
222 let panel = document.getElementById("servicesInstall-notification"); |
|
223 ok(panel.hidden, "activation panel still hidden"); |
|
224 checkSocialUI(); |
|
225 Services.prefs.clearUserPref("social.whitelist"); |
|
226 next(); |
|
227 }); |
|
228 }, |
|
229 |
|
230 testActivationFirstProvider: function(next) { |
|
231 Services.prefs.setCharPref("social.whitelist", gTestDomains.join(",")); |
|
232 // first up we add a manifest entry for a single provider. |
|
233 activateOneProvider(gProviders[0], false, function() { |
|
234 // we deactivated leaving no providers left, so Social is disabled. |
|
235 ok(!SocialSidebar.provider, "should be no provider left after disabling"); |
|
236 checkSocialUI(); |
|
237 Services.prefs.clearUserPref("social.whitelist"); |
|
238 next(); |
|
239 }); |
|
240 }, |
|
241 |
|
242 testActivationBuiltin: function(next) { |
|
243 let prefname = addBuiltinManifest(gProviders[0]); |
|
244 is(SocialService.getOriginActivationType(gTestDomains[0]), "builtin", "manifest is builtin"); |
|
245 // first up we add a manifest entry for a single provider. |
|
246 activateOneProvider(gProviders[0], false, function() { |
|
247 // we deactivated leaving no providers left, so Social is disabled. |
|
248 ok(!SocialSidebar.provider, "should be no provider left after disabling"); |
|
249 checkSocialUI(); |
|
250 resetBuiltinManifestPref(prefname); |
|
251 next(); |
|
252 }); |
|
253 }, |
|
254 |
|
255 testActivationMultipleProvider: function(next) { |
|
256 // The trick with this test is to make sure that Social.providers[1] is |
|
257 // the current provider when doing the undo - this makes sure that the |
|
258 // Social code doesn't fallback to Social.providers[0], which it will |
|
259 // do in some cases (but those cases do not include what this test does) |
|
260 // first enable the 2 providers |
|
261 Services.prefs.setCharPref("social.whitelist", gTestDomains.join(",")); |
|
262 SocialService.addProvider(gProviders[0], function() { |
|
263 SocialService.addProvider(gProviders[1], function() { |
|
264 checkSocialUI(); |
|
265 // activate the last provider. |
|
266 let prefname = addBuiltinManifest(gProviders[2]); |
|
267 activateOneProvider(gProviders[2], false, function() { |
|
268 // we deactivated - the first provider should be enabled. |
|
269 is(SocialSidebar.provider.origin, Social.providers[1].origin, "original provider should have been reactivated"); |
|
270 checkSocialUI(); |
|
271 Services.prefs.clearUserPref("social.whitelist"); |
|
272 resetBuiltinManifestPref(prefname); |
|
273 next(); |
|
274 }); |
|
275 }); |
|
276 }); |
|
277 }, |
|
278 |
|
279 testAddonManagerDoubleInstall: function(next) { |
|
280 Services.prefs.setCharPref("social.whitelist", gTestDomains.join(",")); |
|
281 // Create a new tab and load about:addons |
|
282 let blanktab = gBrowser.addTab(); |
|
283 gBrowser.selectedTab = blanktab; |
|
284 BrowserOpenAddonsMgr('addons://list/service'); |
|
285 |
|
286 is(blanktab, gBrowser.selectedTab, "Current tab should be blank tab"); |
|
287 |
|
288 gBrowser.selectedBrowser.addEventListener("load", function tabLoad() { |
|
289 gBrowser.selectedBrowser.removeEventListener("load", tabLoad, true); |
|
290 let browser = blanktab.linkedBrowser; |
|
291 is(browser.currentURI.spec, "about:addons", "about:addons should load into blank tab."); |
|
292 |
|
293 let prefname = addBuiltinManifest(gProviders[0]); |
|
294 activateOneProvider(gProviders[0], true, function() { |
|
295 info("first activation completed"); |
|
296 is(gBrowser.contentDocument.location.href, gProviders[0].origin + "/browser/browser/base/content/test/social/social_postActivation.html"); |
|
297 gBrowser.removeTab(gBrowser.selectedTab); |
|
298 is(gBrowser.contentDocument.location.href, gProviders[0].origin + "/browser/browser/base/content/test/social/social_activate.html"); |
|
299 gBrowser.removeTab(gBrowser.selectedTab); |
|
300 tabsToRemove.pop(); |
|
301 // uninstall the provider |
|
302 clickAddonRemoveButton(blanktab, function(addon) { |
|
303 checkSocialUI(); |
|
304 activateOneProvider(gProviders[0], true, function() { |
|
305 info("second activation completed"); |
|
306 is(gBrowser.contentDocument.location.href, gProviders[0].origin + "/browser/browser/base/content/test/social/social_postActivation.html"); |
|
307 gBrowser.removeTab(gBrowser.selectedTab); |
|
308 |
|
309 // after closing the addons tab, verify provider is still installed |
|
310 gBrowser.tabContainer.addEventListener("TabClose", function onTabClose() { |
|
311 gBrowser.tabContainer.removeEventListener("TabClose", onTabClose); |
|
312 AddonManager.getAddonsByTypes(["service"], function(aAddons) { |
|
313 is(aAddons.length, 1, "there can be only one"); |
|
314 Services.prefs.clearUserPref("social.whitelist"); |
|
315 resetBuiltinManifestPref(prefname); |
|
316 next(); |
|
317 }); |
|
318 }); |
|
319 |
|
320 // verify only one provider in list |
|
321 AddonManager.getAddonsByTypes(["service"], function(aAddons) { |
|
322 is(aAddons.length, 1, "there can be only one"); |
|
323 |
|
324 let doc = blanktab.linkedBrowser.contentDocument; |
|
325 let list = doc.getElementById("addon-list"); |
|
326 is(list.childNodes.length, 1, "only one addon is displayed"); |
|
327 |
|
328 gBrowser.removeTab(blanktab); |
|
329 }); |
|
330 |
|
331 }); |
|
332 }); |
|
333 }); |
|
334 }, true); |
|
335 } |
|
336 } |