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', function() { |
| 61 connection.connect( |
| 62 'xmpp.example.com:123', 'testUsername@gmail.com', 'testToken'); |
| 63 sinon.assert.calledWith(onStateChange, |
| 64 remoting.XmppConnection.State.CONNECTING); |
| 65 sinon.assert.calledWith(chrome.socket.create, "tcp", {}); |
| 66 chrome.socket.create.getCall(0).args[2]({socketId: socketId}); |
| 67 |
| 68 sinon.assert.calledWith( |
| 69 chrome.socket.connect, socketId, "xmpp.example.com", 123); |
| 70 chrome.socket.connect.getCall(0).args[3](0); |
| 71 |
| 72 sinon.assert.calledWith(onStateChange, |
| 73 remoting.XmppConnection.State.HANDSHAKE); |
| 74 |
| 75 connection.loginHandler_.onHandshakeDoneCallback_(); |
| 76 sinon.assert.calledWith(onStateChange, |
| 77 remoting.XmppConnection.State.CONNECTED); |
| 78 }); |
| 79 |
| 80 test('should read data', function() { |
| 81 connection.connect( |
| 82 'xmpp.example.com:123', 'testUsername@gmail.com', 'testToken'); |
| 83 sinon.assert.calledWith(onStateChange, |
| 84 remoting.XmppConnection.State.CONNECTING); |
| 85 sinon.assert.calledWith(chrome.socket.create, "tcp", {}); |
| 86 chrome.socket.create.getCall(0).args[2]({socketId: socketId}); |
| 87 |
| 88 sinon.assert.calledWith( |
| 89 chrome.socket.connect, socketId, "xmpp.example.com", 123); |
| 90 chrome.socket.connect.getCall(0).args[3](0); |
| 91 |
| 92 sinon.assert.calledWith(onStateChange, |
| 93 remoting.XmppConnection.State.HANDSHAKE); |
| 94 }); |
| 95 |
| 96 })(); |
OLD | NEW |