Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / remoting / webapp / crd / js / mock_client_plugin.js
blobefb624b43ea62d4f1e27b6b6856c912ae56c18f9
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();
52 remoting.MockClientPlugin.prototype.dispose = function() {
53   this.container_.removeChild(this.element_);
54   this.element_ = null;
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() {
67   return this.element_;
70 remoting.MockClientPlugin.prototype.initialize = function(onDone) {
71   var that = this;
72   Promise.resolve().then(function() {
73     onDone(that.mock$initializationResult);
74   });
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() {
98   return true;
101 remoting.MockClientPlugin.prototype.hasFeature = function(feature) {
102   return false;
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 =
113     function(enable) {};
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;
133   return result;
136 remoting.MockClientPlugin.prototype.setConnectionEventHandler =
137     function(handler) {
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
164  */
165 remoting.MockClientPlugin.prototype.mock$setConnectionStatus = function(
166     status, opt_error) {
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
176  * @return {Promise}
177  */
178 remoting.MockClientPlugin.prototype.mock$authenticate = function(authMethod) {
179   var AuthMethod = remoting.MockClientPlugin.AuthMethod;
180   var deferred = new base.Deferred();
182   var that = this;
183   switch(authMethod) {
184     case AuthMethod.PIN:
185     case AuthMethod.ACCESS_CODE:
186       this.credentials_.getPIN(true).then(function() {
187         deferred.resolve();
188       });
189       break;
190     case AuthMethod.THIRD_PARTY:
191       this.credentials_.getThirdPartyToken(
192           'fake_token_url', 'fake_host_publicKey', 'fake_scope'
193       ).then(function() {
194         deferred.resolve();
195       });
196       break;
197     case AuthMethod.PAIRING:
198       deferred.resolve();
199   }
200   return deferred.promise();
204  * @param {remoting.MockClientPlugin.AuthMethod} authMethod
205  */
206 remoting.MockClientPlugin.prototype.mock$useDefaultBehavior =
207     function(authMethod) {
208   var that = this;
209   var State = remoting.ClientSession.State;
210   this.mock$onConnect().then(function() {
211     that.mock$setConnectionStatus(State.CONNECTING);
212     return that.mock$authenticate(authMethod);
213   }).then(function() {
214     that.mock$setConnectionStatus(State.CONNECTED);
215   });
219  * @constructor
220  * @implements {remoting.ClientPluginFactory}
221  */
222 remoting.MockClientPluginFactory = function() {
223   /** @private */
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;
231   return this.plugin_;
234 /** @return {remoting.MockClientPlugin} */
235 remoting.MockClientPluginFactory.prototype.plugin = function() {
236   return this.plugin_;
239 remoting.MockClientPluginFactory.prototype.preloadPlugin = function() {};
242  * A class that sets up all the dependencies required for mocking a connection.
244  * @constructor
245  */
246 remoting.MockConnection = function() {
247   /** @private */
248   this.originalPluginFactory_ = remoting.ClientPlugin.factory;
250   /** @private */
251   this.pluginFactory_ = new remoting.MockClientPluginFactory();
252   remoting.ClientPlugin.factory = this.pluginFactory_;
254   /** @private */
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_);
263   /** @private */
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'}));
269   /** @private */
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();
291 })();
293 /** @enum {string} */
294 remoting.MockClientPlugin.AuthMethod = {
295   ACCESS_CODE: 'accessCode',
296   PIN: 'pin',
297   THIRD_PARTY: 'thirdParty',
298   PAIRING: 'pairing'