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);
88 afterEach: function() {
90 onIncomingStanzaCallback = null;
99 * @param {remoting.SignalStrategy} strategy
101 function addSpies(strategy) {
102 sinon.spy(strategy, 'connect');
103 sinon.spy(strategy, 'sendMessage');
104 sinon.spy(strategy, 'dispose');
107 QUnit.test('primary succeeds; send & receive routed to it',
109 assert.ok(!onStateChange.called);
110 assert.ok(!primary.connect.called);
111 strategy.connect('server', 'username', 'authToken');
112 assert.ok(primary.connect.calledWith('server', 'username', 'authToken'));
114 setState(assert, primary, remoting.SignalStrategy.State.NOT_CONNECTED,
116 setState(assert, primary, remoting.SignalStrategy.State.CONNECTING, true);
117 setState(assert, primary, remoting.SignalStrategy.State.HANDSHAKE, true);
119 setState(assert, primary, remoting.SignalStrategy.State.CONNECTED, true);
120 assert.equal(strategy.getJid(), 'primary-jid');
122 strategy.sendConnectionSetupResults(logToServer);
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 strategy.sendConnectionSetupResults(logToServer);
172 logToServer.assertProgress(
173 remoting.SignalStrategy.Type.XMPP,
174 remoting.FallbackSignalStrategy.Progress.FAILED,
175 remoting.SignalStrategy.Type.WCS,
176 remoting.FallbackSignalStrategy.Progress.SUCCEEDED);
178 assert.ok(!onIncomingStanzaCallback.called);
179 primary.onIncomingStanzaCallback_('test-receive-primary');
180 secondary.onIncomingStanzaCallback_('test-receive-secondary');
181 assert.ok(onIncomingStanzaCallback.calledOnce);
182 assert.ok(onIncomingStanzaCallback.calledWith('test-receive-secondary'));
184 assert.ok(!secondary.sendMessage.called);
185 strategy.sendMessage('test-send');
186 assert.ok(!primary.sendMessage.called);
187 assert.ok(secondary.sendMessage.calledOnce);
188 assert.ok(secondary.sendMessage.calledWith('test-send'));
192 QUnit.test('primary fails; secondary fails',
194 assert.ok(!onStateChange.called);
195 assert.ok(!primary.connect.called);
196 strategy.connect('server', 'username', 'authToken');
197 assert.ok(primary.connect.calledWith('server', 'username', 'authToken'));
199 setState(assert, primary, remoting.SignalStrategy.State.NOT_CONNECTED,
201 assert.ok(!secondary.connect.called);
202 setState(assert, primary, remoting.SignalStrategy.State.CONNECTING, true);
203 setState(assert, primary, remoting.SignalStrategy.State.FAILED, false);
204 assert.ok(secondary.connect.calledWith('server', 'username', 'authToken'));
205 setState(assert, secondary, remoting.SignalStrategy.State.NOT_CONNECTED,
207 setState(assert, primary, remoting.SignalStrategy.State.CONNECTING, false);
208 setState(assert, secondary, remoting.SignalStrategy.State.FAILED, true);
212 QUnit.test('primary times out; secondary succeeds',
214 assert.ok(!onStateChange.called);
215 assert.ok(!primary.connect.called);
216 strategy.connect('server', 'username', 'authToken');
217 assert.ok(primary.connect.calledWith('server', 'username', 'authToken'));
219 setState(assert, primary, remoting.SignalStrategy.State.NOT_CONNECTED,
221 setState(assert, primary, remoting.SignalStrategy.State.CONNECTING, true);
222 this.clock.tick(strategy.PRIMARY_CONNECT_TIMEOUT_MS_ - 1);
223 assert.ok(!secondary.connect.called);
225 assert.ok(secondary.connect.calledWith('server', 'username', 'authToken'));
226 setState(assert, secondary, remoting.SignalStrategy.State.NOT_CONNECTED,
228 setState(assert, secondary, remoting.SignalStrategy.State.CONNECTING,
230 setState(assert, secondary, remoting.SignalStrategy.State.HANDSHAKE, true);
231 setState(assert, secondary, remoting.SignalStrategy.State.CONNECTED, true);
232 strategy.sendConnectionSetupResults(logToServer);
234 setState(assert, secondary, remoting.SignalStrategy.State.CLOSED, true);
235 setState(assert, primary, remoting.SignalStrategy.State.FAILED, false);
237 logToServer.assertProgress(
238 remoting.SignalStrategy.Type.XMPP,
239 remoting.FallbackSignalStrategy.Progress.TIMED_OUT,
240 remoting.SignalStrategy.Type.WCS,
241 remoting.FallbackSignalStrategy.Progress.SUCCEEDED,
242 remoting.SignalStrategy.Type.XMPP,
243 remoting.FallbackSignalStrategy.Progress.FAILED_LATE);
247 QUnit.test('primary times out; secondary fails',
249 assert.ok(!onStateChange.called);
250 assert.ok(!primary.connect.called);
251 strategy.connect('server', 'username', 'authToken');
252 assert.ok(primary.connect.calledWith('server', 'username', 'authToken'));
254 setState(assert, primary, remoting.SignalStrategy.State.NOT_CONNECTED,
256 setState(assert, primary, remoting.SignalStrategy.State.CONNECTING, true);
257 this.clock.tick(strategy.PRIMARY_CONNECT_TIMEOUT_MS_ - 1);
258 assert.ok(!secondary.connect.called);
260 assert.ok(secondary.connect.calledWith('server', 'username', 'authToken'));
261 setState(assert, secondary, remoting.SignalStrategy.State.NOT_CONNECTED,
263 setState(assert, secondary, remoting.SignalStrategy.State.CONNECTING,
265 setState(assert, secondary, remoting.SignalStrategy.State.FAILED, true);
269 QUnit.test('primary times out; secondary succeeds; primary succeeds late',
271 assert.ok(!onStateChange.called);
272 assert.ok(!primary.connect.called);
273 strategy.connect('server', 'username', 'authToken');
274 assert.ok(primary.connect.calledWith('server', 'username', 'authToken'));
276 setState(assert, primary, remoting.SignalStrategy.State.NOT_CONNECTED,
278 setState(assert, primary, remoting.SignalStrategy.State.CONNECTING, true);
279 this.clock.tick(strategy.PRIMARY_CONNECT_TIMEOUT_MS_);
280 assert.ok(secondary.connect.calledWith('server', 'username', 'authToken'));
281 setState(assert, secondary, remoting.SignalStrategy.State.NOT_CONNECTED,
283 setState(assert, secondary, remoting.SignalStrategy.State.CONNECTING,
285 setState(assert, secondary, remoting.SignalStrategy.State.HANDSHAKE, true);
286 setState(assert, secondary, remoting.SignalStrategy.State.CONNECTED, true);
287 strategy.sendConnectionSetupResults(logToServer);
289 setState(assert, primary, remoting.SignalStrategy.State.HANDSHAKE, false);
290 setState(assert, primary, remoting.SignalStrategy.State.CONNECTED, false);
292 logToServer.assertProgress(
293 remoting.SignalStrategy.Type.XMPP,
294 remoting.FallbackSignalStrategy.Progress.TIMED_OUT,
295 remoting.SignalStrategy.Type.WCS,
296 remoting.FallbackSignalStrategy.Progress.SUCCEEDED,
297 remoting.SignalStrategy.Type.XMPP,
298 remoting.FallbackSignalStrategy.Progress.SUCCEEDED_LATE);