1 // Copyright (c) 2012 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 const serial
= chrome
.serial
;
7 // TODO(miket): opening Bluetooth ports on OSX is unreliable. Investigate.
8 function shouldSkipPort(portName
) {
9 return portName
.match(/[Bb]luetooth/);
12 var createTestArrayBuffer = function() {
14 var buffer
= new ArrayBuffer(bufferSize
);
16 var uint8View
= new Uint8Array(buffer
);
17 for (var i
= 0; i
< bufferSize
; i
++) {
18 uint8View
[i
] = 42 + i
* 2; // An arbitrary pattern.
23 var testSerial = function() {
24 var serialPort
= null;
25 var connectionId
= -1;
26 var receiveTries
= 10;
27 var sendBuffer
= createTestArrayBuffer();
28 var sendBufferUint8View
= new Uint8Array(sendBuffer
);
29 var bufferLength
= sendBufferUint8View
.length
;
30 var receiveBuffer
= new ArrayBuffer(bufferLength
);
31 var receiveBufferUint8View
= new Uint8Array(receiveBuffer
);
32 var bytesToReceive
= bufferLength
;
35 var doNextOperation = function() {
36 switch (operation
++) {
38 serial
.getDevices(onGetDevices
);
42 console
.log('Connecting to serial device ' + serialPort
+ ' at ' +
44 serial
.connect(serialPort
, {bitrate
: bitrate
}, onConnect
);
47 serial
.setControlSignals(connectionId
, {dtr
: true}, onSetControlSignals
);
50 serial
.getControlSignals(connectionId
,onGetControlSignals
);
53 serial
.onReceive
.addListener(onReceive
);
54 serial
.onReceiveError
.addListener(onReceiveError
);
55 serial
.send(connectionId
, sendBuffer
, onSend
);
57 case 50: // GOTO 4 EVER
58 serial
.disconnect(connectionId
, onDisconnect
);
61 // Beware! If you forget to assign a case for your next test, the whole
62 // test suite will appear to succeed!
63 chrome
.test
.succeed();
68 var skipToTearDown = function() {
73 var repeatOperation = function() {
78 var onDisconnect = function(result
) {
79 chrome
.test
.assertTrue(result
);
83 var onReceive = function(receiveInfo
) {
84 var data
= new Uint8Array(receiveInfo
.data
);
85 bytesToReceive
-= data
.length
;
86 var receiveBufferIndex
= bufferLength
- data
.length
;
87 for (var i
= 0; i
< data
.length
; i
++)
88 receiveBufferUint8View
[i
+ receiveBufferIndex
] = data
[i
];
89 if (bytesToReceive
== 0) {
90 chrome
.test
.assertEq(sendBufferUint8View
, receiveBufferUint8View
,
91 'Buffer received was not equal to buffer sent.');
93 } else if (--receiveTries
<= 0) {
94 chrome
.test
.fail('receive() failed to return requested number of bytes.');
98 var onReceiveError = function(errorInfo
) {
99 chrome
.test
.fail('Failed to receive serial data');
102 var onSend = function(sendInfo
) {
103 chrome
.test
.assertEq(bufferLength
, sendInfo
.bytesSent
,
104 'Failed to send byte.');
107 var onGetControlSignals = function(options
) {
108 chrome
.test
.assertTrue(typeof options
.dcd
!= 'undefined', "No DCD set");
109 chrome
.test
.assertTrue(typeof options
.cts
!= 'undefined', "No CTS set");
110 chrome
.test
.assertTrue(typeof options
.ri
!= 'undefined', "No RI set");
111 chrome
.test
.assertTrue(typeof options
.dsr
!= 'undefined', "No DSR set");
115 var onSetControlSignals = function(result
) {
116 chrome
.test
.assertTrue(result
);
120 var onConnect = function(connectionInfo
) {
121 chrome
.test
.assertTrue(!!connectionInfo
,
122 'Failed to connect to serial port.');
123 connectionId
= connectionInfo
.connectionId
;
127 var onGetDevices = function(devices
) {
128 if (devices
.length
> 0) {
130 while (portNumber
< devices
.length
) {
131 if (shouldSkipPort(devices
[portNumber
].path
)) {
137 if (portNumber
< devices
.length
) {
138 serialPort
= devices
[portNumber
].path
;
141 // We didn't find a port that we think we should try.
142 chrome
.test
.succeed();
145 // No serial device found. This is still considered a success because we
146 // can't rely on specific hardware being present on the machine.
147 chrome
.test
.succeed();
154 chrome
.test
.runTests([testSerial
]);