| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 serialPort = null; | |
| 6 | |
| 7 var testSerial = function() { | |
| 8 var connectionId = -1; | |
| 9 var testString = 'x'; | |
| 10 | |
| 11 var onClose = function(result) { | |
| 12 chrome.test.assertTrue(result); | |
| 13 chrome.test.succeed(); | |
| 14 } | |
| 15 | |
| 16 var onRead = function(readInfo) { | |
| 17 chrome.test.assertEq(testString, readInfo.message); | |
| 18 chrome.experimental.serial.close(connectionId, onClose); | |
| 19 } | |
| 20 | |
| 21 var onWrite = function(writeInfo) { | |
| 22 chrome.test.assertEq(1, writeInfo.bytesWritten); | |
| 23 chrome.experimental.serial.read(connectionId, onRead); | |
| 24 } | |
| 25 | |
| 26 var onOpen = function(connectionInfo) { | |
| 27 connectionId = connectionInfo.connectionId; | |
| 28 chrome.test.assertTrue(connectionId > 0, 'Failed to open serial port.'); | |
| 29 chrome.experimental.serial.write(connectionId, testString, onWrite); | |
| 30 } | |
| 31 | |
| 32 chrome.experimental.serial.open(serialPort, onOpen); | |
| 33 }; | |
| 34 | |
| 35 var testGetPorts = function() { | 5 var testGetPorts = function() { |
| 36 var onGetPorts = function(ports) { | 6 var onGetPorts = function(ports) { |
| 37 // Any length is potentially valid, because we're on unknown hardware. But | 7 // Any length is potentially valid, because we're on unknown hardware. But |
| 38 // we are testing at least that the ports member was filled in, so it's | 8 // we are testing at least that the ports member was filled in, so it's |
| 39 // still a somewhat meaningful test. | 9 // still a somewhat meaningful test. |
| 40 chrome.test.assertTrue(ports.length >= 0); | 10 chrome.test.assertTrue(ports.length >= 0); |
| 41 chrome.test.succeed(); | 11 chrome.test.succeed(); |
| 42 } | 12 } |
| 43 | 13 |
| 44 chrome.experimental.serial.getPorts(onGetPorts); | 14 chrome.experimental.serial.getPorts(onGetPorts); |
| 45 }; | 15 }; |
| 46 | 16 |
| 47 var testMaybeOpenPort = function() { | 17 var testMaybeOpenPort = function() { |
| 48 var onGetPorts = function(ports) { | 18 var onGetPorts = function(ports) { |
| 49 // We're testing as much as we can here without actually assuming the | 19 // We're testing as much as we can here without actually assuming the |
| 50 // existence of attached hardware. | 20 // existence of attached hardware. |
| 51 // | 21 // |
| 52 // TODO(miket): is there any chance that just opening a serial port but not | 22 // TODO(miket): is there any chance that just opening a serial port but not |
| 53 // doing anything could be harmful to devices attached to a developer's | 23 // doing anything could be harmful to devices attached to a developer's |
| 54 // machine? | 24 // machine? |
| 55 if (ports.length > 0) { | 25 if (ports.length > 0) { |
| 56 var closedCount = ports.length; | 26 var currentPort = 0; |
| 27 |
| 28 var onFinishedWithPort = function() { |
| 29 if (currentPort >= ports.length) |
| 30 chrome.test.succeed(); |
| 31 else |
| 32 onStartTest(); |
| 33 }; |
| 34 |
| 57 var onClose = function(r) { | 35 var onClose = function(r) { |
| 58 if (--closedCount == 0) { | 36 onFinishedWithPort(); |
| 59 chrome.test.succeed(); | 37 }; |
| 38 |
| 39 var onOpen = function(connectionInfo) { |
| 40 var id = connectionInfo.connectionId; |
| 41 if (id > 0) |
| 42 chrome.experimental.serial.close(id, onClose); |
| 43 else |
| 44 onFinishedWithPort(); |
| 45 }; |
| 46 |
| 47 var onStartTest = function() { |
| 48 var port = ports[currentPort++]; |
| 49 |
| 50 // TODO(miket): opening Bluetooth ports on OSX is unreliable. |
| 51 // Investigate. |
| 52 if (port.match(/[Bb]luetooth/)) { |
| 53 console.log("Skipping Bluetooth serial device " + port); |
| 54 onFinishedWithPort(); |
| 55 } else { |
| 56 console.log("Opening serial device " + port); |
| 57 chrome.experimental.serial.open(port, onOpen); |
| 60 } | 58 } |
| 61 }; | |
| 62 var onOpen = function(connectionInfo) { | |
| 63 chrome.experimental.serial.close(connectionInfo.connectionId, | |
| 64 onClose); | |
| 65 }; | |
| 66 for (var i = 0; i < ports.length; i++) { | |
| 67 chrome.experimental.serial.open(ports[i], onOpen); | |
| 68 } | 59 } |
| 60 |
| 61 onStartTest(); |
| 69 } else { | 62 } else { |
| 70 // There aren't any valid ports on this machine. That's OK. | 63 // There aren't any valid ports on this machine. That's OK. |
| 71 chrome.test.succeed(); | 64 chrome.test.succeed(); |
| 72 } | 65 } |
| 73 } | 66 } |
| 74 | 67 |
| 75 chrome.experimental.serial.getPorts(onGetPorts); | 68 chrome.experimental.serial.getPorts(onGetPorts); |
| 76 }; | 69 }; |
| 77 | 70 |
| 71 var testSerial = function() { |
| 72 var serialPort = null; |
| 73 var connectionId = -1; |
| 74 var testBuffer = new ArrayBuffer(1); |
| 75 |
| 76 var uint8View = new Uint8Array(testBuffer); |
| 77 uint8View[0] = 42; |
| 78 |
| 79 var onClose = function(result) { |
| 80 chrome.test.assertTrue(result); |
| 81 chrome.test.succeed(); |
| 82 }; |
| 83 |
| 84 var onRead = function(readInfo) { |
| 85 chrome.test.assertEq(1, readInfo.bytesRead, |
| 86 "read() failed to return bytesRead 1."); |
| 87 if (readInfo.bytesRead == 1) { |
| 88 var messageUint8View = new Uint8Array(readInfo.data); |
| 89 chrome.test.assertEq(uint8View[0], messageUint8View[0], |
| 90 "Byte read was not equal to byte written."); |
| 91 } |
| 92 chrome.experimental.serial.close(connectionId, onClose); |
| 93 }; |
| 94 |
| 95 var onWrite = function(writeInfo) { |
| 96 chrome.test.assertEq(1, writeInfo.bytesWritten, |
| 97 "Failed to write byte."); |
| 98 if (writeInfo.bytesWritten == 1) { |
| 99 chrome.experimental.serial.read(connectionId, onRead); |
| 100 } else |
| 101 chrome.experimental.serial.close(connectionId, onClose); |
| 102 }; |
| 103 |
| 104 var onOpen = function(connectionInfo) { |
| 105 connectionId = connectionInfo.connectionId; |
| 106 chrome.test.assertTrue(connectionId > 0, 'Failed to open serial port.'); |
| 107 chrome.experimental.serial.write(connectionId, testBuffer, onWrite); |
| 108 }; |
| 109 |
| 110 var onGetPorts = function(ports) { |
| 111 if (ports.length > 0) { |
| 112 serialPort = ports[0]; |
| 113 console.log("Connecting to serial device at " + serialPort); |
| 114 chrome.experimental.serial.open(serialPort, onOpen); |
| 115 } else { |
| 116 // No serial device found. This is still considered a success because we |
| 117 // can't rely on specific hardware being present on the machine. |
| 118 chrome.test.succeed(); |
| 119 } |
| 120 }; |
| 121 |
| 122 chrome.experimental.serial.getPorts(onGetPorts); |
| 123 }; |
| 124 |
| 78 var onMessageReply = function(message) { | 125 var onMessageReply = function(message) { |
| 79 serialPort = message; | 126 var tests = [testGetPorts, testMaybeOpenPort]; |
| 80 var generalTests = [ testGetPorts, testMaybeOpenPort ]; | |
| 81 | 127 |
| 82 chrome.test.runTests(generalTests); | 128 if (message == 'echo_device_attached') { |
| 83 if (message != 'none') { | |
| 84 // We have a specific serial port set up to respond to test traffic. This | 129 // We have a specific serial port set up to respond to test traffic. This |
| 85 // is a rare situation. TODO(miket): mock to make it testable under any | 130 // is a rare situation. TODO(miket): mock to make it testable under any |
| 86 // hardware conditions. | 131 // hardware conditions. |
| 87 chrome.test.runTests([ testSerial ]); | 132 tests.push(testSerial); |
| 88 } | 133 } |
| 134 chrome.test.runTests(tests); |
| 89 }; | 135 }; |
| 90 | 136 |
| 91 chrome.test.sendMessage("serial_port", onMessageReply); | 137 chrome.test.sendMessage("serial_port", onMessageReply); |
| OLD | NEW |