Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / extensions / test / data / serial_unittest.js
blobdc7bbea56e06cddd8453f3a73358d177fe90f9fb
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.
5 /**
6  * Unit tests for the JS serial service client.
7  *
8  * These test that configuration and data are correctly transmitted between the
9  * client and the service.
10  */
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',
21     bufferSize: 8192,
22     receiveTimeout: 12345,
23     sendTimeout: 6789,
24     persistent: true,
25   }
26   serial.connect('device', options, test.callbackPass(function(connectionInfo) {
27     connectionId = connectionInfo.connectionId;
28     callback(connectionInfo);
29   }));
32 function disconnect() {
33   serial.disconnect(connectionId, test.callbackPass(function(success) {
34     test.assertTrue(success);
35     connectionId = null;
36   }));
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);
74     }));
75   },
77   function testConnectFail() {
78     serial.connect('device',
79                    test.callbackFail('Failed to connect to the port.'));
80   },
82   function testGetInfoFailOnConnect() {
83     serial.connect('device',
84                    test.callbackFail('Failed to connect to the port.'));
85   },
87   function testConnectInvalidBitrate() {
88     serial.connect('device', {bitrate: -1}, test.callbackFail(
89         'Failed to connect to the port.'));
90   },
92   function testConnect() {
93     connect(function(connectionInfo) {
94       disconnect();
95       checkConnectionInfo(connectionInfo);
96     });
97   },
99   function testConnectDefaultOptions() {
100     connect(function(connectionInfo) {
101       disconnect();
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);
112     }, {});
113   },
115   function testGetInfo() {
116     connect(function() {
117       serial.getInfo(connectionId,
118                      test.callbackPass(function(connectionInfo) {
119         disconnect();
120         checkConnectionInfo(connectionInfo);
121       }));
122     });
123   },
125   function testGetInfoFailToGetPortInfo() {
126     connect(function() {
127       serial.getInfo(connectionId,
128                      test.callbackPass(function(connectionInfo) {
129         disconnect();
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);
136       }));
137     });
138   },
140   function testGetConnections() {
141     connect(function() {
142       serial.getConnections(test.callbackPass(function(connections) {
143         disconnect();
144         test.assertEq(1, connections.length);
145         checkConnectionInfo(connections[0]);
146       }));
147     });
148   },
150   function testGetControlSignals() {
151     connect(function() {
152       var calls = 0;
153       function checkControlSignals(signals) {
154         if (calls == 15) {
155           disconnect();
156         } else {
157           serial.getControlSignals(
158               connectionId,
159               test.callbackPass(checkControlSignals));
160         }
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);
165         calls++;
166       }
167       serial.getControlSignals(connectionId,
168                                test.callbackPass(checkControlSignals));
169     });
170   },
172   function testSetControlSignals() {
173     connect(function() {
174       var signalsValues = [
175         {},
176         {dtr: false},
177         {dtr: true},
178         {rts: false},
179         {dtr: false, rts: false},
180         {dtr: true, rts: false},
181         {rts: true},
182         {dtr: false, rts: true},
183         {dtr: true, rts: true},
184       ];
185       var calls = 0;
186       function setControlSignals(success) {
187         if (calls == signalsValues.length) {
188           disconnect();
189         } else {
190           serial.setControlSignals(connectionId,
191                                    signalsValues[calls++],
192                                    test.callbackPass(setControlSignals));
193         }
194         test.assertTrue(success);
195       }
196       setControlSignals(true);
197     });
198   },
200   function testUpdate() {
201     connect(function() {
202       var optionsValues = [
203         {},  // SetPortOptions is called once during connection.
204         {bitrate: 57600},
205         {dataBits: 'seven'},
206         {dataBits: 'eight'},
207         {parityBit: 'no'},
208         {parityBit: 'odd'},
209         {parityBit: 'even'},
210         {stopBits: 'one'},
211         {stopBits: 'two'},
212         {ctsFlowControl: false},
213         {ctsFlowControl: true},
214         {bufferSize: 1},
215         {sendTimeout: 0},
216         {receiveTimeout: 0},
217         {persistent: false},
218         {name: 'name'},
219       ];
220       var calls = 0;
221       function checkInfo(info) {
222         for (var key in optionsValues[calls]) {
223           test.assertEq(optionsValues[calls][key], info[key]);
224         }
225         setOptions();
226       }
227       function setOptions() {
228         if (++calls == optionsValues.length) {
229           disconnect();
230         } else {
231           serial.update(connectionId,
232                         optionsValues[calls],
233                         test.callbackPass(function(success) {
234             serial.getInfo(connectionId, test.callbackPass(checkInfo));
235             test.assertTrue(success);
236           }));
237         }
238       }
239       setOptions();
240     });
241   },
243   function testUpdateInvalidBitrate() {
244     connect(function() {
245       serial.update(connectionId,
246                     {bitrate: -1},
247                     test.callbackPass(function(success) {
248         disconnect();
249         test.assertFalse(success);
250       }));
251     });
252   },
254   function testFlush() {
255     connect(function() {
256       serial.flush(connectionId,
257                      test.callbackPass(function(success) {
258         disconnect();
259         test.assertTrue(success);
260       }));
261     });
262   },
264   function testSetPaused() {
265     connect(function() {
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);
271               disconnect();
272             }));
273           }));
274           test.assertTrue(info.paused);
275         }));
276       }));
277     });
278   },
280   function testDisconnectUnknownConnectionId() {
281     serial.disconnect(-1, test.callbackFail('Serial connection not found.'));
282   },
284   function testGetInfoUnknownConnectionId() {
285     serial.getInfo(-1, test.callbackFail('Serial connection not found.'));
286   },
288   function testUpdateUnknownConnectionId() {
289     serial.update(-1, {}, test.callbackFail('Serial connection not found.'));
290   },
292   function testSetControlSignalsUnknownConnectionId() {
293     serial.setControlSignals(-1, {}, test.callbackFail(
294         'Serial connection not found.'));
295   },
297   function testGetControlSignalsUnknownConnectionId() {
298     serial.getControlSignals(-1, test.callbackFail(
299         'Serial connection not found.'));
300   },
302   function testFlushUnknownConnectionId() {
303     serial.flush(-1, test.callbackFail('Serial connection not found.'));
304   },
306   function testSetPausedUnknownConnectionId() {
307     serial.setPaused(
308         -1, true, test.callbackFail('Serial connection not found.'));
309     serial.setPaused(
310         -1, false, test.callbackFail('Serial connection not found.'));
311   },
312 ], test.runTests, exports);