Debian Codegraph @

closure-library/closure/goog/net/xpc/iframepollingtransport_test.js

raw
1 // Copyright 2012 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 goog.provide('goog.net.xpc.IframePollingTransportTest');
16 goog.setTestOnly('goog.net.xpc.IframePollingTransportTest');
17
18 goog.require('goog.Timer');
19 goog.require('goog.dom');
20 goog.require('goog.dom.TagName');
21 goog.require('goog.functions');
22 goog.require('goog.net.xpc.CfgFields');
23 goog.require('goog.net.xpc.CrossPageChannel');
24 goog.require('goog.net.xpc.CrossPageChannelRole');
25 goog.require('goog.net.xpc.TransportTypes');
26 goog.require('goog.object');
27 goog.require('goog.testing.MockClock');
28 goog.require('goog.testing.jsunit');
29 goog.require('goog.testing.recordFunction');
30
31 /** @type {?goog.testing.MockClock} */
32 var mockClock = null;
33
34 /** @type {?goog.net.xpc.CrossPageChannel} */
35 var outerChannel = null;
36
37 /** @type {?goog.net.xpc.CrossPageChannel} */
38 var innerChannel = null;
39
40 function setUp() {
41 mockClock = new goog.testing.MockClock(true /* opt_autoInstall */);
42
43 // Create the peer windows.
44 var outerPeerHostName = 'https://www.youtube.com';
45 var outerPeerWindow = createMockPeerWindow(outerPeerHostName);
46
47 var innerPeerHostName = 'https://www.google.com';
48 var innerPeerWindow = createMockPeerWindow(innerPeerHostName);
49
50 // Create the channels.
51 outerChannel = createChannel(
52 goog.net.xpc.CrossPageChannelRole.OUTER, 'test', outerPeerHostName,
53 outerPeerWindow, innerPeerHostName, innerPeerWindow);
54 innerChannel = createChannel(
55 goog.net.xpc.CrossPageChannelRole.INNER, 'test', innerPeerHostName,
56 innerPeerWindow, outerPeerHostName, outerPeerWindow);
57 }
58
59
60 function tearDown() {
61 outerChannel.dispose();
62 innerChannel.dispose();
63 mockClock.uninstall();
64 }
65
66
67 /** Tests that connection happens normally and callbacks are invoked. */
68 function testConnect() {
69 var outerConnectCallback = goog.testing.recordFunction();
70 var innerConnectCallback = goog.testing.recordFunction();
71
72 // Connect the two channels.
73 outerChannel.connect(outerConnectCallback);
74 innerChannel.connect(innerConnectCallback);
75 mockClock.tick(1000);
76
77 // Check that channels were connected and callbacks invoked.
78 assertEquals(1, outerConnectCallback.getCallCount());
79 assertEquals(1, innerConnectCallback.getCallCount());
80 assertTrue(outerChannel.isConnected());
81 assertTrue(innerChannel.isConnected());
82 }
83
84
85 /** Tests that messages are successfully delivered to the inner peer. */
86 function testSend_outerToInner() {
87 var serviceCallback = goog.testing.recordFunction();
88
89 // Register a service handler in the inner channel.
90 innerChannel.registerService('svc', function(payload) {
91 assertEquals('hello', payload);
92 serviceCallback();
93 });
94
95 // Connect the two channels.
96 outerChannel.connect();
97 innerChannel.connect();
98 mockClock.tick(1000);
99
100 // Send a message.
101 outerChannel.send('svc', 'hello');
102 mockClock.tick(1000);
103
104 // Check that the message was handled.
105 assertEquals(1, serviceCallback.getCallCount());
106 }
107
108
109 /** Tests that messages are successfully delivered to the outer peer. */
110 function testSend_innerToOuter() {
111 var serviceCallback = goog.testing.recordFunction();
112
113 // Register a service handler in the inner channel.
114 outerChannel.registerService('svc', function(payload) {
115 assertEquals('hello', payload);
116 serviceCallback();
117 });
118
119 // Connect the two channels.
120 outerChannel.connect();
121 innerChannel.connect();
122 mockClock.tick(1000);
123
124 // Send a message.
125 innerChannel.send('svc', 'hello');
126 mockClock.tick(1000);
127
128 // Check that the message was handled.
129 assertEquals(1, serviceCallback.getCallCount());
130 }
131
132
133 /** Tests that closing the outer peer does not cause an error. */
134 function testSend_outerPeerClosed() {
135 // Connect the inner channel.
136 innerChannel.connect();
137 mockClock.tick(1000);
138
139 // Close the outer peer before it has a chance to connect.
140 closeWindow(innerChannel.getPeerWindowObject());
141
142 // Allow timers to execute (and fail).
143 mockClock.tick(1000);
144 }
145
146
147 /** Tests that closing the inner peer does not cause an error. */
148 function testSend_innerPeerClosed() {
149 // Connect the outer channel.
150 outerChannel.connect();
151 mockClock.tick(1000);
152
153 // Close the inner peer before it has a chance to connect.
154 closeWindow(outerChannel.getPeerWindowObject());
155
156 // Allow timers to execute (and fail).
157 mockClock.tick(1000);
158 }
159
160
161 /** Tests that partially closing the outer peer does not cause an error. */
162 function testSend_outerPeerClosing() {
163 // Connect the inner channel.
164 innerChannel.connect();
165 mockClock.tick(1000);
166
167 // Close the outer peer before it has a chance to connect, but
168 // leave closed set to false to simulate a partially closed window.
169 closeWindow(innerChannel.getPeerWindowObject());
170 innerChannel.getPeerWindowObject().closed = false;
171
172 // Allow timers to execute (and fail).
173 mockClock.tick(1000);
174 }
175
176
177 /** Tests that partially closing the inner peer does not cause an error. */
178 function testSend_innerPeerClosing() {
179 // Connect the outer channel.
180 outerChannel.connect();
181 mockClock.tick(1000);
182
183 // Close the inner peer before it has a chance to connect, but
184 // leave closed set to false to simulate a partially closed window.
185 closeWindow(outerChannel.getPeerWindowObject());
186 outerChannel.getPeerWindowObject().closed = false;
187
188 // Allow timers to execute (and fail).
189 mockClock.tick(1000);
190 }
191
192
193 /**
194 * Creates a channel with the specified configuration, using frame polling.
195 * @param {!goog.net.xpc.CrossPageChannelRole} role The channel role.
196 * @param {string} channelName The channel name.
197 * @param {string} fromHostName The host name of the window hosting the channel.
198 * @param {!Object} fromWindow The window hosting the channel.
199 * @param {string} toHostName The host name of the peer window.
200 * @param {!Object} toWindow The peer window.
201 * @return {!goog.net.xpc.CrossPageChannel}
202 */
203 function createChannel(
204 role, channelName, fromHostName, fromWindow, toHostName, toWindow) {
205 // Build a channel config using frame polling.
206 var channelConfig = goog.object.create(
207 goog.net.xpc.CfgFields.ROLE, role, goog.net.xpc.CfgFields.PEER_HOSTNAME,
208 toHostName, goog.net.xpc.CfgFields.CHANNEL_NAME, channelName,
209 goog.net.xpc.CfgFields.LOCAL_POLL_URI, fromHostName + '/robots.txt',
210 goog.net.xpc.CfgFields.PEER_POLL_URI, toHostName + '/robots.txt',
211 goog.net.xpc.CfgFields.TRANSPORT,
212 goog.net.xpc.TransportTypes.IFRAME_POLLING);
213
214 // Build the channel.
215 var channel = new goog.net.xpc.CrossPageChannel(channelConfig);
216 channel.setPeerWindowObject(toWindow);
217
218 // Update the transport's getWindow, to return the correct host window.
219 channel.createTransport_();
220 channel.transport_.getWindow = goog.functions.constant(fromWindow);
221 return channel;
222 }
223
224
225 /**
226 * Creates a mock window to use as a peer. The peer window will host the frame
227 * elements.
228 * @param {string} url The peer window's initial URL.
229 */
230 function createMockPeerWindow(url) {
231 var mockPeer = createMockWindow(url);
232
233 // Update the appendChild method to use a mock frame window.
234 mockPeer.document.body.appendChild = function(el) {
235 assertEquals(String(goog.dom.TagName.IFRAME), el.tagName);
236 mockPeer.frames[el.name] = createMockWindow(el.src);
237 mockPeer.document.body.element.appendChild(el);
238 };
239
240 return mockPeer;
241 }
242
243
244 /**
245 * Creates a mock window.
246 * @param {string} url The window's initial URL.
247 */
248 function createMockWindow(url) {
249 // Create the mock window, document and body.
250 var mockWindow = {};
251 var mockDocument = {};
252 var mockBody = {};
253 var mockLocation = {};
254
255 // Configure the mock window's document body.
256 mockBody.element = goog.dom.createDom(goog.dom.TagName.BODY);
257
258 // Configure the mock window's document.
259 mockDocument.body = mockBody;
260
261 // Configure the mock window's location.
262 mockLocation.href = url;
263 mockLocation.replace = function(value) { mockLocation.href = value; };
264
265 // Configure the mock window.
266 mockWindow.document = mockDocument;
267 mockWindow.frames = {};
268 mockWindow.location = mockLocation;
269 mockWindow.setTimeout = goog.Timer.callOnce;
270
271 return mockWindow;
272 }
273
274
275 /**
276 * Emulates closing the specified window by clearing frames, document and
277 * location.
278 */
279 function closeWindow(targetWindow) {
280 // Close any child frame windows.
281 for (var frameName in targetWindow.frames) {
282 closeWindow(targetWindow.frames[frameName]);
283 }
284
285 // Clear the target window, set closed to true.
286 targetWindow.closed = true;
287 targetWindow.frames = null;
288 targetWindow.document = null;
289 targetWindow.location = null;
290 }