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
3 // found in the LICENSE file.
6 * Unit tests for the JS serial service client.
8 * These test that configuration and data are correctly transmitted between the
9 * client and the service.
12 var test = require('test').binding;
13 var serial = require('serial').binding;
14 var unittestBindings = require('test_environment_specific_bindings');
16 var connectionId = null;
18 function connect(callback, options) {
19 options = options || {
20 name: 'test connection',
22 receiveTimeout: 12345,
26 serial.connect('device', options, test.callbackPass(function(connectionInfo) {
27 connectionId = connectionInfo.connectionId;
28 callback(connectionInfo);
32 function disconnect() {
33 serial.disconnect(connectionId, test.callbackPass(function(success) {
34 test.assertTrue(success);
39 function checkClientConnectionInfo(connectionInfo) {
40 test.assertFalse(connectionInfo.persistent);
41 test.assertEq('test connection', connectionInfo.name);
42 test.assertEq(12345, connectionInfo.receiveTimeout);
43 test.assertEq(6789, connectionInfo.sendTimeout);
44 test.assertEq(8192, connectionInfo.bufferSize);
45 test.assertFalse(connectionInfo.paused);
48 function checkServiceConnectionInfo(connectionInfo) {
49 test.assertEq(9600, connectionInfo.bitrate);
50 test.assertEq('eight', connectionInfo.dataBits);
51 test.assertEq('no', connectionInfo.parityBit);
52 test.assertEq('one', connectionInfo.stopBits);
53 test.assertFalse(connectionInfo.ctsFlowControl);
56 function checkConnectionInfo(connectionInfo) {
57 checkClientConnectionInfo(connectionInfo);
58 checkServiceConnectionInfo(connectionInfo);
61 unittestBindings.exportTests([
62 function testGetDevices() {
63 serial.getDevices(test.callbackPass(function(devices) {
64 test.assertEq(3, devices.length);
65 test.assertEq(4, $Object.keys(devices[0]).length);
66 test.assertEq('device', devices[0].path);
67 test.assertEq(1234, devices[0].vendorId);
68 test.assertEq(5678, devices[0].productId);
69 test.assertEq('foo', devices[0].displayName);
70 test.assertEq(1, $Object.keys(devices[1]).length);
71 test.assertEq('another_device', devices[1].path);
72 test.assertEq(1, $Object.keys(devices[2]).length);
73 test.assertEq('', devices[2].path);
77 function testConnectFail() {
78 serial.connect('device',
79 test.callbackFail('Failed to connect to the port.'));
82 function testGetInfoFailOnConnect() {
83 serial.connect('device',
84 test.callbackFail('Failed to connect to the port.'));
87 function testConnectInvalidBitrate() {
88 serial.connect('device', {bitrate: -1}, test.callbackFail(
89 'Failed to connect to the port.'));
92 function testConnect() {
93 connect(function(connectionInfo) {
95 checkConnectionInfo(connectionInfo);
99 function testConnectDefaultOptions() {
100 connect(function(connectionInfo) {
102 test.assertEq(9600, connectionInfo.bitrate);
103 test.assertEq('eight', connectionInfo.dataBits);
104 test.assertEq('no', connectionInfo.parityBit);
105 test.assertEq('one', connectionInfo.stopBits);
106 test.assertFalse(connectionInfo.ctsFlowControl);
107 test.assertFalse(connectionInfo.persistent);
108 test.assertEq('', connectionInfo.name);
109 test.assertEq(0, connectionInfo.receiveTimeout);
110 test.assertEq(0, connectionInfo.sendTimeout);
111 test.assertEq(4096, connectionInfo.bufferSize);
115 function testGetInfo() {
117 serial.getInfo(connectionId,
118 test.callbackPass(function(connectionInfo) {
120 checkConnectionInfo(connectionInfo);
125 function testGetInfoFailToGetPortInfo() {
127 serial.getInfo(connectionId,
128 test.callbackPass(function(connectionInfo) {
130 checkClientConnectionInfo(connectionInfo);
131 test.assertFalse('bitrate' in connectionInfo);
132 test.assertFalse('dataBits' in connectionInfo);
133 test.assertFalse('parityBit' in connectionInfo);
134 test.assertFalse('stopBit' in connectionInfo);
135 test.assertFalse('ctsFlowControl' in connectionInfo);
140 function testGetConnections() {
142 serial.getConnections(test.callbackPass(function(connections) {
144 test.assertEq(1, connections.length);
145 checkConnectionInfo(connections[0]);
150 function testGetControlSignals() {
153 function checkControlSignals(signals) {
157 serial.getControlSignals(
159 test.callbackPass(checkControlSignals));
161 test.assertEq(!!(calls & 1), signals.dcd);
162 test.assertEq(!!(calls & 2), signals.cts);
163 test.assertEq(!!(calls & 4), signals.ri);
164 test.assertEq(!!(calls & 8), signals.dsr);
167 serial.getControlSignals(connectionId,
168 test.callbackPass(checkControlSignals));
172 function testSetControlSignals() {
174 var signalsValues = [
179 {dtr: false, rts: false},
180 {dtr: true, rts: false},
182 {dtr: false, rts: true},
183 {dtr: true, rts: true},
186 function setControlSignals(success) {
187 if (calls == signalsValues.length) {
190 serial.setControlSignals(connectionId,
191 signalsValues[calls++],
192 test.callbackPass(setControlSignals));
194 test.assertTrue(success);
196 setControlSignals(true);
200 function testUpdate() {
202 var optionsValues = [
203 {}, // SetPortOptions is called once during connection.
212 {ctsFlowControl: false},
213 {ctsFlowControl: true},
221 function checkInfo(info) {
222 for (var key in optionsValues[calls]) {
223 test.assertEq(optionsValues[calls][key], info[key]);
227 function setOptions() {
228 if (++calls == optionsValues.length) {
231 serial.update(connectionId,
232 optionsValues[calls],
233 test.callbackPass(function(success) {
234 serial.getInfo(connectionId, test.callbackPass(checkInfo));
235 test.assertTrue(success);
243 function testUpdateInvalidBitrate() {
245 serial.update(connectionId,
247 test.callbackPass(function(success) {
249 test.assertFalse(success);
254 function testFlush() {
256 serial.flush(connectionId,
257 test.callbackPass(function(success) {
259 test.assertTrue(success);
264 function testSetPaused() {
266 serial.setPaused(connectionId, true, test.callbackPass(function() {
267 serial.getInfo(connectionId, test.callbackPass(function(info) {
268 serial.setPaused(connectionId, false, test.callbackPass(function() {
269 serial.getInfo(connectionId, test.callbackPass(function(info) {
270 test.assertFalse(info.paused);
274 test.assertTrue(info.paused);
280 function testDisconnectUnknownConnectionId() {
281 serial.disconnect(-1, test.callbackFail('Serial connection not found.'));
284 function testGetInfoUnknownConnectionId() {
285 serial.getInfo(-1, test.callbackFail('Serial connection not found.'));
288 function testUpdateUnknownConnectionId() {
289 serial.update(-1, {}, test.callbackFail('Serial connection not found.'));
292 function testSetControlSignalsUnknownConnectionId() {
293 serial.setControlSignals(-1, {}, test.callbackFail(
294 'Serial connection not found.'));
297 function testGetControlSignalsUnknownConnectionId() {
298 serial.getControlSignals(-1, test.callbackFail(
299 'Serial connection not found.'));
302 function testFlushUnknownConnectionId() {
303 serial.flush(-1, test.callbackFail('Serial connection not found.'));
306 function testSetPausedUnknownConnectionId() {
308 -1, true, test.callbackFail('Serial connection not found.'));
310 -1, false, test.callbackFail('Serial connection not found.'));
312 ], test.runTests, exports);