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