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.requestPairing =
113 function(clientName, onDone) {};
115 remoting.MockClientPlugin.prototype.allowMouseLock = function() {};
117 remoting.MockClientPlugin.prototype.pauseAudio = function(pause) {};
119 remoting.MockClientPlugin.prototype.pauseVideo = function(pause) {};
121 remoting.MockClientPlugin.prototype.getPerfStats = function() {
122 var result = new remoting.ClientSession.PerfStats;
123 result.videoBandwidth = 999;
124 result.videoFrameRate = 60;
125 result.captureLatency = 10;
126 result.encodeLatency = 10;
127 result.decodeLatency = 10;
128 result.renderLatency = 10;
129 result.roundtripLatency = 10;
133 remoting.MockClientPlugin.prototype.setConnectionEventHandler =
135 this.connectionEventHandler_ = handler;
138 remoting.MockClientPlugin.prototype.setMouseCursorHandler =
139 function(handler) {};
141 remoting.MockClientPlugin.prototype.setClipboardHandler = function(handler) {};
143 remoting.MockClientPlugin.prototype.setDebugDirtyRegionHandler =
144 function(handler) {};
146 /** @param {Element} container */
147 remoting.MockClientPlugin.prototype.mock$setContainer = function(container) {
148 this.container_ = container;
149 this.container_.appendChild(this.element_);
152 /** @return {Promise} */
153 remoting.MockClientPlugin.prototype.mock$onConnect = function() {
154 this.onConnectDeferred_ = new base.Deferred();
155 return this.onConnectDeferred_.promise();
159 * @param {remoting.ClientSession.State} status
160 * @param {remoting.ClientSession.ConnectionError=} opt_error
162 remoting.MockClientPlugin.prototype.mock$setConnectionStatus = function(
164 base.debug.assert(this.connectionEventHandler_ !== null);
165 var PluginError = remoting.ClientSession.ConnectionError;
166 var error = opt_error ? opt_error : PluginError.NONE;
167 this.connectionEventHandler_.onConnectionStatusUpdate(status, error);
171 * @param {remoting.MockClientPlugin.AuthMethod} authMethod
174 remoting.MockClientPlugin.prototype.mock$authenticate = function(authMethod) {
175 var AuthMethod = remoting.MockClientPlugin.AuthMethod;
176 var deferred = new base.Deferred();
181 case AuthMethod.ACCESS_CODE:
182 this.credentials_.getPIN(true).then(function() {
186 case AuthMethod.THIRD_PARTY:
187 this.credentials_.getThirdPartyToken(
188 'fake_token_url', 'fake_host_publicKey', 'fake_scope'
193 case AuthMethod.PAIRING:
196 return deferred.promise();
200 * @param {remoting.MockClientPlugin.AuthMethod} authMethod
202 remoting.MockClientPlugin.prototype.mock$useDefaultBehavior =
203 function(authMethod) {
205 var State = remoting.ClientSession.State;
206 this.mock$onConnect().then(function() {
207 that.mock$setConnectionStatus(State.CONNECTING);
208 return that.mock$authenticate(authMethod);
210 that.mock$setConnectionStatus(State.CONNECTED);
216 * @implements {remoting.ClientPluginFactory}
218 remoting.MockClientPluginFactory = function() {
220 this.plugin_ = new remoting.MockClientPlugin();
223 remoting.MockClientPluginFactory.prototype.createPlugin =
224 function(container, capabilities) {
225 this.plugin_.mock$setContainer(container);
226 this.plugin_.mock$capabilities = capabilities;
230 /** @return {remoting.MockClientPlugin} */
231 remoting.MockClientPluginFactory.prototype.plugin = function() {
235 remoting.MockClientPluginFactory.prototype.preloadPlugin = function() {};
238 * A class that sets up all the dependencies required for mocking a connection.
242 remoting.MockConnection = function() {
244 this.originalPluginFactory_ = remoting.ClientPlugin.factory;
247 this.pluginFactory_ = new remoting.MockClientPluginFactory();
248 remoting.ClientPlugin.factory = this.pluginFactory_;
251 this.mockSignalStrategy_ = new remoting.MockSignalStrategy(
252 'fake_jid', remoting.SignalStrategy.Type.XMPP);
254 /** @private {sinon.TestStub} */
255 this.createSignalStrategyStub_ =
256 sinon.stub(remoting.SignalStrategy, 'create');
257 this.createSignalStrategyStub_.returns(this.mockSignalStrategy_);
260 this.originalIdentity_ = remoting.identity;
261 remoting.identity = new remoting.Identity();
262 var identityStub = sinon.stub(remoting.identity, 'getUserInfo');
263 identityStub.returns(Promise.resolve({email: 'email', userName: 'userName'}));
266 this.originalSettings_ = remoting.settings;
267 remoting.settings = new remoting.Settings();
270 /** @return {remoting.MockClientPlugin} */
271 remoting.MockConnection.prototype.plugin = function() {
272 return this.pluginFactory_.plugin();
275 /** @return {remoting.MockSignalStrategy} */
276 remoting.MockConnection.prototype.signalStrategy = function() {
277 return this.mockSignalStrategy_;
280 remoting.MockConnection.prototype.restore = function() {
281 remoting.settings = this.originalSettings_;
282 remoting.identity = this.originalIdentity_;
283 remoting.ClientPlugin.factory = this.originalPluginFactory_;
284 this.createSignalStrategyStub_.restore();
289 /** @enum {string} */
290 remoting.MockClientPlugin.AuthMethod = {
291 ACCESS_CODE: 'accessCode',
293 THIRD_PARTY: 'thirdParty',