Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / remoting / webapp / crd / js / mock_client_plugin.js
blob319fec1ff8b6d3b8775e2f8bd3260f04f0440b72
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 /**
6  * @fileoverview
7  * Mock implementation of ClientPlugin for testing.
8  */
10 /** @suppress {duplicate} */
11 var remoting = remoting || {};
13 (function() {
15 'use strict';
17 /**
18  * @constructor
19  * @implements {remoting.ClientPlugin}
20  */
21 remoting.MockClientPlugin = function() {
22   /** @private {Element} */
23   this.container_ = null;
24   /** @private */
25   this.element_ = /** @type {HTMLElement} */ (document.createElement('div'));
26   this.element_.style.backgroundImage = 'linear-gradient(45deg, blue, red)';
27   /** @private */
28   this.hostDesktop_ =
29       new remoting.ClientPlugin.HostDesktopImpl(this, base.doNothing);
30   /** @private */
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
46   ];
48   /** @private */
49   this.onConnectDeferred_ = new base.Deferred();
51   /** @private */
52   this.onDisposedDeferred_ = new base.Deferred();
54   /**
55    * @private {?function(remoting.MockClientPlugin,
56    *                     remoting.ClientSession.State)}
57    */
58   this.mock$onPluginStatusChanged_ = null;
61 remoting.MockClientPlugin.prototype.dispose = function() {
62   this.container_.removeChild(this.element_);
63   this.element_ = null;
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() {
77   return this.element_;
80 remoting.MockClientPlugin.prototype.initialize = function(onDone) {
81   var that = this;
82   Promise.resolve().then(function() {
83     onDone(that.mock$initializationResult);
84   });
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() {
108   return true;
111 remoting.MockClientPlugin.prototype.hasFeature = function(feature) {
112   return false;
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 =
123     function(enable) {};
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;
143   return result;
146 remoting.MockClientPlugin.prototype.setConnectionEventHandler =
147     function(handler) {
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
173  *     disposed.
174  */
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
183  */
184 remoting.MockClientPlugin.prototype.mock$setConnectionStatus = function(
185     status, opt_error) {
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);
193   }
197  * @param {remoting.MockClientPlugin.AuthMethod} authMethod
198  * @return {Promise}
199  */
200 remoting.MockClientPlugin.prototype.mock$authenticate = function(authMethod) {
201   var AuthMethod = remoting.MockClientPlugin.AuthMethod;
202   var deferred = new base.Deferred();
204   var that = this;
205   switch(authMethod) {
206     case AuthMethod.PIN:
207     case AuthMethod.ACCESS_CODE:
208       this.credentials_.getPIN(true).then(function() {
209         deferred.resolve();
210       });
211       break;
212     case AuthMethod.THIRD_PARTY:
213       this.credentials_.getThirdPartyToken(
214           'fake_token_url', 'fake_host_publicKey', 'fake_scope'
215       ).then(function() {
216         deferred.resolve();
217       });
218       break;
219     case AuthMethod.PAIRING:
220       deferred.resolve();
221   }
222   return deferred.promise();
226  * @param {?function(remoting.MockClientPlugin, remoting.ClientSession.State)}
227  *    callback
228  */
229 remoting.MockClientPlugin.prototype.mock$setPluginStatusChanged =
230     function(callback) {
231   this.mock$onPluginStatusChanged_ = callback;
235  * @param {remoting.MockClientPlugin.AuthMethod} authMethod
236  */
237 remoting.MockClientPlugin.prototype.mock$useDefaultBehavior =
238     function(authMethod) {
239   var that = this;
240   var State = remoting.ClientSession.State;
241   this.mock$onConnect().then(function() {
242     that.mock$setConnectionStatus(State.CONNECTING);
243     return that.mock$authenticate(authMethod);
244   }).then(function() {
245     that.mock$setConnectionStatus(State.AUTHENTICATED);
246   }).then(function() {
247     that.mock$setConnectionStatus(State.CONNECTED);
248   });
252  * @param {remoting.ClientSession.ConnectionError} error
253  */
254 remoting.MockClientPlugin.prototype.mock$returnErrorOnConnect = function(error){
255   var that = this;
256   var State = remoting.ClientSession.State;
257   this.mock$onConnect().then(function() {
258     that.mock$setConnectionStatus(State.CONNECTING);
259   }).then(function() {
260     that.mock$setConnectionStatus(State.FAILED, error);
261   });
265  * @constructor
266  * @implements {remoting.ClientPluginFactory}
267  */
268 remoting.MockClientPluginFactory = function() {
269   /** @private {?remoting.MockClientPlugin} */
270   this.plugin_ = null;
272   /**
273    * @private {?function(remoting.MockClientPlugin)}
274    */
275   this.onPluginCreated_ = null;
277   /**
278    * @private {?function(remoting.MockClientPlugin,
279    *                     remoting.ClientSession.State)}
280    */
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_);
293   } else {
294     this.plugin_.mock$useDefaultBehavior(
295         remoting.MockClientPlugin.AuthMethod.PIN);
296   }
298   // Listens for plugin status changed.
299   if (this.onPluginStatusChanged_) {
300     this.plugin_.mock$setPluginStatusChanged(this.onPluginStatusChanged_);
301   }
302   return this.plugin_;
306  * Register a callback to configure the plugin before it returning to the
307  * caller.
309  * @param {function(remoting.MockClientPlugin)} callback
310  */
311 remoting.MockClientPluginFactory.prototype.mock$setPluginCreated =
312     function(callback) {
313   this.onPluginCreated_ = callback;
317  * @param {?function(remoting.MockClientPlugin, remoting.ClientSession.State)}
318  *    callback
319  */
320 remoting.MockClientPluginFactory.prototype.mock$setPluginStatusChanged =
321     function(callback) {
322   this.onPluginStatusChanged_ = callback;
326 /** @return {remoting.MockClientPlugin} */
327 remoting.MockClientPluginFactory.prototype.plugin = function() {
328   return this.plugin_;
331 remoting.MockClientPluginFactory.prototype.preloadPlugin = function() {};
334  * A class that sets up all the dependencies required for mocking a connection.
336  * @constructor
337  */
338 remoting.MockConnection = function() {
339   /** @private */
340   this.originalPluginFactory_ = remoting.ClientPlugin.factory;
342   /** @private */
343   this.pluginFactory_ = new remoting.MockClientPluginFactory();
344   remoting.ClientPlugin.factory = this.pluginFactory_;
346   /** @private */
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_);
355   /** @private */
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'}));
361   /** @private */
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();
388 })();
390 /** @enum {string} */
391 remoting.MockClientPlugin.AuthMethod = {
392   ACCESS_CODE: 'accessCode',
393   PIN: 'pin',
394   THIRD_PARTY: 'thirdParty',
395   PAIRING: 'pairing'