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.
9 var testUsername = 'testUsername@gmail.com';
10 var testToken = 'testToken';
13 var onStateChange = null;
14 var onStanzaStr = null;
15 var connection = null;
17 module('XmppConnection', {
19 onStateChange = sinon.spy();
20 onStanzaStr = sinon.spy();
21 function onStanza(stanza) {
22 onStanzaStr(new XMLSerializer().serializeToString(stanza));
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');
32 connection = new remoting.XmppConnection(onStateChange);
33 connection.setIncomingStanzaCallback(onStanza);
36 teardown: function() {
37 chrome.socket.create.restore();
38 chrome.socket.connect.restore();
39 chrome.socket.write.restore();
40 chrome.socket.read.restore();
41 chrome.socket.destroy.restore();
42 chrome.socket.secure.restore();
46 test('should go to FAILED state when failed to connect', function() {
48 'xmpp.example.com:123', 'testUsername@gmail.com', 'testToken');
49 sinon.assert.calledWith(onStateChange,
50 remoting.SignalStrategy.State.CONNECTING);
51 sinon.assert.calledWith(chrome.socket.create, "tcp", {});
52 chrome.socket.create.getCall(0).args[2]({socketId: socketId});
54 sinon.assert.calledWith(
55 chrome.socket.connect, socketId, "xmpp.example.com", 123);
56 chrome.socket.connect.getCall(0).args[3](-1);
58 QUnit.equal(connection.getError(), remoting.Error.NETWORK_FAILURE);
61 test('should use XmppLoginHandler to complete handshake and read data',
64 'xmpp.example.com:123', 'testUsername@gmail.com', 'testToken');
65 sinon.assert.calledWith(onStateChange,
66 remoting.SignalStrategy.State.CONNECTING);
67 sinon.assert.calledWith(chrome.socket.create, "tcp", {});
68 chrome.socket.create.getCall(0).args[2]({socketId: socketId});
70 sinon.assert.calledWith(
71 chrome.socket.connect, socketId, "xmpp.example.com", 123);
72 chrome.socket.connect.getCall(0).args[3](0);
74 sinon.assert.calledWith(onStateChange,
75 remoting.SignalStrategy.State.HANDSHAKE);
77 var parser = new remoting.XmppStreamParser();
78 var parserMock = sinon.mock(parser);
79 var setCallbacksCalled = parserMock.expects('setCallbacks').once();
80 connection.loginHandler_.onHandshakeDoneCallback_('test@example.com/123123',
82 sinon.assert.calledWith(onStateChange,
83 remoting.SignalStrategy.State.CONNECTED);
84 setCallbacksCalled.verify();
86 // Simulate read() callback with |data|. It should be passed to the parser.
87 var data = base.encodeUtf8('<iq id="1">hello</iq>');
88 sinon.assert.calledWith(chrome.socket.read, socketId);
89 var appendDataCalled = parserMock.expects('appendData').once().withArgs(data);
90 chrome.socket.read.getCall(0).args[1]({resultCode: 0, data: data});
91 appendDataCalled.verify();