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 onStanzaStr
= null;
15 /** @type {remoting.XmppConnection} */
16 var connection
= null;
18 /** @type {remoting.TcpSocket} */
21 var stateChangeHandler = function(/** remoting.SignalStrategy.State */ state
) {}
23 function onStateChange(/** remoting.SignalStrategy.State */ state
) {
24 stateChangeHandler(state
)
28 * @param {QUnit.Assert} assert
29 * @param {remoting.SignalStrategy.State} expectedState
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
);
42 QUnit
.module('XmppConnection', {
43 beforeEach: function() {
44 remoting
.settings
= new remoting
.Settings();
46 onStanzaStr
= sinon
.spy();
47 /** @param {Element} stanza */
48 function onStanza(stanza
) {
49 onStanzaStr(new XMLSerializer().serializeToString(stanza
));
52 socket
= /** @type{remoting.TcpSocket} */
53 (sinon
.createStubInstance(remoting
.TcpSocket
));
55 connection
= new remoting
.XmppConnection();
56 connection
.setSocketForTests(socket
);
57 connection
.setStateChangedCallback(onStateChange
);
58 connection
.setIncomingStanzaCallback(onStanza
);
60 afterEach: function() {
61 remoting
.settings
= null;
65 QUnit
.test('should go to FAILED state when failed to connect',
67 var done
= assert
.async();
68 $testStub(socket
.connect
).withArgs("xmpp.example.com", 123)
69 .returns(new Promise(function(resolve
, reject
) { reject(-1); }));
71 var deferredSend
= new base
.Deferred();
72 $testStub(socket
.send
).onFirstCall().returns(deferredSend
.promise());
74 expectNextState(assert
, remoting
.SignalStrategy
.State
.CONNECTING
)
76 connection
.connect('xmpp.example.com:123', 'testUsername@gmail.com',
79 function onConnecting() {
80 expectNextState(assert
, remoting
.SignalStrategy
.State
.FAILED
)
85 sinon
.assert
.calledWith(socket
.dispose
);
86 assert
.ok(connection
.getError().hasTag(remoting
.Error
.Tag
.NETWORK_FAILURE
));
91 QUnit
.test('should use XmppLoginHandler for handshake', function(assert
) {
93 $testStub(socket
.connect
).withArgs("xmpp.example.com", 123)
94 .returns(new Promise(function(resolve
, reject
) { resolve(0) }));
96 var deferredSend
= new base
.Deferred();
97 $testStub(socket
.send
).onFirstCall().returns(deferredSend
.promise());
99 var parser
= new remoting
.XmppStreamParser();
100 var parserMock
= sinon
.mock(parser
);
101 var setCallbacksCalled
= parserMock
.expects('setCallbacks').once();
102 var State
= remoting
.SignalStrategy
.State
;
104 var promise
= expectNextState(assert
, State
.CONNECTING
).then(function() {
105 return expectNextState(assert
, State
.HANDSHAKE
);
107 var handshakeDoneCallback
=
108 connection
.loginHandler_
.getHandshakeDoneCallbackForTesting();
109 var onConnected
= expectNextState(assert
, State
.CONNECTED
);
110 handshakeDoneCallback('test@example.com/123123', parser
);
113 setCallbacksCalled
.verify();
115 // Simulate read() callback with |data|. It should be passed to
117 var data
= base
.encodeUtf8('<iq id="1">hello</iq>');
118 sinon
.assert
.calledWith(socket
.startReceiving
);
119 var appendDataCalled
=
120 parserMock
.expects('appendData').once().withArgs(data
);
121 $testStub(socket
.startReceiving
).getCall(0).args
[0](data
);
122 appendDataCalled
.verify();
126 'xmpp.example.com:123', 'testUsername@gmail.com', 'testToken');