directtransport_test.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // Copyright 2013 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Tests the direct transport.
  16. */
  17. goog.provide('goog.net.xpc.DirectTransportTest');
  18. goog.require('goog.Promise');
  19. goog.require('goog.dom');
  20. goog.require('goog.dom.TagName');
  21. goog.require('goog.labs.userAgent.browser');
  22. goog.require('goog.log');
  23. goog.require('goog.log.Level');
  24. goog.require('goog.net.xpc');
  25. goog.require('goog.net.xpc.CfgFields');
  26. goog.require('goog.net.xpc.CrossPageChannel');
  27. goog.require('goog.net.xpc.CrossPageChannelRole');
  28. goog.require('goog.net.xpc.TransportTypes');
  29. goog.require('goog.testing.TestCase');
  30. goog.require('goog.testing.jsunit');
  31. goog.setTestOnly('goog.net.xpc.DirectTransportTest');
  32. /**
  33. * Echo service name.
  34. * @type {string}
  35. * @const
  36. */
  37. var ECHO_SERVICE_NAME = 'echo';
  38. /**
  39. * Response service name.
  40. * @type {string}
  41. * @const
  42. */
  43. var RESPONSE_SERVICE_NAME = 'response';
  44. /**
  45. * Test Payload.
  46. * @type {string}
  47. * @const
  48. */
  49. var MESSAGE_PAYLOAD_1 = 'This is message payload 1.';
  50. /**
  51. * The name id of the peer iframe.
  52. * @type {string}
  53. * @const
  54. */
  55. var PEER_IFRAME_ID = 'peer-iframe';
  56. // Class aliases.
  57. var CfgFields = goog.net.xpc.CfgFields;
  58. var CrossPageChannel = goog.net.xpc.CrossPageChannel;
  59. var CrossPageChannelRole = goog.net.xpc.CrossPageChannelRole;
  60. var TransportTypes = goog.net.xpc.TransportTypes;
  61. var outerXpc;
  62. var innerXpc;
  63. var peerIframe;
  64. var channelName;
  65. var messageIsSync = false;
  66. var savedHtml;
  67. var debugDiv;
  68. function setUpPage() {
  69. // Show debug log
  70. debugDiv = goog.dom.createElement(goog.dom.TagName.DIV);
  71. var logger = goog.log.getLogger('goog.net.xpc');
  72. logger.setLevel(goog.log.Level.ALL);
  73. goog.log.addHandler(logger, function(logRecord) {
  74. var msgElm = goog.dom.createDom(goog.dom.TagName.DIV);
  75. msgElm.innerHTML = logRecord.getMessage();
  76. goog.dom.appendChild(debugDiv, msgElm);
  77. });
  78. goog.testing.TestCase.getActiveTestCase().promiseTimeout = 10000; // 10s
  79. }
  80. function setUp() {
  81. savedHtml = document.body.innerHTML;
  82. document.body.appendChild(debugDiv);
  83. }
  84. function tearDown() {
  85. if (peerIframe) {
  86. document.body.removeChild(peerIframe);
  87. peerIframe = null;
  88. }
  89. if (outerXpc) {
  90. outerXpc.dispose();
  91. outerXpc = null;
  92. }
  93. if (innerXpc) {
  94. innerXpc.dispose();
  95. innerXpc = null;
  96. }
  97. window.iframeLoadHandler = null;
  98. channelName = null;
  99. messageIsSync = false;
  100. document.body.innerHTML = savedHtml;
  101. }
  102. function createIframe() {
  103. peerIframe = goog.dom.createElement(goog.dom.TagName.IFRAME);
  104. peerIframe.id = PEER_IFRAME_ID;
  105. document.body.insertBefore(peerIframe, document.body.firstChild);
  106. }
  107. /**
  108. * Tests 2 same domain frames using direct transport.
  109. */
  110. function testDirectTransport() {
  111. // This test has been flaky on IE.
  112. // For now, disable.
  113. // Flakiness is tracked in http://b/18595666
  114. if (goog.labs.userAgent.browser.isIE()) {
  115. return;
  116. }
  117. createIframe();
  118. channelName = goog.net.xpc.getRandomString(10);
  119. outerXpc = new CrossPageChannel(
  120. getConfiguration(CrossPageChannelRole.OUTER, PEER_IFRAME_ID));
  121. // Outgoing service.
  122. outerXpc.registerService(ECHO_SERVICE_NAME, goog.nullFunction);
  123. // Incoming service.
  124. var resolver = goog.Promise.withResolver();
  125. outerXpc.registerService(RESPONSE_SERVICE_NAME, function(message) {
  126. assertEquals(
  127. 'Received payload is equal to sent payload.', message,
  128. MESSAGE_PAYLOAD_1);
  129. resolver.resolve();
  130. });
  131. outerXpc.connect(function() {
  132. assertTrue('XPC over direct channel is connected', outerXpc.isConnected());
  133. outerXpc.send(ECHO_SERVICE_NAME, MESSAGE_PAYLOAD_1);
  134. });
  135. // inner_peer.html calls this method at end of html.
  136. window.iframeLoadHandler = function() {
  137. peerIframe.contentWindow.instantiateChannel(
  138. getConfiguration(CrossPageChannelRole.INNER));
  139. };
  140. peerIframe.src = 'testdata/inner_peer.html';
  141. return resolver.promise;
  142. }
  143. /**
  144. * Tests 2 xpc's communicating with each other in the same window.
  145. */
  146. function testSameWindowDirectTransport() {
  147. channelName = goog.net.xpc.getRandomString(10);
  148. outerXpc = new CrossPageChannel(getConfiguration(CrossPageChannelRole.OUTER));
  149. outerXpc.setPeerWindowObject(self);
  150. // Outgoing service.
  151. outerXpc.registerService(ECHO_SERVICE_NAME, goog.nullFunction);
  152. var resolver = goog.Promise.withResolver();
  153. // Incoming service.
  154. outerXpc.registerService(RESPONSE_SERVICE_NAME, function(message) {
  155. assertEquals(
  156. 'Received payload is equal to sent payload.', message,
  157. MESSAGE_PAYLOAD_1);
  158. resolver.resolve();
  159. });
  160. outerXpc.connect(function() {
  161. assertTrue(
  162. 'XPC over direct channel, same window, is connected',
  163. outerXpc.isConnected());
  164. outerXpc.send(ECHO_SERVICE_NAME, MESSAGE_PAYLOAD_1);
  165. });
  166. innerXpc = new CrossPageChannel(getConfiguration(CrossPageChannelRole.INNER));
  167. innerXpc.setPeerWindowObject(self);
  168. // Incoming service.
  169. innerXpc.registerService(ECHO_SERVICE_NAME, function(message) {
  170. innerXpc.send(RESPONSE_SERVICE_NAME, message);
  171. });
  172. // Outgoing service.
  173. innerXpc.registerService(RESPONSE_SERVICE_NAME, goog.nullFunction);
  174. innerXpc.connect();
  175. return resolver.promise;
  176. }
  177. function getConfiguration(role, opt_peerFrameId) {
  178. var cfg = {};
  179. cfg[CfgFields.TRANSPORT] = TransportTypes.DIRECT;
  180. if (goog.isDefAndNotNull(opt_peerFrameId)) {
  181. cfg[CfgFields.IFRAME_ID] = opt_peerFrameId;
  182. }
  183. cfg[CfgFields.CHANNEL_NAME] = channelName;
  184. cfg[CfgFields.ROLE] = role;
  185. return cfg;
  186. }
  187. /**
  188. * Tests 2 same domain frames using direct transport using sync mode.
  189. */
  190. function testSyncMode() {
  191. // This test has been flaky on IE.
  192. // For now, disable.
  193. // Flakiness is tracked in http://b/18595666
  194. if (goog.labs.userAgent.browser.isIE()) {
  195. return;
  196. }
  197. createIframe();
  198. channelName = goog.net.xpc.getRandomString(10);
  199. var cfg = getConfiguration(CrossPageChannelRole.OUTER, PEER_IFRAME_ID);
  200. cfg[CfgFields.DIRECT_TRANSPORT_SYNC_MODE] = true;
  201. outerXpc = new CrossPageChannel(cfg);
  202. // Outgoing service.
  203. outerXpc.registerService(ECHO_SERVICE_NAME, goog.nullFunction);
  204. var resolver = goog.Promise.withResolver();
  205. // Incoming service.
  206. outerXpc.registerService(RESPONSE_SERVICE_NAME, function(message) {
  207. assertTrue('The message response was syncronous', messageIsSync);
  208. assertEquals(
  209. 'Received payload is equal to sent payload.', message,
  210. MESSAGE_PAYLOAD_1);
  211. resolver.resolve();
  212. });
  213. outerXpc.connect(function() {
  214. assertTrue('XPC over direct channel is connected', outerXpc.isConnected());
  215. messageIsSync = true;
  216. outerXpc.send(ECHO_SERVICE_NAME, MESSAGE_PAYLOAD_1);
  217. messageIsSync = false;
  218. });
  219. // inner_peer.html calls this method at end of html.
  220. window.iframeLoadHandler = function() {
  221. var cfg = getConfiguration(CrossPageChannelRole.INNER);
  222. cfg[CfgFields.DIRECT_TRANSPORT_SYNC_MODE] = true;
  223. peerIframe.contentWindow.instantiateChannel(cfg);
  224. };
  225. peerIframe.src = 'testdata/inner_peer.html';
  226. return resolver.promise;
  227. }