Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(299)

Side by Side Diff: chrome/test/data/extensions/api_test/serial/api/background.js

Issue 10392181: Implement serial API for Windows. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: From -> At. Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/common/extensions/api/experimental_serial.idl ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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; 5 // TODO(miket): opening Bluetooth ports on OSX is unreliable. Investigate.
6 6 function shouldSkipPort(portName) {
7 var testSerial = function() { 7 return portName.match(/[Bb]luetooth/);
8 var connectionId = -1; 8 }
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 9
35 var testGetPorts = function() { 10 var testGetPorts = function() {
36 var onGetPorts = function(ports) { 11 var onGetPorts = function(ports) {
37 // Any length is potentially valid, because we're on unknown hardware. But 12 // 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 13 // we are testing at least that the ports member was filled in, so it's
39 // still a somewhat meaningful test. 14 // still a somewhat meaningful test.
40 chrome.test.assertTrue(ports.length >= 0); 15 chrome.test.assertTrue(ports.length >= 0);
41 chrome.test.succeed(); 16 chrome.test.succeed();
42 } 17 }
43 18
44 chrome.experimental.serial.getPorts(onGetPorts); 19 chrome.experimental.serial.getPorts(onGetPorts);
45 }; 20 };
46 21
47 var testMaybeOpenPort = function() { 22 var testMaybeOpenPort = function() {
48 var onGetPorts = function(ports) { 23 var onGetPorts = function(ports) {
49 // We're testing as much as we can here without actually assuming the 24 // We're testing as much as we can here without actually assuming the
50 // existence of attached hardware. 25 // existence of attached hardware.
51 // 26 //
52 // TODO(miket): is there any chance that just opening a serial port but not 27 // 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 28 // doing anything could be harmful to devices attached to a developer's
54 // machine? 29 // machine?
55 if (ports.length > 0) { 30 if (ports.length > 0) {
56 var closedCount = ports.length; 31 var currentPort = 0;
32
33 var onFinishedWithPort = function() {
34 if (currentPort >= ports.length)
35 chrome.test.succeed();
36 else
37 onStartTest();
38 };
39
57 var onClose = function(r) { 40 var onClose = function(r) {
58 if (--closedCount == 0) { 41 onFinishedWithPort();
59 chrome.test.succeed(); 42 };
43
44 var onOpen = function(connectionInfo) {
45 var id = connectionInfo.connectionId;
46 if (id > 0)
47 chrome.experimental.serial.close(id, onClose);
48 else
49 onFinishedWithPort();
50 };
51
52 var onStartTest = function() {
53 var port = ports[currentPort++];
54
55 if (shouldSkipPort(port)) {
56 onFinishedWithPort();
57 } else {
58 console.log("Opening serial device " + port);
59 chrome.experimental.serial.open(port, onOpen);
60 } 60 }
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 } 61 }
62
63 onStartTest();
69 } else { 64 } else {
70 // There aren't any valid ports on this machine. That's OK. 65 // There aren't any valid ports on this machine. That's OK.
71 chrome.test.succeed(); 66 chrome.test.succeed();
72 } 67 }
73 } 68 }
74 69
75 chrome.experimental.serial.getPorts(onGetPorts); 70 chrome.experimental.serial.getPorts(onGetPorts);
76 }; 71 };
77 72
73 var testSerial = function() {
74 var serialPort = null;
75 var connectionId = -1;
76 var testBuffer = new ArrayBuffer(1);
77 var readTries = 10;
78
79 var uint8View = new Uint8Array(testBuffer);
80 uint8View[0] = 42;
81
82 var onClose = function(result) {
83 chrome.test.assertTrue(result);
84 chrome.test.succeed();
85 };
86
87 var doRead = function() {
88 chrome.experimental.serial.read(connectionId, onRead);
89 }
90
91 var onRead = function(readInfo) {
92 if (readInfo.bytesRead == 1) {
93 var messageUint8View = new Uint8Array(readInfo.data);
94 chrome.test.assertEq(uint8View[0], messageUint8View[0],
95 'Byte read was not equal to byte written.');
96 chrome.experimental.serial.close(connectionId, onClose);
97 } else {
98 if (--readTries > 0) {
99 setTimeout(doRead, 100);
100 } else {
101 chrome.test.assertEq(1, readInfo.bytesRead,
102 'read() failed to return bytesRead 1.');
103 }
104 }
105 };
106
107 var onWrite = function(writeInfo) {
108 chrome.test.assertEq(1, writeInfo.bytesWritten,
109 'Failed to write byte.');
110 if (writeInfo.bytesWritten == 1) {
111 doRead();
112 } else
113 chrome.experimental.serial.close(connectionId, onClose);
114 };
115
116 var onOpen = function(connectionInfo) {
117 connectionId = connectionInfo.connectionId;
118 chrome.test.assertTrue(connectionId > 0, 'Failed to open serial port.');
119 chrome.experimental.serial.write(connectionId, testBuffer, onWrite);
120 };
121
122 var onGetPorts = function(ports) {
123 if (ports.length > 0) {
124 var portNumber = 0;
125 while (portNumber < ports.length) {
126 if (shouldSkipPort(ports[portNumber])) {
127 portNumber++;
128 continue;
129 } else
130 break;
131 }
132 if (portNumber < ports.length) {
133 serialPort = ports[portNumber];
134 console.log('Connecting to serial device at ' + serialPort);
135 chrome.experimental.serial.open(serialPort, onOpen);
136 } else {
137 // We didn't find a port that we think we should try.
138 chrome.test.succeed();
139 }
140 } else {
141 // No serial device found. This is still considered a success because we
142 // can't rely on specific hardware being present on the machine.
143 chrome.test.succeed();
144 }
145 };
146
147 chrome.experimental.serial.getPorts(onGetPorts);
148 };
149
78 var onMessageReply = function(message) { 150 var onMessageReply = function(message) {
79 serialPort = message; 151 var tests = [testGetPorts, testMaybeOpenPort];
80 var generalTests = [ testGetPorts, testMaybeOpenPort ];
81 152
82 chrome.test.runTests(generalTests); 153 if (message == 'echo_device_attached') {
83 if (message != 'none') {
84 // We have a specific serial port set up to respond to test traffic. This 154 // 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 155 // is a rare situation. TODO(miket): mock to make it testable under any
86 // hardware conditions. 156 // hardware conditions.
87 chrome.test.runTests([ testSerial ]); 157 tests.push(testSerial);
88 } 158 }
159 chrome.test.runTests(tests);
89 }; 160 };
90 161
91 chrome.test.sendMessage("serial_port", onMessageReply); 162 chrome.test.sendMessage('serial_port', onMessageReply);
OLDNEW
« no previous file with comments | « chrome/common/extensions/api/experimental_serial.idl ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698