Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / remoting / webapp / base / js / fallback_signal_strategy_unittest.js
blob670aa8f11ec17e78749470f1a2820426745b6576
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]);
31   }
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
57  */
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);
68   }
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   },
88   afterEach: function() {
89     onStateChange = null;
90     onIncomingStanzaCallback = null;
91     strategy = null;
92     primary = null;
93     secondary = null;
94     logToServer = null;
95   },
96 });
98 /**
99  * @param {remoting.SignalStrategy} strategy
100  */
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',
108   function(assert) {
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,
115              true);
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);
141     strategy.dispose();
142     assert.ok(primary.dispose.calledOnce);
143     assert.ok(secondary.dispose.calledOnce);
144   }
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     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'));
189   }
192 QUnit.test('primary fails; secondary fails',
193   function(assert) {
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,
200              true);
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,
206              false);
207     setState(assert, primary, remoting.SignalStrategy.State.CONNECTING, false);
208     setState(assert, secondary, remoting.SignalStrategy.State.FAILED, true);
209   }
212 QUnit.test('primary times out; secondary succeeds',
213   function(assert) {
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,
220                                true);
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);
224     this.clock.tick(1);
225     assert.ok(secondary.connect.calledWith('server', 'username', 'authToken'));
226     setState(assert, secondary, remoting.SignalStrategy.State.NOT_CONNECTED,
227              false);
228     setState(assert, secondary, remoting.SignalStrategy.State.CONNECTING,
229              false);
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);
244   }
247 QUnit.test('primary times out; secondary fails',
248   function(assert) {
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,
255                                true);
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);
259     this.clock.tick(1);
260     assert.ok(secondary.connect.calledWith('server', 'username', 'authToken'));
261     setState(assert, secondary, remoting.SignalStrategy.State.NOT_CONNECTED,
262              false);
263     setState(assert, secondary, remoting.SignalStrategy.State.CONNECTING,
264              false);
265     setState(assert, secondary, remoting.SignalStrategy.State.FAILED, true);
266   }
269 QUnit.test('primary times out; secondary succeeds; primary succeeds late',
270   function(assert) {
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,
277                                true);
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,
282              false);
283     setState(assert, secondary, remoting.SignalStrategy.State.CONNECTING,
284              false);
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);
299   }
302 })();