Chromium Code Reviews| 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 <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" |
| 15 #include "extensions/browser/service_registration_manager.h" | |
| 12 #include "extensions/common/api/serial.h" | 16 #include "extensions/common/api/serial.h" |
| 17 #include "extensions/common/switches.h" | |
| 13 #include "extensions/test/result_catcher.h" | 18 #include "extensions/test/result_catcher.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 virtual bool RunAsync() override { | 29 virtual 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 virtual ~FakeSerialGetDevicesFunction() {} | 43 virtual ~FakeSerialGetDevicesFunction() {} |
| 47 }; | 44 }; |
| 48 | 45 |
| 46 class FakeSerialDeviceEnumerator : public device::SerialDeviceEnumerator { | |
| 47 public: | |
| 48 virtual ~FakeSerialDeviceEnumerator() {} | |
| 49 | |
| 50 virtual 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 } | |
| 60 }; | |
| 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() { | |
|
raymes
2014/10/15 21:07:09
Where is this called from?
Sam McNally
2014/10/20 07:21:07
CreateTestSerialServiceOnFileThread.
| |
| 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 virtual ~FakeEchoSerialIoHandler() {} |
| 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 virtual SerialConnection* CreateSerialConnection( | 88 virtual 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)); | |
|
raymes
2014/10/15 21:07:09
Why did this line get moved?
Sam McNally
2014/10/20 07:21:06
So FakeEchoSerialIoHandlers created elsewhere have
| |
| 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 virtual ~FakeSerialConnectFunction() {} | 100 virtual ~FakeSerialConnectFunction() {} |
| 85 }; | 101 }; |
| 86 | 102 |
| 87 } // namespace extensions | 103 class SerialApiTest : public ExtensionApiTest, |
| 104 public testing::WithParamInterface<bool> { | |
| 105 public: | |
| 106 SerialApiTest() {} | |
| 107 virtual void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 108 ExtensionApiTest::SetUpCommandLine(command_line); | |
| 109 if (GetParam()) | |
|
raymes
2014/10/15 21:07:09
I don't really understand these GetParam related c
Sam McNally
2014/10/20 07:21:07
Yes. INSTANTIATE_TEST_CASE_P(SerialApiTest, Serial
raymes
2014/10/24 00:09:30
nit: Cool. It's probably worth making a comment ab
| |
| 110 command_line->AppendSwitch(switches::kEnableMojoSerialService); | |
| 111 } | |
| 112 | |
| 113 virtual void TearDown() override { | |
| 114 ServiceRegistrationManager::GetSharedInstance()->ClearOverridesForTest(); | |
| 115 ExtensionApiTest::TearDown(); | |
| 116 } | |
| 117 }; | |
| 88 | 118 |
| 89 ExtensionFunction* FakeSerialGetDevicesFunctionFactory() { | 119 ExtensionFunction* FakeSerialGetDevicesFunctionFactory() { |
| 90 return new extensions::FakeSerialGetDevicesFunction(); | 120 return new extensions::FakeSerialGetDevicesFunction(); |
| 91 } | 121 } |
| 92 | 122 |
| 93 ExtensionFunction* FakeSerialConnectFunctionFactory() { | 123 ExtensionFunction* FakeSerialConnectFunctionFactory() { |
| 94 return new extensions::FakeSerialConnectFunction(); | 124 return new extensions::FakeSerialConnectFunction(); |
| 95 } | 125 } |
| 96 | 126 |
| 127 void CreateTestSerialServiceOnFileThread( | |
| 128 mojo::InterfaceRequest<device::serial::SerialService> request) { | |
| 129 mojo::BindToRequest( | |
| 130 new device::SerialServiceImpl( | |
| 131 new device::SerialConnectionFactory( | |
| 132 base::Bind(&FakeEchoSerialIoHandler::Create), | |
| 133 content::BrowserThread::GetMessageLoopProxyForThread( | |
| 134 content::BrowserThread::IO)), | |
| 135 scoped_ptr<device::SerialDeviceEnumerator>( | |
| 136 new FakeSerialDeviceEnumerator)), | |
| 137 &request); | |
|
raymes
2014/10/15 21:07:09
Could you split this up into a few statements? :)
Sam McNally
2014/10/20 07:21:07
Done.
| |
| 138 } | |
| 139 | |
| 140 void CreateTestSerialService( | |
| 141 mojo::InterfaceRequest<device::serial::SerialService> request) { | |
| 142 content::BrowserThread::PostTask( | |
| 143 content::BrowserThread::FILE, | |
| 144 FROM_HERE, | |
| 145 base::Bind(&CreateTestSerialServiceOnFileThread, base::Passed(&request))); | |
| 146 } | |
| 147 | |
| 148 } // namespace | |
| 149 | |
| 97 // Disable SIMULATE_SERIAL_PORTS only if all the following are true: | 150 // Disable SIMULATE_SERIAL_PORTS only if all the following are true: |
| 98 // | 151 // |
| 99 // 1. You have an Arduino or compatible board attached to your machine and | 152 // 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 | 153 // 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 | 154 // 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 | 155 // the Atmega32u4 Breakout Board and Arduino Leonardo; note that both these |
| 103 // boards are based on the Atmel ATmega32u4, rather than the more common | 156 // 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 | 157 // Arduino '328p with either FTDI or '8/16u2 USB interfaces. TODO: test more |
| 105 // widely. | 158 // widely. |
| 106 // | 159 // |
| 107 // 2. Your user has permission to read/write the port. For example, this might | 160 // 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 | 161 // 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 | 162 // Linux, or else that the port's path (e.g., /dev/ttyACM0) has global |
| 110 // read/write permissions. | 163 // read/write permissions. |
| 111 // | 164 // |
| 112 // 3. You have uploaded a program to the board that does a byte-for-byte echo | 165 // 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 | 166 // 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. | 167 // chrome/test/data/extensions/api_test/serial/api/serial_arduino_test.ino. |
| 115 // | 168 // |
| 116 #define SIMULATE_SERIAL_PORTS (1) | 169 #define SIMULATE_SERIAL_PORTS (1) |
| 117 IN_PROC_BROWSER_TEST_F(SerialApiTest, SerialFakeHardware) { | 170 IN_PROC_BROWSER_TEST_P(SerialApiTest, SerialFakeHardware) { |
| 118 extensions::ResultCatcher catcher; | 171 ResultCatcher catcher; |
| 119 catcher.RestrictToBrowserContext(browser()->profile()); | 172 catcher.RestrictToBrowserContext(browser()->profile()); |
| 120 | 173 |
| 121 #if SIMULATE_SERIAL_PORTS | 174 #if SIMULATE_SERIAL_PORTS |
| 122 ASSERT_TRUE(extensions::ExtensionFunctionDispatcher::OverrideFunction( | 175 if (GetParam()) { |
| 123 "serial.getDevices", FakeSerialGetDevicesFunctionFactory)); | 176 ServiceRegistrationManager::GetSharedInstance() |
| 124 ASSERT_TRUE(extensions::ExtensionFunctionDispatcher::OverrideFunction( | 177 ->OverrideServiceFactoryForTest(base::Bind(&CreateTestSerialService)); |
| 125 "serial.connect", FakeSerialConnectFunctionFactory)); | 178 } else { |
| 179 ASSERT_TRUE(extensions::ExtensionFunctionDispatcher::OverrideFunction( | |
| 180 "serial.getDevices", FakeSerialGetDevicesFunctionFactory)); | |
| 181 ASSERT_TRUE(extensions::ExtensionFunctionDispatcher::OverrideFunction( | |
| 182 "serial.connect", FakeSerialConnectFunctionFactory)); | |
| 183 } | |
| 126 #endif | 184 #endif |
| 127 | 185 |
| 128 ASSERT_TRUE(RunExtensionTest("serial/api")) << message_; | 186 ASSERT_TRUE(RunExtensionTest("serial/api")) << message_; |
| 129 } | 187 } |
| 130 | 188 |
| 131 IN_PROC_BROWSER_TEST_F(SerialApiTest, SerialRealHardware) { | 189 IN_PROC_BROWSER_TEST_P(SerialApiTest, SerialRealHardware) { |
| 132 extensions::ResultCatcher catcher; | 190 ResultCatcher catcher; |
| 133 catcher.RestrictToBrowserContext(browser()->profile()); | 191 catcher.RestrictToBrowserContext(browser()->profile()); |
| 134 | 192 |
| 135 ASSERT_TRUE(RunExtensionTest("serial/real_hardware")) << message_; | 193 ASSERT_TRUE(RunExtensionTest("serial/real_hardware")) << message_; |
| 136 } | 194 } |
| 195 | |
| 196 INSTANTIATE_TEST_CASE_P(SerialApiTest, SerialApiTest, testing::Bool()); | |
| 197 | |
| 198 } // namespace extensions | |
| OLD | NEW |