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.
7 * Mock implementation of ClientPlugin for testing.
10 /** @suppress {duplicate} */
11 var remoting
= remoting
|| {};
19 * @implements {remoting.ClientPlugin}
21 remoting
.MockClientPlugin = function() {
22 /** @private {Element} */
23 this.container_
= null;
25 this.element_
= /** @type {HTMLElement} */ (document
.createElement('div'));
26 this.element_
.style
.backgroundImage
= 'linear-gradient(45deg, blue, red)';
29 new remoting
.ClientPlugin
.HostDesktopImpl(this, base
.doNothing
);
31 this.extensions_
= new remoting
.ProtocolExtensionManager(base
.doNothing
);
33 /** @private {remoting.CredentialsProvider} */
34 this.credentials_
= null;
36 /** @private {remoting.ClientPlugin.ConnectionEventHandler} */
37 this.connectionEventHandler_
= null;
39 // Fake initialization result to return.
40 this.mock
$initializationResult
= true;
42 // Fake capabilities to return.
43 this.mock
$capabilities
= [
44 remoting
.ClientSession
.Capability
.SEND_INITIAL_RESOLUTION
,
45 remoting
.ClientSession
.Capability
.RATE_LIMIT_RESIZE_REQUESTS
49 this.onConnectDeferred_
= new base
.Deferred();
52 remoting
.MockClientPlugin
.prototype.dispose = function() {
53 this.container_
.removeChild(this.element_
);
55 this.connectionStatusUpdateHandler_
= null;
58 remoting
.MockClientPlugin
.prototype.extensions = function() {
59 return this.extensions_
;
62 remoting
.MockClientPlugin
.prototype.hostDesktop = function() {
63 return this.hostDesktop_
;
66 remoting
.MockClientPlugin
.prototype.element = function() {
70 remoting
.MockClientPlugin
.prototype.initialize = function(onDone
) {
72 Promise
.resolve().then(function() {
73 onDone(that
.mock
$initializationResult
);
78 remoting
.MockClientPlugin
.prototype.connect
=
79 function(host
, localJid
, credentialsProvider
) {
80 this.credentials_
= credentialsProvider
;
81 this.onConnectDeferred_
.resolve();
84 remoting
.MockClientPlugin
.prototype.injectKeyCombination = function(keys
) {};
86 remoting
.MockClientPlugin
.prototype.injectKeyEvent
=
87 function(key
, down
) {};
89 remoting
.MockClientPlugin
.prototype.setRemapKeys = function(remappings
) {};
91 remoting
.MockClientPlugin
.prototype.remapKey = function(from, to
) {};
93 remoting
.MockClientPlugin
.prototype.releaseAllKeys = function() {};
95 remoting
.MockClientPlugin
.prototype.onIncomingIq = function(iq
) {};
97 remoting
.MockClientPlugin
.prototype.isSupportedVersion = function() {
101 remoting
.MockClientPlugin
.prototype.hasFeature = function(feature
) {
105 remoting
.MockClientPlugin
.prototype.hasCapability = function(capability
) {
106 return this.mock
$capabilities
.indexOf(capability
) !== -1;
109 remoting
.MockClientPlugin
.prototype.sendClipboardItem
=
110 function(mimeType
, item
) {};
112 remoting
.MockClientPlugin
.prototype.enableTouchEvents
=
115 remoting
.MockClientPlugin
.prototype.requestPairing
=
116 function(clientName
, onDone
) {};
118 remoting
.MockClientPlugin
.prototype.allowMouseLock = function() {};
120 remoting
.MockClientPlugin
.prototype.pauseAudio = function(pause
) {};
122 remoting
.MockClientPlugin
.prototype.pauseVideo = function(pause
) {};
124 remoting
.MockClientPlugin
.prototype.getPerfStats = function() {
125 var result
= new remoting
.ClientSession
.PerfStats
;
126 result
.videoBandwidth
= 999;
127 result
.videoFrameRate
= 60;
128 result
.captureLatency
= 10;
129 result
.encodeLatency
= 10;
130 result
.decodeLatency
= 10;
131 result
.renderLatency
= 10;
132 result
.roundtripLatency
= 10;
136 remoting
.MockClientPlugin
.prototype.setConnectionEventHandler
=
138 this.connectionEventHandler_
= handler
;
141 remoting
.MockClientPlugin
.prototype.setMouseCursorHandler
=
142 function(handler
) {};
144 remoting
.MockClientPlugin
.prototype.setClipboardHandler = function(handler
) {};
146 remoting
.MockClientPlugin
.prototype.setDebugDirtyRegionHandler
=
147 function(handler
) {};
149 /** @param {Element} container */
150 remoting
.MockClientPlugin
.prototype.mock
$setContainer = function(container
) {
151 this.container_
= container
;
152 this.container_
.appendChild(this.element_
);
155 /** @return {Promise} */
156 remoting
.MockClientPlugin
.prototype.mock
$onConnect = function() {
157 this.onConnectDeferred_
= new base
.Deferred();
158 return this.onConnectDeferred_
.promise();
162 * @param {remoting.ClientSession.State} status
163 * @param {remoting.ClientSession.ConnectionError=} opt_error
165 remoting
.MockClientPlugin
.prototype.mock
$setConnectionStatus = function(
167 console
.assert(this.connectionEventHandler_
!== null,
168 '|connectionEventHandler_| is null.');
169 var PluginError
= remoting
.ClientSession
.ConnectionError
;
170 var error
= opt_error
? opt_error
: PluginError
.NONE
;
171 this.connectionEventHandler_
.onConnectionStatusUpdate(status
, error
);
175 * @param {remoting.MockClientPlugin.AuthMethod} authMethod
178 remoting
.MockClientPlugin
.prototype.mock
$authenticate = function(authMethod
) {
179 var AuthMethod
= remoting
.MockClientPlugin
.AuthMethod
;
180 var deferred
= new base
.Deferred();
185 case AuthMethod
.ACCESS_CODE
:
186 this.credentials_
.getPIN(true).then(function() {
190 case AuthMethod
.THIRD_PARTY
:
191 this.credentials_
.getThirdPartyToken(
192 'fake_token_url', 'fake_host_publicKey', 'fake_scope'
197 case AuthMethod
.PAIRING
:
200 return deferred
.promise();
204 * @param {remoting.MockClientPlugin.AuthMethod} authMethod
206 remoting
.MockClientPlugin
.prototype.mock
$useDefaultBehavior
=
207 function(authMethod
) {
209 var State
= remoting
.ClientSession
.State
;
210 this.mock
$onConnect().then(function() {
211 that
.mock
$setConnectionStatus(State
.CONNECTING
);
212 return that
.mock
$authenticate(authMethod
);
214 that
.mock
$setConnectionStatus(State
.CONNECTED
);
220 * @implements {remoting.ClientPluginFactory}
222 remoting
.MockClientPluginFactory = function() {
224 this.plugin_
= new remoting
.MockClientPlugin();
227 remoting
.MockClientPluginFactory
.prototype.createPlugin
=
228 function(container
, capabilities
) {
229 this.plugin_
.mock
$setContainer(container
);
230 this.plugin_
.mock
$capabilities
= capabilities
;
234 /** @return {remoting.MockClientPlugin} */
235 remoting
.MockClientPluginFactory
.prototype.plugin = function() {
239 remoting
.MockClientPluginFactory
.prototype.preloadPlugin = function() {};
242 * A class that sets up all the dependencies required for mocking a connection.
246 remoting
.MockConnection = function() {
248 this.originalPluginFactory_
= remoting
.ClientPlugin
.factory
;
251 this.pluginFactory_
= new remoting
.MockClientPluginFactory();
252 remoting
.ClientPlugin
.factory
= this.pluginFactory_
;
255 this.mockSignalStrategy_
= new remoting
.MockSignalStrategy(
256 'fake_jid', remoting
.SignalStrategy
.Type
.XMPP
);
258 /** @private {sinon.TestStub} */
259 this.createSignalStrategyStub_
=
260 sinon
.stub(remoting
.SignalStrategy
, 'create');
261 this.createSignalStrategyStub_
.returns(this.mockSignalStrategy_
);
264 this.originalIdentity_
= remoting
.identity
;
265 remoting
.identity
= new remoting
.Identity();
266 var identityStub
= sinon
.stub(remoting
.identity
, 'getUserInfo');
267 identityStub
.returns(Promise
.resolve({email
: 'email', userName
: 'userName'}));
270 this.originalSettings_
= remoting
.settings
;
271 remoting
.settings
= new remoting
.Settings();
274 /** @return {remoting.MockClientPlugin} */
275 remoting
.MockConnection
.prototype.plugin = function() {
276 return this.pluginFactory_
.plugin();
279 /** @return {remoting.MockSignalStrategy} */
280 remoting
.MockConnection
.prototype.signalStrategy = function() {
281 return this.mockSignalStrategy_
;
284 remoting
.MockConnection
.prototype.restore = function() {
285 remoting
.settings
= this.originalSettings_
;
286 remoting
.identity
= this.originalIdentity_
;
287 remoting
.ClientPlugin
.factory
= this.originalPluginFactory_
;
288 this.createSignalStrategyStub_
.restore();
293 /** @enum {string} */
294 remoting
.MockClientPlugin
.AuthMethod
= {
295 ACCESS_CODE
: 'accessCode',
297 THIRD_PARTY
: 'thirdParty',