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