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.
7 #include "chrome/browser/extensions/extension_apitest.h"
8 #include "device/serial/test_serial_io_handler.h"
9 #include "extensions/browser/api/serial/serial_api.h"
10 #include "extensions/browser/api/serial/serial_connection.h"
11 #include "extensions/browser/extension_function.h"
12 #include "extensions/common/api/serial.h"
13 #include "extensions/test/result_catcher.h"
14 #include "testing/gmock/include/gmock/gmock.h"
17 using testing::Return;
21 class SerialApiTest : public ExtensionApiTest {
28 namespace extensions {
30 class FakeSerialGetDevicesFunction : public AsyncExtensionFunction {
32 virtual bool RunAsync() OVERRIDE {
33 base::ListValue* devices = new base::ListValue();
34 base::DictionaryValue* device0 = new base::DictionaryValue();
35 device0->SetString("path", "/dev/fakeserial");
36 base::DictionaryValue* device1 = new base::DictionaryValue();
37 device1->SetString("path", "\\\\COM800\\");
38 devices->Append(device0);
39 devices->Append(device1);
46 virtual ~FakeSerialGetDevicesFunction() {}
49 class FakeEchoSerialIoHandler : public device::TestSerialIoHandler {
51 explicit FakeEchoSerialIoHandler() {
52 device_control_signals()->dcd = true;
53 device_control_signals()->cts = true;
54 device_control_signals()->ri = true;
55 device_control_signals()->dsr = true;
58 MOCK_METHOD1(SetControlSignals,
59 bool(const device::serial::HostControlSignals&));
62 virtual ~FakeEchoSerialIoHandler() {}
65 DISALLOW_COPY_AND_ASSIGN(FakeEchoSerialIoHandler);
68 class FakeSerialConnectFunction : public core_api::SerialConnectFunction {
70 virtual SerialConnection* CreateSerialConnection(
71 const std::string& port,
72 const std::string& owner_extension_id) const OVERRIDE {
73 scoped_refptr<FakeEchoSerialIoHandler> io_handler =
74 new FakeEchoSerialIoHandler;
75 EXPECT_CALL(*io_handler.get(), SetControlSignals(_)).Times(1).WillOnce(
77 SerialConnection* serial_connection =
78 new SerialConnection(port, owner_extension_id);
79 serial_connection->SetIoHandlerForTest(io_handler);
80 return serial_connection;
84 virtual ~FakeSerialConnectFunction() {}
87 } // namespace extensions
89 ExtensionFunction* FakeSerialGetDevicesFunctionFactory() {
90 return new extensions::FakeSerialGetDevicesFunction();
93 ExtensionFunction* FakeSerialConnectFunctionFactory() {
94 return new extensions::FakeSerialConnectFunction();
97 // Disable SIMULATE_SERIAL_PORTS only if all the following are true:
99 // 1. You have an Arduino or compatible board attached to your machine and
100 // properly appearing as the first virtual serial port ("first" is very loosely
101 // defined as whichever port shows up in serial.getPorts). We've tested only
102 // the Atmega32u4 Breakout Board and Arduino Leonardo; note that both these
103 // boards are based on the Atmel ATmega32u4, rather than the more common
104 // Arduino '328p with either FTDI or '8/16u2 USB interfaces. TODO: test more
107 // 2. Your user has permission to read/write the port. For example, this might
108 // mean that your user is in the "tty" or "uucp" group on Ubuntu flavors of
109 // Linux, or else that the port's path (e.g., /dev/ttyACM0) has global
110 // read/write permissions.
112 // 3. You have uploaded a program to the board that does a byte-for-byte echo
113 // on the virtual serial port at 57600 bps. An example is at
114 // chrome/test/data/extensions/api_test/serial/api/serial_arduino_test.ino.
116 #define SIMULATE_SERIAL_PORTS (1)
117 IN_PROC_BROWSER_TEST_F(SerialApiTest, SerialFakeHardware) {
118 extensions::ResultCatcher catcher;
119 catcher.RestrictToBrowserContext(browser()->profile());
121 #if SIMULATE_SERIAL_PORTS
122 ASSERT_TRUE(extensions::ExtensionFunctionDispatcher::OverrideFunction(
123 "serial.getDevices", FakeSerialGetDevicesFunctionFactory));
124 ASSERT_TRUE(extensions::ExtensionFunctionDispatcher::OverrideFunction(
125 "serial.connect", FakeSerialConnectFunctionFactory));
128 ASSERT_TRUE(RunExtensionTest("serial/api")) << message_;
131 IN_PROC_BROWSER_TEST_F(SerialApiTest, SerialRealHardware) {
132 extensions::ResultCatcher catcher;
133 catcher.RestrictToBrowserContext(browser()->profile());
135 ASSERT_TRUE(RunExtensionTest("serial/real_hardware")) << message_;