browser/base/content/test/social/browser_social_marks.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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 manifest = { // builtin provider
michael@0 8 name: "provider example.com",
michael@0 9 origin: "https://example.com",
michael@0 10 sidebarURL: "https://example.com/browser/browser/base/content/test/social/social_sidebar.html",
michael@0 11 workerURL: "https://example.com/browser/browser/base/content/test/social/social_worker.js",
michael@0 12 iconURL: "https://example.com/browser/browser/base/content/test/general/moz.png"
michael@0 13 };
michael@0 14 let manifest2 = { // used for testing install
michael@0 15 name: "provider test1",
michael@0 16 origin: "https://test1.example.com",
michael@0 17 workerURL: "https://test1.example.com/browser/browser/base/content/test/social/social_worker.js",
michael@0 18 markURL: "https://test1.example.com/browser/browser/base/content/test/social/social_mark.html?url=%{url}",
michael@0 19 markedIcon: "https://test1.example.com/browser/browser/base/content/test/social/unchecked.jpg",
michael@0 20 unmarkedIcon: "https://test1.example.com/browser/browser/base/content/test/social/checked.jpg",
michael@0 21
michael@0 22 iconURL: "https://test1.example.com/browser/browser/base/content/test/general/moz.png",
michael@0 23 version: 1
michael@0 24 };
michael@0 25 let manifest3 = { // used for testing install
michael@0 26 name: "provider test2",
michael@0 27 origin: "https://test2.example.com",
michael@0 28 sidebarURL: "https://test2.example.com/browser/browser/base/content/test/social/social_sidebar.html",
michael@0 29 iconURL: "https://test2.example.com/browser/browser/base/content/test/general/moz.png",
michael@0 30 version: 1
michael@0 31 };
michael@0 32 function makeMarkProvider(origin) {
michael@0 33 return { // used for testing install
michael@0 34 name: "mark provider " + origin,
michael@0 35 origin: "https://" + origin + ".example.com",
michael@0 36 workerURL: "https://" + origin + ".example.com/browser/browser/base/content/test/social/social_worker.js",
michael@0 37 markURL: "https://" + origin + ".example.com/browser/browser/base/content/test/social/social_mark.html?url=%{url}",
michael@0 38 markedIcon: "https://" + origin + ".example.com/browser/browser/base/content/test/social/unchecked.jpg",
michael@0 39 unmarkedIcon: "https://" + origin + ".example.com/browser/browser/base/content/test/social/checked.jpg",
michael@0 40
michael@0 41 iconURL: "https://" + origin + ".example.com/browser/browser/base/content/test/general/moz.png",
michael@0 42 version: 1
michael@0 43 }
michael@0 44 }
michael@0 45
michael@0 46 function openWindowAndWaitForInit(callback) {
michael@0 47 let topic = "browser-delayed-startup-finished";
michael@0 48 let w = OpenBrowserWindow();
michael@0 49 Services.obs.addObserver(function providerSet(subject, topic, data) {
michael@0 50 Services.obs.removeObserver(providerSet, topic);
michael@0 51 executeSoon(() => callback(w));
michael@0 52 }, topic, false);
michael@0 53 }
michael@0 54
michael@0 55 function test() {
michael@0 56 waitForExplicitFinish();
michael@0 57
michael@0 58 let toolbar = document.getElementById("nav-bar");
michael@0 59 let currentsetAtStart = toolbar.currentSet;
michael@0 60 runSocialTestWithProvider(manifest, function (finishcb) {
michael@0 61 runSocialTests(tests, undefined, undefined, function () {
michael@0 62 Services.prefs.clearUserPref("social.remote-install.enabled");
michael@0 63 // just in case the tests failed, clear these here as well
michael@0 64 Services.prefs.clearUserPref("social.whitelist");
michael@0 65 ok(CustomizableUI.inDefaultState, "Should be in the default state when we finish");
michael@0 66 CustomizableUI.reset();
michael@0 67 finishcb();
michael@0 68 });
michael@0 69 });
michael@0 70 }
michael@0 71
michael@0 72 var tests = {
michael@0 73 testButtonDisabledOnActivate: function(next) {
michael@0 74 // starting on about:blank page, share should be visible but disabled when
michael@0 75 // adding provider
michael@0 76 is(gBrowser.contentDocument.location.href, "about:blank");
michael@0 77 SocialService.addProvider(manifest2, function(provider) {
michael@0 78 is(provider.origin, manifest2.origin, "provider is installed");
michael@0 79 let id = SocialMarks._toolbarHelper.idFromOrigin(manifest2.origin);
michael@0 80 let widget = CustomizableUI.getWidget(id).forWindow(window)
michael@0 81 ok(widget.node, "button added to widget set");
michael@0 82
michael@0 83 // bypass widget go directly to dom, check attribute states
michael@0 84 let button = document.getElementById(id);
michael@0 85 is(button.disabled, true, "mark button is disabled");
michael@0 86 // verify the attribute for proper css
michael@0 87 is(button.getAttribute("disabled"), "true", "mark button attribute is disabled");
michael@0 88 // button should be visible
michael@0 89 is(button.hidden, false, "mark button is visible");
michael@0 90
michael@0 91 checkSocialUI(window);
michael@0 92 SocialService.removeProvider(manifest2.origin, next);
michael@0 93 });
michael@0 94 },
michael@0 95 testNoButtonOnEnable: function(next) {
michael@0 96 // we expect the addon install dialog to appear, we need to accept the
michael@0 97 // install from the dialog.
michael@0 98 let panel = document.getElementById("servicesInstall-notification");
michael@0 99 PopupNotifications.panel.addEventListener("popupshown", function onpopupshown() {
michael@0 100 PopupNotifications.panel.removeEventListener("popupshown", onpopupshown);
michael@0 101 info("servicesInstall-notification panel opened");
michael@0 102 panel.button.click();
michael@0 103 });
michael@0 104
michael@0 105 let activationURL = manifest3.origin + "/browser/browser/base/content/test/social/social_activate.html"
michael@0 106 addTab(activationURL, function(tab) {
michael@0 107 let doc = tab.linkedBrowser.contentDocument;
michael@0 108 Social.installProvider(doc, manifest3, function(addonManifest) {
michael@0 109 // enable the provider so we know the button would have appeared
michael@0 110 SocialService.addBuiltinProvider(manifest3.origin, function(provider) {
michael@0 111 is(provider.origin, manifest3.origin, "provider is installed");
michael@0 112 let id = SocialMarks._toolbarHelper.idFromOrigin(provider.origin);
michael@0 113 let widget = CustomizableUI.getWidget(id);
michael@0 114 ok(!widget || !widget.forWindow(window).node, "no button added to widget set");
michael@0 115 Social.uninstallProvider(manifest3.origin, function() {
michael@0 116 gBrowser.removeTab(tab);
michael@0 117 next();
michael@0 118 });
michael@0 119 });
michael@0 120 });
michael@0 121 });
michael@0 122 },
michael@0 123
michael@0 124 testButtonOnEnable: function(next) {
michael@0 125 let panel = document.getElementById("servicesInstall-notification");
michael@0 126 PopupNotifications.panel.addEventListener("popupshown", function onpopupshown() {
michael@0 127 PopupNotifications.panel.removeEventListener("popupshown", onpopupshown);
michael@0 128 info("servicesInstall-notification panel opened");
michael@0 129 panel.button.click();
michael@0 130 });
michael@0 131
michael@0 132 // enable the provider now
michael@0 133 let activationURL = manifest2.origin + "/browser/browser/base/content/test/social/social_activate.html"
michael@0 134 addTab(activationURL, function(tab) {
michael@0 135 let doc = tab.linkedBrowser.contentDocument;
michael@0 136 Social.installProvider(doc, manifest2, function(addonManifest) {
michael@0 137 SocialService.addBuiltinProvider(manifest2.origin, function(provider) {
michael@0 138 is(provider.origin, manifest2.origin, "provider is installed");
michael@0 139 let id = SocialMarks._toolbarHelper.idFromOrigin(manifest2.origin);
michael@0 140 let widget = CustomizableUI.getWidget(id).forWindow(window)
michael@0 141 ok(widget.node, "button added to widget set");
michael@0 142
michael@0 143 // bypass widget go directly to dom, check attribute states
michael@0 144 let button = document.getElementById(id);
michael@0 145 is(button.disabled, false, "mark button is disabled");
michael@0 146 // verify the attribute for proper css
michael@0 147 ok(!button.hasAttribute("disabled"), "mark button attribute is disabled");
michael@0 148 // button should be visible
michael@0 149 is(button.hidden, false, "mark button is visible");
michael@0 150
michael@0 151 checkSocialUI(window);
michael@0 152 gBrowser.removeTab(tab);
michael@0 153 next();
michael@0 154 });
michael@0 155 });
michael@0 156 });
michael@0 157 },
michael@0 158
michael@0 159 testMarkPanel: function(next) {
michael@0 160 // click on panel to open and wait for visibility
michael@0 161 let provider = Social._getProviderFromOrigin(manifest2.origin);
michael@0 162 ok(provider.enabled, "provider is enabled");
michael@0 163 let id = SocialMarks._toolbarHelper.idFromOrigin(manifest2.origin);
michael@0 164 let widget = CustomizableUI.getWidget(id);
michael@0 165 let btn = widget.forWindow(window).node;
michael@0 166 ok(btn, "got a mark button");
michael@0 167 let port = provider.getWorkerPort();
michael@0 168 ok(port, "got a port");
michael@0 169
michael@0 170 // verify markbutton is disabled when there is no browser url
michael@0 171 ok(btn.disabled, "button is disabled");
michael@0 172 let activationURL = manifest2.origin + "/browser/browser/base/content/test/social/social_activate.html"
michael@0 173 addTab(activationURL, function(tab) {
michael@0 174 ok(!btn.disabled, "button is enabled");
michael@0 175 port.onmessage = function (e) {
michael@0 176 let topic = e.data.topic;
michael@0 177 switch (topic) {
michael@0 178 case "test-init-done":
michael@0 179 ok(true, "test-init-done received");
michael@0 180 ok(provider.profile.userName, "profile was set by test worker");
michael@0 181 // first click marks the page, second click opens the page. We have to
michael@0 182 // synthesize so the command event happens
michael@0 183 EventUtils.synthesizeMouseAtCenter(btn, {});
michael@0 184 // wait for the button to be marked, click to open panel
michael@0 185 waitForCondition(function() btn.isMarked, function() {
michael@0 186 is(btn.panel.state, "closed", "panel should not be visible yet");
michael@0 187 EventUtils.synthesizeMouseAtCenter(btn, {});
michael@0 188 }, "button is marked");
michael@0 189 break;
michael@0 190 case "got-social-panel-visibility":
michael@0 191 ok(true, "got the panel message " + e.data.result);
michael@0 192 if (e.data.result == "shown") {
michael@0 193 // unmark the page via the button in the page
michael@0 194 let doc = btn.contentDocument;
michael@0 195 let unmarkBtn = doc.getElementById("unmark");
michael@0 196 ok(unmarkBtn, "got the panel unmark button");
michael@0 197 EventUtils.sendMouseEvent({type: "click"}, unmarkBtn, btn.contentWindow);
michael@0 198 } else {
michael@0 199 // page should no longer be marked
michael@0 200 port.close();
michael@0 201 waitForCondition(function() !btn.isMarked, function() {
michael@0 202 // cleanup after the page has been unmarked
michael@0 203 gBrowser.tabContainer.addEventListener("TabClose", function onTabClose() {
michael@0 204 gBrowser.tabContainer.removeEventListener("TabClose", onTabClose);
michael@0 205 executeSoon(function () {
michael@0 206 ok(btn.disabled, "button is disabled");
michael@0 207 next();
michael@0 208 });
michael@0 209 });
michael@0 210 gBrowser.removeTab(tab);
michael@0 211 }, "button unmarked");
michael@0 212 }
michael@0 213 break;
michael@0 214 }
michael@0 215 };
michael@0 216 port.postMessage({topic: "test-init"});
michael@0 217 });
michael@0 218 },
michael@0 219
michael@0 220 testMarkPanelLoggedOut: function(next) {
michael@0 221 // click on panel to open and wait for visibility
michael@0 222 let provider = Social._getProviderFromOrigin(manifest2.origin);
michael@0 223 ok(provider.enabled, "provider is enabled");
michael@0 224 let id = SocialMarks._toolbarHelper.idFromOrigin(manifest2.origin);
michael@0 225 let widget = CustomizableUI.getWidget(id);
michael@0 226 let btn = widget.forWindow(window).node;
michael@0 227 ok(btn, "got a mark button");
michael@0 228 let port = provider.getWorkerPort();
michael@0 229 ok(port, "got a port");
michael@0 230
michael@0 231 // verify markbutton is disabled when there is no browser url
michael@0 232 ok(btn.disabled, "button is disabled");
michael@0 233 let activationURL = manifest2.origin + "/browser/browser/base/content/test/social/social_activate.html"
michael@0 234 addTab(activationURL, function(tab) {
michael@0 235 ok(!btn.disabled, "button is enabled");
michael@0 236 port.onmessage = function (e) {
michael@0 237 let topic = e.data.topic;
michael@0 238 switch (topic) {
michael@0 239 case "test-init-done":
michael@0 240 ok(true, "test-init-done received");
michael@0 241 ok(provider.profile.userName, "profile was set by test worker");
michael@0 242 port.postMessage({topic: "test-logout"});
michael@0 243 waitForCondition(function() !provider.profile.userName,
michael@0 244 function() {
michael@0 245 // when the provider has not indicated to us that a user is
michael@0 246 // logged in, the first click opens the page.
michael@0 247 EventUtils.synthesizeMouseAtCenter(btn, {});
michael@0 248 },
michael@0 249 "profile was unset by test worker");
michael@0 250 break;
michael@0 251 case "got-social-panel-visibility":
michael@0 252 ok(true, "got the panel message " + e.data.result);
michael@0 253 if (e.data.result == "shown") {
michael@0 254 // our test marks the page during the load event (see
michael@0 255 // social_mark.html) regardless of login state, unmark the page
michael@0 256 // via the button in the page
michael@0 257 let doc = btn.contentDocument;
michael@0 258 let unmarkBtn = doc.getElementById("unmark");
michael@0 259 ok(unmarkBtn, "got the panel unmark button");
michael@0 260 EventUtils.sendMouseEvent({type: "click"}, unmarkBtn, btn.contentWindow);
michael@0 261 } else {
michael@0 262 // page should no longer be marked
michael@0 263 port.close();
michael@0 264 waitForCondition(function() !btn.isMarked, function() {
michael@0 265 // cleanup after the page has been unmarked
michael@0 266 gBrowser.tabContainer.addEventListener("TabClose", function onTabClose() {
michael@0 267 gBrowser.tabContainer.removeEventListener("TabClose", onTabClose);
michael@0 268 executeSoon(function () {
michael@0 269 ok(btn.disabled, "button is disabled");
michael@0 270 next();
michael@0 271 });
michael@0 272 });
michael@0 273 gBrowser.removeTab(tab);
michael@0 274 }, "button unmarked");
michael@0 275 }
michael@0 276 break;
michael@0 277 }
michael@0 278 };
michael@0 279 port.postMessage({topic: "test-init"});
michael@0 280 });
michael@0 281 },
michael@0 282
michael@0 283 testButtonOnDisable: function(next) {
michael@0 284 // enable the provider now
michael@0 285 let provider = Social._getProviderFromOrigin(manifest2.origin);
michael@0 286 ok(provider, "provider is installed");
michael@0 287 SocialService.removeProvider(manifest2.origin, function() {
michael@0 288 let id = SocialMarks._toolbarHelper.idFromOrigin(manifest2.origin);
michael@0 289 waitForCondition(function() {
michael@0 290 // getWidget now returns null since we've destroyed the widget
michael@0 291 return !CustomizableUI.getWidget(id)
michael@0 292 },
michael@0 293 function() {
michael@0 294 checkSocialUI(window);
michael@0 295 Social.uninstallProvider(manifest2.origin, next);
michael@0 296 }, "button does not exist after disabling the provider");
michael@0 297 });
michael@0 298 },
michael@0 299
michael@0 300 testContextSubmenu: function(next) {
michael@0 301 // install 4 providers to test that the menu's are added as submenus
michael@0 302 let manifests = [
michael@0 303 makeMarkProvider("sub1.test1"),
michael@0 304 makeMarkProvider("sub2.test1"),
michael@0 305 makeMarkProvider("sub1.test2"),
michael@0 306 makeMarkProvider("sub2.test2")
michael@0 307 ];
michael@0 308 let installed = [];
michael@0 309 let markLinkMenu = document.getElementById("context-marklinkMenu").firstChild;
michael@0 310 let markPageMenu = document.getElementById("context-markpageMenu").firstChild;
michael@0 311
michael@0 312 function addProviders(callback) {
michael@0 313 let manifest = manifests.pop();
michael@0 314 if (!manifest) {
michael@0 315 info("INSTALLATION FINISHED");
michael@0 316 executeSoon(callback);
michael@0 317 return;
michael@0 318 }
michael@0 319 info("INSTALLING " + manifest.origin);
michael@0 320 let panel = document.getElementById("servicesInstall-notification");
michael@0 321 PopupNotifications.panel.addEventListener("popupshown", function onpopupshown() {
michael@0 322 PopupNotifications.panel.removeEventListener("popupshown", onpopupshown);
michael@0 323 info("servicesInstall-notification panel opened");
michael@0 324 panel.button.click();
michael@0 325 })
michael@0 326
michael@0 327 let activationURL = manifest.origin + "/browser/browser/base/content/test/social/social_activate.html"
michael@0 328 let id = SocialMarks._toolbarHelper.idFromOrigin(manifest.origin);
michael@0 329 let toolbar = document.getElementById("nav-bar");
michael@0 330 addTab(activationURL, function(tab) {
michael@0 331 let doc = tab.linkedBrowser.contentDocument;
michael@0 332 Social.installProvider(doc, manifest, function(addonManifest) {
michael@0 333 // enable the provider so we know the button would have appeared
michael@0 334 SocialService.addBuiltinProvider(manifest.origin, function(provider) {
michael@0 335 waitForCondition(function() { return CustomizableUI.getWidget(id) },
michael@0 336 function() {
michael@0 337 gBrowser.removeTab(tab);
michael@0 338 installed.push(manifest.origin);
michael@0 339 // checkSocialUI will properly check where the menus are located
michael@0 340 checkSocialUI(window);
michael@0 341 executeSoon(function() {
michael@0 342 addProviders(callback);
michael@0 343 });
michael@0 344 }, "button exists after enabling social");
michael@0 345 });
michael@0 346 });
michael@0 347 });
michael@0 348 }
michael@0 349
michael@0 350 function removeProviders(callback) {
michael@0 351 let origin = installed.pop();
michael@0 352 if (!origin) {
michael@0 353 executeSoon(callback);
michael@0 354 return;
michael@0 355 }
michael@0 356 Social.uninstallProvider(origin, function(provider) {
michael@0 357 executeSoon(function() {
michael@0 358 removeProviders(callback);
michael@0 359 });
michael@0 360 });
michael@0 361 }
michael@0 362
michael@0 363 addProviders(function() {
michael@0 364 removeProviders(function() {
michael@0 365 is(SocialMarks.getProviders().length, 0, "mark providers removed");
michael@0 366 is(markLinkMenu.childNodes.length, 0, "marklink menu ok");
michael@0 367 is(markPageMenu.childNodes.length, 0, "markpage menu ok");
michael@0 368 checkSocialUI(window);
michael@0 369 next();
michael@0 370 });
michael@0 371 });
michael@0 372 }
michael@0 373 }

mercurial