Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / remoting / webapp / base / js / xmpp_connection_unittest.js
blobe1e2efed24af70ff576ef459e3b4fef924fc4562
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.
5 (function() {
7 'use strict';
9 var testUsername = 'testUsername@gmail.com';
10 var testToken = 'testToken';
11 var socketId = 3;
13 var onStanzaStr = null;
15 /** @type {remoting.XmppConnection} */
16 var connection = null;
18 /** @type {remoting.TcpSocket} */
19 var socket = null;
21 var stateChangeHandler = function(/** remoting.SignalStrategy.State */ state) {}
23 function onStateChange(/** remoting.SignalStrategy.State */ state) {
24 stateChangeHandler(state)
27 /**
28 * @param {QUnit.Assert} assert
29 * @param {remoting.SignalStrategy.State} expectedState
30 * @returns {Promise}
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);
39 });
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;
63 });
65 QUnit.test('should go to FAILED state when failed to connect',
66 function(assert) {
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)
75 .then(onConnecting);
76 connection.connect('xmpp.example.com:123', 'testUsername@gmail.com',
77 'testToken');
79 function onConnecting() {
80 expectNextState(assert, remoting.SignalStrategy.State.FAILED)
81 .then(onFailed);
84 function onFailed() {
85 sinon.assert.calledWith(socket.dispose);
86 assert.ok(connection.getError().hasTag(remoting.Error.Tag.NETWORK_FAILURE));
87 done();
89 });
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);
106 }).then(function() {
107 var handshakeDoneCallback =
108 connection.loginHandler_.getHandshakeDoneCallbackForTesting();
109 var onConnected = expectNextState(assert, State.CONNECTED);
110 handshakeDoneCallback('test@example.com/123123', parser);
111 return onConnected;
112 }).then(function() {
113 setCallbacksCalled.verify();
115 // Simulate read() callback with |data|. It should be passed to
116 // the parser.
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();
125 connection.connect(
126 'xmpp.example.com:123', 'testUsername@gmail.com', 'testToken');
127 return promise;
130 })();