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

Side by Side Diff: extensions/browser/api/serial/serial_apitest.cc

Issue 652313002: Enable the mojo-based serial API in the renderer behind a flag. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@mojo-js-natives-registration
Patch Set: rebase Created 6 years, 2 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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 #include <string> 5 #include <string>
6 6
7 #include "chrome/browser/extensions/extension_apitest.h" 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"
8 #include "device/serial/test_serial_io_handler.h" 11 #include "device/serial/test_serial_io_handler.h"
9 #include "extensions/browser/api/serial/serial_api.h" 12 #include "extensions/browser/api/serial/serial_api.h"
10 #include "extensions/browser/api/serial/serial_connection.h" 13 #include "extensions/browser/api/serial/serial_connection.h"
11 #include "extensions/browser/extension_function.h" 14 #include "extensions/browser/extension_function.h"
12 #include "extensions/common/api/serial.h" 15 #include "extensions/common/api/serial.h"
16 #include "extensions/common/switches.h"
13 #include "extensions/test/result_catcher.h" 17 #include "extensions/test/result_catcher.h"
18 #include "extensions/test/service_registration_manager_test_api.h"
14 #include "testing/gmock/include/gmock/gmock.h" 19 #include "testing/gmock/include/gmock/gmock.h"
15 20
16 using testing::_; 21 using testing::_;
17 using testing::Return; 22 using testing::Return;
18 23
24 namespace extensions {
19 namespace { 25 namespace {
20 26
21 class SerialApiTest : public ExtensionApiTest {
22 public:
23 SerialApiTest() {}
24 };
25
26 } // namespace
27
28 namespace extensions {
29
30 class FakeSerialGetDevicesFunction : public AsyncExtensionFunction { 27 class FakeSerialGetDevicesFunction : public AsyncExtensionFunction {
31 public: 28 public:
32 bool RunAsync() override { 29 bool RunAsync() override {
33 base::ListValue* devices = new base::ListValue(); 30 base::ListValue* devices = new base::ListValue();
34 base::DictionaryValue* device0 = new base::DictionaryValue(); 31 base::DictionaryValue* device0 = new base::DictionaryValue();
35 device0->SetString("path", "/dev/fakeserial"); 32 device0->SetString("path", "/dev/fakeserial");
36 base::DictionaryValue* device1 = new base::DictionaryValue(); 33 base::DictionaryValue* device1 = new base::DictionaryValue();
37 device1->SetString("path", "\\\\COM800\\"); 34 device1->SetString("path", "\\\\COM800\\");
38 devices->Append(device0); 35 devices->Append(device0);
39 devices->Append(device1); 36 devices->Append(device1);
40 SetResult(devices); 37 SetResult(devices);
41 SendResponse(true); 38 SendResponse(true);
42 return true; 39 return true;
43 } 40 }
44 41
45 protected: 42 protected:
46 ~FakeSerialGetDevicesFunction() override {} 43 ~FakeSerialGetDevicesFunction() {}
44 };
45
46 class FakeSerialDeviceEnumerator : public device::SerialDeviceEnumerator {
47 public:
48 ~FakeSerialDeviceEnumerator() override {}
49
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();
59 }
47 }; 60 };
48 61
49 class FakeEchoSerialIoHandler : public device::TestSerialIoHandler { 62 class FakeEchoSerialIoHandler : public device::TestSerialIoHandler {
50 public: 63 public:
51 explicit FakeEchoSerialIoHandler() { 64 FakeEchoSerialIoHandler() {
52 device_control_signals()->dcd = true; 65 device_control_signals()->dcd = true;
53 device_control_signals()->cts = true; 66 device_control_signals()->cts = true;
54 device_control_signals()->ri = true; 67 device_control_signals()->ri = true;
55 device_control_signals()->dsr = true; 68 device_control_signals()->dsr = true;
69 EXPECT_CALL(*this, SetControlSignals(_)).Times(1).WillOnce(Return(true));
70 }
71
72 static scoped_refptr<device::SerialIoHandler> Create() {
73 return new FakeEchoSerialIoHandler();
56 } 74 }
57 75
58 MOCK_METHOD1(SetControlSignals, 76 MOCK_METHOD1(SetControlSignals,
59 bool(const device::serial::HostControlSignals&)); 77 bool(const device::serial::HostControlSignals&));
60 78
61 protected: 79 protected:
62 virtual ~FakeEchoSerialIoHandler() {} 80 ~FakeEchoSerialIoHandler() override {}
63 81
64 private: 82 private:
65 DISALLOW_COPY_AND_ASSIGN(FakeEchoSerialIoHandler); 83 DISALLOW_COPY_AND_ASSIGN(FakeEchoSerialIoHandler);
66 }; 84 };
67 85
68 class FakeSerialConnectFunction : public core_api::SerialConnectFunction { 86 class FakeSerialConnectFunction : public core_api::SerialConnectFunction {
69 protected: 87 protected:
70 SerialConnection* CreateSerialConnection( 88 SerialConnection* CreateSerialConnection(
71 const std::string& port, 89 const std::string& port,
72 const std::string& owner_extension_id) const override { 90 const std::string& owner_extension_id) const override {
73 scoped_refptr<FakeEchoSerialIoHandler> io_handler = 91 scoped_refptr<FakeEchoSerialIoHandler> io_handler =
74 new FakeEchoSerialIoHandler; 92 new FakeEchoSerialIoHandler;
75 EXPECT_CALL(*io_handler.get(), SetControlSignals(_)).Times(1).WillOnce(
76 Return(true));
77 SerialConnection* serial_connection = 93 SerialConnection* serial_connection =
78 new SerialConnection(port, owner_extension_id); 94 new SerialConnection(port, owner_extension_id);
79 serial_connection->SetIoHandlerForTest(io_handler); 95 serial_connection->SetIoHandlerForTest(io_handler);
80 return serial_connection; 96 return serial_connection;
81 } 97 }
82 98
83 protected: 99 protected:
84 ~FakeSerialConnectFunction() override {} 100 ~FakeSerialConnectFunction() {}
85 }; 101 };
86 102
87 } // namespace extensions 103 class SerialApiTest : public ExtensionApiTest,
104 public testing::WithParamInterface<bool> {
105 public:
106 SerialApiTest() {}
107
108 void SetUpCommandLine(base::CommandLine* command_line) override {
109 ExtensionApiTest::SetUpCommandLine(command_line);
110 if (GetParam())
111 command_line->AppendSwitch(switches::kEnableMojoSerialService);
112 service_registration_manager_test_api_.reset(
113 new ServiceRegistrationManagerTestApi);
114 }
115
116 protected:
117 scoped_ptr<ServiceRegistrationManagerTestApi>
118 service_registration_manager_test_api_;
119 };
88 120
89 ExtensionFunction* FakeSerialGetDevicesFunctionFactory() { 121 ExtensionFunction* FakeSerialGetDevicesFunctionFactory() {
90 return new extensions::FakeSerialGetDevicesFunction(); 122 return new extensions::FakeSerialGetDevicesFunction();
91 } 123 }
92 124
93 ExtensionFunction* FakeSerialConnectFunctionFactory() { 125 ExtensionFunction* FakeSerialConnectFunctionFactory() {
94 return new extensions::FakeSerialConnectFunction(); 126 return new extensions::FakeSerialConnectFunction();
95 } 127 }
96 128
129 void CreateTestSerialServiceOnFileThread(
130 mojo::InterfaceRequest<device::serial::SerialService> request) {
131 auto io_handler_factory = base::Bind(&FakeEchoSerialIoHandler::Create);
132 auto connection_factory = new device::SerialConnectionFactory(
133 io_handler_factory,
134 content::BrowserThread::GetMessageLoopProxyForThread(
135 content::BrowserThread::IO));
136 scoped_ptr<device::SerialDeviceEnumerator> device_enumerator(
137 new FakeSerialDeviceEnumerator);
138 mojo::BindToRequest(new device::SerialServiceImpl(connection_factory,
139 device_enumerator.Pass()),
140 &request);
141 }
142
143 void CreateTestSerialService(
144 mojo::InterfaceRequest<device::serial::SerialService> request) {
145 content::BrowserThread::PostTask(
146 content::BrowserThread::FILE,
147 FROM_HERE,
148 base::Bind(&CreateTestSerialServiceOnFileThread, base::Passed(&request)));
149 }
150
151 } // namespace
152
97 // Disable SIMULATE_SERIAL_PORTS only if all the following are true: 153 // Disable SIMULATE_SERIAL_PORTS only if all the following are true:
98 // 154 //
99 // 1. You have an Arduino or compatible board attached to your machine and 155 // 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 156 // 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 157 // 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 158 // the Atmega32u4 Breakout Board and Arduino Leonardo; note that both these
103 // boards are based on the Atmel ATmega32u4, rather than the more common 159 // 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 160 // Arduino '328p with either FTDI or '8/16u2 USB interfaces. TODO: test more
105 // widely. 161 // widely.
106 // 162 //
107 // 2. Your user has permission to read/write the port. For example, this might 163 // 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 164 // 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 165 // Linux, or else that the port's path (e.g., /dev/ttyACM0) has global
110 // read/write permissions. 166 // read/write permissions.
111 // 167 //
112 // 3. You have uploaded a program to the board that does a byte-for-byte echo 168 // 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 169 // 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. 170 // chrome/test/data/extensions/api_test/serial/api/serial_arduino_test.ino.
115 // 171 //
116 #define SIMULATE_SERIAL_PORTS (1) 172 #define SIMULATE_SERIAL_PORTS (1)
117 IN_PROC_BROWSER_TEST_F(SerialApiTest, SerialFakeHardware) { 173 IN_PROC_BROWSER_TEST_P(SerialApiTest, SerialFakeHardware) {
118 extensions::ResultCatcher catcher; 174 ResultCatcher catcher;
119 catcher.RestrictToBrowserContext(browser()->profile()); 175 catcher.RestrictToBrowserContext(browser()->profile());
120 176
121 #if SIMULATE_SERIAL_PORTS 177 #if SIMULATE_SERIAL_PORTS
122 ASSERT_TRUE(extensions::ExtensionFunctionDispatcher::OverrideFunction( 178 if (GetParam()) {
123 "serial.getDevices", FakeSerialGetDevicesFunctionFactory)); 179 service_registration_manager_test_api_->OverrideServiceFactoryForTest(
124 ASSERT_TRUE(extensions::ExtensionFunctionDispatcher::OverrideFunction( 180 base::Bind(&CreateTestSerialService));
125 "serial.connect", FakeSerialConnectFunctionFactory)); 181 } else {
182 ASSERT_TRUE(extensions::ExtensionFunctionDispatcher::OverrideFunction(
183 "serial.getDevices", FakeSerialGetDevicesFunctionFactory));
184 ASSERT_TRUE(extensions::ExtensionFunctionDispatcher::OverrideFunction(
185 "serial.connect", FakeSerialConnectFunctionFactory));
186 }
126 #endif 187 #endif
127 188
128 ASSERT_TRUE(RunExtensionTest("serial/api")) << message_; 189 ASSERT_TRUE(RunExtensionTest("serial/api")) << message_;
129 } 190 }
130 191
131 IN_PROC_BROWSER_TEST_F(SerialApiTest, SerialRealHardware) { 192 IN_PROC_BROWSER_TEST_P(SerialApiTest, SerialRealHardware) {
132 extensions::ResultCatcher catcher; 193 ResultCatcher catcher;
133 catcher.RestrictToBrowserContext(browser()->profile()); 194 catcher.RestrictToBrowserContext(browser()->profile());
134 195
135 ASSERT_TRUE(RunExtensionTest("serial/real_hardware")) << message_; 196 ASSERT_TRUE(RunExtensionTest("serial/real_hardware")) << message_;
136 } 197 }
198
199 INSTANTIATE_TEST_CASE_P(SerialApiTest, SerialApiTest, testing::Bool());
200
201 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698