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 {remoting.SessionLogger} */
17 /** @type {sinon.TestStub} */
22 * @implements {remoting.ClientSession.EventHandler}
24 var SessionListener = function() {};
25 SessionListener
.prototype.onConnectionFailed = function(error
) {};
26 SessionListener
.prototype.onConnected = function(connectionInfo
) {};
27 SessionListener
.prototype.onDisconnected = function(error
) {};
30 * @param {remoting.ClientSession.ConnectionError=} opt_error
33 function connect(opt_error
) {
34 var deferred
= new base
.Deferred();
35 var host
= new remoting
.Host('fake_hostId');
36 host
.jabberId
= 'fake_jid';
38 function onPluginCreated(/** remoting.MockClientPlugin */ plugin
) {
39 var State
= remoting
.ClientSession
.State
;
40 plugin
.mock
$onConnect().then(function() {
41 plugin
.mock
$setConnectionStatus(State
.CONNECTING
);
43 var status
= (opt_error
) ? State
.FAILED
: State
.CONNECTED
;
44 plugin
.mock
$setConnectionStatus(status
, opt_error
);
47 mockConnection
.pluginFactory().mock
$setPluginCreated(onPluginCreated
);
49 var sessionFactory
= new remoting
.ClientSessionFactory(
50 document
.createElement('div'), ['fake_capability']);
52 sessionFactory
.createSession(listener
, logger
, true).then(
53 function(clientSession
) {
54 session
= clientSession
;
55 clientSession
.connect(host
, new remoting
.CredentialsProvider({
56 pairingInfo
: { clientId
: 'fake_clientId', sharedSecret
: 'fake_secret' }
60 listener
.onConnected = function() {
64 listener
.onConnectionFailed = function(/** remoting.Error */ error
) {
65 deferred
.reject(error
);
68 return deferred
.promise();
71 QUnit
.module('ClientSession', {
72 beforeEach: function() {
73 chromeMocks
.identity
.mock
$setToken('fake_token');
75 mockConnection
= new remoting
.MockConnection();
76 listener
= new SessionListener();
77 logger
= new remoting
.SessionLogger(remoting
.ChromotingEvent
.Role
.CLIENT
,
79 logToServerStub
= sinon
.stub(logger
, 'logClientSessionStateChange');
81 afterEach: function() {
83 mockConnection
.restore();
87 QUnit
.test('should raise CONNECTED event on connected', function(assert
) {
88 return connect().then(function(){
89 assert
.ok(true, 'Expect session to connect.');
93 QUnit
.test('onOutgoingIq() should send Iq to signalStrategy', function(assert
) {
94 var sendMessage
= sinon
.stub(mockConnection
.signalStrategy(), 'sendMessage');
95 return connect().then(function(){
96 session
.onOutgoingIq('sample message');
97 assert
.ok(sendMessage
.calledWith('sample message'));
101 QUnit
.test('should foward Iq from signalStrategy to plugin', function(assert
) {
102 return connect().then(function() {
103 var onIncomingIq
= sinon
.stub(mockConnection
.plugin(), 'onIncomingIq');
104 var stanza
= new DOMParser()
105 .parseFromString('<iq>sample</iq>', 'text/xml')
107 mockConnection
.signalStrategy().mock
$onIncomingStanza(stanza
);
108 assert
.ok(onIncomingIq
.calledWith('<iq>sample</iq>'));
112 QUnit
.test('disconnect() should raise the CLOSED event', function(assert
) {
113 return connect().then(function() {
114 var onDisconnected
= sinon
.stub(listener
, 'onDisconnected');
115 session
.disconnect(remoting
.Error
.none());
116 assert
.equal(onDisconnected
.callCount
, 1);
121 'Connection error after CONNECTED should raise the CONNECTION_DROPPED event',
124 var State
= remoting
.ClientSession
.State
;
126 return connect().then(function() {
127 var onDisconnected
= sinon
.stub(listener
, 'onDisconnected');
128 session
.disconnect(new remoting
.Error(remoting
.Error
.Tag
.P2P_FAILURE
));
129 assert
.equal(onDisconnected
.callCount
, 1);
130 assert
.equal(logToServerStub
.args
[2][0], State
.CONNECTION_DROPPED
);
135 'Connection error before CONNECTED should raise the CONNECTION_FAILED event',
138 var PluginError
= remoting
.ClientSession
.ConnectionError
;
139 var State
= remoting
.ClientSession
.State
;
141 return connect(PluginError
.SESSION_REJECTED
).then(function() {
142 assert
.ok(false, 'Expect connection to fail');
143 }).catch(function(/** remoting.Error */ error
) {
144 assert
.ok(error
.hasTag(remoting
.Error
.Tag
.INVALID_ACCESS_CODE
));
145 assert
.equal(logToServerStub
.args
[1][0], State
.FAILED
);
146 var errorLogged
= /** @type {remoting.Error} */(logToServerStub
.args
[1][1]);
147 assert
.equal(errorLogged
.getTag(), remoting
.Error
.Tag
.INVALID_ACCESS_CODE
);