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