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