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.
7 * Unit test for host_daemon_facade.js.
10 /** @type {chromeMocks.runtime.Port} */
16 /** @type {sinon.TestStub} */
19 /** @type {Array<Object>} */
20 var mockHostResponses;
22 /** @type {remoting.HostDaemonFacade} */
25 QUnit.module('host_daemon_facade', {
26 beforeEach: function(/** QUnit.Assert */ assert) {
27 chromeMocks.identity.mock$setToken('my_token');
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();
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);
54 QUnit.test('initialize/hasFeature true', function(assert) {
55 mockHostResponses.push({
57 type: 'helloResponse',
60 remoting.HostController.Feature.OAUTH_CLIENT,
61 remoting.HostController.Feature.PAIRING_REGISTRY
64 it = new remoting.HostDaemonFacade();
65 assert.deepEqual(postMessageStub.args[0][0], {
69 return it.hasFeature(remoting.HostController.Feature.PAIRING_REGISTRY).
71 assert.equal(arg, true);
75 QUnit.test('initialize/hasFeature false', function(assert) {
76 mockHostResponses.push({
78 type: 'helloResponse',
82 it = new remoting.HostDaemonFacade();
83 assert.deepEqual(postMessageStub.args[0][0], {
87 return it.hasFeature(remoting.HostController.Feature.PAIRING_REGISTRY).
89 assert.equal(arg, false);
93 QUnit.test('initialize/getDaemonVersion', function(assert) {
94 mockHostResponses.push({
96 type: 'helloResponse',
97 version: '<daemonVersion>',
100 it = new remoting.HostDaemonFacade();
101 assert.deepEqual(postMessageStub.args[0][0], {
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({
119 type: 'helloResponse',
122 console.assert(it == null, 'Daemon facade already exists.');
123 it = new remoting.HostDaemonFacade();
124 assert.deepEqual(postMessageStub.args[0][0], {
128 return it.getDaemonVersion().then(function() {
129 return callback(assert);
134 postInitTest('getHostName', function(assert) {
135 mockHostResponses.push({
137 type: 'getHostNameResponse',
138 hostname: '<fakeHostName>'
140 return it.getHostName().then(function(hostName) {
141 assert.deepEqual(postMessageStub.args[1][0], {
145 assert.equal(hostName, '<fakeHostName>');
149 postInitTest('getPinHash', function(assert) {
150 mockHostResponses.push({
152 type: 'getPinHashResponse',
153 hash: '<fakePinHash>'
155 return it.getPinHash('<hostId>', '<pin>').then(function(hostName) {
156 assert.deepEqual(postMessageStub.args[1][0], {
162 assert.equal(hostName, '<fakePinHash>');
166 postInitTest('generateKeyPair', function(assert) {
167 mockHostResponses.push({
169 type: 'generateKeyPairResponse',
170 privateKey: '<fakePrivateKey>',
171 publicKey: '<fakePublicKey>'
173 return it.generateKeyPair().then(function(pair) {
174 assert.deepEqual(postMessageStub.args[1][0], {
176 type: 'generateKeyPair'
178 assert.deepEqual(pair, {
179 privateKey: '<fakePrivateKey>',
180 publicKey: '<fakePublicKey>'
185 postInitTest('updateDaemonConfig', function(assert) {
186 mockHostResponses.push({
188 type: 'updateDaemonConfigResponse',
191 return it.updateDaemonConfig({
192 fakeDaemonConfig: true
193 }).then(function(result) {
194 assert.deepEqual(postMessageStub.args[1][0], {
196 type: 'updateDaemonConfig',
197 config: { fakeDaemonConfig: true }
199 assert.equal(result, remoting.HostController.AsyncResult.OK);
203 postInitTest('getDaemonConfig', function(assert) {
204 mockHostResponses.push({
206 type: 'getDaemonConfigResponse',
207 config: { fakeDaemonConfig: true }
209 return it.getDaemonConfig().then(function(result) {
210 assert.deepEqual(postMessageStub.args[1][0], {
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({
225 type: 'getUsageStatsConsentResponse',
226 supported: supported,
228 setByPolicy: setByPolicy
230 return it.getUsageStatsConsent().then(function(result) {
231 assert.deepEqual(postMessageStub.args[1][0], {
233 type: 'getUsageStatsConsent'
235 assert.deepEqual(result, {
236 supported: supported,
238 setByPolicy: setByPolicy
244 [false, true].forEach(function(/** boolean */ consent) {
245 postInitTest('startDaemon, consent=' + consent, function(assert) {
246 mockHostResponses.push({
248 type: 'startDaemonResponse',
251 return it.startDaemon({
253 }, consent).then(function(result) {
254 assert.deepEqual(postMessageStub.args[1][0], {
257 config: { fakeConfig: true },
260 assert.equal(result, remoting.HostController.AsyncResult.FAILED);
265 postInitTest('stopDaemon', function(assert) {
266 mockHostResponses.push({
268 type: 'stopDaemonResponse',
271 return it.stopDaemon().then(function(result) {
272 assert.deepEqual(postMessageStub.args[1][0], {
276 assert.equal(result, remoting.HostController.AsyncResult.CANCELLED);
280 postInitTest('getPairedClients', function(assert) {
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({
297 type: 'getPairedClientsResponse',
298 pairedClients: [client0, client1]
300 return it.getPairedClients().then(function(result) {
301 assert.deepEqual(postMessageStub.args[1][0], {
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({
323 type: 'clearPairedClientsResponse',
326 return it.clearPairedClients().then(function(result) {
327 assert.deepEqual(postMessageStub.args[1][0], {
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({
340 type: 'deletePairedClientResponse',
343 return it.deletePairedClient('<fakeClientId>').then(function(result) {
344 assert.deepEqual(postMessageStub.args[1][0], {
346 type: 'deletePairedClient',
347 clientId: '<fakeClientId>'
349 assert.equal(result, deleted);
354 postInitTest('getHostClientId', function(assert) {
355 mockHostResponses.push({
357 type: 'getHostClientIdResponse',
358 clientId: '<fakeClientId>'
360 return it.getHostClientId().then(function(result) {
361 assert.deepEqual(postMessageStub.args[1][0], {
363 type: 'getHostClientId'
365 assert.equal(result, '<fakeClientId>');
369 postInitTest('getCredentialsFromAuthCode', function(assert) {
370 mockHostResponses.push({
372 type: 'getCredentialsFromAuthCodeResponse',
373 userEmail: '<fakeUserEmail>',
374 refreshToken: '<fakeRefreshToken>'
376 return it.getCredentialsFromAuthCode('<fakeAuthCode>').then(function(result) {
377 assert.deepEqual(postMessageStub.args[1][0], {
379 type: 'getCredentialsFromAuthCode',
380 authorizationCode: '<fakeAuthCode>'
382 assert.deepEqual(result, {
383 userEmail: '<fakeUserEmail>',
384 refreshToken: '<fakeRefreshToken>'