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 onStanzaStr = sinon.spy();
45 /** @param {Element} stanza */
46 function onStanza(stanza) {
47 onStanzaStr(new XMLSerializer().serializeToString(stanza));
50 socket = /** @type{remoting.TcpSocket} */
51 (sinon.createStubInstance(remoting.TcpSocket));
53 connection = new remoting.XmppConnection();
54 connection.setSocketForTests(socket);
55 connection.setStateChangedCallback(onStateChange);
56 connection.setIncomingStanzaCallback(onStanza);
60 QUnit.test('should go to FAILED state when failed to connect',
62 var done = assert.async();
63 $testStub(socket.connect).withArgs("xmpp.example.com", 123)
64 .returns(new Promise(function(resolve, reject) { reject(-1); }));
66 var deferredSend = new base.Deferred();
67 $testStub(socket.send).onFirstCall().returns(deferredSend.promise());
69 expectNextState(assert, remoting.SignalStrategy.State.CONNECTING)
71 connection.connect('xmpp.example.com:123', 'testUsername@gmail.com',
74 function onConnecting() {
75 expectNextState(assert, remoting.SignalStrategy.State.FAILED)
80 sinon.assert.calledWith(socket.dispose);
81 assert.ok(connection.getError().hasTag(remoting.Error.Tag.NETWORK_FAILURE));
86 QUnit.test('should use XmppLoginHandler for handshake', function(assert) {
88 $testStub(socket.connect).withArgs("xmpp.example.com", 123)
89 .returns(new Promise(function(resolve, reject) { resolve(0) }));
91 var deferredSend = new base.Deferred();
92 $testStub(socket.send).onFirstCall().returns(deferredSend.promise());
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;
99 var promise = expectNextState(assert, State.CONNECTING).then(function() {
100 return expectNextState(assert, State.HANDSHAKE);
102 var handshakeDoneCallback =
103 connection.loginHandler_.getHandshakeDoneCallbackForTesting();
104 var onConnected = expectNextState(assert, State.CONNECTED);
105 handshakeDoneCallback('test@example.com/123123', parser);
108 setCallbacksCalled.verify();
110 // Simulate read() callback with |data|. It should be passed to
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();
121 'xmpp.example.com:123', 'testUsername@gmail.com', 'testToken');