| 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'; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 function expectNextState(/** remoting.SignalStrategy.State */ expectedState) { | 28 function expectNextState(/** remoting.SignalStrategy.State */ expectedState) { |
| 29 return new Promise(function(resolve, reject) { | 29 return new Promise(function(resolve, reject) { |
| 30 stateChangeHandler = function(/** remoting.SignalStrategy.State */ state) { | 30 stateChangeHandler = function(/** remoting.SignalStrategy.State */ state) { |
| 31 QUnit.equal(state, expectedState); | 31 QUnit.equal(state, expectedState); |
| 32 QUnit.equal(connection.getState(), expectedState); | 32 QUnit.equal(connection.getState(), expectedState); |
| 33 resolve(0); | 33 resolve(0); |
| 34 } | 34 } |
| 35 }); | 35 }); |
| 36 } | 36 } |
| 37 | 37 |
| 38 module('XmppConnection', { | 38 QUnit.module('XmppConnection', { |
| 39 setup: function() { | 39 beforeEach: function() { |
| 40 onStanzaStr = sinon.spy(); | 40 onStanzaStr = sinon.spy(); |
| 41 /** @param {Element} stanza */ | 41 /** @param {Element} stanza */ |
| 42 function onStanza(stanza) { | 42 function onStanza(stanza) { |
| 43 onStanzaStr(new XMLSerializer().serializeToString(stanza)); | 43 onStanzaStr(new XMLSerializer().serializeToString(stanza)); |
| 44 } | 44 } |
| 45 | 45 |
| 46 socket = /** @type{remoting.TcpSocket} */ | 46 socket = /** @type{remoting.TcpSocket} */ |
| 47 (sinon.createStubInstance(remoting.TcpSocket)); | 47 (sinon.createStubInstance(remoting.TcpSocket)); |
| 48 | 48 |
| 49 connection = new remoting.XmppConnection(); | 49 connection = new remoting.XmppConnection(); |
| 50 connection.setSocketForTests(socket); | 50 connection.setSocketForTests(socket); |
| 51 connection.setStateChangedCallback(onStateChange); | 51 connection.setStateChangedCallback(onStateChange); |
| 52 connection.setIncomingStanzaCallback(onStanza); | 52 connection.setIncomingStanzaCallback(onStanza); |
| 53 } | 53 } |
| 54 }); | 54 }); |
| 55 | 55 |
| 56 QUnit.asyncTest('should go to FAILED state when failed to connect', function() { | 56 QUnit.test('should go to FAILED state when failed to connect', |
| 57 function(assert) { |
| 58 var done = assert.async(); |
| 57 $testStub(socket.connect).withArgs("xmpp.example.com", 123) | 59 $testStub(socket.connect).withArgs("xmpp.example.com", 123) |
| 58 .returns(new Promise(function(resolve, reject) { reject(-1); })); | 60 .returns(new Promise(function(resolve, reject) { reject(-1); })); |
| 59 | 61 |
| 60 var deferredSend = new base.Deferred(); | 62 var deferredSend = new base.Deferred(); |
| 61 $testStub(socket.send).onFirstCall().returns(deferredSend.promise()); | 63 $testStub(socket.send).onFirstCall().returns(deferredSend.promise()); |
| 62 | 64 |
| 63 expectNextState(remoting.SignalStrategy.State.CONNECTING).then(onConnecting); | 65 expectNextState(remoting.SignalStrategy.State.CONNECTING).then(onConnecting); |
| 64 connection.connect('xmpp.example.com:123', 'testUsername@gmail.com', | 66 connection.connect('xmpp.example.com:123', 'testUsername@gmail.com', |
| 65 'testToken'); | 67 'testToken'); |
| 66 | 68 |
| 67 function onConnecting() { | 69 function onConnecting() { |
| 68 expectNextState(remoting.SignalStrategy.State.FAILED).then(onFailed); | 70 expectNextState(remoting.SignalStrategy.State.FAILED).then(onFailed); |
| 69 } | 71 } |
| 70 | 72 |
| 71 function onFailed() { | 73 function onFailed() { |
| 72 sinon.assert.calledWith(socket.dispose); | 74 sinon.assert.calledWith(socket.dispose); |
| 73 QUnit.ok(connection.getError().hasTag(remoting.Error.Tag.NETWORK_FAILURE)); | 75 QUnit.ok(connection.getError().hasTag(remoting.Error.Tag.NETWORK_FAILURE)); |
| 74 | 76 done(); |
| 75 QUnit.start(); | |
| 76 } | 77 } |
| 77 }); | 78 }); |
| 78 | 79 |
| 79 QUnit.asyncTest('should use XmppLoginHandler for handshake', function() { | 80 QUnit.test('should use XmppLoginHandler for handshake', function() { |
| 81 |
| 80 $testStub(socket.connect).withArgs("xmpp.example.com", 123) | 82 $testStub(socket.connect).withArgs("xmpp.example.com", 123) |
| 81 .returns(new Promise(function(resolve, reject) { resolve(0) })); | 83 .returns(new Promise(function(resolve, reject) { resolve(0) })); |
| 82 | 84 |
| 83 var deferredSend = new base.Deferred(); | 85 var deferredSend = new base.Deferred(); |
| 84 $testStub(socket.send).onFirstCall().returns(deferredSend.promise()); | 86 $testStub(socket.send).onFirstCall().returns(deferredSend.promise()); |
| 85 | 87 |
| 86 var parser = new remoting.XmppStreamParser(); | 88 var parser = new remoting.XmppStreamParser(); |
| 87 var parserMock = sinon.mock(parser); | 89 var parserMock = sinon.mock(parser); |
| 88 var setCallbacksCalled = parserMock.expects('setCallbacks').once(); | 90 var setCallbacksCalled = parserMock.expects('setCallbacks').once(); |
| 91 var State = remoting.SignalStrategy.State; |
| 89 | 92 |
| 90 expectNextState(remoting.SignalStrategy.State.CONNECTING).then(onConnecting); | 93 var promise = expectNextState(State.CONNECTING).then(function() { |
| 91 connection.connect( | 94 return expectNextState(State.HANDSHAKE); |
| 92 'xmpp.example.com:123', 'testUsername@gmail.com', 'testToken'); | 95 }).then(function() { |
| 93 | |
| 94 function onConnecting() { | |
| 95 expectNextState(remoting.SignalStrategy.State.HANDSHAKE).then(onHandshake); | |
| 96 } | |
| 97 | |
| 98 function onHandshake() { | |
| 99 var handshakeDoneCallback = | 96 var handshakeDoneCallback = |
| 100 connection.loginHandler_.getHandshakeDoneCallbackForTesting(); | 97 connection.loginHandler_.getHandshakeDoneCallbackForTesting(); |
| 101 | 98 var onConnected = expectNextState(State.CONNECTED); |
| 102 expectNextState(remoting.SignalStrategy.State.CONNECTED).then(onConnected); | |
| 103 handshakeDoneCallback('test@example.com/123123', parser); | 99 handshakeDoneCallback('test@example.com/123123', parser); |
| 104 } | 100 return onConnected; |
| 105 | 101 }).then(function() { |
| 106 function onConnected() { | |
| 107 setCallbacksCalled.verify(); | 102 setCallbacksCalled.verify(); |
| 108 | 103 |
| 109 // Simulate read() callback with |data|. It should be passed to | 104 // Simulate read() callback with |data|. It should be passed to |
| 110 // the parser. | 105 // the parser. |
| 111 var data = base.encodeUtf8('<iq id="1">hello</iq>'); | 106 var data = base.encodeUtf8('<iq id="1">hello</iq>'); |
| 112 sinon.assert.calledWith(socket.startReceiving); | 107 sinon.assert.calledWith(socket.startReceiving); |
| 113 var appendDataCalled = | 108 var appendDataCalled = |
| 114 parserMock.expects('appendData').once().withArgs(data); | 109 parserMock.expects('appendData').once().withArgs(data); |
| 115 $testStub(socket.startReceiving).getCall(0).args[0](data); | 110 $testStub(socket.startReceiving).getCall(0).args[0](data); |
| 116 appendDataCalled.verify(); | 111 appendDataCalled.verify(); |
| 112 }); |
| 117 | 113 |
| 118 QUnit.start(); | 114 connection.connect( |
| 119 } | 115 'xmpp.example.com:123', 'testUsername@gmail.com', 'testToken'); |
| 116 return promise; |
| 120 }); | 117 }); |
| 121 | 118 |
| 122 })(); | 119 })(); |
| OLD | NEW |