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 "testing/gmock/include/gmock/gmock.h"
16 using testing::Return;
20 class SerialApiTest : public ExtensionApiTest {
27 namespace extensions {
29 class FakeSerialGetDevicesFunction : public AsyncExtensionFunction {
31 virtual bool RunAsync() OVERRIDE {
32 base::ListValue* devices = new base::ListValue();
33 base::DictionaryValue* device0 = new base::DictionaryValue();
34 device0->SetString("path", "/dev/fakeserial");
35 base::DictionaryValue* device1 = new base::DictionaryValue();
36 device1->SetString("path", "\\\\COM800\\");
37 devices->Append(device0);
38 devices->Append(device1);
45 virtual ~FakeSerialGetDevicesFunction() {}
48 class FakeEchoSerialIoHandler : public device::TestSerialIoHandler {
50 explicit FakeEchoSerialIoHandler() {
51 device_control_signals()->dcd = true;
52 device_control_signals()->cts = true;
53 device_control_signals()->ri = true;
54 device_control_signals()->dsr = true;
57 MOCK_METHOD1(SetControlSignals,
58 bool(const device::serial::HostControlSignals&));
61 virtual ~FakeEchoSerialIoHandler() {}
64 DISALLOW_COPY_AND_ASSIGN(FakeEchoSerialIoHandler);
67 class FakeSerialConnectFunction : public core_api::SerialConnectFunction {
69 virtual SerialConnection* CreateSerialConnection(
70 const std::string& port,
71 const std::string& owner_extension_id) const OVERRIDE {
72 scoped_refptr<FakeEchoSerialIoHandler> io_handler =
73 new FakeEchoSerialIoHandler;
74 EXPECT_CALL(*io_handler, SetControlSignals(_)).Times(1).WillOnce(
76 SerialConnection* serial_connection =
77 new SerialConnection(port, owner_extension_id);
78 serial_connection->SetIoHandlerForTest(io_handler);
79 return serial_connection;
83 virtual ~FakeSerialConnectFunction() {}
86 } // namespace extensions
88 ExtensionFunction* FakeSerialGetDevicesFunctionFactory() {
89 return new extensions::FakeSerialGetDevicesFunction();
92 ExtensionFunction* FakeSerialConnectFunctionFactory() {
93 return new extensions::FakeSerialConnectFunction();
96 // Disable SIMULATE_SERIAL_PORTS only if all the following are true:
98 // 1. You have an Arduino or compatible board attached to your machine and
99 // properly appearing as the first virtual serial port ("first" is very loosely
100 // defined as whichever port shows up in serial.getPorts). We've tested only
101 // the Atmega32u4 Breakout Board and Arduino Leonardo; note that both these
102 // boards are based on the Atmel ATmega32u4, rather than the more common
103 // Arduino '328p with either FTDI or '8/16u2 USB interfaces. TODO: test more
106 // 2. Your user has permission to read/write the port. For example, this might
107 // mean that your user is in the "tty" or "uucp" group on Ubuntu flavors of
108 // Linux, or else that the port's path (e.g., /dev/ttyACM0) has global
109 // read/write permissions.
111 // 3. You have uploaded a program to the board that does a byte-for-byte echo
112 // on the virtual serial port at 57600 bps. An example is at
113 // chrome/test/data/extensions/api_test/serial/api/serial_arduino_test.ino.
115 #define SIMULATE_SERIAL_PORTS (1)
116 IN_PROC_BROWSER_TEST_F(SerialApiTest, SerialFakeHardware) {
117 ResultCatcher catcher;
118 catcher.RestrictToProfile(browser()->profile());
120 #if SIMULATE_SERIAL_PORTS
121 ASSERT_TRUE(extensions::ExtensionFunctionDispatcher::OverrideFunction(
122 "serial.getDevices", FakeSerialGetDevicesFunctionFactory));
123 ASSERT_TRUE(extensions::ExtensionFunctionDispatcher::OverrideFunction(
124 "serial.connect", FakeSerialConnectFunctionFactory));
127 ASSERT_TRUE(RunExtensionTest("serial/api")) << message_;
130 IN_PROC_BROWSER_TEST_F(SerialApiTest, SerialRealHardware) {
131 ResultCatcher catcher;
132 catcher.RestrictToProfile(browser()->profile());
134 ASSERT_TRUE(RunExtensionTest("serial/real_hardware")) << message_;