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