OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 (function() { |
| 6 |
| 7 'use strict'; |
| 8 |
| 9 var testUsername = 'testUsername@gmail.com'; |
| 10 var testToken = 'testToken'; |
| 11 var socketId = 3; |
| 12 |
| 13 var onStateChange = null; |
| 14 var onStanzaStr = null; |
| 15 var connection = null; |
| 16 |
| 17 module('XmppConnection', { |
| 18 setup: function() { |
| 19 onStateChange = sinon.spy(); |
| 20 onStanzaStr = sinon.spy(); |
| 21 function onStanza(stanza) { |
| 22 onStanzaStr(new XMLSerializer().serializeToString(stanza)); |
| 23 } |
| 24 |
| 25 sinon.stub(chrome.socket, 'create'); |
| 26 sinon.stub(chrome.socket, 'connect'); |
| 27 sinon.stub(chrome.socket, 'write'); |
| 28 sinon.stub(chrome.socket, 'read'); |
| 29 sinon.stub(chrome.socket, 'destroy'); |
| 30 sinon.stub(chrome.socket, 'secure'); |
| 31 |
| 32 connection = new remoting.XmppConnection(onStateChange, onStanza); |
| 33 }, |
| 34 |
| 35 teardown: function() { |
| 36 chrome.socket.create.restore(); |
| 37 chrome.socket.connect.restore(); |
| 38 chrome.socket.write.restore(); |
| 39 chrome.socket.read.restore(); |
| 40 chrome.socket.destroy.restore(); |
| 41 chrome.socket.secure.restore(); |
| 42 } |
| 43 }); |
| 44 |
| 45 test('should go to FAILED state when failed to connect', function() { |
| 46 connection.connect( |
| 47 'xmpp.example.com:123', 'testUsername@gmail.com', 'testToken'); |
| 48 sinon.assert.calledWith(onStateChange, |
| 49 remoting.XmppConnection.State.CONNECTING); |
| 50 sinon.assert.calledWith(chrome.socket.create, "tcp", {}); |
| 51 chrome.socket.create.getCall(0).args[2]({socketId: socketId}); |
| 52 |
| 53 sinon.assert.calledWith( |
| 54 chrome.socket.connect, socketId, "xmpp.example.com", 123); |
| 55 chrome.socket.connect.getCall(0).args[3](-1); |
| 56 |
| 57 QUnit.equal(connection.getError(), remoting.Error.NETWORK_FAILURE); |
| 58 }); |
| 59 |
| 60 test('should use XmppLoginHandler to complete handshake and read data', |
| 61 function() { |
| 62 connection.connect( |
| 63 'xmpp.example.com:123', 'testUsername@gmail.com', 'testToken'); |
| 64 sinon.assert.calledWith(onStateChange, |
| 65 remoting.XmppConnection.State.CONNECTING); |
| 66 sinon.assert.calledWith(chrome.socket.create, "tcp", {}); |
| 67 chrome.socket.create.getCall(0).args[2]({socketId: socketId}); |
| 68 |
| 69 sinon.assert.calledWith( |
| 70 chrome.socket.connect, socketId, "xmpp.example.com", 123); |
| 71 chrome.socket.connect.getCall(0).args[3](0); |
| 72 |
| 73 sinon.assert.calledWith(onStateChange, |
| 74 remoting.XmppConnection.State.HANDSHAKE); |
| 75 |
| 76 var parser = new remoting.XmppStreamParser(); |
| 77 var parserMock = sinon.mock(parser); |
| 78 var setCallbacksCalled = parserMock.expects('setCallbacks').once(); |
| 79 connection.loginHandler_.onHandshakeDoneCallback_('test@example.com/123123', |
| 80 parser); |
| 81 sinon.assert.calledWith(onStateChange, |
| 82 remoting.XmppConnection.State.CONNECTED); |
| 83 setCallbacksCalled.verify(); |
| 84 |
| 85 // Simulate read() callback with |data|. It should be passed to the parser. |
| 86 var data = base.encodeUtf8('<iq id="1">hello</iq>'); |
| 87 sinon.assert.calledWith(chrome.socket.read, socketId); |
| 88 var appendDataCalled = parserMock.expects('appendData').once().withArgs(data); |
| 89 chrome.socket.read.getCall(0).args[1]({resultCode: 0, data: data}); |
| 90 appendDataCalled.verify(); |
| 91 }); |
| 92 |
| 93 })(); |
OLD | NEW |