1 // Copyright 2015 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 /** @type {remoting.MockConnection} */
11 /** @type {remoting.ClientSession} */
13 /** @type {remoting.ClientSession.EventHandler} */
15 /** @type {sinon.TestStub} */
20 * @implements {remoting.ClientSession.EventHandler}
22 var SessionListener = function() {};
23 SessionListener.prototype.onConnectionFailed = function(error) {};
24 SessionListener.prototype.onConnected = function(connectionInfo) {};
25 SessionListener.prototype.onDisconnected = function(error) {};
28 * @param {remoting.ClientSession.ConnectionError=} opt_error
31 function connect(opt_error) {
32 var deferred = new base.Deferred();
33 var host = new remoting.Host('fake_hostId');
34 host.jabberId = 'fake_jid';
36 var plugin = mockConnection.plugin();
37 var State = remoting.ClientSession.State;
39 plugin.mock$onConnect().then(function() {
40 plugin.mock$setConnectionStatus(State.CONNECTING);
42 var status = (opt_error) ? State.FAILED : State.CONNECTED;
43 plugin.mock$setConnectionStatus(status, opt_error);
46 session.connect(host, new remoting.CredentialsProvider({
47 pairingInfo: { clientId: 'fake_clientId', sharedSecret: 'fake_secret' }
50 listener.onConnected = function() {
54 listener.onConnectionFailed = function(/** remoting.Error */ error) {
55 deferred.reject(error);
58 return deferred.promise();
61 QUnit.module('ClientSession', {
62 beforeEach: function() {
63 chromeMocks.identity.mock$setToken('fake_token');
65 mockConnection = new remoting.MockConnection();
66 listener = new SessionListener();
68 var sessionFactory = new remoting.ClientSessionFactory(
69 document.createElement('div'), ['fake_capability']);
71 return sessionFactory.createSession(listener).then(function(clientSession) {
72 session = clientSession;
74 sinon.stub(session.getLogger(), 'logClientSessionStateChange');
77 afterEach: function() {
79 mockConnection.restore();
83 QUnit.test('should raise CONNECTED event on connected', function(assert) {
84 return connect().then(function(){
85 assert.ok(true, 'Expect session to connect.');
89 QUnit.test('onOutgoingIq() should send Iq to signalStrategy', function(assert) {
90 var sendMessage = sinon.stub(mockConnection.signalStrategy(), 'sendMessage');
91 return connect().then(function(){
92 session.onOutgoingIq('sample message');
93 assert.ok(sendMessage.calledWith('sample message'));
97 QUnit.test('should foward Iq from signalStrategy to plugin', function(assert) {
98 var onIncomingIq = sinon.stub(mockConnection.plugin(), 'onIncomingIq');
99 return connect().then(function() {
100 var stanza = new DOMParser()
101 .parseFromString('<iq>sample</iq>', 'text/xml')
103 mockConnection.signalStrategy().mock$onIncomingStanza(stanza);
104 assert.ok(onIncomingIq.calledWith('<iq>sample</iq>'));
108 QUnit.test('logHostOfflineErrors(false) should suppress offline errors',
111 session.logHostOfflineErrors(false);
113 var PluginError = remoting.ClientSession.ConnectionError;
114 var State = remoting.ClientSession.State;
116 return connect(PluginError.HOST_IS_OFFLINE).then(function() {
117 assert.ok(false, 'Expect connection to fail');
118 }).catch(function(/** remoting.Error */ error) {
119 assert.ok(error.hasTag(remoting.Error.Tag.HOST_IS_OFFLINE));
120 assert.equal(logToServerStub.args[1][0], State.CONNECTION_CANCELED);
121 var errorLogged = /** @type {remoting.Error} */(logToServerStub.args[1][1]);
122 assert.equal(errorLogged.getTag(), remoting.Error.Tag.HOST_IS_OFFLINE);
127 QUnit.test('disconnect() should raise the CLOSED event', function(assert) {
128 return connect().then(function() {
129 var onDisconnected = sinon.stub(listener, 'onDisconnected');
130 session.disconnect(remoting.Error.none());
131 assert.equal(onDisconnected.callCount, 1);
136 'Connection error after CONNECTED should raise the CONNECTION_DROPPED event',
139 var State = remoting.ClientSession.State;
141 return connect().then(function() {
142 var onDisconnected = sinon.stub(listener, 'onDisconnected');
143 session.disconnect(new remoting.Error(remoting.Error.Tag.P2P_FAILURE));
144 assert.equal(onDisconnected.callCount, 1);
145 assert.equal(logToServerStub.args[2][0], State.CONNECTION_DROPPED);
150 'Connection error before CONNECTED should raise the CONNECTION_FAILED event',
153 session.logHostOfflineErrors(true);
155 var PluginError = remoting.ClientSession.ConnectionError;
156 var State = remoting.ClientSession.State;
158 return connect(PluginError.SESSION_REJECTED).then(function() {
159 assert.ok(false, 'Expect connection to fail');
160 }).catch(function(/** remoting.Error */ error) {
161 assert.ok(error.hasTag(remoting.Error.Tag.INVALID_ACCESS_CODE));
162 assert.equal(logToServerStub.args[1][0], State.FAILED);
163 var errorLogged = /** @type {remoting.Error} */(logToServerStub.args[1][1]);
164 assert.equal(errorLogged.getTag(), remoting.Error.Tag.INVALID_ACCESS_CODE);
168 QUnit.test('dispose() should dispose the plugin', function(assert) {
169 var pluginDispose = sinon.stub(mockConnection.plugin(), 'dispose');
171 assert.equal(pluginDispose.callCount, 1);