Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / remoting / webapp / base / js / fallback_signal_strategy_unittest.js
blob3d02263cc800925b132d0f3a391e80b7c7bd7319
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 /**
6 * @fileoverview
7 * TODO(garykac): Create interfaces for LogToServer and SignalStrategy.
8 * @suppress {checkTypes|checkVars|reportUnknownTypes|visibility}
9 */
11 (function() {
13 'use strict';
15 /** @constructor */
16 var MockLogToServer = function(/** QUnit.Assert */ assert) {
17 /** @type {(sinon.Spy|Function)} */
18 this.logSignalStrategyProgress = sinon.spy();
19 this.assert_ = assert;
22 /** @type {function(...)} */
23 MockLogToServer.prototype.assertProgress = function() {
24 this.assert_.equal(this.logSignalStrategyProgress.callCount * 2,
25 arguments.length);
26 for (var i = 0; i < this.logSignalStrategyProgress.callCount; ++i) {
27 this.assert_.equal(
28 this.logSignalStrategyProgress.getCall(i).args[0], arguments[2 * i]);
29 this.assert_.equal(this.logSignalStrategyProgress.getCall(i).args[1],
30 arguments[2 * i + 1]);
34 /** @type {(sinon.Spy|function(remoting.SignalStrategy.State))} */
35 var onStateChange = null;
37 /** @type {(sinon.Spy|function(Element):void)} */
38 var onIncomingStanzaCallback = null;
40 /** @type {remoting.FallbackSignalStrategy} */
41 var strategy = null;
43 /** @type {remoting.SignalStrategy} */
44 var primary = null;
46 /** @type {remoting.SignalStrategy} */
47 var secondary = null;
49 /** @type {MockLogToServer} */
50 var logToServer = null;
52 /**
53 * @param {QUnit.Assert} assert
54 * @param {remoting.MockSignalStrategy} baseSignalStrategy
55 * @param {remoting.SignalStrategy.State} state
56 * @param {boolean} expectCallback
58 function setState(assert, baseSignalStrategy, state, expectCallback) {
59 onStateChange.reset();
60 baseSignalStrategy.setStateForTesting(state);
62 if (expectCallback) {
63 assert.equal(onStateChange.callCount, 1);
64 assert.ok(onStateChange.calledWith(state));
65 assert.equal(strategy.getState(), state);
66 } else {
67 assert.ok(!onStateChange.called);
71 QUnit.module('fallback_signal_strategy', {
72 beforeEach: function(/** QUnit.Assert */ assert) {
73 onStateChange = sinon.spy();
74 onIncomingStanzaCallback = sinon.spy();
75 strategy = new remoting.FallbackSignalStrategy(
76 new remoting.MockSignalStrategy('primary-jid',
77 remoting.SignalStrategy.Type.XMPP),
78 new remoting.MockSignalStrategy('secondary-jid',
79 remoting.SignalStrategy.Type.WCS));
80 strategy.setStateChangedCallback(onStateChange);
81 strategy.setIncomingStanzaCallback(onIncomingStanzaCallback);
82 primary = strategy.primary_;
83 addSpies(primary);
84 secondary = strategy.secondary_;
85 addSpies(secondary);
86 logToServer = new MockLogToServer(assert);
87 strategy.logger_ = logToServer;
89 afterEach: function() {
90 onStateChange = null;
91 onIncomingStanzaCallback = null;
92 strategy = null;
93 primary = null;
94 secondary = null;
95 logToServer = null;
97 });
99 /**
100 * @param {remoting.SignalStrategy} strategy
102 function addSpies(strategy) {
103 sinon.spy(strategy, 'connect');
104 sinon.spy(strategy, 'sendMessage');
105 sinon.spy(strategy, 'dispose');
108 QUnit.test('primary succeeds; send & receive routed to it',
109 function(assert) {
110 assert.ok(!onStateChange.called);
111 assert.ok(!primary.connect.called);
112 strategy.connect('server', 'username', 'authToken');
113 assert.ok(primary.connect.calledWith('server', 'username', 'authToken'));
115 setState(assert, primary, remoting.SignalStrategy.State.NOT_CONNECTED,
116 true);
117 setState(assert, primary, remoting.SignalStrategy.State.CONNECTING, true);
118 setState(assert, primary, remoting.SignalStrategy.State.HANDSHAKE, true);
120 setState(assert, primary, remoting.SignalStrategy.State.CONNECTED, true);
121 assert.equal(strategy.getJid(), 'primary-jid');
123 logToServer.assertProgress(
124 remoting.SignalStrategy.Type.XMPP,
125 remoting.FallbackSignalStrategy.Progress.SUCCEEDED);
127 assert.ok(!onIncomingStanzaCallback.called);
128 primary.onIncomingStanzaCallback_('test-receive-primary');
129 secondary.onIncomingStanzaCallback_('test-receive-secondary');
130 assert.ok(onIncomingStanzaCallback.calledOnce);
131 assert.ok(onIncomingStanzaCallback.calledWith('test-receive-primary'));
133 assert.ok(!primary.sendMessage.called);
134 strategy.sendMessage('test-send');
135 assert.ok(primary.sendMessage.calledOnce);
136 assert.ok(primary.sendMessage.calledWith('test-send'));
138 assert.ok(!primary.dispose.called);
139 assert.ok(!secondary.dispose.called);
140 setState(assert, primary, remoting.SignalStrategy.State.CLOSED, true);
141 strategy.dispose();
142 assert.ok(primary.dispose.calledOnce);
143 assert.ok(secondary.dispose.calledOnce);
147 QUnit.test('primary fails; secondary succeeds; send & receive routed to it',
148 function(assert) {
149 assert.ok(!onStateChange.called);
150 assert.ok(!primary.connect.called);
151 strategy.connect('server', 'username', 'authToken');
152 assert.ok(primary.connect.calledWith('server', 'username', 'authToken'));
154 setState(assert, primary, remoting.SignalStrategy.State.NOT_CONNECTED,
155 true);
156 setState(assert, primary, remoting.SignalStrategy.State.CONNECTING, true);
158 assert.ok(!secondary.connect.called);
159 setState(assert, primary, remoting.SignalStrategy.State.FAILED, false);
160 assert.ok(secondary.connect.calledWith('server', 'username', 'authToken'));
162 setState(assert, secondary, remoting.SignalStrategy.State.NOT_CONNECTED,
163 false);
164 setState(assert, secondary, remoting.SignalStrategy.State.CONNECTING,
165 false);
166 setState(assert, secondary, remoting.SignalStrategy.State.HANDSHAKE, true);
168 setState(assert, secondary, remoting.SignalStrategy.State.CONNECTED, true);
169 assert.equal(strategy.getJid(), 'secondary-jid');
171 logToServer.assertProgress(
172 remoting.SignalStrategy.Type.XMPP,
173 remoting.FallbackSignalStrategy.Progress.FAILED,
174 remoting.SignalStrategy.Type.WCS,
175 remoting.FallbackSignalStrategy.Progress.SUCCEEDED);
177 assert.ok(!onIncomingStanzaCallback.called);
178 primary.onIncomingStanzaCallback_('test-receive-primary');
179 secondary.onIncomingStanzaCallback_('test-receive-secondary');
180 assert.ok(onIncomingStanzaCallback.calledOnce);
181 assert.ok(onIncomingStanzaCallback.calledWith('test-receive-secondary'));
183 assert.ok(!secondary.sendMessage.called);
184 strategy.sendMessage('test-send');
185 assert.ok(!primary.sendMessage.called);
186 assert.ok(secondary.sendMessage.calledOnce);
187 assert.ok(secondary.sendMessage.calledWith('test-send'));
191 QUnit.test('primary fails; secondary fails',
192 function(assert) {
193 assert.ok(!onStateChange.called);
194 assert.ok(!primary.connect.called);
195 strategy.connect('server', 'username', 'authToken');
196 assert.ok(primary.connect.calledWith('server', 'username', 'authToken'));
198 setState(assert, primary, remoting.SignalStrategy.State.NOT_CONNECTED,
199 true);
200 assert.ok(!secondary.connect.called);
201 setState(assert, primary, remoting.SignalStrategy.State.CONNECTING, true);
202 setState(assert, primary, remoting.SignalStrategy.State.FAILED, false);
203 assert.ok(secondary.connect.calledWith('server', 'username', 'authToken'));
204 setState(assert, secondary, remoting.SignalStrategy.State.NOT_CONNECTED,
205 false);
206 setState(assert, primary, remoting.SignalStrategy.State.CONNECTING, false);
207 setState(assert, secondary, remoting.SignalStrategy.State.FAILED, true);
211 QUnit.test('primary times out; secondary succeeds',
212 function(assert) {
213 assert.ok(!onStateChange.called);
214 assert.ok(!primary.connect.called);
215 strategy.connect('server', 'username', 'authToken');
216 assert.ok(primary.connect.calledWith('server', 'username', 'authToken'));
218 setState(assert, primary, remoting.SignalStrategy.State.NOT_CONNECTED,
219 true);
220 setState(assert, primary, remoting.SignalStrategy.State.CONNECTING, true);
221 this.clock.tick(strategy.PRIMARY_CONNECT_TIMEOUT_MS_ - 1);
222 assert.ok(!secondary.connect.called);
223 this.clock.tick(1);
224 assert.ok(secondary.connect.calledWith('server', 'username', 'authToken'));
225 setState(assert, secondary, remoting.SignalStrategy.State.NOT_CONNECTED,
226 false);
227 setState(assert, secondary, remoting.SignalStrategy.State.CONNECTING,
228 false);
229 setState(assert, secondary, remoting.SignalStrategy.State.HANDSHAKE, true);
230 setState(assert, secondary, remoting.SignalStrategy.State.CONNECTED, true);
232 setState(assert, secondary, remoting.SignalStrategy.State.CLOSED, true);
233 setState(assert, primary, remoting.SignalStrategy.State.FAILED, false);
235 logToServer.assertProgress(
236 remoting.SignalStrategy.Type.XMPP,
237 remoting.FallbackSignalStrategy.Progress.TIMED_OUT,
238 remoting.SignalStrategy.Type.WCS,
239 remoting.FallbackSignalStrategy.Progress.SUCCEEDED,
240 remoting.SignalStrategy.Type.XMPP,
241 remoting.FallbackSignalStrategy.Progress.FAILED_LATE);
245 QUnit.test('primary times out; secondary fails',
246 function(assert) {
247 assert.ok(!onStateChange.called);
248 assert.ok(!primary.connect.called);
249 strategy.connect('server', 'username', 'authToken');
250 assert.ok(primary.connect.calledWith('server', 'username', 'authToken'));
252 setState(assert, primary, remoting.SignalStrategy.State.NOT_CONNECTED,
253 true);
254 setState(assert, primary, remoting.SignalStrategy.State.CONNECTING, true);
255 this.clock.tick(strategy.PRIMARY_CONNECT_TIMEOUT_MS_ - 1);
256 assert.ok(!secondary.connect.called);
257 this.clock.tick(1);
258 assert.ok(secondary.connect.calledWith('server', 'username', 'authToken'));
259 setState(assert, secondary, remoting.SignalStrategy.State.NOT_CONNECTED,
260 false);
261 setState(assert, secondary, remoting.SignalStrategy.State.CONNECTING,
262 false);
263 setState(assert, secondary, remoting.SignalStrategy.State.FAILED, true);
267 QUnit.test('primary times out; secondary succeeds; primary succeeds late',
268 function(assert) {
269 assert.ok(!onStateChange.called);
270 assert.ok(!primary.connect.called);
271 strategy.connect('server', 'username', 'authToken');
272 assert.ok(primary.connect.calledWith('server', 'username', 'authToken'));
274 setState(assert, primary, remoting.SignalStrategy.State.NOT_CONNECTED,
275 true);
276 setState(assert, primary, remoting.SignalStrategy.State.CONNECTING, true);
277 this.clock.tick(strategy.PRIMARY_CONNECT_TIMEOUT_MS_);
278 assert.ok(secondary.connect.calledWith('server', 'username', 'authToken'));
279 setState(assert, secondary, remoting.SignalStrategy.State.NOT_CONNECTED,
280 false);
281 setState(assert, secondary, remoting.SignalStrategy.State.CONNECTING,
282 false);
283 setState(assert, secondary, remoting.SignalStrategy.State.HANDSHAKE, true);
284 setState(assert, secondary, remoting.SignalStrategy.State.CONNECTED, true);
286 setState(assert, primary, remoting.SignalStrategy.State.HANDSHAKE, false);
287 setState(assert, primary, remoting.SignalStrategy.State.CONNECTED, false);
289 logToServer.assertProgress(
290 remoting.SignalStrategy.Type.XMPP,
291 remoting.FallbackSignalStrategy.Progress.TIMED_OUT,
292 remoting.SignalStrategy.Type.WCS,
293 remoting.FallbackSignalStrategy.Progress.SUCCEEDED,
294 remoting.SignalStrategy.Type.XMPP,
295 remoting.FallbackSignalStrategy.Progress.SUCCEEDED_LATE);
299 })();