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.
7 * TODO(garykac): Create interfaces for LogToServer and SignalStrategy.
8 * @suppress {checkTypes|checkVars|reportUnknownTypes|visibility}
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,
26 for (var i = 0; i < this.logSignalStrategyProgress.callCount; ++i) {
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} */
43 /** @type {remoting.SignalStrategy} */
46 /** @type {remoting.SignalStrategy} */
49 /** @type {MockLogToServer} */
50 var logToServer = null;
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);
63 assert.equal(onStateChange.callCount, 1);
64 assert.ok(onStateChange.calledWith(state));
65 assert.equal(strategy.getState(), state);
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_;
84 secondary = strategy.secondary_;
86 logToServer = new MockLogToServer(assert);
87 strategy.logger_ = logToServer;
89 afterEach: function() {
91 onIncomingStanzaCallback = null;
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',
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,
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);
142 assert.ok(primary.dispose.calledOnce);
143 assert.ok(secondary.dispose.calledOnce);
147 QUnit.test('primary fails; secondary succeeds; send & receive routed to it',
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,
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,
164 setState(assert, secondary, remoting.SignalStrategy.State.CONNECTING,
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',
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,
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,
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',
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,
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);
224 assert.ok(secondary.connect.calledWith('server', 'username', 'authToken'));
225 setState(assert, secondary, remoting.SignalStrategy.State.NOT_CONNECTED,
227 setState(assert, secondary, remoting.SignalStrategy.State.CONNECTING,
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',
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,
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);
258 assert.ok(secondary.connect.calledWith('server', 'username', 'authToken'));
259 setState(assert, secondary, remoting.SignalStrategy.State.NOT_CONNECTED,
261 setState(assert, secondary, remoting.SignalStrategy.State.CONNECTING,
263 setState(assert, secondary, remoting.SignalStrategy.State.FAILED, true);
267 QUnit.test('primary times out; secondary succeeds; primary succeeds late',
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,
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,
281 setState(assert, secondary, remoting.SignalStrategy.State.CONNECTING,
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);