|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 const {classes: Cc, interfaces: Ci, utils: Cu} = Components; |
|
5 |
|
6 Cu.import("resource://gre/modules/InterAppCommService.jsm"); |
|
7 |
|
8 let UUIDGenerator = Cc["@mozilla.org/uuid-generator;1"] |
|
9 .getService(Ci.nsIUUIDGenerator); |
|
10 |
|
11 const MESSAGE_PORT_ID = UUIDGenerator.generateUUID().toString(); |
|
12 const FAKE_MESSAGE_PORT_ID = UUIDGenerator.generateUUID().toString(); |
|
13 const OUTER_WINDOW_ID = UUIDGenerator.generateUUID().toString(); |
|
14 const REQUEST_ID = UUIDGenerator.generateUUID().toString(); |
|
15 |
|
16 const PUB_APP_MANIFEST_URL = "app://pubApp.gaiamobile.org/manifest.webapp"; |
|
17 const SUB_APP_MANIFEST_URL = "app://subApp.gaiamobile.org/manifest.webapp"; |
|
18 |
|
19 const PUB_APP_PAGE_URL = "app://pubApp.gaiamobile.org/handler.html"; |
|
20 const SUB_APP_PAGE_URL = "app://subApp.gaiamobile.org/handler.html"; |
|
21 |
|
22 const KEYWORD = "test"; |
|
23 |
|
24 function create_message_port_pair(aMessagePortId, |
|
25 aKeyword, |
|
26 aPubManifestURL, |
|
27 aSubManifestURL) { |
|
28 InterAppCommService._messagePortPairs[aMessagePortId] = { |
|
29 keyword: aKeyword, |
|
30 publisher: { |
|
31 manifestURL: aPubManifestURL |
|
32 }, |
|
33 subscriber: { |
|
34 manifestURL: aSubManifestURL |
|
35 } |
|
36 }; |
|
37 } |
|
38 |
|
39 function clear_message_port_pairs() { |
|
40 InterAppCommService._messagePortPairs = {}; |
|
41 } |
|
42 |
|
43 function register_message_port(aMessagePortId, |
|
44 aManifestURL, |
|
45 aPageURL, |
|
46 aTargetSendAsyncMessage) { |
|
47 let message = { |
|
48 name: "InterAppMessagePort:Register", |
|
49 json: { |
|
50 messagePortID: aMessagePortId, |
|
51 manifestURL: aManifestURL, |
|
52 pageURL: aPageURL |
|
53 }, |
|
54 target: { |
|
55 sendAsyncMessage: function(aName, aData) { |
|
56 if (aTargetSendAsyncMessage) { |
|
57 aTargetSendAsyncMessage(aName, aData); |
|
58 } |
|
59 }, |
|
60 assertContainApp: function(_manifestURL) { |
|
61 return (aManifestURL == _manifestURL); |
|
62 } |
|
63 } |
|
64 }; |
|
65 |
|
66 InterAppCommService.receiveMessage(message); |
|
67 |
|
68 return message.target; |
|
69 } |
|
70 |
|
71 function register_message_ports(aMessagePortId, |
|
72 aPubTargetSendAsyncMessage, |
|
73 aSubTargetSendAsyncMessage) { |
|
74 let pubTarget = register_message_port(aMessagePortId, |
|
75 PUB_APP_MANIFEST_URL, |
|
76 PUB_APP_PAGE_URL, |
|
77 aPubTargetSendAsyncMessage); |
|
78 |
|
79 let subTarget = register_message_port(aMessagePortId, |
|
80 SUB_APP_MANIFEST_URL, |
|
81 SUB_APP_PAGE_URL, |
|
82 aSubTargetSendAsyncMessage); |
|
83 |
|
84 return { pubTarget: pubTarget, subTarget: subTarget }; |
|
85 } |
|
86 |
|
87 function unregister_message_port(aMessagePortId, |
|
88 aManifestURL) { |
|
89 let message = { |
|
90 name: "InterAppMessagePort:Unregister", |
|
91 json: { |
|
92 messagePortID: aMessagePortId, |
|
93 manifestURL: aManifestURL |
|
94 }, |
|
95 target: { |
|
96 assertContainApp: function(_manifestURL) { |
|
97 return (aManifestURL == _manifestURL); |
|
98 } |
|
99 } |
|
100 }; |
|
101 |
|
102 InterAppCommService.receiveMessage(message); |
|
103 } |
|
104 |
|
105 function remove_target(aTarget) { |
|
106 let message = { |
|
107 name: "child-process-shutdown", |
|
108 target: aTarget |
|
109 }; |
|
110 |
|
111 InterAppCommService.receiveMessage(message); |
|
112 } |
|
113 |
|
114 function post_message(aMessagePortId, |
|
115 aManifestURL, |
|
116 aMessage) { |
|
117 let message = { |
|
118 name: "InterAppMessagePort:PostMessage", |
|
119 json: { |
|
120 messagePortID: aMessagePortId, |
|
121 manifestURL: aManifestURL, |
|
122 message: aMessage |
|
123 }, |
|
124 target: { |
|
125 assertContainApp: function(_manifestURL) { |
|
126 return (aManifestURL == _manifestURL); |
|
127 } |
|
128 } |
|
129 }; |
|
130 |
|
131 InterAppCommService.receiveMessage(message); |
|
132 } |
|
133 |
|
134 function create_allowed_connections(aKeyword, |
|
135 aPubManifestURL, |
|
136 aSubManifestURL) { |
|
137 let allowedPubAppManifestURLs = |
|
138 InterAppCommService._allowedConnections[aKeyword] = {}; |
|
139 |
|
140 allowedPubAppManifestURLs[aPubManifestURL] = [aSubManifestURL]; |
|
141 } |
|
142 |
|
143 function clear_allowed_connections() { |
|
144 InterAppCommService._allowedConnections = {}; |
|
145 } |
|
146 |
|
147 function get_connections(aManifestURL, |
|
148 aOuterWindowID, |
|
149 aRequestID, |
|
150 aTargetSendAsyncMessage) { |
|
151 let message = { |
|
152 name: "Webapps:GetConnections", |
|
153 json: { |
|
154 manifestURL: aManifestURL, |
|
155 outerWindowID: aOuterWindowID, |
|
156 requestID: aRequestID |
|
157 }, |
|
158 target: { |
|
159 sendAsyncMessage: function(aName, aData) { |
|
160 if (aTargetSendAsyncMessage) { |
|
161 aTargetSendAsyncMessage(aName, aData); |
|
162 } |
|
163 }, |
|
164 assertContainApp: function(_manifestURL) { |
|
165 return (aManifestURL == _manifestURL); |
|
166 } |
|
167 } |
|
168 }; |
|
169 |
|
170 InterAppCommService.receiveMessage(message); |
|
171 } |
|
172 |
|
173 function cancel_connections(aManifestURL, |
|
174 aKeyword, |
|
175 aPubManifestURL, |
|
176 aSubManifestURL) { |
|
177 let message = { |
|
178 name: "InterAppConnection:Cancel", |
|
179 json: { |
|
180 manifestURL: aManifestURL, |
|
181 keyword: aKeyword, |
|
182 pubAppManifestURL: aPubManifestURL, |
|
183 subAppManifestURL: aSubManifestURL |
|
184 }, |
|
185 target: { |
|
186 assertContainApp: function(_manifestURL) { |
|
187 return (aManifestURL == _manifestURL); |
|
188 } |
|
189 } |
|
190 }; |
|
191 |
|
192 InterAppCommService.receiveMessage(message); |
|
193 } |
|
194 |
|
195 add_test(function test_registerMessagePort() { |
|
196 create_message_port_pair(MESSAGE_PORT_ID, |
|
197 KEYWORD, |
|
198 PUB_APP_MANIFEST_URL, |
|
199 SUB_APP_MANIFEST_URL); |
|
200 |
|
201 let targets = register_message_ports(MESSAGE_PORT_ID); |
|
202 |
|
203 let messagePortPair = InterAppCommService._messagePortPairs[MESSAGE_PORT_ID]; |
|
204 |
|
205 do_check_eq(PUB_APP_PAGE_URL, messagePortPair.publisher.pageURL); |
|
206 do_check_eq(SUB_APP_PAGE_URL, messagePortPair.subscriber.pageURL); |
|
207 |
|
208 do_check_true(targets.pubTarget === messagePortPair.publisher.target); |
|
209 do_check_true(targets.subTarget === messagePortPair.subscriber.target); |
|
210 |
|
211 clear_message_port_pairs(); |
|
212 run_next_test(); |
|
213 }); |
|
214 |
|
215 add_test(function test_failToRegisterMessagePort() { |
|
216 create_message_port_pair(MESSAGE_PORT_ID, |
|
217 KEYWORD, |
|
218 PUB_APP_MANIFEST_URL, |
|
219 SUB_APP_MANIFEST_URL); |
|
220 |
|
221 let targets = register_message_ports(FAKE_MESSAGE_PORT_ID); |
|
222 |
|
223 let messagePortPair = InterAppCommService._messagePortPairs[MESSAGE_PORT_ID]; |
|
224 |
|
225 // Because it failed to register, the page URLs and targets don't exist. |
|
226 do_check_true(messagePortPair.publisher.pageURL === undefined); |
|
227 do_check_true(messagePortPair.subscriber.pageURL === undefined); |
|
228 |
|
229 do_check_true(messagePortPair.publisher.target === undefined); |
|
230 do_check_true(messagePortPair.subscriber.target === undefined); |
|
231 |
|
232 clear_message_port_pairs(); |
|
233 run_next_test(); |
|
234 }); |
|
235 |
|
236 add_test(function test_unregisterMessagePort() { |
|
237 create_message_port_pair(MESSAGE_PORT_ID, |
|
238 KEYWORD, |
|
239 PUB_APP_MANIFEST_URL, |
|
240 SUB_APP_MANIFEST_URL); |
|
241 |
|
242 register_message_ports(MESSAGE_PORT_ID); |
|
243 |
|
244 unregister_message_port(MESSAGE_PORT_ID, PUB_APP_MANIFEST_URL); |
|
245 |
|
246 do_check_true(InterAppCommService._messagePortPairs[MESSAGE_PORT_ID] |
|
247 === undefined); |
|
248 |
|
249 clear_message_port_pairs(); |
|
250 run_next_test(); |
|
251 }); |
|
252 |
|
253 add_test(function test_failToUnregisterMessagePort() { |
|
254 create_message_port_pair(MESSAGE_PORT_ID, |
|
255 KEYWORD, |
|
256 PUB_APP_MANIFEST_URL, |
|
257 SUB_APP_MANIFEST_URL); |
|
258 |
|
259 register_message_ports(MESSAGE_PORT_ID); |
|
260 |
|
261 unregister_message_port(FAKE_MESSAGE_PORT_ID, PUB_APP_MANIFEST_URL); |
|
262 |
|
263 // Because it failed to unregister, the entry still exists. |
|
264 do_check_true(InterAppCommService._messagePortPairs[MESSAGE_PORT_ID] |
|
265 !== undefined); |
|
266 |
|
267 clear_message_port_pairs(); |
|
268 run_next_test(); |
|
269 }); |
|
270 |
|
271 add_test(function test_removeTarget() { |
|
272 create_message_port_pair(MESSAGE_PORT_ID, |
|
273 KEYWORD, |
|
274 PUB_APP_MANIFEST_URL, |
|
275 SUB_APP_MANIFEST_URL); |
|
276 |
|
277 let targets = register_message_ports(MESSAGE_PORT_ID); |
|
278 |
|
279 remove_target(targets.pubTarget); |
|
280 |
|
281 do_check_true(InterAppCommService._messagePortPairs[MESSAGE_PORT_ID] |
|
282 === undefined); |
|
283 |
|
284 clear_message_port_pairs(); |
|
285 run_next_test(); |
|
286 }); |
|
287 |
|
288 add_test(function test_postMessage() { |
|
289 create_message_port_pair(MESSAGE_PORT_ID, |
|
290 KEYWORD, |
|
291 PUB_APP_MANIFEST_URL, |
|
292 SUB_APP_MANIFEST_URL); |
|
293 |
|
294 let countPubAppOnMessage = 0; |
|
295 function pubAppOnMessage(aName, aData) { |
|
296 countPubAppOnMessage++; |
|
297 |
|
298 do_check_eq(aName, "InterAppMessagePort:OnMessage"); |
|
299 do_check_eq(aData.manifestURL, PUB_APP_MANIFEST_URL); |
|
300 do_check_eq(aData.pageURL, PUB_APP_PAGE_URL); |
|
301 do_check_eq(aData.messagePortID, MESSAGE_PORT_ID); |
|
302 |
|
303 if (countPubAppOnMessage == 1) { |
|
304 do_check_eq(aData.message.text, "sub app says world"); |
|
305 |
|
306 post_message(MESSAGE_PORT_ID, |
|
307 PUB_APP_MANIFEST_URL, |
|
308 { text: "pub app says hello again" }); |
|
309 |
|
310 } else if (countPubAppOnMessage == 2) { |
|
311 do_check_eq(aData.message.text, "sub app says world again"); |
|
312 |
|
313 clear_message_port_pairs(); |
|
314 run_next_test(); |
|
315 } else { |
|
316 do_throw("pub app receives an unexpected message") |
|
317 } |
|
318 }; |
|
319 |
|
320 let countSubAppOnMessage = 0; |
|
321 function subAppOnMessage(aName, aData) { |
|
322 countSubAppOnMessage++; |
|
323 |
|
324 do_check_eq(aName, "InterAppMessagePort:OnMessage"); |
|
325 do_check_eq(aData.manifestURL, SUB_APP_MANIFEST_URL); |
|
326 do_check_eq(aData.pageURL, SUB_APP_PAGE_URL); |
|
327 do_check_eq(aData.messagePortID, MESSAGE_PORT_ID); |
|
328 |
|
329 if (countSubAppOnMessage == 1) { |
|
330 do_check_eq(aData.message.text, "pub app says hello"); |
|
331 |
|
332 post_message(MESSAGE_PORT_ID, |
|
333 SUB_APP_MANIFEST_URL, |
|
334 { text: "sub app says world" }); |
|
335 |
|
336 } else if (countSubAppOnMessage == 2) { |
|
337 do_check_eq(aData.message.text, "pub app says hello again"); |
|
338 |
|
339 post_message(MESSAGE_PORT_ID, |
|
340 SUB_APP_MANIFEST_URL, |
|
341 { text: "sub app says world again" }); |
|
342 } else { |
|
343 do_throw("sub app receives an unexpected message") |
|
344 } |
|
345 }; |
|
346 |
|
347 register_message_ports(MESSAGE_PORT_ID, pubAppOnMessage, subAppOnMessage); |
|
348 |
|
349 post_message(MESSAGE_PORT_ID, |
|
350 PUB_APP_MANIFEST_URL, |
|
351 { text: "pub app says hello" }); |
|
352 }); |
|
353 |
|
354 add_test(function test_registerMessagePort_with_queued_messages() { |
|
355 create_message_port_pair(MESSAGE_PORT_ID, |
|
356 KEYWORD, |
|
357 PUB_APP_MANIFEST_URL, |
|
358 SUB_APP_MANIFEST_URL); |
|
359 |
|
360 register_message_port(MESSAGE_PORT_ID, |
|
361 PUB_APP_MANIFEST_URL, |
|
362 PUB_APP_PAGE_URL); |
|
363 |
|
364 post_message(MESSAGE_PORT_ID, |
|
365 PUB_APP_MANIFEST_URL, |
|
366 { text: "pub app says hello" }); |
|
367 |
|
368 post_message(MESSAGE_PORT_ID, |
|
369 PUB_APP_MANIFEST_URL, |
|
370 { text: "pub app says hello again" }); |
|
371 |
|
372 let countSubAppOnMessage = 0; |
|
373 function subAppOnMessage(aName, aData) { |
|
374 countSubAppOnMessage++; |
|
375 |
|
376 do_check_eq(aName, "InterAppMessagePort:OnMessage"); |
|
377 do_check_eq(aData.manifestURL, SUB_APP_MANIFEST_URL); |
|
378 do_check_eq(aData.pageURL, SUB_APP_PAGE_URL); |
|
379 do_check_eq(aData.messagePortID, MESSAGE_PORT_ID); |
|
380 |
|
381 if (countSubAppOnMessage == 1) { |
|
382 do_check_eq(aData.message.text, "pub app says hello"); |
|
383 } else if (countSubAppOnMessage == 2) { |
|
384 do_check_eq(aData.message.text, "pub app says hello again"); |
|
385 |
|
386 clear_message_port_pairs(); |
|
387 run_next_test(); |
|
388 } else { |
|
389 do_throw("sub app receives an unexpected message") |
|
390 } |
|
391 }; |
|
392 |
|
393 register_message_port(MESSAGE_PORT_ID, |
|
394 SUB_APP_MANIFEST_URL, |
|
395 SUB_APP_PAGE_URL, |
|
396 subAppOnMessage); |
|
397 }); |
|
398 |
|
399 add_test(function test_getConnections() { |
|
400 create_allowed_connections(KEYWORD, |
|
401 PUB_APP_MANIFEST_URL, |
|
402 SUB_APP_MANIFEST_URL); |
|
403 |
|
404 function onGetConnections(aName, aData) { |
|
405 do_check_eq(aName, "Webapps:GetConnections:Return:OK"); |
|
406 do_check_eq(aData.oid, OUTER_WINDOW_ID); |
|
407 do_check_eq(aData.requestID, REQUEST_ID); |
|
408 |
|
409 let connections = aData.connections; |
|
410 do_check_eq(connections.length, 1); |
|
411 do_check_eq(connections[0].keyword, KEYWORD); |
|
412 do_check_eq(connections[0].pubAppManifestURL, PUB_APP_MANIFEST_URL); |
|
413 do_check_eq(connections[0].subAppManifestURL, SUB_APP_MANIFEST_URL); |
|
414 |
|
415 clear_allowed_connections(); |
|
416 run_next_test(); |
|
417 }; |
|
418 |
|
419 get_connections(PUB_APP_MANIFEST_URL, |
|
420 OUTER_WINDOW_ID, |
|
421 REQUEST_ID, |
|
422 onGetConnections); |
|
423 }); |
|
424 |
|
425 add_test(function test_cancelConnection() { |
|
426 create_allowed_connections(KEYWORD, |
|
427 PUB_APP_MANIFEST_URL, |
|
428 SUB_APP_MANIFEST_URL); |
|
429 |
|
430 create_message_port_pair(MESSAGE_PORT_ID, |
|
431 KEYWORD, |
|
432 PUB_APP_MANIFEST_URL, |
|
433 SUB_APP_MANIFEST_URL); |
|
434 |
|
435 register_message_ports(MESSAGE_PORT_ID); |
|
436 |
|
437 cancel_connections(PUB_APP_MANIFEST_URL, |
|
438 KEYWORD, |
|
439 PUB_APP_MANIFEST_URL, |
|
440 SUB_APP_MANIFEST_URL); |
|
441 |
|
442 do_check_true(InterAppCommService._allowedConnections[KEYWORD] |
|
443 === undefined); |
|
444 |
|
445 do_check_true(InterAppCommService._messagePortPairs[MESSAGE_PORT_ID] |
|
446 === undefined); |
|
447 |
|
448 clear_allowed_connections(); |
|
449 clear_message_port_pairs(); |
|
450 run_next_test(); |
|
451 }); |
|
452 |
|
453 function run_test() { |
|
454 do_get_profile(); |
|
455 |
|
456 run_next_test(); |
|
457 } |