Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(440)

Side by Side Diff: remoting/webapp/crd/js/host_daemon_facade_unittest.js

Issue 1060793003: Added unit test for HostDaemonFacade. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@mock-xhr
Patch Set: license Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « remoting/remoting_webapp_files.gypi ('k') | remoting/webapp/js_proto/chrome_mocks.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 /**
6 * @fileoverview
7 * Unit test for host_daemon_facade.js.
8 */
9
10 /** @type {chromeMocks.runtime.Port} */
11 var nativePortMock;
12
13 (function() {
14 'use strict';
15
16 /** @type {sinon.TestStub} */
17 var postMessageStub;
18
19 /** @type {Array<Object>} */
20 var mockHostResponses;
21
22 /** @type {remoting.HostDaemonFacade} */
23 var it;
24
25 QUnit.module('host_daemon_facade', {
26 beforeEach: function(/** QUnit.Assert */ assert) {
27 chromeMocks.activate(['runtime']);
28 chromeMocks.identity.mock$setToken('my_token');
29 nativePortMock =
30 chromeMocks.runtime.connectNative('com.google.chrome.remote_desktop');
31 mockHostResponses = [];
32 postMessageStub = sinon.stub(
33 nativePortMock, 'postMessage', sendMockHostResponse);
34 },
35 afterEach: function(/** QUnit.Assert */ assert) {
36 if (mockHostResponses.length) {
37 throw new Error('responses not all used');
38 }
39 mockHostResponses = null;
40 postMessageStub.restore();
41 chromeMocks.restore();
42 it = null;
43 }
44 });
45
46 function sendMockHostResponse() {
47 if (mockHostResponses.length == 0) {
48 throw new Error('don\'t know how to responsd');
49 }
50 var toSend = mockHostResponses.pop();
51 Promise.resolve().then(function() {
52 nativePortMock.onMessage.mock$fire(toSend);
53 });
54 }
55
56 QUnit.test('initialize/hasFeature true', function(assert) {
57 mockHostResponses.push({
58 id: 0,
59 type: 'helloResponse',
60 version: '',
61 supportedFeatures: [
62 remoting.HostController.Feature.OAUTH_CLIENT,
63 remoting.HostController.Feature.PAIRING_REGISTRY
64 ]
65 });
66 it = new remoting.HostDaemonFacade();
67 assert.deepEqual(postMessageStub.args[0][0], {
68 id: 0,
69 type: 'hello'
70 });
71 var done = assert.async();
72 it.hasFeature(
73 remoting.HostController.Feature.PAIRING_REGISTRY,
74 function onDone(arg) {
75 assert.equal(arg, true);
76 done();
77 });
78 });
79
80 QUnit.test('initialize/hasFeature false', function(assert) {
81 mockHostResponses.push({
82 id: 0,
83 type: 'helloResponse',
84 version: '',
85 supportedFeatures: []
86 });
87 it = new remoting.HostDaemonFacade();
88 assert.deepEqual(postMessageStub.args[0][0], {
89 id: 0,
90 type: 'hello'
91 });
92 var done = assert.async();
93 it.hasFeature(
94 remoting.HostController.Feature.PAIRING_REGISTRY,
95 function onDone(arg) {
96 assert.equal(arg, false);
97 done();
98 });
99 });
100
101 QUnit.test('initialize/getDaemonVersion', function(assert) {
102 mockHostResponses.push({
103 id: 0,
104 type: 'helloResponse',
105 version: '<daemonVersion>',
106 supportedFeatures: []
107 });
108 it = new remoting.HostDaemonFacade();
109 assert.deepEqual(postMessageStub.args[0][0], {
110 id: 0,
111 type: 'hello'
112 });
113 var done = assert.async();
114 it.getDaemonVersion(
115 function onDone(arg) {
116 assert.equal(arg, '<daemonVersion>');
117 done();
118 },
119 function onError() {
120 assert.ok(false);
121 done();
122 });
123 });
124
125 /**
126 * @param {string} description
127 * @param {function(!QUnit.Assert):*} callback
128 */
129 function postInitTest(description, callback) {
130 QUnit.test(description, function(assert) {
131 mockHostResponses.push({
132 id: 0,
133 type: 'helloResponse',
134 version: ''
135 });
136 base.debug.assert(it == null);
137 it = new remoting.HostDaemonFacade();
138 assert.deepEqual(postMessageStub.args[0][0], {
139 id: 0,
140 type: 'hello'
141 });
142 return new Promise(function(resolve, reject) {
143 it.getDaemonVersion(resolve, reject);
144 }).then(function() {
145 return callback(assert);
146 });
147 });
148 }
149
150 /**
151 * A combinator that turns an ordinary 1-argument resolve function
152 * into a function that accepts multiple arguments and bundles them
153 * into an array.
154 * @param {function(*):void} resolve
155 * @return {function(...*):void}
156 */
157 function resolveMulti(resolve) {
158 return function() {
159 resolve(Array.prototype.slice.call(arguments));
160 };
161 }
162
163 postInitTest('getHostName', function(assert) {
164 mockHostResponses.push({
165 id: 1,
166 type: 'getHostNameResponse',
167 hostname: '<fakeHostName>'
168 });
169 return new Promise(function (resolve, reject) {
170 it.getHostName(resolve, reject);
171 }).then(function(/** string */ hostName) {
172 assert.deepEqual(postMessageStub.args[1][0], {
173 id: 1,
174 type: 'getHostName'
175 });
176 assert.equal(hostName, '<fakeHostName>');
177 });
178 });
179
180 postInitTest('getPinHash', function(assert) {
181 mockHostResponses.push({
182 id: 1,
183 type: 'getPinHashResponse',
184 hash: '<fakePinHash>'
185 });
186 return new Promise(function (resolve, reject) {
187 it.getPinHash('<hostId>', '<pin>', resolve, reject);
188 }).then(function(/** string */ hostName) {
189 assert.deepEqual(postMessageStub.args[1][0], {
190 id: 1,
191 type: 'getPinHash',
192 hostId: '<hostId>',
193 pin: '<pin>'
194 });
195 assert.equal(hostName, '<fakePinHash>');
196 });
197 });
198
199 postInitTest('generateKeyPair', function(assert) {
200 mockHostResponses.push({
201 id: 1,
202 type: 'generateKeyPairResponse',
203 privateKey: '<fakePrivateKey>',
204 publicKey: '<fakePublicKey>'
205 });
206 return new Promise(function (resolve, reject) {
207 it.generateKeyPair(resolveMulti(resolve), reject);
208 }).then(function(/** Array */ result) {
209 assert.deepEqual(postMessageStub.args[1][0], {
210 id: 1,
211 type: 'generateKeyPair'
212 });
213 assert.deepEqual(result, ['<fakePrivateKey>', '<fakePublicKey>']);
214 });
215 });
216
217 postInitTest('updateDaemonConfig', function(assert) {
218 mockHostResponses.push({
219 id: 1,
220 type: 'updateDaemonConfigResponse',
221 result: 'OK'
222 });
223 return new Promise(function (resolve, reject) {
224 it.updateDaemonConfig({ fakeDaemonConfig: true }, resolve, reject);
225 }).then(function(/** * */ result) {
226 assert.deepEqual(postMessageStub.args[1][0], {
227 id: 1,
228 type: 'updateDaemonConfig',
229 config: { fakeDaemonConfig: true }
230 });
231 assert.equal(result, remoting.HostController.AsyncResult.OK);
232 });
233 });
234
235 postInitTest('getDaemonConfig', function(assert) {
236 mockHostResponses.push({
237 id: 1,
238 type: 'getDaemonConfigResponse',
239 config: { fakeDaemonConfig: true }
240 });
241 return new Promise(function (resolve, reject) {
242 it.getDaemonConfig(resolve, reject);
243 }).then(function(/** * */ result) {
244 assert.deepEqual(postMessageStub.args[1][0], {
245 id: 1,
246 type: 'getDaemonConfig'
247 });
248 assert.deepEqual(result, { fakeDaemonConfig: true });
249 });
250 });
251
252 [0,1,2,3,4,5,6,7].forEach(function(/** number */ flags) {
253 postInitTest('getUsageStatsConsent, flags=' + flags, function(assert) {
254 var supported = Boolean(flags & 1);
255 var allowed = Boolean(flags & 2);
256 var setByPolicy = Boolean(flags & 4);
257 mockHostResponses.push({
258 id: 1,
259 type: 'getUsageStatsConsentResponse',
260 supported: supported,
261 allowed: allowed,
262 setByPolicy: setByPolicy
263 });
264 return new Promise(function (resolve, reject) {
265 it.getUsageStatsConsent(resolveMulti(resolve), reject);
266 }).then(function(/** * */ result) {
267 assert.deepEqual(postMessageStub.args[1][0], {
268 id: 1,
269 type: 'getUsageStatsConsent'
270 });
271 assert.deepEqual(result, [supported, allowed, setByPolicy]);
272 });
273 });
274 });
275
276 [false, true].forEach(function(/** boolean */ consent) {
277 postInitTest('startDaemon, consent=' + consent, function(assert) {
278 mockHostResponses.push({
279 id: 1,
280 type: 'startDaemonResponse',
281 result: 'FAILED'
282 });
283 return new Promise(function (resolve, reject) {
284 it.startDaemon({ fakeConfig: true }, consent, resolve, reject);
285 }).then(function(/** * */ result) {
286 assert.deepEqual(postMessageStub.args[1][0], {
287 id: 1,
288 type: 'startDaemon',
289 config: { fakeConfig: true },
290 consent: consent
291 });
292 assert.equal(result, remoting.HostController.AsyncResult.FAILED);
293 });
294 });
295 });
296
297 postInitTest('stopDaemon', function(assert) {
298 mockHostResponses.push({
299 id: 1,
300 type: 'stopDaemonResponse',
301 result: 'CANCELLED'
302 });
303 return new Promise(function (resolve, reject) {
304 it.stopDaemon(resolve, reject);
305 }).then(function(/** * */ result) {
306 assert.deepEqual(postMessageStub.args[1][0], {
307 id: 1,
308 type: 'stopDaemon'
309 });
310 assert.equal(result, remoting.HostController.AsyncResult.CANCELLED);
311 });
312 });
313
314 postInitTest('getPairedClients', function(assert) {
315 /**
316 * @param {number} n
317 * @return {remoting.PairedClient}
318 */
319 function makeClient(n) {
320 return /** @type {remoting.PairedClient} */ ({
321 clientId: '<fakeClientId' + n + '>',
322 clientName: '<fakeClientName' + n + '>',
323 createdTime: n * 316571 // random prime number
324 });
325 };
326
327 var client0 = makeClient(0);
328 var client1 = makeClient(1);
329 mockHostResponses.push({
330 id: 1,
331 type: 'getPairedClientsResponse',
332 pairedClients: [client0, client1]
333 });
334 return new Promise(function (resolve, reject) {
335 it.getPairedClients(resolve, reject);
336 }).then(function(/** Array<remoting.PairedClient> */ result) {
337 assert.deepEqual(postMessageStub.args[1][0], {
338 id: 1,
339 type: 'getPairedClients'
340 });
341 // Our facade is not really a facade! It adds extra fields.
342 // TODO(jrw): Move non-facade logic to host_controller.js.
343 assert.equal(result.length, 2);
344 assert.equal(result[0].clientId, '<fakeClientId0>');
345 assert.equal(result[0].clientName, '<fakeClientName0>');
346 assert.equal(result[0].createdTime, client0.createdTime);
347 assert.equal(typeof result[0].createDom, 'function');
348 assert.equal(result[1].clientId, '<fakeClientId1>');
349 assert.equal(result[1].clientName, '<fakeClientName1>');
350 assert.equal(result[1].createdTime, client1.createdTime);
351 assert.equal(typeof result[1].createDom, 'function');
352 });
353 });
354
355 [false, true].map(function(/** boolean */ deleted) {
356 postInitTest('clearPairedClients, deleted=' + deleted, function(assert) {
357 mockHostResponses.push({
358 id: 1,
359 type: 'clearPairedClientsResponse',
360 result: deleted
361 });
362 return new Promise(function (resolve, reject) {
363 it.clearPairedClients(resolve, reject);
364 }).then(function(/** * */ result) {
365 assert.deepEqual(postMessageStub.args[1][0], {
366 id: 1,
367 type: 'clearPairedClients'
368 });
369 assert.equal(result, deleted);
370 });
371 });
372 });
373
374 [false, true].map(function(/** boolean */ deleted) {
375 postInitTest('deletePairedClient, deleted=' + deleted, function(assert) {
376 mockHostResponses.push({
377 id: 1,
378 type: 'deletePairedClientResponse',
379 result: deleted
380 });
381 return new Promise(function (resolve, reject) {
382 it.deletePairedClient('<fakeClientId>', resolve, reject);
383 }).then(function(/** * */ result) {
384 assert.deepEqual(postMessageStub.args[1][0], {
385 id: 1,
386 type: 'deletePairedClient',
387 clientId: '<fakeClientId>'
388 });
389 assert.equal(result, deleted);
390 });
391 });
392 });
393
394 postInitTest('getHostClientId', function(assert) {
395 mockHostResponses.push({
396 id: 1,
397 type: 'getHostClientIdResponse',
398 clientId: '<fakeClientId>'
399 });
400 return new Promise(function (resolve, reject) {
401 it.getHostClientId(resolve, reject);
402 }).then(function(/** * */ result) {
403 assert.deepEqual(postMessageStub.args[1][0], {
404 id: 1,
405 type: 'getHostClientId'
406 });
407 assert.equal(result, '<fakeClientId>');
408 });
409 });
410
411 postInitTest('getCredentialsFromAuthCode', function(assert) {
412 mockHostResponses.push({
413 id: 1,
414 type: 'getCredentialsFromAuthCodeResponse',
415 userEmail: '<fakeUserEmail>',
416 refreshToken: '<fakeRefreshToken>'
417 });
418 return new Promise(function (resolve, reject) {
419 it.getCredentialsFromAuthCode(
420 '<fakeAuthCode>', resolveMulti(resolve), reject);
421 }).then(function(/** * */ result) {
422 assert.deepEqual(postMessageStub.args[1][0], {
423 id: 1,
424 type: 'getCredentialsFromAuthCode',
425 authorizationCode: '<fakeAuthCode>'
426 });
427 assert.deepEqual(result, ['<fakeUserEmail>', '<fakeRefreshToken>']);
428 });
429 });
430
431 })();
OLDNEW
« no previous file with comments | « remoting/remoting_webapp_files.gypi ('k') | remoting/webapp/js_proto/chrome_mocks.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698