1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/apps/tests/unit/test_inter_app_comm_service.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,457 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +const {classes: Cc, interfaces: Ci, utils: Cu} = Components; 1.8 + 1.9 +Cu.import("resource://gre/modules/InterAppCommService.jsm"); 1.10 + 1.11 +let UUIDGenerator = Cc["@mozilla.org/uuid-generator;1"] 1.12 + .getService(Ci.nsIUUIDGenerator); 1.13 + 1.14 +const MESSAGE_PORT_ID = UUIDGenerator.generateUUID().toString(); 1.15 +const FAKE_MESSAGE_PORT_ID = UUIDGenerator.generateUUID().toString(); 1.16 +const OUTER_WINDOW_ID = UUIDGenerator.generateUUID().toString(); 1.17 +const REQUEST_ID = UUIDGenerator.generateUUID().toString(); 1.18 + 1.19 +const PUB_APP_MANIFEST_URL = "app://pubApp.gaiamobile.org/manifest.webapp"; 1.20 +const SUB_APP_MANIFEST_URL = "app://subApp.gaiamobile.org/manifest.webapp"; 1.21 + 1.22 +const PUB_APP_PAGE_URL = "app://pubApp.gaiamobile.org/handler.html"; 1.23 +const SUB_APP_PAGE_URL = "app://subApp.gaiamobile.org/handler.html"; 1.24 + 1.25 +const KEYWORD = "test"; 1.26 + 1.27 +function create_message_port_pair(aMessagePortId, 1.28 + aKeyword, 1.29 + aPubManifestURL, 1.30 + aSubManifestURL) { 1.31 + InterAppCommService._messagePortPairs[aMessagePortId] = { 1.32 + keyword: aKeyword, 1.33 + publisher: { 1.34 + manifestURL: aPubManifestURL 1.35 + }, 1.36 + subscriber: { 1.37 + manifestURL: aSubManifestURL 1.38 + } 1.39 + }; 1.40 +} 1.41 + 1.42 +function clear_message_port_pairs() { 1.43 + InterAppCommService._messagePortPairs = {}; 1.44 +} 1.45 + 1.46 +function register_message_port(aMessagePortId, 1.47 + aManifestURL, 1.48 + aPageURL, 1.49 + aTargetSendAsyncMessage) { 1.50 + let message = { 1.51 + name: "InterAppMessagePort:Register", 1.52 + json: { 1.53 + messagePortID: aMessagePortId, 1.54 + manifestURL: aManifestURL, 1.55 + pageURL: aPageURL 1.56 + }, 1.57 + target: { 1.58 + sendAsyncMessage: function(aName, aData) { 1.59 + if (aTargetSendAsyncMessage) { 1.60 + aTargetSendAsyncMessage(aName, aData); 1.61 + } 1.62 + }, 1.63 + assertContainApp: function(_manifestURL) { 1.64 + return (aManifestURL == _manifestURL); 1.65 + } 1.66 + } 1.67 + }; 1.68 + 1.69 + InterAppCommService.receiveMessage(message); 1.70 + 1.71 + return message.target; 1.72 +} 1.73 + 1.74 +function register_message_ports(aMessagePortId, 1.75 + aPubTargetSendAsyncMessage, 1.76 + aSubTargetSendAsyncMessage) { 1.77 + let pubTarget = register_message_port(aMessagePortId, 1.78 + PUB_APP_MANIFEST_URL, 1.79 + PUB_APP_PAGE_URL, 1.80 + aPubTargetSendAsyncMessage); 1.81 + 1.82 + let subTarget = register_message_port(aMessagePortId, 1.83 + SUB_APP_MANIFEST_URL, 1.84 + SUB_APP_PAGE_URL, 1.85 + aSubTargetSendAsyncMessage); 1.86 + 1.87 + return { pubTarget: pubTarget, subTarget: subTarget }; 1.88 +} 1.89 + 1.90 +function unregister_message_port(aMessagePortId, 1.91 + aManifestURL) { 1.92 + let message = { 1.93 + name: "InterAppMessagePort:Unregister", 1.94 + json: { 1.95 + messagePortID: aMessagePortId, 1.96 + manifestURL: aManifestURL 1.97 + }, 1.98 + target: { 1.99 + assertContainApp: function(_manifestURL) { 1.100 + return (aManifestURL == _manifestURL); 1.101 + } 1.102 + } 1.103 + }; 1.104 + 1.105 + InterAppCommService.receiveMessage(message); 1.106 +} 1.107 + 1.108 +function remove_target(aTarget) { 1.109 + let message = { 1.110 + name: "child-process-shutdown", 1.111 + target: aTarget 1.112 + }; 1.113 + 1.114 + InterAppCommService.receiveMessage(message); 1.115 +} 1.116 + 1.117 +function post_message(aMessagePortId, 1.118 + aManifestURL, 1.119 + aMessage) { 1.120 + let message = { 1.121 + name: "InterAppMessagePort:PostMessage", 1.122 + json: { 1.123 + messagePortID: aMessagePortId, 1.124 + manifestURL: aManifestURL, 1.125 + message: aMessage 1.126 + }, 1.127 + target: { 1.128 + assertContainApp: function(_manifestURL) { 1.129 + return (aManifestURL == _manifestURL); 1.130 + } 1.131 + } 1.132 + }; 1.133 + 1.134 + InterAppCommService.receiveMessage(message); 1.135 +} 1.136 + 1.137 +function create_allowed_connections(aKeyword, 1.138 + aPubManifestURL, 1.139 + aSubManifestURL) { 1.140 + let allowedPubAppManifestURLs = 1.141 + InterAppCommService._allowedConnections[aKeyword] = {}; 1.142 + 1.143 + allowedPubAppManifestURLs[aPubManifestURL] = [aSubManifestURL]; 1.144 +} 1.145 + 1.146 +function clear_allowed_connections() { 1.147 + InterAppCommService._allowedConnections = {}; 1.148 +} 1.149 + 1.150 +function get_connections(aManifestURL, 1.151 + aOuterWindowID, 1.152 + aRequestID, 1.153 + aTargetSendAsyncMessage) { 1.154 + let message = { 1.155 + name: "Webapps:GetConnections", 1.156 + json: { 1.157 + manifestURL: aManifestURL, 1.158 + outerWindowID: aOuterWindowID, 1.159 + requestID: aRequestID 1.160 + }, 1.161 + target: { 1.162 + sendAsyncMessage: function(aName, aData) { 1.163 + if (aTargetSendAsyncMessage) { 1.164 + aTargetSendAsyncMessage(aName, aData); 1.165 + } 1.166 + }, 1.167 + assertContainApp: function(_manifestURL) { 1.168 + return (aManifestURL == _manifestURL); 1.169 + } 1.170 + } 1.171 + }; 1.172 + 1.173 + InterAppCommService.receiveMessage(message); 1.174 +} 1.175 + 1.176 +function cancel_connections(aManifestURL, 1.177 + aKeyword, 1.178 + aPubManifestURL, 1.179 + aSubManifestURL) { 1.180 + let message = { 1.181 + name: "InterAppConnection:Cancel", 1.182 + json: { 1.183 + manifestURL: aManifestURL, 1.184 + keyword: aKeyword, 1.185 + pubAppManifestURL: aPubManifestURL, 1.186 + subAppManifestURL: aSubManifestURL 1.187 + }, 1.188 + target: { 1.189 + assertContainApp: function(_manifestURL) { 1.190 + return (aManifestURL == _manifestURL); 1.191 + } 1.192 + } 1.193 + }; 1.194 + 1.195 + InterAppCommService.receiveMessage(message); 1.196 +} 1.197 + 1.198 +add_test(function test_registerMessagePort() { 1.199 + create_message_port_pair(MESSAGE_PORT_ID, 1.200 + KEYWORD, 1.201 + PUB_APP_MANIFEST_URL, 1.202 + SUB_APP_MANIFEST_URL); 1.203 + 1.204 + let targets = register_message_ports(MESSAGE_PORT_ID); 1.205 + 1.206 + let messagePortPair = InterAppCommService._messagePortPairs[MESSAGE_PORT_ID]; 1.207 + 1.208 + do_check_eq(PUB_APP_PAGE_URL, messagePortPair.publisher.pageURL); 1.209 + do_check_eq(SUB_APP_PAGE_URL, messagePortPair.subscriber.pageURL); 1.210 + 1.211 + do_check_true(targets.pubTarget === messagePortPair.publisher.target); 1.212 + do_check_true(targets.subTarget === messagePortPair.subscriber.target); 1.213 + 1.214 + clear_message_port_pairs(); 1.215 + run_next_test(); 1.216 +}); 1.217 + 1.218 +add_test(function test_failToRegisterMessagePort() { 1.219 + create_message_port_pair(MESSAGE_PORT_ID, 1.220 + KEYWORD, 1.221 + PUB_APP_MANIFEST_URL, 1.222 + SUB_APP_MANIFEST_URL); 1.223 + 1.224 + let targets = register_message_ports(FAKE_MESSAGE_PORT_ID); 1.225 + 1.226 + let messagePortPair = InterAppCommService._messagePortPairs[MESSAGE_PORT_ID]; 1.227 + 1.228 + // Because it failed to register, the page URLs and targets don't exist. 1.229 + do_check_true(messagePortPair.publisher.pageURL === undefined); 1.230 + do_check_true(messagePortPair.subscriber.pageURL === undefined); 1.231 + 1.232 + do_check_true(messagePortPair.publisher.target === undefined); 1.233 + do_check_true(messagePortPair.subscriber.target === undefined); 1.234 + 1.235 + clear_message_port_pairs(); 1.236 + run_next_test(); 1.237 +}); 1.238 + 1.239 +add_test(function test_unregisterMessagePort() { 1.240 + create_message_port_pair(MESSAGE_PORT_ID, 1.241 + KEYWORD, 1.242 + PUB_APP_MANIFEST_URL, 1.243 + SUB_APP_MANIFEST_URL); 1.244 + 1.245 + register_message_ports(MESSAGE_PORT_ID); 1.246 + 1.247 + unregister_message_port(MESSAGE_PORT_ID, PUB_APP_MANIFEST_URL); 1.248 + 1.249 + do_check_true(InterAppCommService._messagePortPairs[MESSAGE_PORT_ID] 1.250 + === undefined); 1.251 + 1.252 + clear_message_port_pairs(); 1.253 + run_next_test(); 1.254 +}); 1.255 + 1.256 +add_test(function test_failToUnregisterMessagePort() { 1.257 + create_message_port_pair(MESSAGE_PORT_ID, 1.258 + KEYWORD, 1.259 + PUB_APP_MANIFEST_URL, 1.260 + SUB_APP_MANIFEST_URL); 1.261 + 1.262 + register_message_ports(MESSAGE_PORT_ID); 1.263 + 1.264 + unregister_message_port(FAKE_MESSAGE_PORT_ID, PUB_APP_MANIFEST_URL); 1.265 + 1.266 + // Because it failed to unregister, the entry still exists. 1.267 + do_check_true(InterAppCommService._messagePortPairs[MESSAGE_PORT_ID] 1.268 + !== undefined); 1.269 + 1.270 + clear_message_port_pairs(); 1.271 + run_next_test(); 1.272 +}); 1.273 + 1.274 +add_test(function test_removeTarget() { 1.275 + create_message_port_pair(MESSAGE_PORT_ID, 1.276 + KEYWORD, 1.277 + PUB_APP_MANIFEST_URL, 1.278 + SUB_APP_MANIFEST_URL); 1.279 + 1.280 + let targets = register_message_ports(MESSAGE_PORT_ID); 1.281 + 1.282 + remove_target(targets.pubTarget); 1.283 + 1.284 + do_check_true(InterAppCommService._messagePortPairs[MESSAGE_PORT_ID] 1.285 + === undefined); 1.286 + 1.287 + clear_message_port_pairs(); 1.288 + run_next_test(); 1.289 +}); 1.290 + 1.291 +add_test(function test_postMessage() { 1.292 + create_message_port_pair(MESSAGE_PORT_ID, 1.293 + KEYWORD, 1.294 + PUB_APP_MANIFEST_URL, 1.295 + SUB_APP_MANIFEST_URL); 1.296 + 1.297 + let countPubAppOnMessage = 0; 1.298 + function pubAppOnMessage(aName, aData) { 1.299 + countPubAppOnMessage++; 1.300 + 1.301 + do_check_eq(aName, "InterAppMessagePort:OnMessage"); 1.302 + do_check_eq(aData.manifestURL, PUB_APP_MANIFEST_URL); 1.303 + do_check_eq(aData.pageURL, PUB_APP_PAGE_URL); 1.304 + do_check_eq(aData.messagePortID, MESSAGE_PORT_ID); 1.305 + 1.306 + if (countPubAppOnMessage == 1) { 1.307 + do_check_eq(aData.message.text, "sub app says world"); 1.308 + 1.309 + post_message(MESSAGE_PORT_ID, 1.310 + PUB_APP_MANIFEST_URL, 1.311 + { text: "pub app says hello again" }); 1.312 + 1.313 + } else if (countPubAppOnMessage == 2) { 1.314 + do_check_eq(aData.message.text, "sub app says world again"); 1.315 + 1.316 + clear_message_port_pairs(); 1.317 + run_next_test(); 1.318 + } else { 1.319 + do_throw("pub app receives an unexpected message") 1.320 + } 1.321 + }; 1.322 + 1.323 + let countSubAppOnMessage = 0; 1.324 + function subAppOnMessage(aName, aData) { 1.325 + countSubAppOnMessage++; 1.326 + 1.327 + do_check_eq(aName, "InterAppMessagePort:OnMessage"); 1.328 + do_check_eq(aData.manifestURL, SUB_APP_MANIFEST_URL); 1.329 + do_check_eq(aData.pageURL, SUB_APP_PAGE_URL); 1.330 + do_check_eq(aData.messagePortID, MESSAGE_PORT_ID); 1.331 + 1.332 + if (countSubAppOnMessage == 1) { 1.333 + do_check_eq(aData.message.text, "pub app says hello"); 1.334 + 1.335 + post_message(MESSAGE_PORT_ID, 1.336 + SUB_APP_MANIFEST_URL, 1.337 + { text: "sub app says world" }); 1.338 + 1.339 + } else if (countSubAppOnMessage == 2) { 1.340 + do_check_eq(aData.message.text, "pub app says hello again"); 1.341 + 1.342 + post_message(MESSAGE_PORT_ID, 1.343 + SUB_APP_MANIFEST_URL, 1.344 + { text: "sub app says world again" }); 1.345 + } else { 1.346 + do_throw("sub app receives an unexpected message") 1.347 + } 1.348 + }; 1.349 + 1.350 + register_message_ports(MESSAGE_PORT_ID, pubAppOnMessage, subAppOnMessage); 1.351 + 1.352 + post_message(MESSAGE_PORT_ID, 1.353 + PUB_APP_MANIFEST_URL, 1.354 + { text: "pub app says hello" }); 1.355 +}); 1.356 + 1.357 +add_test(function test_registerMessagePort_with_queued_messages() { 1.358 + create_message_port_pair(MESSAGE_PORT_ID, 1.359 + KEYWORD, 1.360 + PUB_APP_MANIFEST_URL, 1.361 + SUB_APP_MANIFEST_URL); 1.362 + 1.363 + register_message_port(MESSAGE_PORT_ID, 1.364 + PUB_APP_MANIFEST_URL, 1.365 + PUB_APP_PAGE_URL); 1.366 + 1.367 + post_message(MESSAGE_PORT_ID, 1.368 + PUB_APP_MANIFEST_URL, 1.369 + { text: "pub app says hello" }); 1.370 + 1.371 + post_message(MESSAGE_PORT_ID, 1.372 + PUB_APP_MANIFEST_URL, 1.373 + { text: "pub app says hello again" }); 1.374 + 1.375 + let countSubAppOnMessage = 0; 1.376 + function subAppOnMessage(aName, aData) { 1.377 + countSubAppOnMessage++; 1.378 + 1.379 + do_check_eq(aName, "InterAppMessagePort:OnMessage"); 1.380 + do_check_eq(aData.manifestURL, SUB_APP_MANIFEST_URL); 1.381 + do_check_eq(aData.pageURL, SUB_APP_PAGE_URL); 1.382 + do_check_eq(aData.messagePortID, MESSAGE_PORT_ID); 1.383 + 1.384 + if (countSubAppOnMessage == 1) { 1.385 + do_check_eq(aData.message.text, "pub app says hello"); 1.386 + } else if (countSubAppOnMessage == 2) { 1.387 + do_check_eq(aData.message.text, "pub app says hello again"); 1.388 + 1.389 + clear_message_port_pairs(); 1.390 + run_next_test(); 1.391 + } else { 1.392 + do_throw("sub app receives an unexpected message") 1.393 + } 1.394 + }; 1.395 + 1.396 + register_message_port(MESSAGE_PORT_ID, 1.397 + SUB_APP_MANIFEST_URL, 1.398 + SUB_APP_PAGE_URL, 1.399 + subAppOnMessage); 1.400 +}); 1.401 + 1.402 +add_test(function test_getConnections() { 1.403 + create_allowed_connections(KEYWORD, 1.404 + PUB_APP_MANIFEST_URL, 1.405 + SUB_APP_MANIFEST_URL); 1.406 + 1.407 + function onGetConnections(aName, aData) { 1.408 + do_check_eq(aName, "Webapps:GetConnections:Return:OK"); 1.409 + do_check_eq(aData.oid, OUTER_WINDOW_ID); 1.410 + do_check_eq(aData.requestID, REQUEST_ID); 1.411 + 1.412 + let connections = aData.connections; 1.413 + do_check_eq(connections.length, 1); 1.414 + do_check_eq(connections[0].keyword, KEYWORD); 1.415 + do_check_eq(connections[0].pubAppManifestURL, PUB_APP_MANIFEST_URL); 1.416 + do_check_eq(connections[0].subAppManifestURL, SUB_APP_MANIFEST_URL); 1.417 + 1.418 + clear_allowed_connections(); 1.419 + run_next_test(); 1.420 + }; 1.421 + 1.422 + get_connections(PUB_APP_MANIFEST_URL, 1.423 + OUTER_WINDOW_ID, 1.424 + REQUEST_ID, 1.425 + onGetConnections); 1.426 +}); 1.427 + 1.428 +add_test(function test_cancelConnection() { 1.429 + create_allowed_connections(KEYWORD, 1.430 + PUB_APP_MANIFEST_URL, 1.431 + SUB_APP_MANIFEST_URL); 1.432 + 1.433 + create_message_port_pair(MESSAGE_PORT_ID, 1.434 + KEYWORD, 1.435 + PUB_APP_MANIFEST_URL, 1.436 + SUB_APP_MANIFEST_URL); 1.437 + 1.438 + register_message_ports(MESSAGE_PORT_ID); 1.439 + 1.440 + cancel_connections(PUB_APP_MANIFEST_URL, 1.441 + KEYWORD, 1.442 + PUB_APP_MANIFEST_URL, 1.443 + SUB_APP_MANIFEST_URL); 1.444 + 1.445 + do_check_true(InterAppCommService._allowedConnections[KEYWORD] 1.446 + === undefined); 1.447 + 1.448 + do_check_true(InterAppCommService._messagePortPairs[MESSAGE_PORT_ID] 1.449 + === undefined); 1.450 + 1.451 + clear_allowed_connections(); 1.452 + clear_message_port_pairs(); 1.453 + run_next_test(); 1.454 +}); 1.455 + 1.456 +function run_test() { 1.457 + do_get_profile(); 1.458 + 1.459 + run_next_test(); 1.460 +}