| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 (function() { | 5 (function() { |
| 6 | 6 |
| 7 'use strict'; | 7 'use strict'; |
| 8 | 8 |
| 9 var testUsername = 'testUsername@gmail.com'; | 9 var testUsername = 'testUsername@gmail.com'; |
| 10 var testToken = 'testToken'; | 10 var testToken = 'testToken'; |
| 11 var socketId = 3; | 11 var socketId = 3; |
| 12 | 12 |
| 13 var onStanzaStr = null; | 13 var onStanzaStr = null; |
| 14 | 14 |
| 15 /** @type {remoting.XmppConnection} */ | 15 /** @type {remoting.XmppConnection} */ |
| 16 var connection = null; | 16 var connection = null; |
| 17 | 17 |
| 18 /** @type {remoting.TcpSocket} */ | 18 /** @type {remoting.TcpSocket} */ |
| 19 var socket = null; | 19 var socket = null; |
| 20 | 20 |
| 21 var stateChangeHandler = function(/** remoting.SignalStrategy.State */ state) {} | 21 var stateChangeHandler = function(/** remoting.SignalStrategy.State */ state) {} |
| 22 | 22 |
| 23 function onStateChange(/** remoting.SignalStrategy.State */ state) { | 23 function onStateChange(/** remoting.SignalStrategy.State */ state) { |
| 24 stateChangeHandler(state) | 24 stateChangeHandler(state) |
| 25 }; | 25 }; |
| 26 | 26 |
| 27 /** @returns {Promise} */ | 27 /** |
| 28 function expectNextState(/** remoting.SignalStrategy.State */ expectedState) { | 28 * @param {QUnit.Assert} assert |
| 29 * @param {remoting.SignalStrategy.State} expectedState |
| 30 * @returns {Promise} |
| 31 */ |
| 32 function expectNextState(assert, expectedState) { |
| 29 return new Promise(function(resolve, reject) { | 33 return new Promise(function(resolve, reject) { |
| 30 stateChangeHandler = function(/** remoting.SignalStrategy.State */ state) { | 34 stateChangeHandler = function(/** remoting.SignalStrategy.State */ state) { |
| 31 QUnit.equal(state, expectedState); | 35 assert.equal(state, expectedState); |
| 32 QUnit.equal(connection.getState(), expectedState); | 36 assert.equal(connection.getState(), expectedState); |
| 33 resolve(0); | 37 resolve(0); |
| 34 } | 38 } |
| 35 }); | 39 }); |
| 36 } | 40 } |
| 37 | 41 |
| 38 module('XmppConnection', { | 42 QUnit.module('XmppConnection', { |
| 39 setup: function() { | 43 beforeEach: function() { |
| 40 onStanzaStr = sinon.spy(); | 44 onStanzaStr = sinon.spy(); |
| 41 /** @param {Element} stanza */ | 45 /** @param {Element} stanza */ |
| 42 function onStanza(stanza) { | 46 function onStanza(stanza) { |
| 43 onStanzaStr(new XMLSerializer().serializeToString(stanza)); | 47 onStanzaStr(new XMLSerializer().serializeToString(stanza)); |
| 44 } | 48 } |
| 45 | 49 |
| 46 socket = /** @type{remoting.TcpSocket} */ | 50 socket = /** @type{remoting.TcpSocket} */ |
| 47 (sinon.createStubInstance(remoting.TcpSocket)); | 51 (sinon.createStubInstance(remoting.TcpSocket)); |
| 48 | 52 |
| 49 connection = new remoting.XmppConnection(); | 53 connection = new remoting.XmppConnection(); |
| 50 connection.setSocketForTests(socket); | 54 connection.setSocketForTests(socket); |
| 51 connection.setStateChangedCallback(onStateChange); | 55 connection.setStateChangedCallback(onStateChange); |
| 52 connection.setIncomingStanzaCallback(onStanza); | 56 connection.setIncomingStanzaCallback(onStanza); |
| 53 } | 57 } |
| 54 }); | 58 }); |
| 55 | 59 |
| 56 QUnit.asyncTest('should go to FAILED state when failed to connect', function() { | 60 QUnit.test('should go to FAILED state when failed to connect', |
| 61 function(assert) { |
| 62 var done = assert.async(); |
| 57 $testStub(socket.connect).withArgs("xmpp.example.com", 123) | 63 $testStub(socket.connect).withArgs("xmpp.example.com", 123) |
| 58 .returns(new Promise(function(resolve, reject) { reject(-1); })); | 64 .returns(new Promise(function(resolve, reject) { reject(-1); })); |
| 59 | 65 |
| 60 var deferredSend = new base.Deferred(); | 66 var deferredSend = new base.Deferred(); |
| 61 $testStub(socket.send).onFirstCall().returns(deferredSend.promise()); | 67 $testStub(socket.send).onFirstCall().returns(deferredSend.promise()); |
| 62 | 68 |
| 63 expectNextState(remoting.SignalStrategy.State.CONNECTING).then(onConnecting); | 69 expectNextState(assert, remoting.SignalStrategy.State.CONNECTING) |
| 70 .then(onConnecting); |
| 64 connection.connect('xmpp.example.com:123', 'testUsername@gmail.com', | 71 connection.connect('xmpp.example.com:123', 'testUsername@gmail.com', |
| 65 'testToken'); | 72 'testToken'); |
| 66 | 73 |
| 67 function onConnecting() { | 74 function onConnecting() { |
| 68 expectNextState(remoting.SignalStrategy.State.FAILED).then(onFailed); | 75 expectNextState(assert, remoting.SignalStrategy.State.FAILED) |
| 76 .then(onFailed); |
| 69 } | 77 } |
| 70 | 78 |
| 71 function onFailed() { | 79 function onFailed() { |
| 72 sinon.assert.calledWith(socket.dispose); | 80 sinon.assert.calledWith(socket.dispose); |
| 73 QUnit.ok(connection.getError().hasTag(remoting.Error.Tag.NETWORK_FAILURE)); | 81 assert.ok(connection.getError().hasTag(remoting.Error.Tag.NETWORK_FAILURE)); |
| 74 | 82 done(); |
| 75 QUnit.start(); | |
| 76 } | 83 } |
| 77 }); | 84 }); |
| 78 | 85 |
| 79 QUnit.asyncTest('should use XmppLoginHandler for handshake', function() { | 86 QUnit.test('should use XmppLoginHandler for handshake', function(assert) { |
| 87 |
| 80 $testStub(socket.connect).withArgs("xmpp.example.com", 123) | 88 $testStub(socket.connect).withArgs("xmpp.example.com", 123) |
| 81 .returns(new Promise(function(resolve, reject) { resolve(0) })); | 89 .returns(new Promise(function(resolve, reject) { resolve(0) })); |
| 82 | 90 |
| 83 var deferredSend = new base.Deferred(); | 91 var deferredSend = new base.Deferred(); |
| 84 $testStub(socket.send).onFirstCall().returns(deferredSend.promise()); | 92 $testStub(socket.send).onFirstCall().returns(deferredSend.promise()); |
| 85 | 93 |
| 86 var parser = new remoting.XmppStreamParser(); | 94 var parser = new remoting.XmppStreamParser(); |
| 87 var parserMock = sinon.mock(parser); | 95 var parserMock = sinon.mock(parser); |
| 88 var setCallbacksCalled = parserMock.expects('setCallbacks').once(); | 96 var setCallbacksCalled = parserMock.expects('setCallbacks').once(); |
| 97 var State = remoting.SignalStrategy.State; |
| 89 | 98 |
| 90 expectNextState(remoting.SignalStrategy.State.CONNECTING).then(onConnecting); | 99 var promise = expectNextState(assert, State.CONNECTING).then(function() { |
| 91 connection.connect( | 100 return expectNextState(assert, State.HANDSHAKE); |
| 92 'xmpp.example.com:123', 'testUsername@gmail.com', 'testToken'); | 101 }).then(function() { |
| 93 | |
| 94 function onConnecting() { | |
| 95 expectNextState(remoting.SignalStrategy.State.HANDSHAKE).then(onHandshake); | |
| 96 } | |
| 97 | |
| 98 function onHandshake() { | |
| 99 var handshakeDoneCallback = | 102 var handshakeDoneCallback = |
| 100 connection.loginHandler_.getHandshakeDoneCallbackForTesting(); | 103 connection.loginHandler_.getHandshakeDoneCallbackForTesting(); |
| 101 | 104 var onConnected = expectNextState(assert, State.CONNECTED); |
| 102 expectNextState(remoting.SignalStrategy.State.CONNECTED).then(onConnected); | |
| 103 handshakeDoneCallback('test@example.com/123123', parser); | 105 handshakeDoneCallback('test@example.com/123123', parser); |
| 104 } | 106 return onConnected; |
| 105 | 107 }).then(function() { |
| 106 function onConnected() { | |
| 107 setCallbacksCalled.verify(); | 108 setCallbacksCalled.verify(); |
| 108 | 109 |
| 109 // Simulate read() callback with |data|. It should be passed to | 110 // Simulate read() callback with |data|. It should be passed to |
| 110 // the parser. | 111 // the parser. |
| 111 var data = base.encodeUtf8('<iq id="1">hello</iq>'); | 112 var data = base.encodeUtf8('<iq id="1">hello</iq>'); |
| 112 sinon.assert.calledWith(socket.startReceiving); | 113 sinon.assert.calledWith(socket.startReceiving); |
| 113 var appendDataCalled = | 114 var appendDataCalled = |
| 114 parserMock.expects('appendData').once().withArgs(data); | 115 parserMock.expects('appendData').once().withArgs(data); |
| 115 $testStub(socket.startReceiving).getCall(0).args[0](data); | 116 $testStub(socket.startReceiving).getCall(0).args[0](data); |
| 116 appendDataCalled.verify(); | 117 appendDataCalled.verify(); |
| 118 }); |
| 117 | 119 |
| 118 QUnit.start(); | 120 connection.connect( |
| 119 } | 121 'xmpp.example.com:123', 'testUsername@gmail.com', 'testToken'); |
| 122 return promise; |
| 120 }); | 123 }); |
| 121 | 124 |
| 122 })(); | 125 })(); |
| OLD | NEW |