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
3 // found in the LICENSE file.
9 #include "base/memory/scoped_ptr.h"
10 #include "chrome/browser/extensions/api/serial/serial_api.h"
11 #include "chrome/browser/extensions/api/serial/serial_connection.h"
12 #include "chrome/browser/extensions/extension_apitest.h"
13 #include "chrome/browser/extensions/extension_function.h"
14 #include "chrome/browser/extensions/extension_function_test_utils.h"
15 #include "chrome/browser/extensions/extension_test_message_listener.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "testing/gmock/include/gmock/gmock.h"
21 using testing::Return;
23 using content::BrowserThread;
25 namespace serial = extensions::api::serial;
29 class SerialApiTest : public ExtensionApiTest {
36 namespace extensions {
38 class FakeSerialGetPortsFunction : public AsyncExtensionFunction {
40 virtual bool RunImpl() OVERRIDE {
41 base::ListValue* ports = new base::ListValue();
42 ports->Append(Value::CreateStringValue("/dev/fakeserial"));
43 ports->Append(Value::CreateStringValue("\\\\COM800\\"));
49 virtual ~FakeSerialGetPortsFunction() {}
52 class FakeEchoSerialConnection : public SerialConnection {
54 explicit FakeEchoSerialConnection(
55 const std::string& port,
57 serial::DataBit databit,
58 serial::ParityBit parity,
59 serial::StopBit stopbit,
60 const std::string& owner_extension_id)
61 : SerialConnection(port, bitrate, databit, parity, stopbit,
68 virtual ~FakeEchoSerialConnection() {
77 virtual void Close() {
81 virtual void Flush() {
86 virtual int Read(scoped_refptr<net::IOBufferWithSize> io_buffer) {
87 DCHECK(io_buffer->data());
89 if (buffer_.empty()) {
92 char *data = io_buffer->data();
93 int bytes_to_copy = io_buffer->size();
94 while (bytes_to_copy-- && !buffer_.empty()) {
95 *data++ = buffer_.front();
98 return io_buffer->size();
101 virtual int Write(scoped_refptr<net::IOBuffer> io_buffer, int byte_count) {
102 DCHECK(io_buffer.get());
103 DCHECK_GE(byte_count, 0);
105 char *data = io_buffer->data();
106 int count = byte_count;
108 buffer_.push_back(*data++);
112 MOCK_METHOD1(GetControlSignals, bool(ControlSignals &));
113 MOCK_METHOD1(SetControlSignals, bool(const ControlSignals &));
117 std::deque<char> buffer_;
119 DISALLOW_COPY_AND_ASSIGN(FakeEchoSerialConnection);
122 class FakeSerialOpenFunction : public SerialOpenFunction {
124 virtual SerialConnection* CreateSerialConnection(
125 const std::string& port,
127 serial::DataBit databit,
128 serial::ParityBit parity,
129 serial::StopBit stopbit,
130 const std::string& owner_extension_id) OVERRIDE {
131 FakeEchoSerialConnection* serial_connection =
132 new FakeEchoSerialConnection(port, bitrate, databit, parity, stopbit,
134 EXPECT_CALL(*serial_connection, GetControlSignals(_)).
135 Times(1).WillOnce(Return(true));
136 EXPECT_CALL(*serial_connection, SetControlSignals(_)).
137 Times(1).WillOnce(Return(true));
138 return serial_connection;
140 virtual bool DoesPortExist(const std::string& port) OVERRIDE {
145 virtual ~FakeSerialOpenFunction() {}
148 } // namespace extensions
150 ExtensionFunction* FakeSerialGetPortsFunctionFactory() {
151 return new extensions::FakeSerialGetPortsFunction();
154 ExtensionFunction* FakeSerialOpenFunctionFactory() {
155 return new extensions::FakeSerialOpenFunction();
158 // Disable SIMULATE_SERIAL_PORTS only if all the following are true:
160 // 1. You have an Arduino or compatible board attached to your machine and
161 // properly appearing as the first virtual serial port ("first" is very loosely
162 // defined as whichever port shows up in serial.getPorts). We've tested only
163 // the Atmega32u4 Breakout Board and Arduino Leonardo; note that both these
164 // boards are based on the Atmel ATmega32u4, rather than the more common
165 // Arduino '328p with either FTDI or '8/16u2 USB interfaces. TODO: test more
168 // 2. Your user has permission to read/write the port. For example, this might
169 // mean that your user is in the "tty" or "uucp" group on Ubuntu flavors of
170 // Linux, or else that the port's path (e.g., /dev/ttyACM0) has global
171 // read/write permissions.
173 // 3. You have uploaded a program to the board that does a byte-for-byte echo
174 // on the virtual serial port at 57600 bps. An example is at
175 // chrome/test/data/extensions/api_test/serial/api/serial_arduino_test.ino.
177 #define SIMULATE_SERIAL_PORTS (1)
178 IN_PROC_BROWSER_TEST_F(SerialApiTest, SerialFakeHardware) {
179 ResultCatcher catcher;
180 catcher.RestrictToProfile(browser()->profile());
182 #if SIMULATE_SERIAL_PORTS
183 ASSERT_TRUE(ExtensionFunctionDispatcher::OverrideFunction(
185 FakeSerialGetPortsFunctionFactory));
186 ASSERT_TRUE(ExtensionFunctionDispatcher::OverrideFunction(
188 FakeSerialOpenFunctionFactory));
191 ASSERT_TRUE(RunExtensionTest("serial/api")) << message_;
194 IN_PROC_BROWSER_TEST_F(SerialApiTest, SerialRealHardware) {
195 ResultCatcher catcher;
196 catcher.RestrictToProfile(browser()->profile());
198 ASSERT_TRUE(RunExtensionTest("serial/real_hardware")) << message_;