| 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 |
/** |
| 16 |
* @fileoverview Tests the direct transport. |
| 17 |
*/ |
| 18 |
|
| 19 |
goog.provide('goog.net.xpc.DirectTransportTest'); |
| 20 |
|
| 21 |
goog.require('goog.Promise'); |
| 22 |
goog.require('goog.dom'); |
| 23 |
goog.require('goog.dom.TagName'); |
| 24 |
goog.require('goog.labs.userAgent.browser'); |
| 25 |
goog.require('goog.log'); |
| 26 |
goog.require('goog.log.Level'); |
| 27 |
goog.require('goog.net.xpc'); |
| 28 |
goog.require('goog.net.xpc.CfgFields'); |
| 29 |
goog.require('goog.net.xpc.CrossPageChannel'); |
| 30 |
goog.require('goog.net.xpc.CrossPageChannelRole'); |
| 31 |
goog.require('goog.net.xpc.TransportTypes'); |
| 32 |
goog.require('goog.testing.TestCase'); |
| 33 |
goog.require('goog.testing.jsunit'); |
| 34 |
goog.setTestOnly('goog.net.xpc.DirectTransportTest'); |
| 35 |
|
| 36 |
|
| 37 |
/** |
| 38 |
* Echo service name. |
| 39 |
* @type {string} |
| 40 |
* @const |
| 41 |
*/ |
| 42 |
var ECHO_SERVICE_NAME = 'echo'; |
| 43 |
|
| 44 |
|
| 45 |
/** |
| 46 |
* Response service name. |
| 47 |
* @type {string} |
| 48 |
* @const |
| 49 |
*/ |
| 50 |
var RESPONSE_SERVICE_NAME = 'response'; |
| 51 |
|
| 52 |
|
| 53 |
/** |
| 54 |
* Test Payload. |
| 55 |
* @type {string} |
| 56 |
* @const |
| 57 |
*/ |
| 58 |
var MESSAGE_PAYLOAD_1 = 'This is message payload 1.'; |
| 59 |
|
| 60 |
|
| 61 |
/** |
| 62 |
* The name id of the peer iframe. |
| 63 |
* @type {string} |
| 64 |
* @const |
| 65 |
*/ |
| 66 |
var PEER_IFRAME_ID = 'peer-iframe'; |
| 67 |
|
| 68 |
|
| 69 |
// Class aliases. |
| 70 |
var CfgFields = goog.net.xpc.CfgFields; |
| 71 |
var CrossPageChannel = goog.net.xpc.CrossPageChannel; |
| 72 |
var CrossPageChannelRole = goog.net.xpc.CrossPageChannelRole; |
| 73 |
var TransportTypes = goog.net.xpc.TransportTypes; |
| 74 |
|
| 75 |
var outerXpc; |
| 76 |
var innerXpc; |
| 77 |
var peerIframe; |
| 78 |
var channelName; |
| 79 |
var messageIsSync = false; |
| 80 |
var savedHtml; |
| 81 |
var debugDiv; |
| 82 |
|
| 83 |
function setUpPage() { |
| 84 |
// Show debug log |
| 85 |
debugDiv = goog.dom.createElement(goog.dom.TagName.DIV); |
| 86 |
var logger = goog.log.getLogger('goog.net.xpc'); |
| 87 |
logger.setLevel(goog.log.Level.ALL); |
| 88 |
goog.log.addHandler(logger, function(logRecord) { |
| 89 |
var msgElm = goog.dom.createDom(goog.dom.TagName.DIV); |
| 90 |
msgElm.innerHTML = logRecord.getMessage(); |
| 91 |
goog.dom.appendChild(debugDiv, msgElm); |
| 92 |
}); |
| 93 |
goog.testing.TestCase.getActiveTestCase().promiseTimeout = 10000; // 10s |
| 94 |
} |
| 95 |
|
| 96 |
function setUp() { |
| 97 |
savedHtml = document.body.innerHTML; |
| 98 |
document.body.appendChild(debugDiv); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
function tearDown() { |
| 103 |
if (peerIframe) { |
| 104 |
document.body.removeChild(peerIframe); |
| 105 |
peerIframe = null; |
| 106 |
} |
| 107 |
if (outerXpc) { |
| 108 |
outerXpc.dispose(); |
| 109 |
outerXpc = null; |
| 110 |
} |
| 111 |
if (innerXpc) { |
| 112 |
innerXpc.dispose(); |
| 113 |
innerXpc = null; |
| 114 |
} |
| 115 |
window.iframeLoadHandler = null; |
| 116 |
channelName = null; |
| 117 |
messageIsSync = false; |
| 118 |
document.body.innerHTML = savedHtml; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
function createIframe() { |
| 123 |
peerIframe = goog.dom.createElement(goog.dom.TagName.IFRAME); |
| 124 |
peerIframe.id = PEER_IFRAME_ID; |
| 125 |
document.body.insertBefore(peerIframe, document.body.firstChild); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
/** |
| 130 |
* Tests 2 same domain frames using direct transport. |
| 131 |
*/ |
| 132 |
function testDirectTransport() { |
| 133 |
// This test has been flaky on IE. |
| 134 |
// For now, disable. |
| 135 |
// Flakiness is tracked in http://b/18595666 |
| 136 |
if (goog.labs.userAgent.browser.isIE()) { |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
createIframe(); |
| 141 |
channelName = goog.net.xpc.getRandomString(10); |
| 142 |
outerXpc = new CrossPageChannel( |
| 143 |
getConfiguration(CrossPageChannelRole.OUTER, PEER_IFRAME_ID)); |
| 144 |
// Outgoing service. |
| 145 |
outerXpc.registerService(ECHO_SERVICE_NAME, goog.nullFunction); |
| 146 |
// Incoming service. |
| 147 |
var resolver = goog.Promise.withResolver(); |
| 148 |
outerXpc.registerService(RESPONSE_SERVICE_NAME, function(message) { |
| 149 |
assertEquals( |
| 150 |
'Received payload is equal to sent payload.', message, |
| 151 |
MESSAGE_PAYLOAD_1); |
| 152 |
resolver.resolve(); |
| 153 |
}); |
| 154 |
|
| 155 |
outerXpc.connect(function() { |
| 156 |
assertTrue('XPC over direct channel is connected', outerXpc.isConnected()); |
| 157 |
outerXpc.send(ECHO_SERVICE_NAME, MESSAGE_PAYLOAD_1); |
| 158 |
}); |
| 159 |
// inner_peer.html calls this method at end of html. |
| 160 |
window.iframeLoadHandler = function() { |
| 161 |
peerIframe.contentWindow.instantiateChannel( |
| 162 |
getConfiguration(CrossPageChannelRole.INNER)); |
| 163 |
}; |
| 164 |
peerIframe.src = 'testdata/inner_peer.html'; |
| 165 |
|
| 166 |
return resolver.promise; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
/** |
| 171 |
* Tests 2 xpc's communicating with each other in the same window. |
| 172 |
*/ |
| 173 |
function testSameWindowDirectTransport() { |
| 174 |
channelName = goog.net.xpc.getRandomString(10); |
| 175 |
|
| 176 |
outerXpc = new CrossPageChannel(getConfiguration(CrossPageChannelRole.OUTER)); |
| 177 |
outerXpc.setPeerWindowObject(self); |
| 178 |
|
| 179 |
// Outgoing service. |
| 180 |
outerXpc.registerService(ECHO_SERVICE_NAME, goog.nullFunction); |
| 181 |
|
| 182 |
var resolver = goog.Promise.withResolver(); |
| 183 |
// Incoming service. |
| 184 |
outerXpc.registerService(RESPONSE_SERVICE_NAME, function(message) { |
| 185 |
assertEquals( |
| 186 |
'Received payload is equal to sent payload.', message, |
| 187 |
MESSAGE_PAYLOAD_1); |
| 188 |
resolver.resolve(); |
| 189 |
}); |
| 190 |
outerXpc.connect(function() { |
| 191 |
assertTrue( |
| 192 |
'XPC over direct channel, same window, is connected', |
| 193 |
outerXpc.isConnected()); |
| 194 |
outerXpc.send(ECHO_SERVICE_NAME, MESSAGE_PAYLOAD_1); |
| 195 |
}); |
| 196 |
|
| 197 |
innerXpc = new CrossPageChannel(getConfiguration(CrossPageChannelRole.INNER)); |
| 198 |
innerXpc.setPeerWindowObject(self); |
| 199 |
// Incoming service. |
| 200 |
innerXpc.registerService(ECHO_SERVICE_NAME, function(message) { |
| 201 |
innerXpc.send(RESPONSE_SERVICE_NAME, message); |
| 202 |
}); |
| 203 |
// Outgoing service. |
| 204 |
innerXpc.registerService(RESPONSE_SERVICE_NAME, goog.nullFunction); |
| 205 |
innerXpc.connect(); |
| 206 |
return resolver.promise; |
| 207 |
} |
| 208 |
|
| 209 |
function getConfiguration(role, opt_peerFrameId) { |
| 210 |
var cfg = {}; |
| 211 |
cfg[CfgFields.TRANSPORT] = TransportTypes.DIRECT; |
| 212 |
if (goog.isDefAndNotNull(opt_peerFrameId)) { |
| 213 |
cfg[CfgFields.IFRAME_ID] = opt_peerFrameId; |
| 214 |
} |
| 215 |
cfg[CfgFields.CHANNEL_NAME] = channelName; |
| 216 |
cfg[CfgFields.ROLE] = role; |
| 217 |
return cfg; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
/** |
| 222 |
* Tests 2 same domain frames using direct transport using sync mode. |
| 223 |
*/ |
| 224 |
function testSyncMode() { |
| 225 |
// This test has been flaky on IE. |
| 226 |
// For now, disable. |
| 227 |
// Flakiness is tracked in http://b/18595666 |
| 228 |
if (goog.labs.userAgent.browser.isIE()) { |
| 229 |
return; |
| 230 |
} |
| 231 |
|
| 232 |
createIframe(); |
| 233 |
channelName = goog.net.xpc.getRandomString(10); |
| 234 |
|
| 235 |
var cfg = getConfiguration(CrossPageChannelRole.OUTER, PEER_IFRAME_ID); |
| 236 |
cfg[CfgFields.DIRECT_TRANSPORT_SYNC_MODE] = true; |
| 237 |
|
| 238 |
outerXpc = new CrossPageChannel(cfg); |
| 239 |
// Outgoing service. |
| 240 |
outerXpc.registerService(ECHO_SERVICE_NAME, goog.nullFunction); |
| 241 |
var resolver = goog.Promise.withResolver(); |
| 242 |
// Incoming service. |
| 243 |
outerXpc.registerService(RESPONSE_SERVICE_NAME, function(message) { |
| 244 |
assertTrue('The message response was syncronous', messageIsSync); |
| 245 |
assertEquals( |
| 246 |
'Received payload is equal to sent payload.', message, |
| 247 |
MESSAGE_PAYLOAD_1); |
| 248 |
resolver.resolve(); |
| 249 |
}); |
| 250 |
outerXpc.connect(function() { |
| 251 |
assertTrue('XPC over direct channel is connected', outerXpc.isConnected()); |
| 252 |
messageIsSync = true; |
| 253 |
outerXpc.send(ECHO_SERVICE_NAME, MESSAGE_PAYLOAD_1); |
| 254 |
messageIsSync = false; |
| 255 |
}); |
| 256 |
// inner_peer.html calls this method at end of html. |
| 257 |
window.iframeLoadHandler = function() { |
| 258 |
var cfg = getConfiguration(CrossPageChannelRole.INNER); |
| 259 |
cfg[CfgFields.DIRECT_TRANSPORT_SYNC_MODE] = true; |
| 260 |
peerIframe.contentWindow.instantiateChannel(cfg); |
| 261 |
}; |
| 262 |
peerIframe.src = 'testdata/inner_peer.html'; |
| 263 |
return resolver.promise; |
| 264 |
} |