1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/base/content/test/social/browser_social_activation.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,336 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +let SocialService = Cu.import("resource://gre/modules/SocialService.jsm", {}).SocialService; 1.9 + 1.10 +let tabsToRemove = []; 1.11 + 1.12 +function postTestCleanup(callback) { 1.13 + // any tabs opened by the test. 1.14 + for (let tab of tabsToRemove) 1.15 + gBrowser.removeTab(tab); 1.16 + tabsToRemove = []; 1.17 + // theses tests use the notification panel but don't bother waiting for it 1.18 + // to fully open - the end result is that the panel might stay open 1.19 + //SocialUI.activationPanel.hidePopup(); 1.20 + 1.21 + Services.prefs.clearUserPref("social.whitelist"); 1.22 + 1.23 + // all providers may have had their manifests added. 1.24 + for (let manifest of gProviders) 1.25 + Services.prefs.clearUserPref("social.manifest." + manifest.origin); 1.26 + 1.27 + // all the providers may have been added. 1.28 + let providers = gProviders.slice(0) 1.29 + function removeProviders() { 1.30 + if (providers.length < 1) { 1.31 + executeSoon(function() { 1.32 + is(Social.providers.length, 0, "all providers removed"); 1.33 + callback(); 1.34 + }); 1.35 + return; 1.36 + } 1.37 + 1.38 + let provider = providers.pop(); 1.39 + try { 1.40 + SocialService.removeProvider(provider.origin, removeProviders); 1.41 + } catch(ex) { 1.42 + removeProviders(); 1.43 + } 1.44 + } 1.45 + removeProviders(); 1.46 +} 1.47 + 1.48 +function addBuiltinManifest(manifest) { 1.49 + let prefname = getManifestPrefname(manifest); 1.50 + setBuiltinManifestPref(prefname, manifest); 1.51 + return prefname; 1.52 +} 1.53 + 1.54 +function addTab(url, callback) { 1.55 + let tab = gBrowser.selectedTab = gBrowser.addTab(url, {skipAnimation: true}); 1.56 + tab.linkedBrowser.addEventListener("load", function tabLoad(event) { 1.57 + tab.linkedBrowser.removeEventListener("load", tabLoad, true); 1.58 + tabsToRemove.push(tab); 1.59 + executeSoon(function() {callback(tab)}); 1.60 + }, true); 1.61 +} 1.62 + 1.63 +function sendActivationEvent(tab, callback, nullManifest) { 1.64 + // hack Social.lastEventReceived so we don't hit the "too many events" check. 1.65 + Social.lastEventReceived = 0; 1.66 + let doc = tab.linkedBrowser.contentDocument; 1.67 + // if our test has a frame, use it 1.68 + if (doc.defaultView.frames[0]) 1.69 + doc = doc.defaultView.frames[0].document; 1.70 + let button = doc.getElementById(nullManifest ? "activation-old" : "activation"); 1.71 + EventUtils.synthesizeMouseAtCenter(button, {}, doc.defaultView); 1.72 + executeSoon(callback); 1.73 +} 1.74 + 1.75 +function activateProvider(domain, callback, nullManifest) { 1.76 + let activationURL = domain+"/browser/browser/base/content/test/social/social_activate.html" 1.77 + addTab(activationURL, function(tab) { 1.78 + sendActivationEvent(tab, callback, nullManifest); 1.79 + }); 1.80 +} 1.81 + 1.82 +function activateIFrameProvider(domain, callback) { 1.83 + let activationURL = domain+"/browser/browser/base/content/test/social/social_activate_iframe.html" 1.84 + addTab(activationURL, function(tab) { 1.85 + sendActivationEvent(tab, callback, false); 1.86 + }); 1.87 +} 1.88 + 1.89 +function waitForProviderLoad(cb) { 1.90 + Services.obs.addObserver(function providerSet(subject, topic, data) { 1.91 + Services.obs.removeObserver(providerSet, "social:provider-enabled"); 1.92 + info("social:provider-enabled observer was notified"); 1.93 + waitForCondition(function() { 1.94 + let sbrowser = document.getElementById("social-sidebar-browser"); 1.95 + let provider = SocialSidebar.provider; 1.96 + let postActivation = provider && gBrowser.contentDocument.location.href == provider.origin + "/browser/browser/base/content/test/social/social_postActivation.html"; 1.97 + 1.98 + return provider && 1.99 + provider.profile && 1.100 + provider.profile.displayName && 1.101 + postActivation && 1.102 + sbrowser.docShellIsActive; 1.103 + }, function() { 1.104 + // executeSoon to let the browser UI observers run first 1.105 + executeSoon(cb); 1.106 + }, 1.107 + "waitForProviderLoad: provider profile was not set"); 1.108 + }, "social:provider-enabled", false); 1.109 +} 1.110 + 1.111 + 1.112 +function getAddonItemInList(aId, aList) { 1.113 + var item = aList.firstChild; 1.114 + while (item) { 1.115 + if ("mAddon" in item && item.mAddon.id == aId) { 1.116 + aList.ensureElementIsVisible(item); 1.117 + return item; 1.118 + } 1.119 + item = item.nextSibling; 1.120 + } 1.121 + return null; 1.122 +} 1.123 + 1.124 +function clickAddonRemoveButton(tab, aCallback) { 1.125 + AddonManager.getAddonsByTypes(["service"], function(aAddons) { 1.126 + let addon = aAddons[0]; 1.127 + 1.128 + let doc = tab.linkedBrowser.contentDocument; 1.129 + let list = doc.getElementById("addon-list"); 1.130 + 1.131 + let item = getAddonItemInList(addon.id, list); 1.132 + isnot(item, null, "Should have found the add-on in the list"); 1.133 + 1.134 + var button = doc.getAnonymousElementByAttribute(item, "anonid", "remove-btn"); 1.135 + isnot(button, null, "Should have a remove button"); 1.136 + ok(!button.disabled, "Button should not be disabled"); 1.137 + 1.138 + EventUtils.synthesizeMouseAtCenter(button, { }, doc.defaultView); 1.139 + 1.140 + // Force XBL to apply 1.141 + item.clientTop; 1.142 + 1.143 + is(item.getAttribute("pending"), "uninstall", "Add-on should be uninstalling"); 1.144 + 1.145 + executeSoon(function() { aCallback(addon); }); 1.146 + }); 1.147 +} 1.148 + 1.149 +function activateOneProvider(manifest, finishActivation, aCallback) { 1.150 + let panel = document.getElementById("servicesInstall-notification"); 1.151 + PopupNotifications.panel.addEventListener("popupshown", function onpopupshown() { 1.152 + PopupNotifications.panel.removeEventListener("popupshown", onpopupshown); 1.153 + info("servicesInstall-notification panel opened"); 1.154 + if (finishActivation) 1.155 + panel.button.click(); 1.156 + else 1.157 + panel.closebutton.click(); 1.158 + }); 1.159 + 1.160 + activateProvider(manifest.origin, function() { 1.161 + if (!finishActivation) { 1.162 + ok(panel.hidden, "activation panel is not showing"); 1.163 + executeSoon(aCallback); 1.164 + } else { 1.165 + waitForProviderLoad(function() { 1.166 + is(SocialSidebar.provider.origin, manifest.origin, "new provider is active"); 1.167 + ok(SocialSidebar.opened, "sidebar is open"); 1.168 + checkSocialUI(); 1.169 + executeSoon(aCallback); 1.170 + }); 1.171 + } 1.172 + }); 1.173 +} 1.174 + 1.175 +let gTestDomains = ["https://example.com", "https://test1.example.com", "https://test2.example.com"]; 1.176 +let gProviders = [ 1.177 + { 1.178 + name: "provider 1", 1.179 + origin: "https://example.com", 1.180 + sidebarURL: "https://example.com/browser/browser/base/content/test/social/social_sidebar.html?provider1", 1.181 + workerURL: "https://example.com/browser/browser/base/content/test/social/social_worker.js#no-profile,no-recommend", 1.182 + iconURL: "chrome://branding/content/icon48.png" 1.183 + }, 1.184 + { 1.185 + name: "provider 2", 1.186 + origin: "https://test1.example.com", 1.187 + sidebarURL: "https://test1.example.com/browser/browser/base/content/test/social/social_sidebar.html?provider2", 1.188 + workerURL: "https://test1.example.com/browser/browser/base/content/test/social/social_worker.js#no-profile,no-recommend", 1.189 + iconURL: "chrome://branding/content/icon64.png" 1.190 + }, 1.191 + { 1.192 + name: "provider 3", 1.193 + origin: "https://test2.example.com", 1.194 + sidebarURL: "https://test2.example.com/browser/browser/base/content/test/social/social_sidebar.html?provider2", 1.195 + workerURL: "https://test2.example.com/browser/browser/base/content/test/social/social_worker.js#no-profile,no-recommend", 1.196 + iconURL: "chrome://branding/content/about-logo.png" 1.197 + } 1.198 +]; 1.199 + 1.200 + 1.201 +function test() { 1.202 + waitForExplicitFinish(); 1.203 + runSocialTests(tests, undefined, postTestCleanup); 1.204 +} 1.205 + 1.206 +var tests = { 1.207 + testActivationWrongOrigin: function(next) { 1.208 + // At this stage none of our providers exist, so we expect failure. 1.209 + Services.prefs.setBoolPref("social.remote-install.enabled", false); 1.210 + activateProvider(gTestDomains[0], function() { 1.211 + is(SocialUI.enabled, false, "SocialUI is not enabled"); 1.212 + let panel = document.getElementById("servicesInstall-notification"); 1.213 + ok(panel.hidden, "activation panel still hidden"); 1.214 + checkSocialUI(); 1.215 + Services.prefs.clearUserPref("social.remote-install.enabled"); 1.216 + next(); 1.217 + }); 1.218 + }, 1.219 + 1.220 + testIFrameActivation: function(next) { 1.221 + Services.prefs.setCharPref("social.whitelist", gTestDomains.join(",")); 1.222 + activateIFrameProvider(gTestDomains[0], function() { 1.223 + is(SocialUI.enabled, false, "SocialUI is not enabled"); 1.224 + ok(!SocialSidebar.provider, "provider is not installed"); 1.225 + let panel = document.getElementById("servicesInstall-notification"); 1.226 + ok(panel.hidden, "activation panel still hidden"); 1.227 + checkSocialUI(); 1.228 + Services.prefs.clearUserPref("social.whitelist"); 1.229 + next(); 1.230 + }); 1.231 + }, 1.232 + 1.233 + testActivationFirstProvider: function(next) { 1.234 + Services.prefs.setCharPref("social.whitelist", gTestDomains.join(",")); 1.235 + // first up we add a manifest entry for a single provider. 1.236 + activateOneProvider(gProviders[0], false, function() { 1.237 + // we deactivated leaving no providers left, so Social is disabled. 1.238 + ok(!SocialSidebar.provider, "should be no provider left after disabling"); 1.239 + checkSocialUI(); 1.240 + Services.prefs.clearUserPref("social.whitelist"); 1.241 + next(); 1.242 + }); 1.243 + }, 1.244 + 1.245 + testActivationBuiltin: function(next) { 1.246 + let prefname = addBuiltinManifest(gProviders[0]); 1.247 + is(SocialService.getOriginActivationType(gTestDomains[0]), "builtin", "manifest is builtin"); 1.248 + // first up we add a manifest entry for a single provider. 1.249 + activateOneProvider(gProviders[0], false, function() { 1.250 + // we deactivated leaving no providers left, so Social is disabled. 1.251 + ok(!SocialSidebar.provider, "should be no provider left after disabling"); 1.252 + checkSocialUI(); 1.253 + resetBuiltinManifestPref(prefname); 1.254 + next(); 1.255 + }); 1.256 + }, 1.257 + 1.258 + testActivationMultipleProvider: function(next) { 1.259 + // The trick with this test is to make sure that Social.providers[1] is 1.260 + // the current provider when doing the undo - this makes sure that the 1.261 + // Social code doesn't fallback to Social.providers[0], which it will 1.262 + // do in some cases (but those cases do not include what this test does) 1.263 + // first enable the 2 providers 1.264 + Services.prefs.setCharPref("social.whitelist", gTestDomains.join(",")); 1.265 + SocialService.addProvider(gProviders[0], function() { 1.266 + SocialService.addProvider(gProviders[1], function() { 1.267 + checkSocialUI(); 1.268 + // activate the last provider. 1.269 + let prefname = addBuiltinManifest(gProviders[2]); 1.270 + activateOneProvider(gProviders[2], false, function() { 1.271 + // we deactivated - the first provider should be enabled. 1.272 + is(SocialSidebar.provider.origin, Social.providers[1].origin, "original provider should have been reactivated"); 1.273 + checkSocialUI(); 1.274 + Services.prefs.clearUserPref("social.whitelist"); 1.275 + resetBuiltinManifestPref(prefname); 1.276 + next(); 1.277 + }); 1.278 + }); 1.279 + }); 1.280 + }, 1.281 + 1.282 + testAddonManagerDoubleInstall: function(next) { 1.283 + Services.prefs.setCharPref("social.whitelist", gTestDomains.join(",")); 1.284 + // Create a new tab and load about:addons 1.285 + let blanktab = gBrowser.addTab(); 1.286 + gBrowser.selectedTab = blanktab; 1.287 + BrowserOpenAddonsMgr('addons://list/service'); 1.288 + 1.289 + is(blanktab, gBrowser.selectedTab, "Current tab should be blank tab"); 1.290 + 1.291 + gBrowser.selectedBrowser.addEventListener("load", function tabLoad() { 1.292 + gBrowser.selectedBrowser.removeEventListener("load", tabLoad, true); 1.293 + let browser = blanktab.linkedBrowser; 1.294 + is(browser.currentURI.spec, "about:addons", "about:addons should load into blank tab."); 1.295 + 1.296 + let prefname = addBuiltinManifest(gProviders[0]); 1.297 + activateOneProvider(gProviders[0], true, function() { 1.298 + info("first activation completed"); 1.299 + is(gBrowser.contentDocument.location.href, gProviders[0].origin + "/browser/browser/base/content/test/social/social_postActivation.html"); 1.300 + gBrowser.removeTab(gBrowser.selectedTab); 1.301 + is(gBrowser.contentDocument.location.href, gProviders[0].origin + "/browser/browser/base/content/test/social/social_activate.html"); 1.302 + gBrowser.removeTab(gBrowser.selectedTab); 1.303 + tabsToRemove.pop(); 1.304 + // uninstall the provider 1.305 + clickAddonRemoveButton(blanktab, function(addon) { 1.306 + checkSocialUI(); 1.307 + activateOneProvider(gProviders[0], true, function() { 1.308 + info("second activation completed"); 1.309 + is(gBrowser.contentDocument.location.href, gProviders[0].origin + "/browser/browser/base/content/test/social/social_postActivation.html"); 1.310 + gBrowser.removeTab(gBrowser.selectedTab); 1.311 + 1.312 + // after closing the addons tab, verify provider is still installed 1.313 + gBrowser.tabContainer.addEventListener("TabClose", function onTabClose() { 1.314 + gBrowser.tabContainer.removeEventListener("TabClose", onTabClose); 1.315 + AddonManager.getAddonsByTypes(["service"], function(aAddons) { 1.316 + is(aAddons.length, 1, "there can be only one"); 1.317 + Services.prefs.clearUserPref("social.whitelist"); 1.318 + resetBuiltinManifestPref(prefname); 1.319 + next(); 1.320 + }); 1.321 + }); 1.322 + 1.323 + // verify only one provider in list 1.324 + AddonManager.getAddonsByTypes(["service"], function(aAddons) { 1.325 + is(aAddons.length, 1, "there can be only one"); 1.326 + 1.327 + let doc = blanktab.linkedBrowser.contentDocument; 1.328 + let list = doc.getElementById("addon-list"); 1.329 + is(list.childNodes.length, 1, "only one addon is displayed"); 1.330 + 1.331 + gBrowser.removeTab(blanktab); 1.332 + }); 1.333 + 1.334 + }); 1.335 + }); 1.336 + }); 1.337 + }, true); 1.338 + } 1.339 +}