| OLD | NEW |
| 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 <memory> | 5 #include <memory> |
| 6 #include <string> | 6 #include <string> |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "chrome/browser/extensions/extension_apitest.h" | 10 #include "chrome/browser/extensions/extension_apitest.h" |
| 11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 12 #include "device/serial/serial_device_enumerator.h" | 12 #include "device/serial/serial_device_enumerator.h" |
| 13 #include "device/serial/serial_service_impl.h" | |
| 14 #include "device/serial/test_serial_io_handler.h" | 13 #include "device/serial/test_serial_io_handler.h" |
| 15 #include "extensions/browser/api/serial/serial_api.h" | 14 #include "extensions/browser/api/serial/serial_api.h" |
| 16 #include "extensions/browser/api/serial/serial_connection.h" | 15 #include "extensions/browser/api/serial/serial_connection.h" |
| 17 #include "extensions/browser/api/serial/serial_service_factory.h" | |
| 18 #include "extensions/browser/extension_function.h" | 16 #include "extensions/browser/extension_function.h" |
| 19 #include "extensions/common/api/serial.h" | 17 #include "extensions/common/api/serial.h" |
| 20 #include "extensions/common/switches.h" | 18 #include "extensions/common/switches.h" |
| 21 #include "extensions/test/result_catcher.h" | 19 #include "extensions/test/result_catcher.h" |
| 22 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 23 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
| 24 | 21 |
| 25 using testing::_; | 22 using testing::_; |
| 26 using testing::Return; | 23 using testing::Return; |
| 27 | 24 |
| 28 namespace extensions { | 25 namespace extensions { |
| 29 namespace { | 26 namespace { |
| 30 | 27 |
| 31 class FakeSerialGetDevicesFunction : public AsyncExtensionFunction { | 28 class FakeSerialGetDevicesFunction : public AsyncExtensionFunction { |
| 32 public: | 29 public: |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 SerialConnection* serial_connection = | 94 SerialConnection* serial_connection = |
| 98 new SerialConnection(port, owner_extension_id); | 95 new SerialConnection(port, owner_extension_id); |
| 99 serial_connection->SetIoHandlerForTest(io_handler); | 96 serial_connection->SetIoHandlerForTest(io_handler); |
| 100 return serial_connection; | 97 return serial_connection; |
| 101 } | 98 } |
| 102 | 99 |
| 103 protected: | 100 protected: |
| 104 ~FakeSerialConnectFunction() override {} | 101 ~FakeSerialConnectFunction() override {} |
| 105 }; | 102 }; |
| 106 | 103 |
| 107 class SerialApiTest : public ExtensionApiTest, | 104 class SerialApiTest : public ExtensionApiTest { |
| 108 public testing::WithParamInterface<bool> { | |
| 109 public: | 105 public: |
| 110 SerialApiTest() {} | 106 SerialApiTest() {} |
| 111 | 107 |
| 112 void SetUpCommandLine(base::CommandLine* command_line) override { | 108 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 113 ExtensionApiTest::SetUpCommandLine(command_line); | 109 ExtensionApiTest::SetUpCommandLine(command_line); |
| 114 if (GetParam()) | |
| 115 command_line->AppendSwitch(switches::kEnableMojoSerialService); | |
| 116 } | 110 } |
| 117 | 111 |
| 118 void TearDownOnMainThread() override { | 112 void TearDownOnMainThread() override { |
| 119 SetSerialServiceFactoryForTest(nullptr); | |
| 120 ExtensionApiTest::TearDownOnMainThread(); | 113 ExtensionApiTest::TearDownOnMainThread(); |
| 121 } | 114 } |
| 122 | |
| 123 protected: | |
| 124 base::Callback<void(mojo::InterfaceRequest<device::serial::SerialService>)> | |
| 125 serial_service_factory_; | |
| 126 }; | 115 }; |
| 127 | 116 |
| 128 ExtensionFunction* FakeSerialGetDevicesFunctionFactory() { | 117 ExtensionFunction* FakeSerialGetDevicesFunctionFactory() { |
| 129 return new FakeSerialGetDevicesFunction(); | 118 return new FakeSerialGetDevicesFunction(); |
| 130 } | 119 } |
| 131 | 120 |
| 132 ExtensionFunction* FakeSerialConnectFunctionFactory() { | 121 ExtensionFunction* FakeSerialConnectFunctionFactory() { |
| 133 return new FakeSerialConnectFunction(); | 122 return new FakeSerialConnectFunction(); |
| 134 } | 123 } |
| 135 | 124 |
| 136 void CreateTestSerialServiceOnFileThread( | |
| 137 mojo::InterfaceRequest<device::serial::SerialService> request) { | |
| 138 auto io_handler_factory = base::Bind(&FakeEchoSerialIoHandler::Create); | |
| 139 auto* connection_factory = new device::SerialConnectionFactory( | |
| 140 io_handler_factory, content::BrowserThread::GetTaskRunnerForThread( | |
| 141 content::BrowserThread::IO)); | |
| 142 std::unique_ptr<device::SerialDeviceEnumerator> device_enumerator( | |
| 143 new FakeSerialDeviceEnumerator); | |
| 144 mojo::MakeStrongBinding(base::MakeUnique<device::SerialServiceImpl>( | |
| 145 connection_factory, std::move(device_enumerator)), | |
| 146 std::move(request)); | |
| 147 } | |
| 148 | |
| 149 void CreateTestSerialService( | |
| 150 mojo::InterfaceRequest<device::serial::SerialService> request) { | |
| 151 content::BrowserThread::PostTask( | |
| 152 content::BrowserThread::FILE, | |
| 153 FROM_HERE, | |
| 154 base::Bind(&CreateTestSerialServiceOnFileThread, base::Passed(&request))); | |
| 155 } | |
| 156 | |
| 157 } // namespace | 125 } // namespace |
| 158 | 126 |
| 159 // Disable SIMULATE_SERIAL_PORTS only if all the following are true: | 127 // Disable SIMULATE_SERIAL_PORTS only if all the following are true: |
| 160 // | 128 // |
| 161 // 1. You have an Arduino or compatible board attached to your machine and | 129 // 1. You have an Arduino or compatible board attached to your machine and |
| 162 // properly appearing as the first virtual serial port ("first" is very loosely | 130 // properly appearing as the first virtual serial port ("first" is very loosely |
| 163 // defined as whichever port shows up in serial.getPorts). We've tested only | 131 // defined as whichever port shows up in serial.getPorts). We've tested only |
| 164 // the Atmega32u4 Breakout Board and Arduino Leonardo; note that both these | 132 // the Atmega32u4 Breakout Board and Arduino Leonardo; note that both these |
| 165 // boards are based on the Atmel ATmega32u4, rather than the more common | 133 // boards are based on the Atmel ATmega32u4, rather than the more common |
| 166 // Arduino '328p with either FTDI or '8/16u2 USB interfaces. TODO: test more | 134 // Arduino '328p with either FTDI or '8/16u2 USB interfaces. TODO: test more |
| 167 // widely. | 135 // widely. |
| 168 // | 136 // |
| 169 // 2. Your user has permission to read/write the port. For example, this might | 137 // 2. Your user has permission to read/write the port. For example, this might |
| 170 // mean that your user is in the "tty" or "uucp" group on Ubuntu flavors of | 138 // mean that your user is in the "tty" or "uucp" group on Ubuntu flavors of |
| 171 // Linux, or else that the port's path (e.g., /dev/ttyACM0) has global | 139 // Linux, or else that the port's path (e.g., /dev/ttyACM0) has global |
| 172 // read/write permissions. | 140 // read/write permissions. |
| 173 // | 141 // |
| 174 // 3. You have uploaded a program to the board that does a byte-for-byte echo | 142 // 3. You have uploaded a program to the board that does a byte-for-byte echo |
| 175 // on the virtual serial port at 57600 bps. An example is at | 143 // on the virtual serial port at 57600 bps. An example is at |
| 176 // chrome/test/data/extensions/api_test/serial/api/serial_arduino_test.ino. | 144 // chrome/test/data/extensions/api_test/serial/api/serial_arduino_test.ino. |
| 177 // | 145 // |
| 178 #define SIMULATE_SERIAL_PORTS (1) | 146 #define SIMULATE_SERIAL_PORTS (1) |
| 179 IN_PROC_BROWSER_TEST_P(SerialApiTest, SerialFakeHardware) { | 147 IN_PROC_BROWSER_TEST_F(SerialApiTest, SerialFakeHardware) { |
| 180 ResultCatcher catcher; | 148 ResultCatcher catcher; |
| 181 catcher.RestrictToBrowserContext(browser()->profile()); | 149 catcher.RestrictToBrowserContext(browser()->profile()); |
| 182 | 150 |
| 183 #if SIMULATE_SERIAL_PORTS | 151 #if SIMULATE_SERIAL_PORTS |
| 184 if (GetParam()) { | 152 ASSERT_TRUE(ExtensionFunctionDispatcher::OverrideFunction( |
| 185 serial_service_factory_ = base::Bind(&CreateTestSerialService); | 153 "serial.getDevices", FakeSerialGetDevicesFunctionFactory)); |
| 186 SetSerialServiceFactoryForTest(&serial_service_factory_); | 154 ASSERT_TRUE(ExtensionFunctionDispatcher::OverrideFunction( |
| 187 } else { | 155 "serial.connect", FakeSerialConnectFunctionFactory)); |
| 188 ASSERT_TRUE(ExtensionFunctionDispatcher::OverrideFunction( | |
| 189 "serial.getDevices", FakeSerialGetDevicesFunctionFactory)); | |
| 190 ASSERT_TRUE(ExtensionFunctionDispatcher::OverrideFunction( | |
| 191 "serial.connect", FakeSerialConnectFunctionFactory)); | |
| 192 } | |
| 193 #endif | 156 #endif |
| 194 | 157 |
| 195 ASSERT_TRUE(RunExtensionTest("serial/api")) << message_; | 158 ASSERT_TRUE(RunExtensionTest("serial/api")) << message_; |
| 196 } | 159 } |
| 197 | 160 |
| 198 IN_PROC_BROWSER_TEST_P(SerialApiTest, SerialRealHardware) { | 161 IN_PROC_BROWSER_TEST_F(SerialApiTest, SerialRealHardware) { |
| 199 ResultCatcher catcher; | 162 ResultCatcher catcher; |
| 200 catcher.RestrictToBrowserContext(browser()->profile()); | 163 catcher.RestrictToBrowserContext(browser()->profile()); |
| 201 | 164 |
| 202 ASSERT_TRUE(RunExtensionTest("serial/real_hardware")) << message_; | 165 ASSERT_TRUE(RunExtensionTest("serial/real_hardware")) << message_; |
| 203 } | 166 } |
| 204 | 167 |
| 205 INSTANTIATE_TEST_CASE_P(SerialApiTest, SerialApiTest, testing::Bool()); | |
| 206 | |
| 207 } // namespace extensions | 168 } // namespace extensions |
| OLD | NEW |