Add ICU message format support
[chromium-blink-merge.git] / extensions / browser / api / serial / serial_apitest.cc
blobac22f661bfd3abf64adfe6ed6f7b40c3a24fa89b
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.
5 #include <string>
7 #include "chrome/browser/extensions/extension_apitest.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "device/serial/serial_device_enumerator.h"
10 #include "device/serial/serial_service_impl.h"
11 #include "device/serial/test_serial_io_handler.h"
12 #include "extensions/browser/api/serial/serial_api.h"
13 #include "extensions/browser/api/serial/serial_connection.h"
14 #include "extensions/browser/api/serial/serial_service_factory.h"
15 #include "extensions/browser/extension_function.h"
16 #include "extensions/common/api/serial.h"
17 #include "extensions/common/switches.h"
18 #include "extensions/test/result_catcher.h"
19 #include "testing/gmock/include/gmock/gmock.h"
21 using testing::_;
22 using testing::Return;
24 namespace extensions {
25 namespace {
27 class FakeSerialGetDevicesFunction : public AsyncExtensionFunction {
28 public:
29 bool RunAsync() override {
30 base::ListValue* devices = new base::ListValue();
31 base::DictionaryValue* device0 = new base::DictionaryValue();
32 device0->SetString("path", "/dev/fakeserial");
33 base::DictionaryValue* device1 = new base::DictionaryValue();
34 device1->SetString("path", "\\\\COM800\\");
35 devices->Append(device0);
36 devices->Append(device1);
37 SetResult(devices);
38 SendResponse(true);
39 return true;
42 protected:
43 ~FakeSerialGetDevicesFunction() override {}
46 class FakeSerialDeviceEnumerator : public device::SerialDeviceEnumerator {
47 public:
48 ~FakeSerialDeviceEnumerator() override {}
50 mojo::Array<device::serial::DeviceInfoPtr> GetDevices() override {
51 mojo::Array<device::serial::DeviceInfoPtr> devices;
52 device::serial::DeviceInfoPtr device0(device::serial::DeviceInfo::New());
53 device0->path = "/dev/fakeserialmojo";
54 device::serial::DeviceInfoPtr device1(device::serial::DeviceInfo::New());
55 device1->path = "\\\\COM800\\";
56 devices.push_back(device0.Pass());
57 devices.push_back(device1.Pass());
58 return devices.Pass();
62 class FakeEchoSerialIoHandler : public device::TestSerialIoHandler {
63 public:
64 FakeEchoSerialIoHandler() {
65 device_control_signals()->dcd = true;
66 device_control_signals()->cts = true;
67 device_control_signals()->ri = true;
68 device_control_signals()->dsr = true;
69 EXPECT_CALL(*this, SetControlSignals(_)).Times(1).WillOnce(Return(true));
72 static scoped_refptr<device::SerialIoHandler> Create() {
73 return new FakeEchoSerialIoHandler();
76 MOCK_METHOD1(SetControlSignals,
77 bool(const device::serial::HostControlSignals&));
79 protected:
80 ~FakeEchoSerialIoHandler() override {}
82 private:
83 DISALLOW_COPY_AND_ASSIGN(FakeEchoSerialIoHandler);
86 class FakeSerialConnectFunction : public api::SerialConnectFunction {
87 protected:
88 SerialConnection* CreateSerialConnection(
89 const std::string& port,
90 const std::string& owner_extension_id) const override {
91 scoped_refptr<FakeEchoSerialIoHandler> io_handler =
92 new FakeEchoSerialIoHandler;
93 SerialConnection* serial_connection =
94 new SerialConnection(port, owner_extension_id);
95 serial_connection->SetIoHandlerForTest(io_handler);
96 return serial_connection;
99 protected:
100 ~FakeSerialConnectFunction() override {}
103 class SerialApiTest : public ExtensionApiTest,
104 public testing::WithParamInterface<bool> {
105 public:
106 SerialApiTest() {}
108 void SetUpCommandLine(base::CommandLine* command_line) override {
109 ExtensionApiTest::SetUpCommandLine(command_line);
110 if (GetParam())
111 command_line->AppendSwitch(switches::kEnableMojoSerialService);
114 void TearDownOnMainThread() override {
115 SetSerialServiceFactoryForTest(nullptr);
116 ExtensionApiTest::TearDownOnMainThread();
119 protected:
120 base::Callback<void(mojo::InterfaceRequest<device::serial::SerialService>)>
121 serial_service_factory_;
124 ExtensionFunction* FakeSerialGetDevicesFunctionFactory() {
125 return new FakeSerialGetDevicesFunction();
128 ExtensionFunction* FakeSerialConnectFunctionFactory() {
129 return new FakeSerialConnectFunction();
132 void CreateTestSerialServiceOnFileThread(
133 mojo::InterfaceRequest<device::serial::SerialService> request) {
134 auto io_handler_factory = base::Bind(&FakeEchoSerialIoHandler::Create);
135 auto connection_factory = new device::SerialConnectionFactory(
136 io_handler_factory,
137 content::BrowserThread::GetMessageLoopProxyForThread(
138 content::BrowserThread::IO));
139 scoped_ptr<device::SerialDeviceEnumerator> device_enumerator(
140 new FakeSerialDeviceEnumerator);
141 new device::SerialServiceImpl(connection_factory, device_enumerator.Pass(),
142 request.Pass());
145 void CreateTestSerialService(
146 mojo::InterfaceRequest<device::serial::SerialService> request) {
147 content::BrowserThread::PostTask(
148 content::BrowserThread::FILE,
149 FROM_HERE,
150 base::Bind(&CreateTestSerialServiceOnFileThread, base::Passed(&request)));
153 } // namespace
155 // Disable SIMULATE_SERIAL_PORTS only if all the following are true:
157 // 1. You have an Arduino or compatible board attached to your machine and
158 // properly appearing as the first virtual serial port ("first" is very loosely
159 // defined as whichever port shows up in serial.getPorts). We've tested only
160 // the Atmega32u4 Breakout Board and Arduino Leonardo; note that both these
161 // boards are based on the Atmel ATmega32u4, rather than the more common
162 // Arduino '328p with either FTDI or '8/16u2 USB interfaces. TODO: test more
163 // widely.
165 // 2. Your user has permission to read/write the port. For example, this might
166 // mean that your user is in the "tty" or "uucp" group on Ubuntu flavors of
167 // Linux, or else that the port's path (e.g., /dev/ttyACM0) has global
168 // read/write permissions.
170 // 3. You have uploaded a program to the board that does a byte-for-byte echo
171 // on the virtual serial port at 57600 bps. An example is at
172 // chrome/test/data/extensions/api_test/serial/api/serial_arduino_test.ino.
174 #define SIMULATE_SERIAL_PORTS (1)
175 IN_PROC_BROWSER_TEST_P(SerialApiTest, SerialFakeHardware) {
176 ResultCatcher catcher;
177 catcher.RestrictToBrowserContext(browser()->profile());
179 #if SIMULATE_SERIAL_PORTS
180 if (GetParam()) {
181 serial_service_factory_ = base::Bind(&CreateTestSerialService);
182 SetSerialServiceFactoryForTest(&serial_service_factory_);
183 } else {
184 ASSERT_TRUE(ExtensionFunctionDispatcher::OverrideFunction(
185 "serial.getDevices", FakeSerialGetDevicesFunctionFactory));
186 ASSERT_TRUE(ExtensionFunctionDispatcher::OverrideFunction(
187 "serial.connect", FakeSerialConnectFunctionFactory));
189 #endif
191 ASSERT_TRUE(RunExtensionTest("serial/api")) << message_;
194 IN_PROC_BROWSER_TEST_P(SerialApiTest, SerialRealHardware) {
195 ResultCatcher catcher;
196 catcher.RestrictToBrowserContext(browser()->profile());
198 ASSERT_TRUE(RunExtensionTest("serial/real_hardware")) << message_;
201 INSTANTIATE_TEST_CASE_P(SerialApiTest, SerialApiTest, testing::Bool());
203 } // namespace extensions