Convert cacheinvalidation_unittests to run exclusively on Swarming
[chromium-blink-merge.git] / remoting / webapp / crd / js / host_daemon_facade_unittest.js
blob2bfe8c84aff5eea3025b6266286f3d78121e2c98
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.
5 /**
6 * @fileoverview
7 * Unit test for host_daemon_facade.js.
8 */
10 /** @type {chromeMocks.runtime.Port} */
11 var nativePortMock;
13 (function() {
14 'use strict';
16 /** @type {sinon.TestStub} */
17 var postMessageStub;
19 /** @type {Array<Object>} */
20 var mockHostResponses;
22 /** @type {remoting.HostDaemonFacade} */
23 var it;
25 QUnit.module('host_daemon_facade', {
26 beforeEach: function(/** QUnit.Assert */ assert) {
27 chromeMocks.identity.mock$setToken('my_token');
28 nativePortMock =
29 chromeMocks.runtime.connectNative('com.google.chrome.remote_desktop');
30 mockHostResponses = [];
31 postMessageStub = sinon.stub(
32 nativePortMock, 'postMessage', sendMockHostResponse);
34 afterEach: function(/** QUnit.Assert */ assert) {
35 if (mockHostResponses.length) {
36 throw new Error('responses not all used');
38 mockHostResponses = null;
39 postMessageStub.restore();
40 it = null;
42 });
44 function sendMockHostResponse() {
45 if (mockHostResponses.length == 0) {
46 throw new Error('don\'t know how to responsd');
48 var toSend = mockHostResponses.pop();
49 Promise.resolve().then(function() {
50 nativePortMock.onMessage.mock$fire(toSend);
51 });
54 QUnit.test('initialize/hasFeature true', function(assert) {
55 mockHostResponses.push({
56 id: 0,
57 type: 'helloResponse',
58 version: '',
59 supportedFeatures: [
60 remoting.HostController.Feature.OAUTH_CLIENT,
61 remoting.HostController.Feature.PAIRING_REGISTRY
63 });
64 it = new remoting.HostDaemonFacade();
65 assert.deepEqual(postMessageStub.args[0][0], {
66 id: 0,
67 type: 'hello'
68 });
69 return it.hasFeature(remoting.HostController.Feature.PAIRING_REGISTRY).
70 then(function(arg) {
71 assert.equal(arg, true);
72 });
73 });
75 QUnit.test('initialize/hasFeature false', function(assert) {
76 mockHostResponses.push({
77 id: 0,
78 type: 'helloResponse',
79 version: '',
80 supportedFeatures: []
81 });
82 it = new remoting.HostDaemonFacade();
83 assert.deepEqual(postMessageStub.args[0][0], {
84 id: 0,
85 type: 'hello'
86 });
87 return it.hasFeature(remoting.HostController.Feature.PAIRING_REGISTRY).
88 then(function(arg) {
89 assert.equal(arg, false);
90 });
91 });
93 QUnit.test('initialize/getDaemonVersion', function(assert) {
94 mockHostResponses.push({
95 id: 0,
96 type: 'helloResponse',
97 version: '<daemonVersion>',
98 supportedFeatures: []
99 });
100 it = new remoting.HostDaemonFacade();
101 assert.deepEqual(postMessageStub.args[0][0], {
102 id: 0,
103 type: 'hello'
105 return it.getDaemonVersion().
106 then(function onDone(arg) {
107 assert.equal(arg, '<daemonVersion>');
112 * @param {string} description
113 * @param {function(!QUnit.Assert):*} callback
115 function postInitTest(description, callback) {
116 QUnit.test(description, function(assert) {
117 mockHostResponses.push({
118 id: 0,
119 type: 'helloResponse',
120 version: ''
122 console.assert(it == null, 'Daemon facade already exists.');
123 it = new remoting.HostDaemonFacade();
124 assert.deepEqual(postMessageStub.args[0][0], {
125 id: 0,
126 type: 'hello'
128 return it.getDaemonVersion().then(function() {
129 return callback(assert);
134 postInitTest('getHostName', function(assert) {
135 mockHostResponses.push({
136 id: 1,
137 type: 'getHostNameResponse',
138 hostname: '<fakeHostName>'
140 return it.getHostName().then(function(hostName) {
141 assert.deepEqual(postMessageStub.args[1][0], {
142 id: 1,
143 type: 'getHostName'
145 assert.equal(hostName, '<fakeHostName>');
149 postInitTest('getPinHash', function(assert) {
150 mockHostResponses.push({
151 id: 1,
152 type: 'getPinHashResponse',
153 hash: '<fakePinHash>'
155 return it.getPinHash('<hostId>', '<pin>').then(function(hostName) {
156 assert.deepEqual(postMessageStub.args[1][0], {
157 id: 1,
158 type: 'getPinHash',
159 hostId: '<hostId>',
160 pin: '<pin>'
162 assert.equal(hostName, '<fakePinHash>');
166 postInitTest('generateKeyPair', function(assert) {
167 mockHostResponses.push({
168 id: 1,
169 type: 'generateKeyPairResponse',
170 privateKey: '<fakePrivateKey>',
171 publicKey: '<fakePublicKey>'
173 return it.generateKeyPair().then(function(pair) {
174 assert.deepEqual(postMessageStub.args[1][0], {
175 id: 1,
176 type: 'generateKeyPair'
178 assert.deepEqual(pair, {
179 privateKey: '<fakePrivateKey>',
180 publicKey: '<fakePublicKey>'
185 postInitTest('updateDaemonConfig', function(assert) {
186 mockHostResponses.push({
187 id: 1,
188 type: 'updateDaemonConfigResponse',
189 result: 'OK'
191 return it.updateDaemonConfig({
192 fakeDaemonConfig: true
193 }).then(function(result) {
194 assert.deepEqual(postMessageStub.args[1][0], {
195 id: 1,
196 type: 'updateDaemonConfig',
197 config: { fakeDaemonConfig: true }
199 assert.equal(result, remoting.HostController.AsyncResult.OK);
203 postInitTest('getDaemonConfig', function(assert) {
204 mockHostResponses.push({
205 id: 1,
206 type: 'getDaemonConfigResponse',
207 config: { fakeDaemonConfig: true }
209 return it.getDaemonConfig().then(function(result) {
210 assert.deepEqual(postMessageStub.args[1][0], {
211 id: 1,
212 type: 'getDaemonConfig'
214 assert.deepEqual(result, { fakeDaemonConfig: true });
218 [0,1,2,3,4,5,6,7].forEach(function(/** number */ flags) {
219 postInitTest('getUsageStatsConsent, flags=' + flags, function(assert) {
220 var supported = Boolean(flags & 1);
221 var allowed = Boolean(flags & 2);
222 var setByPolicy = Boolean(flags & 4);
223 mockHostResponses.push({
224 id: 1,
225 type: 'getUsageStatsConsentResponse',
226 supported: supported,
227 allowed: allowed,
228 setByPolicy: setByPolicy
230 return it.getUsageStatsConsent().then(function(result) {
231 assert.deepEqual(postMessageStub.args[1][0], {
232 id: 1,
233 type: 'getUsageStatsConsent'
235 assert.deepEqual(result, {
236 supported: supported,
237 allowed: allowed,
238 setByPolicy: setByPolicy
244 [false, true].forEach(function(/** boolean */ consent) {
245 postInitTest('startDaemon, consent=' + consent, function(assert) {
246 mockHostResponses.push({
247 id: 1,
248 type: 'startDaemonResponse',
249 result: 'FAILED'
251 return it.startDaemon({
252 fakeConfig: true
253 }, consent).then(function(result) {
254 assert.deepEqual(postMessageStub.args[1][0], {
255 id: 1,
256 type: 'startDaemon',
257 config: { fakeConfig: true },
258 consent: consent
260 assert.equal(result, remoting.HostController.AsyncResult.FAILED);
265 postInitTest('stopDaemon', function(assert) {
266 mockHostResponses.push({
267 id: 1,
268 type: 'stopDaemonResponse',
269 result: 'CANCELLED'
271 return it.stopDaemon().then(function(result) {
272 assert.deepEqual(postMessageStub.args[1][0], {
273 id: 1,
274 type: 'stopDaemon'
276 assert.equal(result, remoting.HostController.AsyncResult.CANCELLED);
280 postInitTest('getPairedClients', function(assert) {
282 * @param {number} n
283 * @return {remoting.PairedClient}
285 function makeClient(n) {
286 return /** @type {remoting.PairedClient} */ ({
287 clientId: '<fakeClientId' + n + '>',
288 clientName: '<fakeClientName' + n + '>',
289 createdTime: n * 316571 // random prime number
293 var client0 = makeClient(0);
294 var client1 = makeClient(1);
295 mockHostResponses.push({
296 id: 1,
297 type: 'getPairedClientsResponse',
298 pairedClients: [client0, client1]
300 return it.getPairedClients().then(function(result) {
301 assert.deepEqual(postMessageStub.args[1][0], {
302 id: 1,
303 type: 'getPairedClients'
305 // Our facade is not really a facade! It adds extra fields.
306 // TODO(jrw): Move non-facade logic to host_controller.js.
307 assert.equal(result.length, 2);
308 assert.equal(result[0].clientId, '<fakeClientId0>');
309 assert.equal(result[0].clientName, '<fakeClientName0>');
310 assert.equal(result[0].createdTime, client0.createdTime);
311 assert.equal(typeof result[0].createDom, 'function');
312 assert.equal(result[1].clientId, '<fakeClientId1>');
313 assert.equal(result[1].clientName, '<fakeClientName1>');
314 assert.equal(result[1].createdTime, client1.createdTime);
315 assert.equal(typeof result[1].createDom, 'function');
319 [false, true].map(function(/** boolean */ deleted) {
320 postInitTest('clearPairedClients, deleted=' + deleted, function(assert) {
321 mockHostResponses.push({
322 id: 1,
323 type: 'clearPairedClientsResponse',
324 result: deleted
326 return it.clearPairedClients().then(function(result) {
327 assert.deepEqual(postMessageStub.args[1][0], {
328 id: 1,
329 type: 'clearPairedClients'
331 assert.equal(result, deleted);
336 [false, true].map(function(/** boolean */ deleted) {
337 postInitTest('deletePairedClient, deleted=' + deleted, function(assert) {
338 mockHostResponses.push({
339 id: 1,
340 type: 'deletePairedClientResponse',
341 result: deleted
343 return it.deletePairedClient('<fakeClientId>').then(function(result) {
344 assert.deepEqual(postMessageStub.args[1][0], {
345 id: 1,
346 type: 'deletePairedClient',
347 clientId: '<fakeClientId>'
349 assert.equal(result, deleted);
354 postInitTest('getHostClientId', function(assert) {
355 mockHostResponses.push({
356 id: 1,
357 type: 'getHostClientIdResponse',
358 clientId: '<fakeClientId>'
360 return it.getHostClientId().then(function(result) {
361 assert.deepEqual(postMessageStub.args[1][0], {
362 id: 1,
363 type: 'getHostClientId'
365 assert.equal(result, '<fakeClientId>');
369 postInitTest('getCredentialsFromAuthCode', function(assert) {
370 mockHostResponses.push({
371 id: 1,
372 type: 'getCredentialsFromAuthCodeResponse',
373 userEmail: '<fakeUserEmail>',
374 refreshToken: '<fakeRefreshToken>'
376 return it.getCredentialsFromAuthCode('<fakeAuthCode>').then(function(result) {
377 assert.deepEqual(postMessageStub.args[1][0], {
378 id: 1,
379 type: 'getCredentialsFromAuthCode',
380 authorizationCode: '<fakeAuthCode>'
382 assert.deepEqual(result, {
383 userEmail: '<fakeUserEmail>',
384 refreshToken: '<fakeRefreshToken>'
389 })();