1 // Copyright 2015 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.
6 * @fileoverview A mock version of HostDaemonFacade. Internally all
7 * delays are implemented with promises, so SpyPromise can be used to
10 * By default, every method fails. Methods can be individually set to
11 * pass specific values to their onDone arguments by setting member
12 * variables of the mock object.
14 * When methods fail, they set the detail field of the remoting.Error
15 * object to the name of the method that failed.
18 /** @suppress {duplicate} */
19 var remoting
= remoting
|| {};
26 * By default, all methods fail.
29 remoting
.MockHostDaemonFacade = function() {
30 /** @type {Array<remoting.HostController.Feature>} */
33 /** @type {?string} */
37 * A function to generate a fake PIN hash given a host ID and a PIN.
38 * @type {?function(string,string):string}
40 this.pinHashFunc
= null;
42 /** @type {?string} */
43 this.privateKey
= null;
45 /** @type {?string} */
46 this.publicKey
= null;
49 this.daemonConfig
= null;
51 /** @type {?string} */
52 this.daemonVersion
= null;
54 /** @type {?boolean} */
55 this.consentSupported
= null;
57 /** @type {?boolean} */
58 this.consentAllowed
= null;
60 /** @type {?boolean} */
61 this.consentSetByPolicy
= null;
63 /** @type {?remoting.HostController.AsyncResult} */
64 this.startDaemonResult
= null;
66 /** @type {?remoting.HostController.AsyncResult} */
67 this.stopDaemonResult
= null;
69 /** @type {?remoting.HostController.State} */
70 this.daemonState
= null;
72 /** @type {?remoting.HostController.AsyncResult} */
73 this.updateDaemonConfigResult
= null;
75 /** @type {Array<remoting.PairedClient>} */
76 this.pairedClients
= null;
78 /** @type {?string} */
79 this.hostClientId
= null;
81 /** @type {?string} */
82 this.userEmail
= null;
84 /** @type {?string} */
85 this.refreshToken
= null;
89 * @param {remoting.HostController.Feature} feature
90 * @return {!Promise<boolean>}
92 remoting
.MockHostDaemonFacade
.prototype.hasFeature = function(feature
) {
94 return Promise
.resolve().then(function() {
95 return that
.features
.indexOf(feature
) >= 0;
100 * @return {!Promise<string>}
102 remoting
.MockHostDaemonFacade
.prototype.getHostName = function() {
104 return Promise
.resolve().then(function() {
105 if (that
.hostName
=== null) {
106 throw remoting
.Error
.unexpected('getHostName');
108 return that
.hostName
;
114 * @param {string} hostId
115 * @param {string} pin
116 * @return {!Promise<string>}
118 remoting
.MockHostDaemonFacade
.prototype.getPinHash = function(hostId
, pin
) {
120 return Promise
.resolve().then(function() {
121 if (that
.pinHashFunc
=== null) {
122 throw remoting
.Error
.unexpected('getPinHash');
124 return that
.pinHashFunc(hostId
, pin
);
130 * @return {!Promise<{privateKey:string, publicKey:string}>}
132 remoting
.MockHostDaemonFacade
.prototype.generateKeyPair = function() {
134 return Promise
.resolve().then(function() {
135 if (that
.privateKey
=== null || that
.publicKey
=== null) {
136 throw remoting
.Error
.unexpected('generateKeyPair');
139 privateKey
: that
.privateKey
,
140 publicKey
: that
.publicKey
147 * @param {Object} config The new config parameters.
148 * @return {!Promise<remoting.HostController.AsyncResult>}
150 remoting
.MockHostDaemonFacade
.prototype.updateDaemonConfig = function(config
) {
152 return Promise
.resolve().then(function() {
153 if (that
.daemonConfig
=== null ||
154 that
.updateDaemonConfigResult
=== null ||
155 'host_id' in config
||
156 'xmpp_login' in config
) {
157 throw remoting
.Error
.unexpected('updateDaemonConfig');
158 } else if (that
.updateDaemonConfigResult
!=
159 remoting
.HostController
.AsyncResult
.OK
) {
160 return that
.updateDaemonConfigResult
;
162 base
.mix(that
.daemonConfig
, config
);
163 return remoting
.HostController
.AsyncResult
.OK
;
169 * @return {!Promise<Object>}
171 remoting
.MockHostDaemonFacade
.prototype.getDaemonConfig = function() {
173 return Promise
.resolve().then(function() {
174 if (that
.daemonConfig
=== null) {
175 throw remoting
.Error
.unexpected('getDaemonConfig');
177 return that
.daemonConfig
;
183 * @return {!Promise<string>}
185 remoting
.MockHostDaemonFacade
.prototype.getDaemonVersion = function() {
187 return Promise
.resolve().then(function() {
188 if (that
.daemonVersion
=== null) {
189 throw remoting
.Error
.unexpected('getDaemonVersion');
191 return that
.daemonVersion
;
197 * @return {!Promise<remoting.UsageStatsConsent>}
199 remoting
.MockHostDaemonFacade
.prototype.getUsageStatsConsent = function() {
201 return Promise
.resolve().then(function() {
202 if (that
.consentSupported
=== null ||
203 that
.consentAllowed
=== null ||
204 that
.consentSetByPolicy
=== null) {
205 throw remoting
.Error
.unexpected('getUsageStatsConsent');
208 supported
: that
.consentSupported
,
209 allowed
: that
.consentAllowed
,
210 setByPolicy
: that
.consentSetByPolicy
217 * @param {Object} config
218 * @param {boolean} consent Consent to report crash dumps.
219 * @return {!Promise<remoting.HostController.AsyncResult>}
221 remoting
.MockHostDaemonFacade
.prototype.startDaemon
=
222 function(config
, consent
) {
224 return Promise
.resolve().then(function() {
225 if (that
.startDaemonResult
=== null) {
226 throw remoting
.Error
.unexpected('startDaemon');
228 return that
.startDaemonResult
;
234 * @return {!Promise<remoting.HostController.AsyncResult>}
236 remoting
.MockHostDaemonFacade
.prototype.stopDaemon
=
239 return Promise
.resolve().then(function() {
240 if (that
.stopDaemonResult
=== null) {
241 throw remoting
.Error
.unexpected('stopDaemon');
243 return that
.stopDaemonResult
;
249 * @return {!Promise<remoting.HostController.State>}
251 remoting
.MockHostDaemonFacade
.prototype.getDaemonState
=
254 return Promise
.resolve().then(function() {
255 if (that
.daemonState
=== null) {
256 throw remoting
.Error
.unexpected('getDaemonState');
258 return that
.daemonState
;
264 * @return {!Promise<Array<remoting.PairedClient>>}
266 remoting
.MockHostDaemonFacade
.prototype.getPairedClients
=
269 return Promise
.resolve().then(function() {
270 if (that
.pairedClients
=== null) {
271 throw remoting
.Error
.unexpected('getPairedClients');
273 return that
.pairedClients
;
279 * @return {!Promise<boolean>}
281 remoting
.MockHostDaemonFacade
.prototype.clearPairedClients = function() {
283 return Promise
.resolve().then(function() {
284 if (that
.pairedClients
=== null) {
285 throw remoting
.Error
.unexpected('clearPairedClients');
287 that
.pairedClients
= [];
288 return true; // TODO(jrw): Not always correct.
294 * @param {string} client
295 * @return {!Promise<boolean>}
297 remoting
.MockHostDaemonFacade
.prototype.deletePairedClient = function(client
) {
299 return Promise
.resolve().then(function() {
300 if (that
.pairedClients
=== null) {
301 throw remoting
.Error
.unexpected('deletePairedClient');
303 that
.pairedClients
= that
.pairedClients
.filter(function(c
) {
304 return c
['clientId'] != client
;
306 return true; // TODO(jrw): Not always correct.
312 * @return {!Promise<string>}
314 remoting
.MockHostDaemonFacade
.prototype.getHostClientId = function() {
316 return Promise
.resolve().then(function() {
317 if (that
.hostClientId
=== null) {
318 throw remoting
.Error
.unexpected('getHostClientId');
320 return that
.hostClientId
;
326 * @param {string} authorizationCode
327 * @return {!Promise<{userEmail:string, refreshToken:string}>}
329 remoting
.MockHostDaemonFacade
.prototype.getCredentialsFromAuthCode
=
330 function(authorizationCode
) {
332 return Promise
.resolve().then(function() {
333 if (that
.userEmail
=== null || that
.refreshToken
=== null) {
334 throw remoting
.Error
.unexpected('getCredentialsFromAuthCode');
337 userEmail
: that
.userEmail
,
338 refreshToken
: that
.refreshToken