1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/base/content/test/social/browser_social_marks.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,373 @@ 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 manifest = { // builtin provider 1.11 + name: "provider example.com", 1.12 + origin: "https://example.com", 1.13 + sidebarURL: "https://example.com/browser/browser/base/content/test/social/social_sidebar.html", 1.14 + workerURL: "https://example.com/browser/browser/base/content/test/social/social_worker.js", 1.15 + iconURL: "https://example.com/browser/browser/base/content/test/general/moz.png" 1.16 +}; 1.17 +let manifest2 = { // used for testing install 1.18 + name: "provider test1", 1.19 + origin: "https://test1.example.com", 1.20 + workerURL: "https://test1.example.com/browser/browser/base/content/test/social/social_worker.js", 1.21 + markURL: "https://test1.example.com/browser/browser/base/content/test/social/social_mark.html?url=%{url}", 1.22 + markedIcon: "https://test1.example.com/browser/browser/base/content/test/social/unchecked.jpg", 1.23 + unmarkedIcon: "https://test1.example.com/browser/browser/base/content/test/social/checked.jpg", 1.24 + 1.25 + iconURL: "https://test1.example.com/browser/browser/base/content/test/general/moz.png", 1.26 + version: 1 1.27 +}; 1.28 +let manifest3 = { // used for testing install 1.29 + name: "provider test2", 1.30 + origin: "https://test2.example.com", 1.31 + sidebarURL: "https://test2.example.com/browser/browser/base/content/test/social/social_sidebar.html", 1.32 + iconURL: "https://test2.example.com/browser/browser/base/content/test/general/moz.png", 1.33 + version: 1 1.34 +}; 1.35 +function makeMarkProvider(origin) { 1.36 + return { // used for testing install 1.37 + name: "mark provider " + origin, 1.38 + origin: "https://" + origin + ".example.com", 1.39 + workerURL: "https://" + origin + ".example.com/browser/browser/base/content/test/social/social_worker.js", 1.40 + markURL: "https://" + origin + ".example.com/browser/browser/base/content/test/social/social_mark.html?url=%{url}", 1.41 + markedIcon: "https://" + origin + ".example.com/browser/browser/base/content/test/social/unchecked.jpg", 1.42 + unmarkedIcon: "https://" + origin + ".example.com/browser/browser/base/content/test/social/checked.jpg", 1.43 + 1.44 + iconURL: "https://" + origin + ".example.com/browser/browser/base/content/test/general/moz.png", 1.45 + version: 1 1.46 + } 1.47 +} 1.48 + 1.49 +function openWindowAndWaitForInit(callback) { 1.50 + let topic = "browser-delayed-startup-finished"; 1.51 + let w = OpenBrowserWindow(); 1.52 + Services.obs.addObserver(function providerSet(subject, topic, data) { 1.53 + Services.obs.removeObserver(providerSet, topic); 1.54 + executeSoon(() => callback(w)); 1.55 + }, topic, false); 1.56 +} 1.57 + 1.58 +function test() { 1.59 + waitForExplicitFinish(); 1.60 + 1.61 + let toolbar = document.getElementById("nav-bar"); 1.62 + let currentsetAtStart = toolbar.currentSet; 1.63 + runSocialTestWithProvider(manifest, function (finishcb) { 1.64 + runSocialTests(tests, undefined, undefined, function () { 1.65 + Services.prefs.clearUserPref("social.remote-install.enabled"); 1.66 + // just in case the tests failed, clear these here as well 1.67 + Services.prefs.clearUserPref("social.whitelist"); 1.68 + ok(CustomizableUI.inDefaultState, "Should be in the default state when we finish"); 1.69 + CustomizableUI.reset(); 1.70 + finishcb(); 1.71 + }); 1.72 + }); 1.73 +} 1.74 + 1.75 +var tests = { 1.76 + testButtonDisabledOnActivate: function(next) { 1.77 + // starting on about:blank page, share should be visible but disabled when 1.78 + // adding provider 1.79 + is(gBrowser.contentDocument.location.href, "about:blank"); 1.80 + SocialService.addProvider(manifest2, function(provider) { 1.81 + is(provider.origin, manifest2.origin, "provider is installed"); 1.82 + let id = SocialMarks._toolbarHelper.idFromOrigin(manifest2.origin); 1.83 + let widget = CustomizableUI.getWidget(id).forWindow(window) 1.84 + ok(widget.node, "button added to widget set"); 1.85 + 1.86 + // bypass widget go directly to dom, check attribute states 1.87 + let button = document.getElementById(id); 1.88 + is(button.disabled, true, "mark button is disabled"); 1.89 + // verify the attribute for proper css 1.90 + is(button.getAttribute("disabled"), "true", "mark button attribute is disabled"); 1.91 + // button should be visible 1.92 + is(button.hidden, false, "mark button is visible"); 1.93 + 1.94 + checkSocialUI(window); 1.95 + SocialService.removeProvider(manifest2.origin, next); 1.96 + }); 1.97 + }, 1.98 + testNoButtonOnEnable: function(next) { 1.99 + // we expect the addon install dialog to appear, we need to accept the 1.100 + // install from the dialog. 1.101 + let panel = document.getElementById("servicesInstall-notification"); 1.102 + PopupNotifications.panel.addEventListener("popupshown", function onpopupshown() { 1.103 + PopupNotifications.panel.removeEventListener("popupshown", onpopupshown); 1.104 + info("servicesInstall-notification panel opened"); 1.105 + panel.button.click(); 1.106 + }); 1.107 + 1.108 + let activationURL = manifest3.origin + "/browser/browser/base/content/test/social/social_activate.html" 1.109 + addTab(activationURL, function(tab) { 1.110 + let doc = tab.linkedBrowser.contentDocument; 1.111 + Social.installProvider(doc, manifest3, function(addonManifest) { 1.112 + // enable the provider so we know the button would have appeared 1.113 + SocialService.addBuiltinProvider(manifest3.origin, function(provider) { 1.114 + is(provider.origin, manifest3.origin, "provider is installed"); 1.115 + let id = SocialMarks._toolbarHelper.idFromOrigin(provider.origin); 1.116 + let widget = CustomizableUI.getWidget(id); 1.117 + ok(!widget || !widget.forWindow(window).node, "no button added to widget set"); 1.118 + Social.uninstallProvider(manifest3.origin, function() { 1.119 + gBrowser.removeTab(tab); 1.120 + next(); 1.121 + }); 1.122 + }); 1.123 + }); 1.124 + }); 1.125 + }, 1.126 + 1.127 + testButtonOnEnable: function(next) { 1.128 + let panel = document.getElementById("servicesInstall-notification"); 1.129 + PopupNotifications.panel.addEventListener("popupshown", function onpopupshown() { 1.130 + PopupNotifications.panel.removeEventListener("popupshown", onpopupshown); 1.131 + info("servicesInstall-notification panel opened"); 1.132 + panel.button.click(); 1.133 + }); 1.134 + 1.135 + // enable the provider now 1.136 + let activationURL = manifest2.origin + "/browser/browser/base/content/test/social/social_activate.html" 1.137 + addTab(activationURL, function(tab) { 1.138 + let doc = tab.linkedBrowser.contentDocument; 1.139 + Social.installProvider(doc, manifest2, function(addonManifest) { 1.140 + SocialService.addBuiltinProvider(manifest2.origin, function(provider) { 1.141 + is(provider.origin, manifest2.origin, "provider is installed"); 1.142 + let id = SocialMarks._toolbarHelper.idFromOrigin(manifest2.origin); 1.143 + let widget = CustomizableUI.getWidget(id).forWindow(window) 1.144 + ok(widget.node, "button added to widget set"); 1.145 + 1.146 + // bypass widget go directly to dom, check attribute states 1.147 + let button = document.getElementById(id); 1.148 + is(button.disabled, false, "mark button is disabled"); 1.149 + // verify the attribute for proper css 1.150 + ok(!button.hasAttribute("disabled"), "mark button attribute is disabled"); 1.151 + // button should be visible 1.152 + is(button.hidden, false, "mark button is visible"); 1.153 + 1.154 + checkSocialUI(window); 1.155 + gBrowser.removeTab(tab); 1.156 + next(); 1.157 + }); 1.158 + }); 1.159 + }); 1.160 + }, 1.161 + 1.162 + testMarkPanel: function(next) { 1.163 + // click on panel to open and wait for visibility 1.164 + let provider = Social._getProviderFromOrigin(manifest2.origin); 1.165 + ok(provider.enabled, "provider is enabled"); 1.166 + let id = SocialMarks._toolbarHelper.idFromOrigin(manifest2.origin); 1.167 + let widget = CustomizableUI.getWidget(id); 1.168 + let btn = widget.forWindow(window).node; 1.169 + ok(btn, "got a mark button"); 1.170 + let port = provider.getWorkerPort(); 1.171 + ok(port, "got a port"); 1.172 + 1.173 + // verify markbutton is disabled when there is no browser url 1.174 + ok(btn.disabled, "button is disabled"); 1.175 + let activationURL = manifest2.origin + "/browser/browser/base/content/test/social/social_activate.html" 1.176 + addTab(activationURL, function(tab) { 1.177 + ok(!btn.disabled, "button is enabled"); 1.178 + port.onmessage = function (e) { 1.179 + let topic = e.data.topic; 1.180 + switch (topic) { 1.181 + case "test-init-done": 1.182 + ok(true, "test-init-done received"); 1.183 + ok(provider.profile.userName, "profile was set by test worker"); 1.184 + // first click marks the page, second click opens the page. We have to 1.185 + // synthesize so the command event happens 1.186 + EventUtils.synthesizeMouseAtCenter(btn, {}); 1.187 + // wait for the button to be marked, click to open panel 1.188 + waitForCondition(function() btn.isMarked, function() { 1.189 + is(btn.panel.state, "closed", "panel should not be visible yet"); 1.190 + EventUtils.synthesizeMouseAtCenter(btn, {}); 1.191 + }, "button is marked"); 1.192 + break; 1.193 + case "got-social-panel-visibility": 1.194 + ok(true, "got the panel message " + e.data.result); 1.195 + if (e.data.result == "shown") { 1.196 + // unmark the page via the button in the page 1.197 + let doc = btn.contentDocument; 1.198 + let unmarkBtn = doc.getElementById("unmark"); 1.199 + ok(unmarkBtn, "got the panel unmark button"); 1.200 + EventUtils.sendMouseEvent({type: "click"}, unmarkBtn, btn.contentWindow); 1.201 + } else { 1.202 + // page should no longer be marked 1.203 + port.close(); 1.204 + waitForCondition(function() !btn.isMarked, function() { 1.205 + // cleanup after the page has been unmarked 1.206 + gBrowser.tabContainer.addEventListener("TabClose", function onTabClose() { 1.207 + gBrowser.tabContainer.removeEventListener("TabClose", onTabClose); 1.208 + executeSoon(function () { 1.209 + ok(btn.disabled, "button is disabled"); 1.210 + next(); 1.211 + }); 1.212 + }); 1.213 + gBrowser.removeTab(tab); 1.214 + }, "button unmarked"); 1.215 + } 1.216 + break; 1.217 + } 1.218 + }; 1.219 + port.postMessage({topic: "test-init"}); 1.220 + }); 1.221 + }, 1.222 + 1.223 + testMarkPanelLoggedOut: function(next) { 1.224 + // click on panel to open and wait for visibility 1.225 + let provider = Social._getProviderFromOrigin(manifest2.origin); 1.226 + ok(provider.enabled, "provider is enabled"); 1.227 + let id = SocialMarks._toolbarHelper.idFromOrigin(manifest2.origin); 1.228 + let widget = CustomizableUI.getWidget(id); 1.229 + let btn = widget.forWindow(window).node; 1.230 + ok(btn, "got a mark button"); 1.231 + let port = provider.getWorkerPort(); 1.232 + ok(port, "got a port"); 1.233 + 1.234 + // verify markbutton is disabled when there is no browser url 1.235 + ok(btn.disabled, "button is disabled"); 1.236 + let activationURL = manifest2.origin + "/browser/browser/base/content/test/social/social_activate.html" 1.237 + addTab(activationURL, function(tab) { 1.238 + ok(!btn.disabled, "button is enabled"); 1.239 + port.onmessage = function (e) { 1.240 + let topic = e.data.topic; 1.241 + switch (topic) { 1.242 + case "test-init-done": 1.243 + ok(true, "test-init-done received"); 1.244 + ok(provider.profile.userName, "profile was set by test worker"); 1.245 + port.postMessage({topic: "test-logout"}); 1.246 + waitForCondition(function() !provider.profile.userName, 1.247 + function() { 1.248 + // when the provider has not indicated to us that a user is 1.249 + // logged in, the first click opens the page. 1.250 + EventUtils.synthesizeMouseAtCenter(btn, {}); 1.251 + }, 1.252 + "profile was unset by test worker"); 1.253 + break; 1.254 + case "got-social-panel-visibility": 1.255 + ok(true, "got the panel message " + e.data.result); 1.256 + if (e.data.result == "shown") { 1.257 + // our test marks the page during the load event (see 1.258 + // social_mark.html) regardless of login state, unmark the page 1.259 + // via the button in the page 1.260 + let doc = btn.contentDocument; 1.261 + let unmarkBtn = doc.getElementById("unmark"); 1.262 + ok(unmarkBtn, "got the panel unmark button"); 1.263 + EventUtils.sendMouseEvent({type: "click"}, unmarkBtn, btn.contentWindow); 1.264 + } else { 1.265 + // page should no longer be marked 1.266 + port.close(); 1.267 + waitForCondition(function() !btn.isMarked, function() { 1.268 + // cleanup after the page has been unmarked 1.269 + gBrowser.tabContainer.addEventListener("TabClose", function onTabClose() { 1.270 + gBrowser.tabContainer.removeEventListener("TabClose", onTabClose); 1.271 + executeSoon(function () { 1.272 + ok(btn.disabled, "button is disabled"); 1.273 + next(); 1.274 + }); 1.275 + }); 1.276 + gBrowser.removeTab(tab); 1.277 + }, "button unmarked"); 1.278 + } 1.279 + break; 1.280 + } 1.281 + }; 1.282 + port.postMessage({topic: "test-init"}); 1.283 + }); 1.284 + }, 1.285 + 1.286 + testButtonOnDisable: function(next) { 1.287 + // enable the provider now 1.288 + let provider = Social._getProviderFromOrigin(manifest2.origin); 1.289 + ok(provider, "provider is installed"); 1.290 + SocialService.removeProvider(manifest2.origin, function() { 1.291 + let id = SocialMarks._toolbarHelper.idFromOrigin(manifest2.origin); 1.292 + waitForCondition(function() { 1.293 + // getWidget now returns null since we've destroyed the widget 1.294 + return !CustomizableUI.getWidget(id) 1.295 + }, 1.296 + function() { 1.297 + checkSocialUI(window); 1.298 + Social.uninstallProvider(manifest2.origin, next); 1.299 + }, "button does not exist after disabling the provider"); 1.300 + }); 1.301 + }, 1.302 + 1.303 + testContextSubmenu: function(next) { 1.304 + // install 4 providers to test that the menu's are added as submenus 1.305 + let manifests = [ 1.306 + makeMarkProvider("sub1.test1"), 1.307 + makeMarkProvider("sub2.test1"), 1.308 + makeMarkProvider("sub1.test2"), 1.309 + makeMarkProvider("sub2.test2") 1.310 + ]; 1.311 + let installed = []; 1.312 + let markLinkMenu = document.getElementById("context-marklinkMenu").firstChild; 1.313 + let markPageMenu = document.getElementById("context-markpageMenu").firstChild; 1.314 + 1.315 + function addProviders(callback) { 1.316 + let manifest = manifests.pop(); 1.317 + if (!manifest) { 1.318 + info("INSTALLATION FINISHED"); 1.319 + executeSoon(callback); 1.320 + return; 1.321 + } 1.322 + info("INSTALLING " + manifest.origin); 1.323 + let panel = document.getElementById("servicesInstall-notification"); 1.324 + PopupNotifications.panel.addEventListener("popupshown", function onpopupshown() { 1.325 + PopupNotifications.panel.removeEventListener("popupshown", onpopupshown); 1.326 + info("servicesInstall-notification panel opened"); 1.327 + panel.button.click(); 1.328 + }) 1.329 + 1.330 + let activationURL = manifest.origin + "/browser/browser/base/content/test/social/social_activate.html" 1.331 + let id = SocialMarks._toolbarHelper.idFromOrigin(manifest.origin); 1.332 + let toolbar = document.getElementById("nav-bar"); 1.333 + addTab(activationURL, function(tab) { 1.334 + let doc = tab.linkedBrowser.contentDocument; 1.335 + Social.installProvider(doc, manifest, function(addonManifest) { 1.336 + // enable the provider so we know the button would have appeared 1.337 + SocialService.addBuiltinProvider(manifest.origin, function(provider) { 1.338 + waitForCondition(function() { return CustomizableUI.getWidget(id) }, 1.339 + function() { 1.340 + gBrowser.removeTab(tab); 1.341 + installed.push(manifest.origin); 1.342 + // checkSocialUI will properly check where the menus are located 1.343 + checkSocialUI(window); 1.344 + executeSoon(function() { 1.345 + addProviders(callback); 1.346 + }); 1.347 + }, "button exists after enabling social"); 1.348 + }); 1.349 + }); 1.350 + }); 1.351 + } 1.352 + 1.353 + function removeProviders(callback) { 1.354 + let origin = installed.pop(); 1.355 + if (!origin) { 1.356 + executeSoon(callback); 1.357 + return; 1.358 + } 1.359 + Social.uninstallProvider(origin, function(provider) { 1.360 + executeSoon(function() { 1.361 + removeProviders(callback); 1.362 + }); 1.363 + }); 1.364 + } 1.365 + 1.366 + addProviders(function() { 1.367 + removeProviders(function() { 1.368 + is(SocialMarks.getProviders().length, 0, "mark providers removed"); 1.369 + is(markLinkMenu.childNodes.length, 0, "marklink menu ok"); 1.370 + is(markPageMenu.childNodes.length, 0, "markpage menu ok"); 1.371 + checkSocialUI(window); 1.372 + next(); 1.373 + }); 1.374 + }); 1.375 + } 1.376 +}