toolkit/components/social/test/browser/browser_workerAPI.js

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:46d5421ed4d7
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/. */
4
5 let provider;
6
7 function test() {
8 waitForExplicitFinish();
9
10 replaceAlertsService();
11 registerCleanupFunction(restoreAlertsService);
12
13 let manifest = {
14 origin: 'http://example.com',
15 name: "Example Provider",
16 workerURL: "http://example.com/browser/toolkit/components/social/test/browser/worker_social.js"
17 };
18
19 SocialService.addProvider(manifest, function (p) {
20 // toolkit does not enable providers by default, that is handled in the
21 // browser, we need to enable it in our tests.
22 p.enabled = true;
23 provider = p;
24 runTests(tests, undefined, undefined, function () {
25 SocialService.removeProvider(provider.origin, finish);
26 });
27 });
28 }
29
30 let tests = {
31 testProfile: function(next) {
32 let expect = {
33 portrait: "https://example.com/portrait.jpg",
34 userName: "trickster",
35 displayName: "Kuma Lisa",
36 profileURL: "http://en.wikipedia.org/wiki/Kuma_Lisa"
37 }
38 function ob(aSubject, aTopic, aData) {
39 Services.obs.removeObserver(ob, "social:profile-changed");
40 is(aData, provider.origin, "update of profile from our provider");
41 let profile = provider.profile;
42 is(profile.portrait, expect.portrait, "portrait is set");
43 is(profile.userName, expect.userName, "userName is set");
44 is(profile.displayName, expect.displayName, "displayName is set");
45 is(profile.profileURL, expect.profileURL, "profileURL is set");
46 next();
47 }
48 Services.obs.addObserver(ob, "social:profile-changed", false);
49 let port = provider.getWorkerPort();
50 port.postMessage({topic: "test-profile", data: expect});
51 port.close();
52 },
53
54 testAmbientNotification: function(next) {
55 let expect = {
56 name: "test-ambient"
57 }
58 function ob(aSubject, aTopic, aData) {
59 Services.obs.removeObserver(ob, "social:ambient-notification-changed");
60 is(aData, provider.origin, "update is from our provider");
61 let notif = provider.ambientNotificationIcons[expect.name];
62 is(notif.name, expect.name, "ambientNotification reflected");
63
64 next();
65 }
66 Services.obs.addObserver(ob, "social:ambient-notification-changed", false);
67 let port = provider.getWorkerPort();
68 port.postMessage({topic: "test-ambient", data: expect});
69 port.close();
70 },
71
72 testProfileCleared: function(next) {
73 let sent = {
74 userName: ""
75 };
76 function ob(aSubject, aTopic, aData) {
77 Services.obs.removeObserver(ob, "social:profile-changed");
78 is(aData, provider.origin, "update of profile from our provider");
79 is(Object.keys(provider.profile).length, 0, "profile was cleared by empty username");
80 is(Object.keys(provider.ambientNotificationIcons).length, 0, "icons were cleared by empty username");
81
82 next();
83 }
84 Services.obs.addObserver(ob, "social:profile-changed", false);
85 provider.workerAPI._port.postMessage({topic: "test-profile", data: sent});
86 },
87
88 testNoCookies: function(next) {
89 // use a big, blunt stick to remove cookies.
90 Services.cookies.removeAll();
91 let port = provider.getWorkerPort();
92 port.onmessage = function onMessage(event) {
93 let {topic, data} = event.data;
94 if (topic == "test.cookies-get-response") {
95 is(data.length, 0, "got no cookies");
96 port.close();
97 next();
98 }
99 }
100 port.postMessage({topic: "test-initialization"});
101 port.postMessage({topic: "test.cookies-get"});
102 },
103
104 testCookies: function(next) {
105 let port = provider.getWorkerPort();
106 port.onmessage = function onMessage(event) {
107 let {topic, data} = event.data;
108 if (topic == "test.cookies-get-response") {
109 is(data.length, 2, "got 2 cookies");
110 is(data[0].name, "cheez", "cookie has the correct name");
111 is(data[0].value, "burger", "cookie has the correct value");
112 is(data[1].name, "moar", "cookie has the correct name");
113 is(data[1].value, "bacon", "cookie has the correct value");
114 Services.cookies.remove('.example.com', '/', 'cheez', false);
115 Services.cookies.remove('.example.com', '/', 'moar', false);
116 port.close();
117 next();
118 }
119 }
120 var MAX_EXPIRY = Math.pow(2, 62);
121 Services.cookies.add('.example.com', '/', 'cheez', 'burger', false, false, true, MAX_EXPIRY);
122 Services.cookies.add('.example.com', '/', 'moar', 'bacon', false, false, true, MAX_EXPIRY);
123 port.postMessage({topic: "test-initialization"});
124 port.postMessage({topic: "test.cookies-get"});
125 },
126
127 testWorkerReload: function(next) {
128 let fw = {};
129 Cu.import("resource://gre/modules/FrameWorker.jsm", fw);
130
131 let worker = fw.getFrameWorkerHandle(provider.workerURL, undefined, "testWorkerReload");
132 let port = provider.getWorkerPort();
133 // this observer will be fired when the worker reloads - it ensures the
134 // old port was closed and the new worker is functioning.
135 Services.obs.addObserver(function reloadObserver() {
136 Services.obs.removeObserver(reloadObserver, "social:provider-reload");
137 ok(port._closed, "old port was closed by the reload");
138 let newWorker = fw.getFrameWorkerHandle(provider.workerURL, undefined, "testWorkerReload - worker2");
139 let newPort = provider.getWorkerPort();
140 newPort.onmessage = function (e) {
141 let topic = e.data.topic;
142 switch (topic) {
143 case "test-initialization-complete":
144 // and we are done.
145 newPort.close();
146 next();
147 break;
148 }
149 }
150 newPort.postMessage({topic: "test-initialization"});
151 }, "social:provider-reload", false);
152
153 port.onmessage = function (e) {
154 let topic = e.data.topic;
155 switch (topic) {
156 case "test-initialization-complete":
157 // tell the worker to send the reload msg - that will trigger the
158 // frameworker to unload and for our content script's unload
159 // handler to post a "test-result" message.
160 port.postMessage({topic: "test-reload-init"});
161 // and the "social:provider-reload" observer should fire...
162 break;
163 }
164 }
165 port.postMessage({topic: "test-initialization"});
166 },
167
168 testNotificationLinks: function(next) {
169 let port = provider.getWorkerPort();
170 let data = {
171 id: 'an id',
172 body: 'the text',
173 action: 'link',
174 actionArgs: {} // will get a toURL elt during the tests...
175 }
176 let testArgs = [
177 // toURL, expectedLocation, expectedWhere]
178 ["http://example.com", "http://example.com/", "tab"],
179 // bug 815970 - test that a mis-matched scheme gets patched up.
180 ["https://example.com", "http://example.com/", "tab"],
181 // check an off-origin link is not opened.
182 ["https://mochitest:8888/", null, null]
183 ];
184
185 // we monkey-patch openUILinkIn
186 let oldopenUILinkIn = window.openUILinkIn;
187 registerCleanupFunction(function () {
188 // restore the monkey-patch
189 window.openUILinkIn = oldopenUILinkIn;
190 });
191 let openLocation;
192 let openWhere;
193 window.openUILinkIn = function(location, where) {
194 openLocation = location;
195 openWhere = where;
196 }
197
198 // the testing framework.
199 let toURL, expectedLocation, expectedWhere;
200 function nextTest() {
201 if (testArgs.length == 0) {
202 port.close();
203 next(); // all out of tests!
204 return;
205 }
206 openLocation = openWhere = null;
207 [toURL, expectedLocation, expectedWhere] = testArgs.shift();
208 data.actionArgs.toURL = toURL;
209 port.postMessage({topic: 'test-notification-create', data: data});
210 };
211
212 port.onmessage = function(evt) {
213 if (evt.data.topic == "did-notification-create") {
214 is(openLocation, expectedLocation, "url actually opened was " + openLocation);
215 is(openWhere, expectedWhere, "the url was opened in a " + expectedWhere);
216 nextTest();
217 }
218 }
219 // and kick off the tests.
220 port.postMessage({topic: "test-initialization"});
221 nextTest();
222 },
223
224 };

mercurial