Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 (function() { | |
| 6 | |
| 7 'use strict'; | |
| 8 | |
| 9 var ControllableSignalStrategy = function(jid, stateChangeCallback) { | |
| 10 this.jid = jid; | |
| 11 this.stateChangeCallback_ = stateChangeCallback; | |
| 12 this.state_ = null; | |
| 13 this.onIncomingStanzaCallback = function() {}; | |
| 14 this.dispose = sinon.spy(); | |
| 15 this.connect = sinon.spy(); | |
| 16 this.sendMessage = sinon.spy(); | |
| 17 }; | |
| 18 | |
| 19 ControllableSignalStrategy.prototype.setIncomingStanzaCallback = | |
| 20 function(onIncomingStanzaCallback) { | |
| 21 this.onIncomingStanzaCallback = | |
| 22 onIncomingStanzaCallback ? onIncomingStanzaCallback | |
| 23 : function() {}; | |
| 24 } | |
| 25 | |
| 26 ControllableSignalStrategy.prototype.getState = function(message) { | |
| 27 return this.state_; | |
| 28 }; | |
| 29 | |
| 30 ControllableSignalStrategy.prototype.getError = function(message) { | |
| 31 return remoting.Error.UNKNOWN; | |
| 32 }; | |
| 33 | |
| 34 ControllableSignalStrategy.prototype.getJid = function(message) { | |
| 35 return this.jid; | |
| 36 }; | |
| 37 | |
| 38 ControllableSignalStrategy.prototype.setExternalCallbackForTesting = | |
| 39 function(externalCallback) { | |
| 40 this.externalCallback_ = externalCallback; | |
| 41 }; | |
| 42 | |
| 43 ControllableSignalStrategy.prototype.setStateForTesting = | |
| 44 function(state, expectExternalCallback) { | |
| 45 this.state_ = state; | |
| 46 this.externalCallback_.reset(); | |
| 47 this.stateChangeCallback_(state); | |
| 48 if (expectExternalCallback) { | |
| 49 equal(this.externalCallback_.callCount, 1); | |
| 50 ok(this.externalCallback_.calledWith(state)); | |
| 51 equal(strategy.getState(), state); | |
| 52 } else { | |
| 53 ok(!this.externalCallback_.called); | |
| 54 } | |
| 55 }; | |
| 56 | |
| 57 var createControllableSignalStrategy = function(jid, callback) { | |
| 58 return new ControllableSignalStrategy(jid, callback); | |
| 59 }; | |
| 60 | |
| 61 var onStateChange = null; | |
| 62 var onProgressCallback = null; | |
| 63 var onIncomingStanzaCallback = null; | |
| 64 var strategy = null; | |
| 65 var primary = null; | |
| 66 var secondary = null; | |
| 67 | |
| 68 module('fallback_signal_strategy', { | |
| 69 setup: function() { | |
| 70 onStateChange = sinon.spy(); | |
| 71 onProgressCallback = sinon.spy(); | |
| 72 onIncomingStanzaCallback = sinon.spy(); | |
| 73 strategy = new remoting.FallbackSignalStrategy( | |
| 74 createControllableSignalStrategy.bind(null, 'primary-jid'), | |
| 75 createControllableSignalStrategy.bind(null, 'secondary-jid'), | |
| 76 onStateChange, | |
| 77 onProgressCallback); | |
| 78 strategy.setIncomingStanzaCallback(onIncomingStanzaCallback); | |
| 79 primary = strategy.getPrimaryStrategyForTesting_(); | |
| 80 secondary = strategy.getSecondaryStrategyForTesting_(); | |
| 81 primary.setExternalCallbackForTesting(onStateChange); | |
| 82 secondary.setExternalCallbackForTesting(onStateChange); | |
| 83 }, | |
| 84 teardown: function() { | |
| 85 onStateChange = null; | |
| 86 onProgressCallback = null; | |
| 87 onIncomingStanzaCallback = null; | |
| 88 strategy = null; | |
| 89 primary = null; | |
| 90 secondary = null; | |
| 91 }, | |
| 92 // Assert that the progress callback has been called exactly once | |
| 93 // since the last call, and with the specified state. | |
| 94 assertProgress: function(state) { | |
| 95 ok(onProgressCallback.calledOnce); | |
| 96 ok(onProgressCallback.calledWith(state)); | |
| 97 onProgressCallback.reset(); | |
| 98 } | |
| 99 }); | |
| 100 | |
| 101 test('primary succeeds; send & receive routed to it', | |
| 102 function() { | |
| 103 ok(!onStateChange.called); | |
| 104 ok(!primary.connect.called); | |
| 105 strategy.connect('server', 'username', 'authToken'); | |
| 106 ok(primary.connect.calledWith('server', 'username', 'authToken')); | |
| 107 | |
| 108 primary.setStateForTesting(remoting.SignalStrategy.State.NOT_CONNECTED, | |
| 109 true); | |
| 110 primary.setStateForTesting(remoting.SignalStrategy.State.CONNECTING, true); | |
| 111 primary.setStateForTesting(remoting.SignalStrategy.State.HANDSHAKE, true); | |
| 112 | |
| 113 ok(!onProgressCallback.called); | |
| 114 primary.setStateForTesting(remoting.SignalStrategy.State.CONNECTED, true); | |
| 115 this.assertProgress( | |
| 116 remoting.FallbackSignalStrategy.Result.PRIMARY_SUCCEEDED); | |
| 117 equal(strategy.getJid(), 'primary-jid'); | |
| 118 | |
| 119 ok(!onIncomingStanzaCallback.called); | |
| 120 primary.onIncomingStanzaCallback('test-receive-primary'); | |
| 121 secondary.onIncomingStanzaCallback('test-receive-secondary'); | |
| 122 ok(onIncomingStanzaCallback.calledOnce); | |
| 123 ok(onIncomingStanzaCallback.calledWith('test-receive-primary')); | |
| 124 | |
| 125 ok(!primary.sendMessage.called); | |
| 126 strategy.sendMessage('test-send'); | |
| 127 ok(primary.sendMessage.calledOnce); | |
| 128 ok(primary.sendMessage.calledWith('test-send')); | |
| 129 | |
| 130 ok(!primary.dispose.called); | |
| 131 ok(!secondary.dispose.called); | |
| 132 primary.setStateForTesting(remoting.SignalStrategy.State.CLOSED, true); | |
| 133 strategy.dispose(); | |
| 134 ok(primary.dispose.calledOnce); | |
| 135 ok(secondary.dispose.calledOnce); | |
| 136 } | |
| 137 ); | |
| 138 | |
| 139 test('primary fails; secondary succeeds; send & receive routed to it', | |
|
kelvinp
2015/01/13 19:22:48
Maybe worthwhile to to add verify that the onState
Jamie
2015/01/14 01:36:26
setStateForTesting already asserts that, based on
kelvinp
2015/01/14 19:46:22
You are right. I must have misread the code earli
| |
| 140 function() { | |
| 141 ok(!onStateChange.called); | |
| 142 ok(!primary.connect.called); | |
| 143 strategy.connect('server', 'username', 'authToken'); | |
| 144 ok(primary.connect.calledWith('server', 'username', 'authToken')); | |
| 145 | |
| 146 primary.setStateForTesting(remoting.SignalStrategy.State.NOT_CONNECTED, | |
| 147 true); | |
| 148 primary.setStateForTesting(remoting.SignalStrategy.State.CONNECTING, true); | |
| 149 | |
| 150 ok(!onProgressCallback.called); | |
| 151 ok(!secondary.connect.called); | |
| 152 primary.setStateForTesting(remoting.SignalStrategy.State.FAILED, false); | |
| 153 ok(secondary.connect.calledWith('server', 'username', 'authToken')); | |
| 154 this.assertProgress(remoting.FallbackSignalStrategy.Result.PRIMARY_FAILED); | |
| 155 | |
| 156 secondary.setStateForTesting(remoting.SignalStrategy.State.NOT_CONNECTED, | |
| 157 false); | |
| 158 secondary.setStateForTesting(remoting.SignalStrategy.State.CONNECTING, | |
| 159 false); | |
| 160 secondary.setStateForTesting(remoting.SignalStrategy.State.HANDSHAKE, true); | |
| 161 | |
| 162 ok(!onProgressCallback.called); | |
| 163 secondary.setStateForTesting(remoting.SignalStrategy.State.CONNECTED, true); | |
| 164 this.assertProgress( | |
| 165 remoting.FallbackSignalStrategy.Result.SECONDARY_SUCCEEDED); | |
| 166 equal(strategy.getJid(), 'secondary-jid'); | |
| 167 | |
| 168 ok(!onIncomingStanzaCallback.called); | |
| 169 primary.onIncomingStanzaCallback('test-receive-primary'); | |
| 170 secondary.onIncomingStanzaCallback('test-receive-secondary'); | |
| 171 ok(onIncomingStanzaCallback.calledOnce); | |
| 172 ok(onIncomingStanzaCallback.calledWith('test-receive-secondary')); | |
| 173 | |
| 174 ok(!secondary.sendMessage.called); | |
| 175 strategy.sendMessage('test-send'); | |
| 176 ok(!primary.sendMessage.called); | |
| 177 ok(secondary.sendMessage.calledOnce); | |
| 178 ok(secondary.sendMessage.calledWith('test-send')); | |
| 179 } | |
| 180 ); | |
| 181 | |
| 182 test('primary fails; secondary fails', | |
| 183 function() { | |
| 184 ok(!onStateChange.called); | |
| 185 ok(!primary.connect.called); | |
| 186 strategy.connect('server', 'username', 'authToken'); | |
| 187 ok(primary.connect.calledWith('server', 'username', 'authToken')); | |
| 188 | |
| 189 primary.setStateForTesting(remoting.SignalStrategy.State.NOT_CONNECTED, | |
| 190 true); | |
| 191 ok(!onProgressCallback.called); | |
| 192 ok(!secondary.connect.called); | |
| 193 primary.setStateForTesting(remoting.SignalStrategy.State.CONNECTING, true); | |
| 194 primary.setStateForTesting(remoting.SignalStrategy.State.FAILED, false); | |
| 195 ok(secondary.connect.calledWith('server', 'username', 'authToken')); | |
| 196 this.assertProgress(remoting.FallbackSignalStrategy.Result.PRIMARY_FAILED); | |
| 197 secondary.setStateForTesting(remoting.SignalStrategy.State.NOT_CONNECTED, | |
| 198 false); | |
| 199 primary.setStateForTesting(remoting.SignalStrategy.State.CONNECTING, false); | |
| 200 secondary.setStateForTesting(remoting.SignalStrategy.State.FAILED, true); | |
| 201 this.assertProgress( | |
| 202 remoting.FallbackSignalStrategy.Result.SECONDARY_FAILED); | |
| 203 } | |
| 204 ); | |
| 205 | |
| 206 test('primary times out; secondary succeeds', | |
| 207 function() { | |
| 208 ok(!onStateChange.called); | |
| 209 ok(!primary.connect.called); | |
| 210 strategy.connect('server', 'username', 'authToken'); | |
| 211 ok(primary.connect.calledWith('server', 'username', 'authToken')); | |
| 212 | |
| 213 primary.setStateForTesting(remoting.SignalStrategy.State.NOT_CONNECTED, | |
| 214 true); | |
| 215 primary.setStateForTesting(remoting.SignalStrategy.State.CONNECTING, true); | |
| 216 this.clock.tick(strategy.PRIMARY_CONNECT_TIMEOUT_MS_ - 1); | |
| 217 ok(!secondary.connect.called); | |
| 218 ok(!onProgressCallback.called); | |
| 219 this.clock.tick(1); | |
| 220 ok(secondary.connect.calledWith('server', 'username', 'authToken')); | |
| 221 this.assertProgress( | |
| 222 remoting.FallbackSignalStrategy.Result.PRIMARY_TIMED_OUT); | |
| 223 secondary.setStateForTesting(remoting.SignalStrategy.State.NOT_CONNECTED, | |
| 224 false); | |
| 225 secondary.setStateForTesting(remoting.SignalStrategy.State.CONNECTING, | |
| 226 false); | |
| 227 secondary.setStateForTesting(remoting.SignalStrategy.State.HANDSHAKE, true); | |
| 228 secondary.setStateForTesting(remoting.SignalStrategy.State.CONNECTED, true); | |
| 229 this.assertProgress( | |
| 230 remoting.FallbackSignalStrategy.Result.SECONDARY_SUCCEEDED); | |
| 231 secondary.setStateForTesting(remoting.SignalStrategy.State.CLOSED, true); | |
| 232 primary.setStateForTesting(remoting.SignalStrategy.State.FAILED, false); | |
| 233 this.assertProgress( | |
| 234 remoting.FallbackSignalStrategy.Result.PRIMARY_FAILED_LATE); | |
| 235 } | |
| 236 ); | |
| 237 | |
| 238 test('primary times out; secondary fails', | |
| 239 function() { | |
| 240 ok(!onStateChange.called); | |
| 241 ok(!primary.connect.called); | |
| 242 strategy.connect('server', 'username', 'authToken'); | |
| 243 ok(primary.connect.calledWith('server', 'username', 'authToken')); | |
| 244 | |
| 245 primary.setStateForTesting(remoting.SignalStrategy.State.NOT_CONNECTED, | |
| 246 true); | |
| 247 primary.setStateForTesting(remoting.SignalStrategy.State.CONNECTING, true); | |
| 248 this.clock.tick(strategy.PRIMARY_CONNECT_TIMEOUT_MS_ - 1); | |
| 249 ok(!secondary.connect.called); | |
| 250 ok(!onProgressCallback.called); | |
| 251 this.clock.tick(1); | |
| 252 ok(secondary.connect.calledWith('server', 'username', 'authToken')); | |
| 253 this.assertProgress( | |
| 254 remoting.FallbackSignalStrategy.Result.PRIMARY_TIMED_OUT); | |
| 255 secondary.setStateForTesting(remoting.SignalStrategy.State.NOT_CONNECTED, | |
| 256 false); | |
| 257 secondary.setStateForTesting(remoting.SignalStrategy.State.CONNECTING, | |
| 258 false); | |
| 259 secondary.setStateForTesting(remoting.SignalStrategy.State.FAILED, true); | |
| 260 this.assertProgress( | |
| 261 remoting.FallbackSignalStrategy.Result.SECONDARY_FAILED); | |
| 262 } | |
| 263 ); | |
| 264 | |
| 265 test('primary times out; secondary succeeds; primary succeeds late', | |
| 266 function() { | |
| 267 ok(!onStateChange.called); | |
| 268 ok(!primary.connect.called); | |
| 269 strategy.connect('server', 'username', 'authToken'); | |
| 270 ok(primary.connect.calledWith('server', 'username', 'authToken')); | |
| 271 | |
| 272 primary.setStateForTesting(remoting.SignalStrategy.State.NOT_CONNECTED, | |
| 273 true); | |
| 274 primary.setStateForTesting(remoting.SignalStrategy.State.CONNECTING, true); | |
| 275 this.clock.tick(strategy.PRIMARY_CONNECT_TIMEOUT_MS_); | |
| 276 ok(secondary.connect.calledWith('server', 'username', 'authToken')); | |
| 277 this.assertProgress( | |
| 278 remoting.FallbackSignalStrategy.Result.PRIMARY_TIMED_OUT); | |
| 279 secondary.setStateForTesting(remoting.SignalStrategy.State.NOT_CONNECTED, | |
| 280 false); | |
| 281 secondary.setStateForTesting(remoting.SignalStrategy.State.CONNECTING, | |
| 282 false); | |
| 283 secondary.setStateForTesting(remoting.SignalStrategy.State.HANDSHAKE, true); | |
| 284 secondary.setStateForTesting(remoting.SignalStrategy.State.CONNECTED, true); | |
| 285 this.assertProgress( | |
| 286 remoting.FallbackSignalStrategy.Result.SECONDARY_SUCCEEDED); | |
| 287 primary.setStateForTesting(remoting.SignalStrategy.State.HANDSHAKE, false); | |
| 288 primary.setStateForTesting(remoting.SignalStrategy.State.CONNECTED, false); | |
| 289 this.assertProgress( | |
| 290 remoting.FallbackSignalStrategy.Result.PRIMARY_SUCCEEDED_LATE); | |
| 291 } | |
| 292 ); | |
| 293 | |
| 294 })(); | |
| OLD | NEW |