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.
10 var MockLogToServer = function() {
11 this.logSignalStrategyProgress = sinon.spy();
14 MockLogToServer.prototype.assertProgress = function() {
15 equal(this.logSignalStrategyProgress.callCount * 2, arguments.length);
16 for (var i = 0; i < this.logSignalStrategyProgress.callCount; ++i) {
17 equal(this.logSignalStrategyProgress.getCall(i).args[0], arguments[2 * i]);
18 equal(this.logSignalStrategyProgress.getCall(i).args[1],
19 arguments[2 * i + 1]);
23 var onStateChange = null;
24 var onIncomingStanzaCallback = null;
28 var logToServer = null;
30 function setState(baseSignalStrategy, state, expectCallback) {
31 onStateChange.reset();
32 baseSignalStrategy.setStateForTesting(state);
35 equal(onStateChange.callCount, 1);
36 ok(onStateChange.calledWith(state));
37 equal(strategy.getState(), state);
39 ok(!onStateChange.called);
43 module('fallback_signal_strategy', {
45 onStateChange = sinon.spy();
46 onIncomingStanzaCallback = sinon.spy();
47 strategy = new remoting.FallbackSignalStrategy(
48 new remoting.MockSignalStrategy('primary-jid',
49 remoting.SignalStrategy.Type.XMPP),
50 new remoting.MockSignalStrategy('secondary-jid',
51 remoting.SignalStrategy.Type.WCS));
52 strategy.setStateChangedCallback(onStateChange);
53 strategy.setIncomingStanzaCallback(onIncomingStanzaCallback);
54 primary = strategy.primary_;
55 secondary = strategy.secondary_;
56 logToServer = new MockLogToServer();
58 teardown: function() {
60 onIncomingStanzaCallback = null;
68 test('primary succeeds; send & receive routed to it',
70 ok(!onStateChange.called);
71 ok(!primary.connect.called);
72 strategy.connect('server', 'username', 'authToken');
73 ok(primary.connect.calledWith('server', 'username', 'authToken'));
75 setState(primary, remoting.SignalStrategy.State.NOT_CONNECTED, true);
76 setState(primary, remoting.SignalStrategy.State.CONNECTING, true);
77 setState(primary, remoting.SignalStrategy.State.HANDSHAKE, true);
79 setState(primary, remoting.SignalStrategy.State.CONNECTED, true);
80 equal(strategy.getJid(), 'primary-jid');
82 strategy.sendConnectionSetupResults(logToServer);
83 logToServer.assertProgress(
84 remoting.SignalStrategy.Type.XMPP,
85 remoting.FallbackSignalStrategy.Progress.SUCCEEDED);
87 ok(!onIncomingStanzaCallback.called);
88 primary.onIncomingStanzaCallback_('test-receive-primary');
89 secondary.onIncomingStanzaCallback_('test-receive-secondary');
90 ok(onIncomingStanzaCallback.calledOnce);
91 ok(onIncomingStanzaCallback.calledWith('test-receive-primary'));
93 ok(!primary.sendMessage.called);
94 strategy.sendMessage('test-send');
95 ok(primary.sendMessage.calledOnce);
96 ok(primary.sendMessage.calledWith('test-send'));
98 ok(!primary.dispose.called);
99 ok(!secondary.dispose.called);
100 setState(primary, remoting.SignalStrategy.State.CLOSED, true);
102 ok(primary.dispose.calledOnce);
103 ok(secondary.dispose.calledOnce);
107 test('primary fails; secondary succeeds; send & receive routed to it',
109 ok(!onStateChange.called);
110 ok(!primary.connect.called);
111 strategy.connect('server', 'username', 'authToken');
112 ok(primary.connect.calledWith('server', 'username', 'authToken'));
114 setState(primary, remoting.SignalStrategy.State.NOT_CONNECTED,
116 setState(primary, remoting.SignalStrategy.State.CONNECTING, true);
118 ok(!secondary.connect.called);
119 setState(primary, remoting.SignalStrategy.State.FAILED, false);
120 ok(secondary.connect.calledWith('server', 'username', 'authToken'));
122 setState(secondary, remoting.SignalStrategy.State.NOT_CONNECTED, false);
123 setState(secondary, remoting.SignalStrategy.State.CONNECTING, false);
124 setState(secondary, remoting.SignalStrategy.State.HANDSHAKE, true);
126 setState(secondary, remoting.SignalStrategy.State.CONNECTED, true);
127 equal(strategy.getJid(), 'secondary-jid');
129 strategy.sendConnectionSetupResults(logToServer);
130 logToServer.assertProgress(
131 remoting.SignalStrategy.Type.XMPP,
132 remoting.FallbackSignalStrategy.Progress.FAILED,
133 remoting.SignalStrategy.Type.WCS,
134 remoting.FallbackSignalStrategy.Progress.SUCCEEDED);
136 ok(!onIncomingStanzaCallback.called);
137 primary.onIncomingStanzaCallback_('test-receive-primary');
138 secondary.onIncomingStanzaCallback_('test-receive-secondary');
139 ok(onIncomingStanzaCallback.calledOnce);
140 ok(onIncomingStanzaCallback.calledWith('test-receive-secondary'));
142 ok(!secondary.sendMessage.called);
143 strategy.sendMessage('test-send');
144 ok(!primary.sendMessage.called);
145 ok(secondary.sendMessage.calledOnce);
146 ok(secondary.sendMessage.calledWith('test-send'));
150 test('primary fails; secondary fails',
152 ok(!onStateChange.called);
153 ok(!primary.connect.called);
154 strategy.connect('server', 'username', 'authToken');
155 ok(primary.connect.calledWith('server', 'username', 'authToken'));
157 setState(primary, remoting.SignalStrategy.State.NOT_CONNECTED, true);
158 ok(!secondary.connect.called);
159 setState(primary, remoting.SignalStrategy.State.CONNECTING, true);
160 setState(primary, remoting.SignalStrategy.State.FAILED, false);
161 ok(secondary.connect.calledWith('server', 'username', 'authToken'));
162 setState(secondary, remoting.SignalStrategy.State.NOT_CONNECTED, false);
163 setState(primary, remoting.SignalStrategy.State.CONNECTING, false);
164 setState(secondary, remoting.SignalStrategy.State.FAILED, true);
168 test('primary times out; secondary succeeds',
170 ok(!onStateChange.called);
171 ok(!primary.connect.called);
172 strategy.connect('server', 'username', 'authToken');
173 ok(primary.connect.calledWith('server', 'username', 'authToken'));
175 setState(primary, remoting.SignalStrategy.State.NOT_CONNECTED,
177 setState(primary, remoting.SignalStrategy.State.CONNECTING, true);
178 this.clock.tick(strategy.PRIMARY_CONNECT_TIMEOUT_MS_ - 1);
179 ok(!secondary.connect.called);
181 ok(secondary.connect.calledWith('server', 'username', 'authToken'));
182 setState(secondary, remoting.SignalStrategy.State.NOT_CONNECTED, false);
183 setState(secondary, remoting.SignalStrategy.State.CONNECTING, false);
184 setState(secondary, remoting.SignalStrategy.State.HANDSHAKE, true);
185 setState(secondary, remoting.SignalStrategy.State.CONNECTED, true);
186 strategy.sendConnectionSetupResults(logToServer);
188 setState(secondary, remoting.SignalStrategy.State.CLOSED, true);
189 setState(primary, remoting.SignalStrategy.State.FAILED, false);
191 logToServer.assertProgress(
192 remoting.SignalStrategy.Type.XMPP,
193 remoting.FallbackSignalStrategy.Progress.TIMED_OUT,
194 remoting.SignalStrategy.Type.WCS,
195 remoting.FallbackSignalStrategy.Progress.SUCCEEDED,
196 remoting.SignalStrategy.Type.XMPP,
197 remoting.FallbackSignalStrategy.Progress.FAILED_LATE);
201 test('primary times out; secondary fails',
203 ok(!onStateChange.called);
204 ok(!primary.connect.called);
205 strategy.connect('server', 'username', 'authToken');
206 ok(primary.connect.calledWith('server', 'username', 'authToken'));
208 setState(primary, remoting.SignalStrategy.State.NOT_CONNECTED,
210 setState(primary, remoting.SignalStrategy.State.CONNECTING, true);
211 this.clock.tick(strategy.PRIMARY_CONNECT_TIMEOUT_MS_ - 1);
212 ok(!secondary.connect.called);
214 ok(secondary.connect.calledWith('server', 'username', 'authToken'));
215 setState(secondary, remoting.SignalStrategy.State.NOT_CONNECTED, false);
216 setState(secondary, remoting.SignalStrategy.State.CONNECTING, false);
217 setState(secondary, remoting.SignalStrategy.State.FAILED, true);
221 test('primary times out; secondary succeeds; primary succeeds late',
223 ok(!onStateChange.called);
224 ok(!primary.connect.called);
225 strategy.connect('server', 'username', 'authToken');
226 ok(primary.connect.calledWith('server', 'username', 'authToken'));
228 setState(primary, remoting.SignalStrategy.State.NOT_CONNECTED,
230 setState(primary, remoting.SignalStrategy.State.CONNECTING, true);
231 this.clock.tick(strategy.PRIMARY_CONNECT_TIMEOUT_MS_);
232 ok(secondary.connect.calledWith('server', 'username', 'authToken'));
233 setState(secondary, remoting.SignalStrategy.State.NOT_CONNECTED, false);
234 setState(secondary, remoting.SignalStrategy.State.CONNECTING, false);
235 setState(secondary, remoting.SignalStrategy.State.HANDSHAKE, true);
236 setState(secondary, remoting.SignalStrategy.State.CONNECTED, true);
237 strategy.sendConnectionSetupResults(logToServer);
239 setState(primary, remoting.SignalStrategy.State.HANDSHAKE, false);
240 setState(primary, remoting.SignalStrategy.State.CONNECTED, false);
242 logToServer.assertProgress(
243 remoting.SignalStrategy.Type.XMPP,
244 remoting.FallbackSignalStrategy.Progress.TIMED_OUT,
245 remoting.SignalStrategy.Type.WCS,
246 remoting.FallbackSignalStrategy.Progress.SUCCEEDED,
247 remoting.SignalStrategy.Type.XMPP,
248 remoting.FallbackSignalStrategy.Progress.SUCCEEDED_LATE);