michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: let SocialService = Cu.import("resource://gre/modules/SocialService.jsm", {}).SocialService; michael@0: michael@0: let manifest = { // builtin provider michael@0: name: "provider example.com", michael@0: origin: "https://example.com", michael@0: sidebarURL: "https://example.com/browser/browser/base/content/test/social/social_sidebar.html", michael@0: workerURL: "https://example.com/browser/browser/base/content/test/social/social_worker.js", michael@0: iconURL: "https://example.com/browser/browser/base/content/test/general/moz.png" michael@0: }; michael@0: let manifest2 = { // used for testing install michael@0: name: "provider test1", michael@0: origin: "https://test1.example.com", michael@0: workerURL: "https://test1.example.com/browser/browser/base/content/test/social/social_worker.js", michael@0: markURL: "https://test1.example.com/browser/browser/base/content/test/social/social_mark.html?url=%{url}", michael@0: markedIcon: "https://test1.example.com/browser/browser/base/content/test/social/unchecked.jpg", michael@0: unmarkedIcon: "https://test1.example.com/browser/browser/base/content/test/social/checked.jpg", michael@0: michael@0: iconURL: "https://test1.example.com/browser/browser/base/content/test/general/moz.png", michael@0: version: 1 michael@0: }; michael@0: let manifest3 = { // used for testing install michael@0: name: "provider test2", michael@0: origin: "https://test2.example.com", michael@0: sidebarURL: "https://test2.example.com/browser/browser/base/content/test/social/social_sidebar.html", michael@0: iconURL: "https://test2.example.com/browser/browser/base/content/test/general/moz.png", michael@0: version: 1 michael@0: }; michael@0: function makeMarkProvider(origin) { michael@0: return { // used for testing install michael@0: name: "mark provider " + origin, michael@0: origin: "https://" + origin + ".example.com", michael@0: workerURL: "https://" + origin + ".example.com/browser/browser/base/content/test/social/social_worker.js", michael@0: markURL: "https://" + origin + ".example.com/browser/browser/base/content/test/social/social_mark.html?url=%{url}", michael@0: markedIcon: "https://" + origin + ".example.com/browser/browser/base/content/test/social/unchecked.jpg", michael@0: unmarkedIcon: "https://" + origin + ".example.com/browser/browser/base/content/test/social/checked.jpg", michael@0: michael@0: iconURL: "https://" + origin + ".example.com/browser/browser/base/content/test/general/moz.png", michael@0: version: 1 michael@0: } michael@0: } michael@0: michael@0: function openWindowAndWaitForInit(callback) { michael@0: let topic = "browser-delayed-startup-finished"; michael@0: let w = OpenBrowserWindow(); michael@0: Services.obs.addObserver(function providerSet(subject, topic, data) { michael@0: Services.obs.removeObserver(providerSet, topic); michael@0: executeSoon(() => callback(w)); michael@0: }, topic, false); michael@0: } michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: michael@0: let toolbar = document.getElementById("nav-bar"); michael@0: let currentsetAtStart = toolbar.currentSet; michael@0: runSocialTestWithProvider(manifest, function (finishcb) { michael@0: runSocialTests(tests, undefined, undefined, function () { michael@0: Services.prefs.clearUserPref("social.remote-install.enabled"); michael@0: // just in case the tests failed, clear these here as well michael@0: Services.prefs.clearUserPref("social.whitelist"); michael@0: ok(CustomizableUI.inDefaultState, "Should be in the default state when we finish"); michael@0: CustomizableUI.reset(); michael@0: finishcb(); michael@0: }); michael@0: }); michael@0: } michael@0: michael@0: var tests = { michael@0: testButtonDisabledOnActivate: function(next) { michael@0: // starting on about:blank page, share should be visible but disabled when michael@0: // adding provider michael@0: is(gBrowser.contentDocument.location.href, "about:blank"); michael@0: SocialService.addProvider(manifest2, function(provider) { michael@0: is(provider.origin, manifest2.origin, "provider is installed"); michael@0: let id = SocialMarks._toolbarHelper.idFromOrigin(manifest2.origin); michael@0: let widget = CustomizableUI.getWidget(id).forWindow(window) michael@0: ok(widget.node, "button added to widget set"); michael@0: michael@0: // bypass widget go directly to dom, check attribute states michael@0: let button = document.getElementById(id); michael@0: is(button.disabled, true, "mark button is disabled"); michael@0: // verify the attribute for proper css michael@0: is(button.getAttribute("disabled"), "true", "mark button attribute is disabled"); michael@0: // button should be visible michael@0: is(button.hidden, false, "mark button is visible"); michael@0: michael@0: checkSocialUI(window); michael@0: SocialService.removeProvider(manifest2.origin, next); michael@0: }); michael@0: }, michael@0: testNoButtonOnEnable: function(next) { michael@0: // we expect the addon install dialog to appear, we need to accept the michael@0: // install from the dialog. michael@0: let panel = document.getElementById("servicesInstall-notification"); michael@0: PopupNotifications.panel.addEventListener("popupshown", function onpopupshown() { michael@0: PopupNotifications.panel.removeEventListener("popupshown", onpopupshown); michael@0: info("servicesInstall-notification panel opened"); michael@0: panel.button.click(); michael@0: }); michael@0: michael@0: let activationURL = manifest3.origin + "/browser/browser/base/content/test/social/social_activate.html" michael@0: addTab(activationURL, function(tab) { michael@0: let doc = tab.linkedBrowser.contentDocument; michael@0: Social.installProvider(doc, manifest3, function(addonManifest) { michael@0: // enable the provider so we know the button would have appeared michael@0: SocialService.addBuiltinProvider(manifest3.origin, function(provider) { michael@0: is(provider.origin, manifest3.origin, "provider is installed"); michael@0: let id = SocialMarks._toolbarHelper.idFromOrigin(provider.origin); michael@0: let widget = CustomizableUI.getWidget(id); michael@0: ok(!widget || !widget.forWindow(window).node, "no button added to widget set"); michael@0: Social.uninstallProvider(manifest3.origin, function() { michael@0: gBrowser.removeTab(tab); michael@0: next(); michael@0: }); michael@0: }); michael@0: }); michael@0: }); michael@0: }, michael@0: michael@0: testButtonOnEnable: function(next) { michael@0: let panel = document.getElementById("servicesInstall-notification"); michael@0: PopupNotifications.panel.addEventListener("popupshown", function onpopupshown() { michael@0: PopupNotifications.panel.removeEventListener("popupshown", onpopupshown); michael@0: info("servicesInstall-notification panel opened"); michael@0: panel.button.click(); michael@0: }); michael@0: michael@0: // enable the provider now michael@0: let activationURL = manifest2.origin + "/browser/browser/base/content/test/social/social_activate.html" michael@0: addTab(activationURL, function(tab) { michael@0: let doc = tab.linkedBrowser.contentDocument; michael@0: Social.installProvider(doc, manifest2, function(addonManifest) { michael@0: SocialService.addBuiltinProvider(manifest2.origin, function(provider) { michael@0: is(provider.origin, manifest2.origin, "provider is installed"); michael@0: let id = SocialMarks._toolbarHelper.idFromOrigin(manifest2.origin); michael@0: let widget = CustomizableUI.getWidget(id).forWindow(window) michael@0: ok(widget.node, "button added to widget set"); michael@0: michael@0: // bypass widget go directly to dom, check attribute states michael@0: let button = document.getElementById(id); michael@0: is(button.disabled, false, "mark button is disabled"); michael@0: // verify the attribute for proper css michael@0: ok(!button.hasAttribute("disabled"), "mark button attribute is disabled"); michael@0: // button should be visible michael@0: is(button.hidden, false, "mark button is visible"); michael@0: michael@0: checkSocialUI(window); michael@0: gBrowser.removeTab(tab); michael@0: next(); michael@0: }); michael@0: }); michael@0: }); michael@0: }, michael@0: michael@0: testMarkPanel: function(next) { michael@0: // click on panel to open and wait for visibility michael@0: let provider = Social._getProviderFromOrigin(manifest2.origin); michael@0: ok(provider.enabled, "provider is enabled"); michael@0: let id = SocialMarks._toolbarHelper.idFromOrigin(manifest2.origin); michael@0: let widget = CustomizableUI.getWidget(id); michael@0: let btn = widget.forWindow(window).node; michael@0: ok(btn, "got a mark button"); michael@0: let port = provider.getWorkerPort(); michael@0: ok(port, "got a port"); michael@0: michael@0: // verify markbutton is disabled when there is no browser url michael@0: ok(btn.disabled, "button is disabled"); michael@0: let activationURL = manifest2.origin + "/browser/browser/base/content/test/social/social_activate.html" michael@0: addTab(activationURL, function(tab) { michael@0: ok(!btn.disabled, "button is enabled"); michael@0: port.onmessage = function (e) { michael@0: let topic = e.data.topic; michael@0: switch (topic) { michael@0: case "test-init-done": michael@0: ok(true, "test-init-done received"); michael@0: ok(provider.profile.userName, "profile was set by test worker"); michael@0: // first click marks the page, second click opens the page. We have to michael@0: // synthesize so the command event happens michael@0: EventUtils.synthesizeMouseAtCenter(btn, {}); michael@0: // wait for the button to be marked, click to open panel michael@0: waitForCondition(function() btn.isMarked, function() { michael@0: is(btn.panel.state, "closed", "panel should not be visible yet"); michael@0: EventUtils.synthesizeMouseAtCenter(btn, {}); michael@0: }, "button is marked"); michael@0: break; michael@0: case "got-social-panel-visibility": michael@0: ok(true, "got the panel message " + e.data.result); michael@0: if (e.data.result == "shown") { michael@0: // unmark the page via the button in the page michael@0: let doc = btn.contentDocument; michael@0: let unmarkBtn = doc.getElementById("unmark"); michael@0: ok(unmarkBtn, "got the panel unmark button"); michael@0: EventUtils.sendMouseEvent({type: "click"}, unmarkBtn, btn.contentWindow); michael@0: } else { michael@0: // page should no longer be marked michael@0: port.close(); michael@0: waitForCondition(function() !btn.isMarked, function() { michael@0: // cleanup after the page has been unmarked michael@0: gBrowser.tabContainer.addEventListener("TabClose", function onTabClose() { michael@0: gBrowser.tabContainer.removeEventListener("TabClose", onTabClose); michael@0: executeSoon(function () { michael@0: ok(btn.disabled, "button is disabled"); michael@0: next(); michael@0: }); michael@0: }); michael@0: gBrowser.removeTab(tab); michael@0: }, "button unmarked"); michael@0: } michael@0: break; michael@0: } michael@0: }; michael@0: port.postMessage({topic: "test-init"}); michael@0: }); michael@0: }, michael@0: michael@0: testMarkPanelLoggedOut: function(next) { michael@0: // click on panel to open and wait for visibility michael@0: let provider = Social._getProviderFromOrigin(manifest2.origin); michael@0: ok(provider.enabled, "provider is enabled"); michael@0: let id = SocialMarks._toolbarHelper.idFromOrigin(manifest2.origin); michael@0: let widget = CustomizableUI.getWidget(id); michael@0: let btn = widget.forWindow(window).node; michael@0: ok(btn, "got a mark button"); michael@0: let port = provider.getWorkerPort(); michael@0: ok(port, "got a port"); michael@0: michael@0: // verify markbutton is disabled when there is no browser url michael@0: ok(btn.disabled, "button is disabled"); michael@0: let activationURL = manifest2.origin + "/browser/browser/base/content/test/social/social_activate.html" michael@0: addTab(activationURL, function(tab) { michael@0: ok(!btn.disabled, "button is enabled"); michael@0: port.onmessage = function (e) { michael@0: let topic = e.data.topic; michael@0: switch (topic) { michael@0: case "test-init-done": michael@0: ok(true, "test-init-done received"); michael@0: ok(provider.profile.userName, "profile was set by test worker"); michael@0: port.postMessage({topic: "test-logout"}); michael@0: waitForCondition(function() !provider.profile.userName, michael@0: function() { michael@0: // when the provider has not indicated to us that a user is michael@0: // logged in, the first click opens the page. michael@0: EventUtils.synthesizeMouseAtCenter(btn, {}); michael@0: }, michael@0: "profile was unset by test worker"); michael@0: break; michael@0: case "got-social-panel-visibility": michael@0: ok(true, "got the panel message " + e.data.result); michael@0: if (e.data.result == "shown") { michael@0: // our test marks the page during the load event (see michael@0: // social_mark.html) regardless of login state, unmark the page michael@0: // via the button in the page michael@0: let doc = btn.contentDocument; michael@0: let unmarkBtn = doc.getElementById("unmark"); michael@0: ok(unmarkBtn, "got the panel unmark button"); michael@0: EventUtils.sendMouseEvent({type: "click"}, unmarkBtn, btn.contentWindow); michael@0: } else { michael@0: // page should no longer be marked michael@0: port.close(); michael@0: waitForCondition(function() !btn.isMarked, function() { michael@0: // cleanup after the page has been unmarked michael@0: gBrowser.tabContainer.addEventListener("TabClose", function onTabClose() { michael@0: gBrowser.tabContainer.removeEventListener("TabClose", onTabClose); michael@0: executeSoon(function () { michael@0: ok(btn.disabled, "button is disabled"); michael@0: next(); michael@0: }); michael@0: }); michael@0: gBrowser.removeTab(tab); michael@0: }, "button unmarked"); michael@0: } michael@0: break; michael@0: } michael@0: }; michael@0: port.postMessage({topic: "test-init"}); michael@0: }); michael@0: }, michael@0: michael@0: testButtonOnDisable: function(next) { michael@0: // enable the provider now michael@0: let provider = Social._getProviderFromOrigin(manifest2.origin); michael@0: ok(provider, "provider is installed"); michael@0: SocialService.removeProvider(manifest2.origin, function() { michael@0: let id = SocialMarks._toolbarHelper.idFromOrigin(manifest2.origin); michael@0: waitForCondition(function() { michael@0: // getWidget now returns null since we've destroyed the widget michael@0: return !CustomizableUI.getWidget(id) michael@0: }, michael@0: function() { michael@0: checkSocialUI(window); michael@0: Social.uninstallProvider(manifest2.origin, next); michael@0: }, "button does not exist after disabling the provider"); michael@0: }); michael@0: }, michael@0: michael@0: testContextSubmenu: function(next) { michael@0: // install 4 providers to test that the menu's are added as submenus michael@0: let manifests = [ michael@0: makeMarkProvider("sub1.test1"), michael@0: makeMarkProvider("sub2.test1"), michael@0: makeMarkProvider("sub1.test2"), michael@0: makeMarkProvider("sub2.test2") michael@0: ]; michael@0: let installed = []; michael@0: let markLinkMenu = document.getElementById("context-marklinkMenu").firstChild; michael@0: let markPageMenu = document.getElementById("context-markpageMenu").firstChild; michael@0: michael@0: function addProviders(callback) { michael@0: let manifest = manifests.pop(); michael@0: if (!manifest) { michael@0: info("INSTALLATION FINISHED"); michael@0: executeSoon(callback); michael@0: return; michael@0: } michael@0: info("INSTALLING " + manifest.origin); michael@0: let panel = document.getElementById("servicesInstall-notification"); michael@0: PopupNotifications.panel.addEventListener("popupshown", function onpopupshown() { michael@0: PopupNotifications.panel.removeEventListener("popupshown", onpopupshown); michael@0: info("servicesInstall-notification panel opened"); michael@0: panel.button.click(); michael@0: }) michael@0: michael@0: let activationURL = manifest.origin + "/browser/browser/base/content/test/social/social_activate.html" michael@0: let id = SocialMarks._toolbarHelper.idFromOrigin(manifest.origin); michael@0: let toolbar = document.getElementById("nav-bar"); michael@0: addTab(activationURL, function(tab) { michael@0: let doc = tab.linkedBrowser.contentDocument; michael@0: Social.installProvider(doc, manifest, function(addonManifest) { michael@0: // enable the provider so we know the button would have appeared michael@0: SocialService.addBuiltinProvider(manifest.origin, function(provider) { michael@0: waitForCondition(function() { return CustomizableUI.getWidget(id) }, michael@0: function() { michael@0: gBrowser.removeTab(tab); michael@0: installed.push(manifest.origin); michael@0: // checkSocialUI will properly check where the menus are located michael@0: checkSocialUI(window); michael@0: executeSoon(function() { michael@0: addProviders(callback); michael@0: }); michael@0: }, "button exists after enabling social"); michael@0: }); michael@0: }); michael@0: }); michael@0: } michael@0: michael@0: function removeProviders(callback) { michael@0: let origin = installed.pop(); michael@0: if (!origin) { michael@0: executeSoon(callback); michael@0: return; michael@0: } michael@0: Social.uninstallProvider(origin, function(provider) { michael@0: executeSoon(function() { michael@0: removeProviders(callback); michael@0: }); michael@0: }); michael@0: } michael@0: michael@0: addProviders(function() { michael@0: removeProviders(function() { michael@0: is(SocialMarks.getProviders().length, 0, "mark providers removed"); michael@0: is(markLinkMenu.childNodes.length, 0, "marklink menu ok"); michael@0: is(markPageMenu.childNodes.length, 0, "markpage menu ok"); michael@0: checkSocialUI(window); michael@0: next(); michael@0: }); michael@0: }); michael@0: } michael@0: }