Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / remoting / webapp / base / js / xmpp_connection_unittest.js
bloba96ef9bde1c5638ed0362f1e3d50ca31ef36400e
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}
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   });
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     }
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);
57   }
58 });
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); }));
66   var deferredSend = new base.Deferred();
67   $testStub(socket.send).onFirstCall().returns(deferredSend.promise());
69   expectNextState(assert, remoting.SignalStrategy.State.CONNECTING)
70       .then(onConnecting);
71   connection.connect('xmpp.example.com:123', 'testUsername@gmail.com',
72                      'testToken');
74   function onConnecting() {
75     expectNextState(assert, remoting.SignalStrategy.State.FAILED)
76         .then(onFailed);
77   }
79   function onFailed() {
80     sinon.assert.calledWith(socket.dispose);
81     assert.ok(connection.getError().hasTag(remoting.Error.Tag.NETWORK_FAILURE));
82     done();
83   }
84 });
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);
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();
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   });
120   connection.connect(
121       'xmpp.example.com:123', 'testUsername@gmail.com', 'testToken');
122   return promise;
125 })();