Chromium Code Reviews| 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 var test = require('test').binding; | 5 var test = require('test').binding; |
| 6 var serial = require('serial').binding; | 6 var serial = require('serial').binding; |
| 7 var unittestBindings = require('test_environment_specific_bindings'); | 7 var unittestBindings = require('test_environment_specific_bindings'); |
| 8 | 8 |
|
raymes
2014/08/14 02:58:05
Please add a comment to this file explaining what
Sam McNally
2014/08/14 04:33:51
Done.
| |
| 9 var connectionId = null; | |
| 10 | |
| 11 function connect(callback, options) { | |
| 12 options = options || { | |
| 13 name: 'test connection', | |
| 14 bufferSize: 8192, | |
| 15 receiveTimeout: 12345, | |
| 16 sendTimeout: 6789, | |
| 17 persistent: true, | |
| 18 } | |
| 19 serial.connect('device', options, test.callbackPass(function(connectionInfo) { | |
| 20 connectionId = connectionInfo.connectionId; | |
| 21 callback(connectionInfo); | |
| 22 })); | |
| 23 } | |
| 24 | |
| 25 function disconnect() { | |
| 26 serial.disconnect(connectionId, test.callbackPass(function(success) { | |
| 27 test.assertTrue(success); | |
| 28 connectionId = null; | |
| 29 })); | |
| 30 } | |
| 31 | |
| 32 function checkClientConnectionInfo(connectionInfo) { | |
| 33 test.assertFalse(connectionInfo.persistent); | |
| 34 test.assertEq('test connection', connectionInfo.name); | |
| 35 test.assertEq(12345, connectionInfo.receiveTimeout); | |
| 36 test.assertEq(6789, connectionInfo.sendTimeout); | |
| 37 test.assertEq(8192, connectionInfo.bufferSize); | |
| 38 test.assertFalse(connectionInfo.paused); | |
| 39 } | |
| 40 | |
| 41 function checkServiceConnectionInfo(connectionInfo) { | |
| 42 test.assertEq(9600, connectionInfo.bitrate); | |
| 43 test.assertEq('eight', connectionInfo.dataBits); | |
| 44 test.assertEq('no', connectionInfo.parityBit); | |
| 45 test.assertEq('one', connectionInfo.stopBits); | |
| 46 test.assertFalse(connectionInfo.ctsFlowControl); | |
| 47 } | |
| 48 | |
| 49 function checkConnectionInfo(connectionInfo) { | |
| 50 checkClientConnectionInfo(connectionInfo); | |
| 51 checkServiceConnectionInfo(connectionInfo); | |
| 52 } | |
| 53 | |
| 9 unittestBindings.exportTests([ | 54 unittestBindings.exportTests([ |
| 10 function testGetDevices() { | 55 function testGetDevices() { |
| 11 serial.getDevices(test.callbackPass(function(devices) { | 56 serial.getDevices(test.callbackPass(function(devices) { |
| 12 test.assertEq(3, devices.length); | 57 test.assertEq(3, devices.length); |
| 13 test.assertEq(4, $Object.keys(devices[0]).length); | 58 test.assertEq(4, $Object.keys(devices[0]).length); |
| 14 test.assertEq('device', devices[0].path); | 59 test.assertEq('device', devices[0].path); |
| 15 test.assertEq(1234, devices[0].vendorId); | 60 test.assertEq(1234, devices[0].vendorId); |
| 16 test.assertEq(5678, devices[0].productId); | 61 test.assertEq(5678, devices[0].productId); |
| 17 test.assertEq('foo', devices[0].displayName); | 62 test.assertEq('foo', devices[0].displayName); |
| 18 test.assertEq(1, $Object.keys(devices[1]).length); | 63 test.assertEq(1, $Object.keys(devices[1]).length); |
| 19 test.assertEq('another_device', devices[1].path); | 64 test.assertEq('another_device', devices[1].path); |
| 20 test.assertEq(1, $Object.keys(devices[2]).length); | 65 test.assertEq(1, $Object.keys(devices[2]).length); |
| 21 test.assertEq('', devices[2].path); | 66 test.assertEq('', devices[2].path); |
| 22 })); | 67 })); |
| 23 }, | 68 }, |
| 69 | |
| 70 function testConnectFail() { | |
| 71 serial.connect('device', | |
| 72 test.callbackFail('Failed to connect to the port.')); | |
| 73 }, | |
| 74 | |
| 75 function testGetInfoFailOnConnect() { | |
| 76 serial.connect('device', | |
| 77 test.callbackFail('Failed to connect to the port.')); | |
| 78 }, | |
| 79 | |
| 80 function testConnectInvalidBitrate() { | |
| 81 serial.connect('device', {bitrate: -1}, test.callbackFail( | |
| 82 'Failed to connect to the port.')); | |
| 83 }, | |
| 84 | |
| 85 function testConnect() { | |
| 86 connect(function(connectionInfo) { | |
| 87 disconnect(); | |
| 88 checkConnectionInfo(connectionInfo); | |
| 89 }); | |
| 90 }, | |
| 91 | |
| 92 function testConnectDefaultOptions() { | |
| 93 connect(function(connectionInfo) { | |
| 94 disconnect(); | |
| 95 test.assertEq(9600, connectionInfo.bitrate); | |
| 96 test.assertEq('eight', connectionInfo.dataBits); | |
| 97 test.assertEq('no', connectionInfo.parityBit); | |
| 98 test.assertEq('one', connectionInfo.stopBits); | |
| 99 test.assertFalse(connectionInfo.ctsFlowControl); | |
| 100 test.assertFalse(connectionInfo.persistent); | |
| 101 test.assertEq('', connectionInfo.name); | |
| 102 test.assertEq(0, connectionInfo.receiveTimeout); | |
| 103 test.assertEq(0, connectionInfo.sendTimeout); | |
| 104 test.assertEq(4096, connectionInfo.bufferSize); | |
| 105 }, {}); | |
| 106 }, | |
| 107 | |
| 108 function testGetInfo() { | |
| 109 connect(function() { | |
| 110 serial.getInfo(connectionId, | |
| 111 test.callbackPass(function(connectionInfo) { | |
| 112 disconnect(); | |
| 113 checkConnectionInfo(connectionInfo); | |
| 114 })); | |
| 115 }); | |
| 116 }, | |
| 117 | |
| 118 function testGetInfoFailToGetPortInfo() { | |
| 119 connect(function() { | |
| 120 serial.getInfo(connectionId, | |
| 121 test.callbackPass(function(connectionInfo) { | |
| 122 disconnect(); | |
| 123 checkClientConnectionInfo(connectionInfo); | |
| 124 test.assertFalse('bitrate' in connectionInfo); | |
| 125 test.assertFalse('dataBits' in connectionInfo); | |
| 126 test.assertFalse('parityBit' in connectionInfo); | |
| 127 test.assertFalse('stopBit' in connectionInfo); | |
| 128 test.assertFalse('ctsFlowControl' in connectionInfo); | |
| 129 })); | |
| 130 }); | |
| 131 }, | |
| 132 | |
| 133 function testGetConnections() { | |
| 134 connect(function() { | |
| 135 serial.getConnections(test.callbackPass(function(connections) { | |
| 136 disconnect(); | |
| 137 test.assertEq(1, connections.length); | |
| 138 checkConnectionInfo(connections[0]); | |
| 139 })); | |
| 140 }); | |
| 141 }, | |
| 142 | |
| 143 function testGetControlSignals() { | |
| 144 connect(function() { | |
| 145 var calls = 0; | |
| 146 function checkControlSignals(signals) { | |
| 147 if (calls == 15) { | |
| 148 disconnect(); | |
| 149 } else { | |
| 150 serial.getControlSignals( | |
| 151 connectionId, | |
| 152 test.callbackPass(checkControlSignals)); | |
| 153 } | |
| 154 test.assertEq(!!(calls & 1), signals.dcd); | |
| 155 test.assertEq(!!(calls & 2), signals.cts); | |
| 156 test.assertEq(!!(calls & 4), signals.ri); | |
| 157 test.assertEq(!!(calls & 8), signals.dsr); | |
| 158 calls++; | |
| 159 } | |
| 160 serial.getControlSignals(connectionId, | |
| 161 test.callbackPass(checkControlSignals)); | |
| 162 }); | |
| 163 }, | |
| 164 | |
| 165 function testSetControlSignals() { | |
| 166 connect(function() { | |
| 167 var signalsValues = [ | |
| 168 {}, | |
| 169 {dtr: false}, | |
| 170 {dtr: true}, | |
| 171 {rts: false}, | |
| 172 {dtr: false, rts: false}, | |
| 173 {dtr: true, rts: false}, | |
| 174 {rts: true}, | |
| 175 {dtr: false, rts: true}, | |
| 176 {dtr: true, rts: true}, | |
| 177 ]; | |
| 178 var calls = 0; | |
| 179 function setControlSignals(success) { | |
| 180 if (calls == signalsValues.length) { | |
| 181 disconnect(); | |
| 182 } else { | |
| 183 serial.setControlSignals(connectionId, | |
| 184 signalsValues[calls++], | |
| 185 test.callbackPass(setControlSignals)); | |
| 186 } | |
| 187 test.assertTrue(success); | |
| 188 } | |
| 189 setControlSignals(true); | |
| 190 }); | |
| 191 }, | |
| 192 | |
| 193 function testUpdate() { | |
| 194 connect(function() { | |
| 195 var optionsValues = [ | |
| 196 {}, // SetPortOptions is called once during connection. | |
| 197 {bitrate: 57600}, | |
| 198 {dataBits: 'seven'}, | |
| 199 {dataBits: 'eight'}, | |
| 200 {parityBit: 'no'}, | |
| 201 {parityBit: 'odd'}, | |
| 202 {parityBit: 'even'}, | |
| 203 {stopBits: 'one'}, | |
| 204 {stopBits: 'two'}, | |
| 205 {ctsFlowControl: false}, | |
| 206 {ctsFlowControl: true}, | |
| 207 {bufferSize: 1}, | |
| 208 {sendTimeout: 0}, | |
| 209 {receiveTimeout: 0}, | |
| 210 {persistent: false}, | |
| 211 {name: 'name'}, | |
| 212 ]; | |
| 213 var calls = 0; | |
| 214 function checkInfo(info) { | |
| 215 for (var key in optionsValues[calls]) { | |
| 216 test.assertEq(optionsValues[calls][key], info[key]); | |
| 217 } | |
| 218 setOptions(); | |
| 219 } | |
| 220 function setOptions() { | |
| 221 if (++calls == optionsValues.length) { | |
| 222 disconnect(); | |
| 223 } else { | |
| 224 serial.update(connectionId, | |
| 225 optionsValues[calls], | |
| 226 test.callbackPass(function(success) { | |
| 227 serial.getInfo(connectionId, test.callbackPass(checkInfo)); | |
| 228 test.assertTrue(success); | |
| 229 })); | |
| 230 } | |
| 231 } | |
| 232 setOptions(); | |
| 233 }); | |
| 234 }, | |
| 235 | |
| 236 function testUpdateInvalidBitrate() { | |
| 237 connect(function() { | |
| 238 serial.update(connectionId, | |
| 239 {bitrate: -1}, | |
| 240 test.callbackPass(function(success) { | |
| 241 disconnect(); | |
| 242 test.assertFalse(success); | |
| 243 })); | |
| 244 }); | |
| 245 }, | |
| 246 | |
| 247 function testFlush() { | |
| 248 connect(function() { | |
| 249 serial.flush(connectionId, | |
| 250 test.callbackPass(function(success) { | |
| 251 disconnect(); | |
| 252 test.assertTrue(success); | |
| 253 })); | |
| 254 }); | |
| 255 }, | |
| 256 | |
| 257 function testSetPaused() { | |
| 258 connect(function() { | |
| 259 serial.setPaused(connectionId, true, test.callbackPass(function() { | |
| 260 serial.getInfo(connectionId, test.callbackPass(function(info) { | |
| 261 serial.setPaused(connectionId, false, test.callbackPass(function() { | |
| 262 serial.getInfo(connectionId, test.callbackPass(function(info) { | |
| 263 test.assertFalse(info.paused); | |
| 264 disconnect(); | |
| 265 })); | |
| 266 })); | |
| 267 test.assertTrue(info.paused); | |
| 268 })); | |
| 269 })); | |
| 270 }); | |
| 271 }, | |
| 272 | |
| 273 function testDisconnectUnknownConnectionId() { | |
| 274 serial.disconnect(-1, test.callbackFail('Serial connection not found.')); | |
| 275 }, | |
| 276 | |
| 277 function testGetInfoUnknownConnectionId() { | |
| 278 serial.getInfo(-1, test.callbackFail('Serial connection not found.')); | |
| 279 }, | |
| 280 | |
| 281 function testUpdateUnknownConnectionId() { | |
| 282 serial.update(-1, {}, test.callbackFail('Serial connection not found.')); | |
| 283 }, | |
| 284 | |
| 285 function testSetControlSignalsUnknownConnectionId() { | |
| 286 serial.setControlSignals(-1, {}, test.callbackFail( | |
| 287 'Serial connection not found.')); | |
| 288 }, | |
| 289 | |
| 290 function testGetControlSignalsUnknownConnectionId() { | |
| 291 serial.getControlSignals(-1, test.callbackFail( | |
| 292 'Serial connection not found.')); | |
| 293 }, | |
| 294 | |
| 295 function testFlushUnknownConnectionId() { | |
| 296 serial.flush(-1, test.callbackFail('Serial connection not found.')); | |
| 297 }, | |
| 298 | |
| 299 function testSetPausedUnknownConnectionId() { | |
| 300 serial.setPaused( | |
| 301 -1, true, test.callbackFail('Serial connection not found.')); | |
| 302 serial.setPaused( | |
| 303 -1, false, test.callbackFail('Serial connection not found.')); | |
| 304 }, | |
| 24 ], test.runTests, exports); | 305 ], test.runTests, exports); |
| OLD | NEW |