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 this.onDisposedDeferred_ = new base.Deferred();
55 * @private {?function(remoting.MockClientPlugin,
56 * remoting.ClientSession.State)}
58 this.mock$onPluginStatusChanged_ = null;
61 remoting.MockClientPlugin.prototype.dispose = function() {
62 this.container_.removeChild(this.element_);
64 this.connectionStatusUpdateHandler_ = null;
65 this.onDisposedDeferred_.resolve();
68 remoting.MockClientPlugin.prototype.extensions = function() {
69 return this.extensions_;
72 remoting.MockClientPlugin.prototype.hostDesktop = function() {
73 return this.hostDesktop_;
76 remoting.MockClientPlugin.prototype.element = function() {
80 remoting.MockClientPlugin.prototype.initialize = function(onDone) {
82 Promise.resolve().then(function() {
83 onDone(that.mock$initializationResult);
88 remoting.MockClientPlugin.prototype.connect =
89 function(host, localJid, credentialsProvider) {
90 this.credentials_ = credentialsProvider;
91 this.onConnectDeferred_.resolve();
94 remoting.MockClientPlugin.prototype.injectKeyCombination = function(keys) {};
96 remoting.MockClientPlugin.prototype.injectKeyEvent =
97 function(key, down) {};
99 remoting.MockClientPlugin.prototype.setRemapKeys = function(remappings) {};
101 remoting.MockClientPlugin.prototype.remapKey = function(from, to) {};
103 remoting.MockClientPlugin.prototype.releaseAllKeys = function() {};
105 remoting.MockClientPlugin.prototype.onIncomingIq = function(iq) {};
107 remoting.MockClientPlugin.prototype.isSupportedVersion = function() {
111 remoting.MockClientPlugin.prototype.hasFeature = function(feature) {
115 remoting.MockClientPlugin.prototype.hasCapability = function(capability) {
116 return this.mock$capabilities.indexOf(capability) !== -1;
119 remoting.MockClientPlugin.prototype.sendClipboardItem =
120 function(mimeType, item) {};
122 remoting.MockClientPlugin.prototype.enableTouchEvents =
125 remoting.MockClientPlugin.prototype.requestPairing =
126 function(clientName, onDone) {};
128 remoting.MockClientPlugin.prototype.allowMouseLock = function() {};
130 remoting.MockClientPlugin.prototype.pauseAudio = function(pause) {};
132 remoting.MockClientPlugin.prototype.pauseVideo = function(pause) {};
134 remoting.MockClientPlugin.prototype.getPerfStats = function() {
135 var result = new remoting.ClientSession.PerfStats;
136 result.videoBandwidth = 999;
137 result.videoFrameRate = 60;
138 result.captureLatency = 10;
139 result.encodeLatency = 10;
140 result.decodeLatency = 10;
141 result.renderLatency = 10;
142 result.roundtripLatency = 10;
146 remoting.MockClientPlugin.prototype.setConnectionEventHandler =
148 this.connectionEventHandler_ = handler;
151 remoting.MockClientPlugin.prototype.setMouseCursorHandler =
152 function(handler) {};
154 remoting.MockClientPlugin.prototype.setClipboardHandler = function(handler) {};
156 remoting.MockClientPlugin.prototype.setDebugDirtyRegionHandler =
157 function(handler) {};
159 /** @param {Element} container */
160 remoting.MockClientPlugin.prototype.mock$setContainer = function(container) {
161 this.container_ = container;
162 this.container_.appendChild(this.element_);
165 /** @return {Promise} */
166 remoting.MockClientPlugin.prototype.mock$onConnect = function() {
167 this.onConnectDeferred_ = new base.Deferred();
168 return this.onConnectDeferred_.promise();
172 * @return {Promise} Returns a promise that will resolve when the plugin is
175 remoting.MockClientPlugin.prototype.mock$onDisposed = function() {
176 this.onDisposedDeferred_ = new base.Deferred();
177 return this.onDisposedDeferred_.promise();
181 * @param {remoting.ClientSession.State} status
182 * @param {remoting.ClientSession.ConnectionError=} opt_error
184 remoting.MockClientPlugin.prototype.mock$setConnectionStatus = function(
186 console.assert(this.connectionEventHandler_ !== null,
187 '|connectionEventHandler_| is null.');
188 var PluginError = remoting.ClientSession.ConnectionError;
189 var error = opt_error ? opt_error : PluginError.NONE;
190 this.connectionEventHandler_.onConnectionStatusUpdate(status, error);
191 if (this.mock$onPluginStatusChanged_) {
192 this.mock$onPluginStatusChanged_(this, status);
197 * @param {remoting.MockClientPlugin.AuthMethod} authMethod
200 remoting.MockClientPlugin.prototype.mock$authenticate = function(authMethod) {
201 var AuthMethod = remoting.MockClientPlugin.AuthMethod;
202 var deferred = new base.Deferred();
207 case AuthMethod.ACCESS_CODE:
208 this.credentials_.getPIN(true).then(function() {
212 case AuthMethod.THIRD_PARTY:
213 this.credentials_.getThirdPartyToken(
214 'fake_token_url', 'fake_host_publicKey', 'fake_scope'
219 case AuthMethod.PAIRING:
222 return deferred.promise();
226 * @param {?function(remoting.MockClientPlugin, remoting.ClientSession.State)}
229 remoting.MockClientPlugin.prototype.mock$setPluginStatusChanged =
231 this.mock$onPluginStatusChanged_ = callback;
235 * @param {remoting.MockClientPlugin.AuthMethod} authMethod
237 remoting.MockClientPlugin.prototype.mock$useDefaultBehavior =
238 function(authMethod) {
240 var State = remoting.ClientSession.State;
241 this.mock$onConnect().then(function() {
242 that.mock$setConnectionStatus(State.CONNECTING);
243 return that.mock$authenticate(authMethod);
245 that.mock$setConnectionStatus(State.AUTHENTICATED);
247 that.mock$setConnectionStatus(State.CONNECTED);
252 * @param {remoting.ClientSession.ConnectionError} error
254 remoting.MockClientPlugin.prototype.mock$returnErrorOnConnect = function(error){
256 var State = remoting.ClientSession.State;
257 this.mock$onConnect().then(function() {
258 that.mock$setConnectionStatus(State.CONNECTING);
260 that.mock$setConnectionStatus(State.FAILED, error);
266 * @implements {remoting.ClientPluginFactory}
268 remoting.MockClientPluginFactory = function() {
269 /** @private {?remoting.MockClientPlugin} */
273 * @private {?function(remoting.MockClientPlugin)}
275 this.onPluginCreated_ = null;
278 * @private {?function(remoting.MockClientPlugin,
279 * remoting.ClientSession.State)}
281 this.onPluginStatusChanged_ = null;
284 remoting.MockClientPluginFactory.prototype.createPlugin =
285 function(container, capabilities) {
286 this.plugin_ = new remoting.MockClientPlugin();
287 this.plugin_.mock$setContainer(container);
288 this.plugin_.mock$capabilities = capabilities;
290 // Notify the listeners on plugin creation.
291 if (Boolean(this.onPluginCreated_)) {
292 this.onPluginCreated_(this.plugin_);
294 this.plugin_.mock$useDefaultBehavior(
295 remoting.MockClientPlugin.AuthMethod.PIN);
298 // Listens for plugin status changed.
299 if (this.onPluginStatusChanged_) {
300 this.plugin_.mock$setPluginStatusChanged(this.onPluginStatusChanged_);
306 * Register a callback to configure the plugin before it returning to the
309 * @param {function(remoting.MockClientPlugin)} callback
311 remoting.MockClientPluginFactory.prototype.mock$setPluginCreated =
313 this.onPluginCreated_ = callback;
317 * @param {?function(remoting.MockClientPlugin, remoting.ClientSession.State)}
320 remoting.MockClientPluginFactory.prototype.mock$setPluginStatusChanged =
322 this.onPluginStatusChanged_ = callback;
326 /** @return {remoting.MockClientPlugin} */
327 remoting.MockClientPluginFactory.prototype.plugin = function() {
331 remoting.MockClientPluginFactory.prototype.preloadPlugin = function() {};
334 * A class that sets up all the dependencies required for mocking a connection.
338 remoting.MockConnection = function() {
340 this.originalPluginFactory_ = remoting.ClientPlugin.factory;
343 this.pluginFactory_ = new remoting.MockClientPluginFactory();
344 remoting.ClientPlugin.factory = this.pluginFactory_;
347 this.mockSignalStrategy_ = new remoting.MockSignalStrategy(
348 'fake_jid', remoting.SignalStrategy.Type.XMPP);
350 /** @private {sinon.TestStub} */
351 this.createSignalStrategyStub_ =
352 sinon.stub(remoting.SignalStrategy, 'create');
353 this.createSignalStrategyStub_.returns(this.mockSignalStrategy_);
356 this.originalIdentity_ = remoting.identity;
357 remoting.identity = new remoting.Identity();
358 var identityStub = sinon.stub(remoting.identity, 'getUserInfo');
359 identityStub.returns(Promise.resolve({email: 'email', userName: 'userName'}));
362 this.originalSettings_ = remoting.settings;
363 remoting.settings = new remoting.Settings();
366 /** @return {remoting.MockClientPluginFactory} */
367 remoting.MockConnection.prototype.pluginFactory = function() {
368 return this.pluginFactory_;
371 /** @return {remoting.MockClientPlugin} */
372 remoting.MockConnection.prototype.plugin = function() {
373 return this.pluginFactory_.plugin();
376 /** @return {remoting.MockSignalStrategy} */
377 remoting.MockConnection.prototype.signalStrategy = function() {
378 return this.mockSignalStrategy_;
381 remoting.MockConnection.prototype.restore = function() {
382 remoting.settings = this.originalSettings_;
383 remoting.identity = this.originalIdentity_;
384 remoting.ClientPlugin.factory = this.originalPluginFactory_;
385 this.createSignalStrategyStub_.restore();
390 /** @enum {string} */
391 remoting.MockClientPlugin.AuthMethod = {
392 ACCESS_CODE: 'accessCode',
394 THIRD_PARTY: 'thirdParty',