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

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: fix windows build Created 6 years, 1 month 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
« no previous file with comments | « extensions/BUILD.gn ('k') | extensions/browser/mojo/service_registration_manager.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/test_service_registration_manager.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 test_service_registration_manager_.reset(
113 new TestServiceRegistrationManager);
114 }
115
116 protected:
117 scoped_ptr<TestServiceRegistrationManager> test_service_registration_manager_;
118 };
88 119
89 ExtensionFunction* FakeSerialGetDevicesFunctionFactory() { 120 ExtensionFunction* FakeSerialGetDevicesFunctionFactory() {
90 return new extensions::FakeSerialGetDevicesFunction(); 121 return new FakeSerialGetDevicesFunction();
91 } 122 }
92 123
93 ExtensionFunction* FakeSerialConnectFunctionFactory() { 124 ExtensionFunction* FakeSerialConnectFunctionFactory() {
94 return new extensions::FakeSerialConnectFunction(); 125 return new FakeSerialConnectFunction();
95 } 126 }
96 127
128 void CreateTestSerialServiceOnFileThread(
129 mojo::InterfaceRequest<device::serial::SerialService> request) {
130 auto io_handler_factory = base::Bind(&FakeEchoSerialIoHandler::Create);
131 auto connection_factory = new device::SerialConnectionFactory(
132 io_handler_factory,
133 content::BrowserThread::GetMessageLoopProxyForThread(
134 content::BrowserThread::IO));
135 scoped_ptr<device::SerialDeviceEnumerator> device_enumerator(
136 new FakeSerialDeviceEnumerator);
137 mojo::BindToRequest(new device::SerialServiceImpl(connection_factory,
138 device_enumerator.Pass()),
139 &request);
140 }
141
142 void CreateTestSerialService(
143 mojo::InterfaceRequest<device::serial::SerialService> request) {
144 content::BrowserThread::PostTask(
145 content::BrowserThread::FILE,
146 FROM_HERE,
147 base::Bind(&CreateTestSerialServiceOnFileThread, base::Passed(&request)));
148 }
149
150 } // namespace
151
97 // Disable SIMULATE_SERIAL_PORTS only if all the following are true: 152 // Disable SIMULATE_SERIAL_PORTS only if all the following are true:
98 // 153 //
99 // 1. You have an Arduino or compatible board attached to your machine and 154 // 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 155 // 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 156 // 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 157 // the Atmega32u4 Breakout Board and Arduino Leonardo; note that both these
103 // boards are based on the Atmel ATmega32u4, rather than the more common 158 // 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 159 // Arduino '328p with either FTDI or '8/16u2 USB interfaces. TODO: test more
105 // widely. 160 // widely.
106 // 161 //
107 // 2. Your user has permission to read/write the port. For example, this might 162 // 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 163 // 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 164 // Linux, or else that the port's path (e.g., /dev/ttyACM0) has global
110 // read/write permissions. 165 // read/write permissions.
111 // 166 //
112 // 3. You have uploaded a program to the board that does a byte-for-byte echo 167 // 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 168 // 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. 169 // chrome/test/data/extensions/api_test/serial/api/serial_arduino_test.ino.
115 // 170 //
116 #define SIMULATE_SERIAL_PORTS (1) 171 #define SIMULATE_SERIAL_PORTS (1)
117 IN_PROC_BROWSER_TEST_F(SerialApiTest, SerialFakeHardware) { 172 IN_PROC_BROWSER_TEST_P(SerialApiTest, SerialFakeHardware) {
118 extensions::ResultCatcher catcher; 173 ResultCatcher catcher;
119 catcher.RestrictToBrowserContext(browser()->profile()); 174 catcher.RestrictToBrowserContext(browser()->profile());
120 175
121 #if SIMULATE_SERIAL_PORTS 176 #if SIMULATE_SERIAL_PORTS
122 ASSERT_TRUE(extensions::ExtensionFunctionDispatcher::OverrideFunction( 177 if (GetParam()) {
123 "serial.getDevices", FakeSerialGetDevicesFunctionFactory)); 178 test_service_registration_manager_->OverrideServiceFactoryForTest(
124 ASSERT_TRUE(extensions::ExtensionFunctionDispatcher::OverrideFunction( 179 base::Bind(&CreateTestSerialService));
125 "serial.connect", FakeSerialConnectFunctionFactory)); 180 } else {
181 ASSERT_TRUE(ExtensionFunctionDispatcher::OverrideFunction(
182 "serial.getDevices", FakeSerialGetDevicesFunctionFactory));
183 ASSERT_TRUE(ExtensionFunctionDispatcher::OverrideFunction(
184 "serial.connect", FakeSerialConnectFunctionFactory));
185 }
126 #endif 186 #endif
127 187
128 ASSERT_TRUE(RunExtensionTest("serial/api")) << message_; 188 ASSERT_TRUE(RunExtensionTest("serial/api")) << message_;
129 } 189 }
130 190
131 IN_PROC_BROWSER_TEST_F(SerialApiTest, SerialRealHardware) { 191 IN_PROC_BROWSER_TEST_P(SerialApiTest, SerialRealHardware) {
132 extensions::ResultCatcher catcher; 192 ResultCatcher catcher;
133 catcher.RestrictToBrowserContext(browser()->profile()); 193 catcher.RestrictToBrowserContext(browser()->profile());
134 194
135 ASSERT_TRUE(RunExtensionTest("serial/real_hardware")) << message_; 195 ASSERT_TRUE(RunExtensionTest("serial/real_hardware")) << message_;
136 } 196 }
197
198 INSTANTIATE_TEST_CASE_P(SerialApiTest, SerialApiTest, testing::Bool());
199
200 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/BUILD.gn ('k') | extensions/browser/mojo/service_registration_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698