OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * Unit tests for the JS serial service client. | 6 * Unit tests for the JS serial service client. |
raymes
2014/08/29 06:07:16
A comment mentioning which CC file drives this wou
Sam McNally
2014/09/01 06:35:19
Done.
| |
7 * | 7 * |
8 * These test that configuration and data are correctly transmitted between the | 8 * These test that configuration and data are correctly transmitted between the |
9 * client and the service. | 9 * client and the service. |
10 */ | 10 */ |
11 | 11 |
12 var test = require('test').binding; | 12 var test = require('test').binding; |
13 var serial = require('serial').binding; | 13 var serial = require('serial').binding; |
14 var unittestBindings = require('test_environment_specific_bindings'); | 14 var unittestBindings = require('test_environment_specific_bindings'); |
15 | 15 |
16 var timeoutManager = new unittestBindings.TimeoutManager(); | |
17 timeoutManager.installGlobals(); | |
18 | |
19 var BUFFER_SIZE = 10; | |
20 | |
16 var connectionId = null; | 21 var connectionId = null; |
17 | 22 |
18 function connect(callback, options) { | 23 function connect(callback, options) { |
19 options = options || { | 24 options = options || { |
20 name: 'test connection', | 25 name: 'test connection', |
21 bufferSize: 8192, | 26 bufferSize: BUFFER_SIZE, |
22 receiveTimeout: 12345, | 27 receiveTimeout: 12345, |
23 sendTimeout: 6789, | 28 sendTimeout: 6789, |
24 persistent: true, | 29 persistent: true, |
25 } | 30 }; |
26 serial.connect('device', options, test.callbackPass(function(connectionInfo) { | 31 serial.connect('device', options, test.callbackPass(function(connectionInfo) { |
27 connectionId = connectionInfo.connectionId; | 32 connectionId = connectionInfo.connectionId; |
28 callback(connectionInfo); | 33 if (callback) |
34 callback(connectionInfo); | |
29 })); | 35 })); |
30 } | 36 } |
31 | 37 |
38 function addReceiveHook(callback) { | |
raymes
2014/08/29 06:07:16
Please add a comment for these functions. Saves pe
Sam McNally
2014/09/01 06:35:19
Done.
| |
39 return requireAsync('serial_service').then(function(serialService) { | |
40 var dataReceived = serialService.Connection.prototype.onDataReceived_; | |
41 serialService.Connection.prototype.onDataReceived_ = function() { | |
42 var result = $Function.apply(dataReceived, this, arguments); | |
43 callback(); | |
44 return result; | |
45 }; | |
46 }); | |
47 } | |
48 | |
49 function addReceiveErrorHook(callback) { | |
50 return requireAsync('serial_service').then(function(serialService) { | |
51 var receiveError = serialService.Connection.prototype.onReceiveError_; | |
52 serialService.Connection.prototype.onReceiveError_ = function() { | |
53 var result = $Function.apply(receiveError, this, arguments); | |
54 callback(); | |
55 return result; | |
56 }; | |
57 }); | |
58 } | |
59 | |
32 function disconnect() { | 60 function disconnect() { |
33 serial.disconnect(connectionId, test.callbackPass(function(success) { | 61 serial.disconnect(connectionId, test.callbackPass(function(success) { |
34 test.assertTrue(success); | 62 test.assertTrue(success); |
35 connectionId = null; | 63 connectionId = null; |
36 })); | 64 })); |
37 } | 65 } |
38 | 66 |
39 function checkClientConnectionInfo(connectionInfo) { | 67 function checkClientConnectionInfo(connectionInfo) { |
40 test.assertFalse(connectionInfo.persistent); | 68 test.assertFalse(connectionInfo.persistent); |
41 test.assertEq('test connection', connectionInfo.name); | 69 test.assertEq('test connection', connectionInfo.name); |
42 test.assertEq(12345, connectionInfo.receiveTimeout); | 70 test.assertEq(12345, connectionInfo.receiveTimeout); |
43 test.assertEq(6789, connectionInfo.sendTimeout); | 71 test.assertEq(6789, connectionInfo.sendTimeout); |
44 test.assertEq(8192, connectionInfo.bufferSize); | 72 test.assertEq(BUFFER_SIZE, connectionInfo.bufferSize); |
45 test.assertFalse(connectionInfo.paused); | 73 test.assertFalse(connectionInfo.paused); |
46 } | 74 } |
47 | 75 |
48 function checkServiceConnectionInfo(connectionInfo) { | 76 function checkServiceConnectionInfo(connectionInfo) { |
49 test.assertEq(9600, connectionInfo.bitrate); | 77 test.assertEq(9600, connectionInfo.bitrate); |
50 test.assertEq('eight', connectionInfo.dataBits); | 78 test.assertEq('eight', connectionInfo.dataBits); |
51 test.assertEq('no', connectionInfo.parityBit); | 79 test.assertEq('no', connectionInfo.parityBit); |
52 test.assertEq('one', connectionInfo.stopBits); | 80 test.assertEq('one', connectionInfo.stopBits); |
53 test.assertFalse(connectionInfo.ctsFlowControl); | 81 test.assertFalse(connectionInfo.ctsFlowControl); |
54 } | 82 } |
55 | 83 |
56 function checkConnectionInfo(connectionInfo) { | 84 function checkConnectionInfo(connectionInfo) { |
57 checkClientConnectionInfo(connectionInfo); | 85 checkClientConnectionInfo(connectionInfo); |
58 checkServiceConnectionInfo(connectionInfo); | 86 checkServiceConnectionInfo(connectionInfo); |
59 } | 87 } |
60 | 88 |
89 function runReceiveErrorTest(expectedError) { | |
90 connect(); | |
91 test.listenOnce(serial.onReceiveError, function(result) { | |
92 serial.getInfo(connectionId, test.callbackPass(function(connectionInfo) { | |
93 disconnect(); | |
94 test.assertTrue(connectionInfo.paused); | |
95 })); | |
96 test.assertEq(connectionId, result.connectionId); | |
97 test.assertEq(expectedError, result.error); | |
98 }); | |
99 } | |
100 | |
101 function runSendErrorTest(expectedError) { | |
102 connect(function() { | |
103 var buffer = new ArrayBuffer(1); | |
104 serial.send(connectionId, buffer, test.callbackPass(function(sendInfo) { | |
105 disconnect(); | |
106 test.assertEq(0, sendInfo.bytesSent); | |
107 test.assertEq(expectedError, sendInfo.error); | |
108 })); | |
109 }); | |
110 } | |
111 | |
61 unittestBindings.exportTests([ | 112 unittestBindings.exportTests([ |
62 function testGetDevices() { | 113 function testGetDevices() { |
63 serial.getDevices(test.callbackPass(function(devices) { | 114 serial.getDevices(test.callbackPass(function(devices) { |
64 test.assertEq(3, devices.length); | 115 test.assertEq(3, devices.length); |
65 test.assertEq(4, $Object.keys(devices[0]).length); | 116 test.assertEq(4, $Object.keys(devices[0]).length); |
66 test.assertEq('device', devices[0].path); | 117 test.assertEq('device', devices[0].path); |
67 test.assertEq(1234, devices[0].vendorId); | 118 test.assertEq(1234, devices[0].vendorId); |
68 test.assertEq(5678, devices[0].productId); | 119 test.assertEq(5678, devices[0].productId); |
69 test.assertEq('foo', devices[0].displayName); | 120 test.assertEq('foo', devices[0].displayName); |
70 test.assertEq(1, $Object.keys(devices[1]).length); | 121 test.assertEq(1, $Object.keys(devices[1]).length); |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
270 test.assertFalse(info.paused); | 321 test.assertFalse(info.paused); |
271 disconnect(); | 322 disconnect(); |
272 })); | 323 })); |
273 })); | 324 })); |
274 test.assertTrue(info.paused); | 325 test.assertTrue(info.paused); |
275 })); | 326 })); |
276 })); | 327 })); |
277 }); | 328 }); |
278 }, | 329 }, |
279 | 330 |
331 function testEcho() { | |
raymes
2014/08/29 06:07:16
This looks like it does more than a simple echo. T
Sam McNally
2014/09/01 06:35:18
Split this test into one test for each thing being
| |
332 connect(function() { | |
333 var data = 'data'; | |
334 var buffer = new ArrayBuffer(data.length); | |
335 var byteBuffer = new Int8Array(buffer); | |
336 for (var i = 0; i < data.length; i++) { | |
337 byteBuffer[i] = data.charCodeAt(i); | |
338 } | |
339 serial.send(connectionId, buffer, test.callbackPass(function(sendInfo) { | |
340 test.assertEq(4, sendInfo.bytesSent); | |
341 test.assertEq(undefined, sendInfo.error); | |
342 })); | |
343 serial.send(connectionId, buffer, test.callbackPass(function(sendInfo) { | |
344 test.assertEq(0, sendInfo.bytesSent); | |
345 test.assertEq('pending', sendInfo.error); | |
346 })); | |
raymes
2014/08/29 06:07:16
It would be useful to comment about what the secon
Sam McNally
2014/09/01 06:35:18
Done.
| |
347 test.listenOnce(serial.onReceive, function(result) { | |
348 serial.send(connectionId, buffer, test.callbackPass(function(sendInfo) { | |
349 test.listenOnce(serial.onReceive, function(result) { | |
350 test.assertEq(connectionId, result.connectionId); | |
351 test.assertEq(4, result.data.byteLength); | |
352 var resultByteBuffer = new Int8Array(result.data); | |
353 for (var i = 0; i < byteBuffer.byteLength; i++) { | |
354 test.assertEq(byteBuffer[i], resultByteBuffer[i]); | |
355 } | |
356 disconnect(); | |
357 }); | |
358 test.assertEq(4, sendInfo.bytesSent); | |
359 test.assertEq(undefined, sendInfo.error); | |
360 })); | |
361 test.assertEq(connectionId, result.connectionId); | |
362 test.assertEq(4, result.data.byteLength); | |
363 var resultByteBuffer = new Int8Array(result.data); | |
364 for (var i = 0; i < byteBuffer.byteLength; i++) { | |
365 test.assertEq(byteBuffer[i], resultByteBuffer[i]); | |
366 } | |
raymes
2014/08/29 06:07:16
maybe move this up above the send.
Sam McNally
2014/09/01 06:35:19
Done.
| |
367 }); | |
368 }); | |
369 }, | |
370 | |
371 function testPausedEcho() { | |
raymes
2014/08/29 06:07:16
Change this to be more like testPausedReceiveError
Sam McNally
2014/09/01 06:35:19
Done.
| |
372 var unpaused = false; | |
373 addReceiveHook(function() { | |
374 if (unpaused) | |
375 return; | |
376 unpaused = true; | |
377 serial.setPaused(connectionId, false, test.callbackPass()); | |
378 serial.setPaused(connectionId, false, test.callbackPass()); | |
raymes
2014/08/29 06:07:16
Why do we need to call this twice?
Sam McNally
2014/09/01 06:35:19
Done.
| |
379 }).then(test.callbackPass(function() { | |
380 connect(function() { | |
381 var data = 'data'; | |
382 var buffer = new ArrayBuffer(data.length); | |
383 var byteBuffer = new Int8Array(buffer); | |
384 for (var i = 0; i < data.length; i++) { | |
385 byteBuffer[i] = data.charCodeAt(i); | |
386 } | |
387 serial.setPaused(connectionId, true, test.callbackPass(function() { | |
388 serial.send(connectionId, buffer, test.callbackPass( | |
389 function(sendInfo) { | |
390 test.assertEq(4, sendInfo.bytesSent); | |
391 test.assertEq(undefined, sendInfo.error); | |
392 })); | |
393 })); | |
394 serial.setPaused(connectionId, true, test.callbackPass()); | |
395 test.listenOnce(serial.onReceive, function(result) { | |
396 disconnect(); | |
397 test.assertEq(connectionId, result.connectionId); | |
398 test.assertEq(4, result.data.byteLength); | |
399 var resultByteBuffer = new Int8Array(result.data); | |
400 for (var i = 0; i < byteBuffer.byteLength; i++) { | |
401 test.assertEq(byteBuffer[i], resultByteBuffer[i]); | |
402 } | |
403 }); | |
404 }); | |
405 })); | |
406 }, | |
407 | |
408 function testPausedReceiveError() { | |
409 var unpaused = false; | |
410 addReceiveErrorHook(function() { | |
411 if (unpaused) | |
412 return; | |
413 unpaused = true; | |
414 serial.setPaused(connectionId, false, test.callbackPass()); | |
415 serial.setPaused(connectionId, false, test.callbackPass()); | |
raymes
2014/08/29 06:07:16
If we've already tested this case, no need to test
Sam McNally
2014/09/01 06:35:19
Done.
| |
416 }).then(test.callbackPass(function() { | |
417 connect(function() { | |
418 serial.setPaused(connectionId, true, test.callbackPass()); | |
419 serial.setPaused(connectionId, true, test.callbackPass()); | |
420 }); | |
421 })); | |
422 | |
423 test.listenOnce(serial.onReceiveError, function(result) { | |
424 serial.getInfo(connectionId, test.callbackPass(function(connectionInfo) { | |
425 disconnect(); | |
426 test.assertTrue(connectionInfo.paused); | |
427 })); | |
428 test.assertEq(connectionId, result.connectionId); | |
429 test.assertEq('device_lost', result.error); | |
430 }); | |
431 serial.onReceive.addListener(function() { | |
432 test.fail('unexpected onReceive event'); | |
433 }); | |
434 }, | |
435 | |
436 function testLargeSend() { | |
437 connect(function() { | |
438 var buffer = new ArrayBuffer(BUFFER_SIZE * 3); | |
439 var byteBuffer = new Int8Array(buffer); | |
440 for (var i = 0; i < buffer.byteLength; i++) { | |
441 byteBuffer[i] = i; | |
442 } | |
443 serial.send(connectionId, buffer, test.callbackPass(function(sendInfo) { | |
444 disconnect(); | |
445 test.assertEq(BUFFER_SIZE * 3, sendInfo.bytesSent); | |
446 test.assertEq(undefined, sendInfo.error); | |
447 })); | |
448 }); | |
449 }, | |
raymes
2014/08/29 06:07:16
Consider removing this test as dicussed since it's
Sam McNally
2014/09/01 06:35:19
Done.
| |
450 | |
451 function testSendPartialSuccessWithError() { | |
452 connect(function() { | |
453 var data = 'data'; | |
454 var buffer = new ArrayBuffer(data.length); | |
455 var byteBuffer = new Int8Array(buffer); | |
456 for (var i = 0; i < data.length; i++) { | |
457 byteBuffer[i] = data.charCodeAt(i); | |
458 } | |
459 serial.send(connectionId, buffer, test.callbackPass(function(sendInfo) { | |
460 serial.send(connectionId, buffer, test.callbackPass(function(sendInfo) { | |
461 test.assertEq(4, sendInfo.bytesSent); | |
462 test.assertEq(undefined, sendInfo.error); | |
463 disconnect(); | |
464 })); | |
465 test.assertEq(2, sendInfo.bytesSent); | |
466 test.assertEq('system_error', sendInfo.error); | |
raymes
2014/08/29 06:07:16
Move this above the send just for clarity.
Sam McNally
2014/09/01 06:35:19
Done.
| |
467 })); | |
468 }); | |
469 }, | |
470 | |
471 function testSendTimeout() { | |
472 connect(function() { | |
473 var data = 'data'; | |
474 var buffer = new ArrayBuffer(data.length); | |
475 var byteBuffer = new Int8Array(buffer); | |
476 for (var i = 0; i < data.length; i++) { | |
477 byteBuffer[i] = data.charCodeAt(i); | |
478 } | |
479 timeoutManager.clearTimeoutCreated(); | |
480 serial.send(connectionId, buffer, test.callbackPass(function(sendInfo) { | |
481 disconnect(); | |
482 test.assertEq(0, sendInfo.bytesSent); | |
483 test.assertEq('timeout', sendInfo.error); | |
484 test.assertEq(6789, timeoutManager.currentTime); | |
485 })); | |
486 // Changing the timeout does not effect a send in progress. | |
487 serial.update(connectionId, {sendTimeout: 10}, test.callbackPass( | |
488 function() { | |
489 timeoutManager.timeoutCreated().then(function() { | |
490 timeoutManager.advanceTime(10); | |
491 timeoutManager.advanceTime(6779); | |
492 }); | |
493 })); | |
494 }); | |
495 }, | |
496 | |
497 function testDisableSendTimeout() { | |
498 connect(function() { | |
499 var data = 'data'; | |
500 var buffer = new ArrayBuffer(data.length); | |
501 var byteBuffer = new Int8Array(buffer); | |
502 for (var i = 0; i < data.length; i++) { | |
503 byteBuffer[i] = data.charCodeAt(i); | |
504 } | |
505 timeoutManager.clearTimeoutCreated(); | |
506 serial.send(connectionId, buffer, test.callbackPass(function(sendInfo) { | |
507 disconnect(); | |
508 test.assertEq(0, sendInfo.bytesSent); | |
509 test.assertEq('timeout', sendInfo.error); | |
510 test.assertEq(6789, timeoutManager.currentTime); | |
511 })); | |
512 // Disabling the timeout does not effect a send in progress. | |
513 serial.update(connectionId, {sendTimeout: 0}, test.callbackPass( | |
514 function() { | |
515 timeoutManager.timeoutCreated().then(function() { | |
516 timeoutManager.advanceTime(6789); | |
517 }); | |
518 })); | |
519 }); | |
520 }, | |
521 | |
522 function testReceiveTimeout() { | |
523 connect(function() { | |
524 test.listenOnce(serial.onReceiveError, function(result) { | |
525 serial.getInfo(connectionId, test.callbackPass( | |
526 function(connectionInfo) { | |
527 disconnect(); | |
528 test.assertFalse(connectionInfo.paused); | |
529 })); | |
530 test.assertEq(connectionId, result.connectionId); | |
531 test.assertEq('timeout', result.error); | |
532 test.assertEq(12345, timeoutManager.currentTime); | |
raymes
2014/08/29 06:07:16
Maybe move these above the call to the timeout
Sam McNally
2014/09/01 06:35:19
Done.
| |
533 }); | |
534 // Changing the timeout does not take effect until the current timeout | |
535 // expires or a receive completes. | |
536 serial.update(connectionId, {receiveTimeout: 10}, test.callbackPass( | |
537 function() { | |
538 timeoutManager.timeoutCreated().then(function() { | |
539 timeoutManager.advanceTime(10); | |
540 timeoutManager.advanceTime(12335); | |
541 }); | |
542 })); | |
543 }); | |
544 }, | |
545 | |
546 function testDisableReceiveTimeout() { | |
547 connect(function() { | |
548 test.listenOnce(serial.onReceiveError, function(result) { | |
549 serial.getInfo(connectionId, test.callbackPass( | |
550 function(connectionInfo) { | |
551 disconnect(); | |
552 test.assertFalse(connectionInfo.paused); | |
553 })); | |
554 test.assertEq(connectionId, result.connectionId); | |
555 test.assertEq('timeout', result.error); | |
556 test.assertEq(12345, timeoutManager.currentTime); | |
557 }); | |
558 // Disabling the timeout does not take effect until the current timeout | |
559 // expires or a receive completes. | |
560 serial.update(connectionId, {receiveTimeout: 0}, test.callbackPass( | |
561 function() { | |
562 timeoutManager.timeoutCreated().then(function() { | |
563 timeoutManager.advanceTime(12345); | |
564 }); | |
565 })); | |
566 }); | |
567 }, | |
568 | |
569 function testReceiveErrorDisconnected() { | |
570 runReceiveErrorTest('disconnected'); | |
571 }, | |
572 | |
573 function testReceiveErrorTimeout() { | |
574 runReceiveErrorTest('timeout'); | |
575 }, | |
576 | |
577 function testReceiveErrorDeviceLost() { | |
578 runReceiveErrorTest('device_lost'); | |
579 }, | |
580 | |
581 function testReceiveErrorSystemError() { | |
582 runReceiveErrorTest('system_error'); | |
583 }, | |
584 | |
585 function testSendErrorDisconnected() { | |
586 runSendErrorTest('disconnected'); | |
587 }, | |
588 | |
589 function testSendErrorTimeout() { | |
590 runSendErrorTest('timeout'); | |
raymes
2014/08/29 06:07:16
make a note that this test should pause receiving
Sam McNally
2014/09/01 06:35:19
Done.
| |
591 }, | |
592 | |
593 function testSendErrorSystemError() { | |
594 runSendErrorTest('system_error'); | |
595 }, | |
596 | |
280 function testDisconnectUnknownConnectionId() { | 597 function testDisconnectUnknownConnectionId() { |
281 serial.disconnect(-1, test.callbackFail('Serial connection not found.')); | 598 serial.disconnect(-1, test.callbackFail('Serial connection not found.')); |
282 }, | 599 }, |
283 | 600 |
284 function testGetInfoUnknownConnectionId() { | 601 function testGetInfoUnknownConnectionId() { |
285 serial.getInfo(-1, test.callbackFail('Serial connection not found.')); | 602 serial.getInfo(-1, test.callbackFail('Serial connection not found.')); |
286 }, | 603 }, |
287 | 604 |
288 function testUpdateUnknownConnectionId() { | 605 function testUpdateUnknownConnectionId() { |
289 serial.update(-1, {}, test.callbackFail('Serial connection not found.')); | 606 serial.update(-1, {}, test.callbackFail('Serial connection not found.')); |
(...skipping 12 matching lines...) Expand all Loading... | |
302 function testFlushUnknownConnectionId() { | 619 function testFlushUnknownConnectionId() { |
303 serial.flush(-1, test.callbackFail('Serial connection not found.')); | 620 serial.flush(-1, test.callbackFail('Serial connection not found.')); |
304 }, | 621 }, |
305 | 622 |
306 function testSetPausedUnknownConnectionId() { | 623 function testSetPausedUnknownConnectionId() { |
307 serial.setPaused( | 624 serial.setPaused( |
308 -1, true, test.callbackFail('Serial connection not found.')); | 625 -1, true, test.callbackFail('Serial connection not found.')); |
309 serial.setPaused( | 626 serial.setPaused( |
310 -1, false, test.callbackFail('Serial connection not found.')); | 627 -1, false, test.callbackFail('Serial connection not found.')); |
311 }, | 628 }, |
629 | |
630 function testSendUnknownConnectionId() { | |
631 var buffer = new ArrayBuffer(1); | |
632 serial.send(-1, buffer, test.callbackFail('Serial connection not found.')); | |
633 }, | |
312 ], test.runTests, exports); | 634 ], test.runTests, exports); |
OLD | NEW |