| 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 "device/serial/serial_connection.h" |
| 6 |
| 5 #include <stdint.h> | 7 #include <stdint.h> |
| 6 | |
| 7 #include <string> | 8 #include <string> |
| 9 #include <utility> |
| 8 | 10 |
| 9 #include "base/bind.h" | 11 #include "base/bind.h" |
| 10 #include "base/macros.h" | 12 #include "base/macros.h" |
| 11 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 13 #include "base/strings/string_piece.h" | 15 #include "base/strings/string_piece.h" |
| 14 #include "device/serial/data_receiver.h" | 16 #include "device/serial/data_receiver.h" |
| 15 #include "device/serial/data_sender.h" | 17 #include "device/serial/data_sender.h" |
| 16 #include "device/serial/data_stream.mojom.h" | 18 #include "device/serial/data_stream.mojom.h" |
| 17 #include "device/serial/serial.mojom.h" | 19 #include "device/serial/serial.mojom.h" |
| 18 #include "device/serial/serial_connection.h" | |
| 19 #include "device/serial/serial_service_impl.h" | 20 #include "device/serial/serial_service_impl.h" |
| 20 #include "device/serial/test_serial_io_handler.h" | 21 #include "device/serial/test_serial_io_handler.h" |
| 21 #include "mojo/public/cpp/bindings/interface_ptr.h" | 22 #include "mojo/public/cpp/bindings/interface_ptr.h" |
| 22 #include "mojo/public/cpp/bindings/interface_request.h" | 23 #include "mojo/public/cpp/bindings/interface_request.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 25 |
| 25 namespace device { | 26 namespace device { |
| 26 namespace { | 27 namespace { |
| 27 | 28 |
| 28 class FakeSerialDeviceEnumerator : public SerialDeviceEnumerator { | 29 class FakeSerialDeviceEnumerator : public SerialDeviceEnumerator { |
| 29 mojo::Array<serial::DeviceInfoPtr> GetDevices() override { | 30 mojo::Array<serial::DeviceInfoPtr> GetDevices() override { |
| 30 mojo::Array<serial::DeviceInfoPtr> devices(1); | 31 mojo::Array<serial::DeviceInfoPtr> devices(1); |
| 31 devices[0] = serial::DeviceInfo::New(); | 32 devices[0] = serial::DeviceInfo::New(); |
| 32 devices[0]->path = "device"; | 33 devices[0]->path = "device"; |
| 33 return devices.Pass(); | 34 return devices; |
| 34 } | 35 } |
| 35 }; | 36 }; |
| 36 | 37 |
| 37 } // namespace | 38 } // namespace |
| 38 | 39 |
| 39 class SerialConnectionTest : public testing::Test { | 40 class SerialConnectionTest : public testing::Test { |
| 40 public: | 41 public: |
| 41 enum Event { | 42 enum Event { |
| 42 EVENT_NONE, | 43 EVENT_NONE, |
| 43 EVENT_GOT_INFO, | 44 EVENT_GOT_INFO, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 mojo::GetProxy(&service)); | 77 mojo::GetProxy(&service)); |
| 77 service.set_connection_error_handler(base::Bind( | 78 service.set_connection_error_handler(base::Bind( |
| 78 &SerialConnectionTest::OnConnectionError, base::Unretained(this))); | 79 &SerialConnectionTest::OnConnectionError, base::Unretained(this))); |
| 79 mojo::InterfacePtr<serial::DataSink> sink; | 80 mojo::InterfacePtr<serial::DataSink> sink; |
| 80 mojo::InterfacePtr<serial::DataSource> source; | 81 mojo::InterfacePtr<serial::DataSource> source; |
| 81 mojo::InterfacePtr<serial::DataSourceClient> source_client; | 82 mojo::InterfacePtr<serial::DataSourceClient> source_client; |
| 82 mojo::InterfaceRequest<serial::DataSourceClient> source_client_request = | 83 mojo::InterfaceRequest<serial::DataSourceClient> source_client_request = |
| 83 mojo::GetProxy(&source_client); | 84 mojo::GetProxy(&source_client); |
| 84 service->Connect("device", serial::ConnectionOptions::New(), | 85 service->Connect("device", serial::ConnectionOptions::New(), |
| 85 mojo::GetProxy(&connection_), mojo::GetProxy(&sink), | 86 mojo::GetProxy(&connection_), mojo::GetProxy(&sink), |
| 86 mojo::GetProxy(&source), source_client.Pass()); | 87 mojo::GetProxy(&source), std::move(source_client)); |
| 87 sender_.reset(new DataSender(sink.Pass(), kBufferSize, | 88 sender_.reset(new DataSender(std::move(sink), kBufferSize, |
| 88 serial::SEND_ERROR_DISCONNECTED)); | 89 serial::SEND_ERROR_DISCONNECTED)); |
| 89 receiver_ = | 90 receiver_ = |
| 90 new DataReceiver(source.Pass(), source_client_request.Pass(), | 91 new DataReceiver(std::move(source), std::move(source_client_request), |
| 91 kBufferSize, serial::RECEIVE_ERROR_DISCONNECTED); | 92 kBufferSize, serial::RECEIVE_ERROR_DISCONNECTED); |
| 92 connection_.set_connection_error_handler(base::Bind( | 93 connection_.set_connection_error_handler(base::Bind( |
| 93 &SerialConnectionTest::OnConnectionError, base::Unretained(this))); | 94 &SerialConnectionTest::OnConnectionError, base::Unretained(this))); |
| 94 connection_->GetInfo( | 95 connection_->GetInfo( |
| 95 base::Bind(&SerialConnectionTest::StoreInfo, base::Unretained(this))); | 96 base::Bind(&SerialConnectionTest::StoreInfo, base::Unretained(this))); |
| 96 WaitForEvent(EVENT_GOT_INFO); | 97 WaitForEvent(EVENT_GOT_INFO); |
| 97 ASSERT_TRUE(io_handler_.get()); | 98 ASSERT_TRUE(io_handler_.get()); |
| 98 } | 99 } |
| 99 | 100 |
| 100 void StoreInfo(serial::ConnectionInfoPtr options) { | 101 void StoreInfo(serial::ConnectionInfoPtr options) { |
| 101 info_ = options.Pass(); | 102 info_ = std::move(options); |
| 102 EventReceived(EVENT_GOT_INFO); | 103 EventReceived(EVENT_GOT_INFO); |
| 103 } | 104 } |
| 104 | 105 |
| 105 void StoreControlSignals(serial::DeviceControlSignalsPtr signals) { | 106 void StoreControlSignals(serial::DeviceControlSignalsPtr signals) { |
| 106 signals_ = signals.Pass(); | 107 signals_ = std::move(signals); |
| 107 EventReceived(EVENT_GOT_CONTROL_SIGNALS); | 108 EventReceived(EVENT_GOT_CONTROL_SIGNALS); |
| 108 } | 109 } |
| 109 | 110 |
| 110 void StoreSuccess(Event event_to_report, bool success) { | 111 void StoreSuccess(Event event_to_report, bool success) { |
| 111 success_ = success; | 112 success_ = success; |
| 112 EventReceived(event_to_report); | 113 EventReceived(event_to_report); |
| 113 } | 114 } |
| 114 | 115 |
| 115 void Send(const base::StringPiece& data) { | 116 void Send(const base::StringPiece& data) { |
| 116 ASSERT_TRUE(sender_->Send( | 117 ASSERT_TRUE(sender_->Send( |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 EXPECT_EQ(serial::STOP_BITS_ONE, info_->stop_bits); | 212 EXPECT_EQ(serial::STOP_BITS_ONE, info_->stop_bits); |
| 212 EXPECT_FALSE(info_->cts_flow_control); | 213 EXPECT_FALSE(info_->cts_flow_control); |
| 213 } | 214 } |
| 214 | 215 |
| 215 TEST_F(SerialConnectionTest, SetOptions) { | 216 TEST_F(SerialConnectionTest, SetOptions) { |
| 216 serial::ConnectionOptionsPtr options(serial::ConnectionOptions::New()); | 217 serial::ConnectionOptionsPtr options(serial::ConnectionOptions::New()); |
| 217 options->bitrate = 12345; | 218 options->bitrate = 12345; |
| 218 options->data_bits = serial::DATA_BITS_SEVEN; | 219 options->data_bits = serial::DATA_BITS_SEVEN; |
| 219 options->has_cts_flow_control = true; | 220 options->has_cts_flow_control = true; |
| 220 options->cts_flow_control = true; | 221 options->cts_flow_control = true; |
| 221 connection_->SetOptions(options.Pass(), | 222 connection_->SetOptions( |
| 222 base::Bind(&SerialConnectionTest::StoreSuccess, | 223 std::move(options), |
| 223 base::Unretained(this), | 224 base::Bind(&SerialConnectionTest::StoreSuccess, base::Unretained(this), |
| 224 EVENT_SET_OPTIONS)); | 225 EVENT_SET_OPTIONS)); |
| 225 WaitForEvent(EVENT_SET_OPTIONS); | 226 WaitForEvent(EVENT_SET_OPTIONS); |
| 226 ASSERT_TRUE(success_); | 227 ASSERT_TRUE(success_); |
| 227 serial::ConnectionInfo* info = io_handler_->connection_info(); | 228 serial::ConnectionInfo* info = io_handler_->connection_info(); |
| 228 EXPECT_EQ(12345u, info->bitrate); | 229 EXPECT_EQ(12345u, info->bitrate); |
| 229 EXPECT_EQ(serial::DATA_BITS_SEVEN, info->data_bits); | 230 EXPECT_EQ(serial::DATA_BITS_SEVEN, info->data_bits); |
| 230 EXPECT_EQ(serial::PARITY_BIT_NO, info->parity_bit); | 231 EXPECT_EQ(serial::PARITY_BIT_NO, info->parity_bit); |
| 231 EXPECT_EQ(serial::STOP_BITS_ONE, info->stop_bits); | 232 EXPECT_EQ(serial::STOP_BITS_ONE, info->stop_bits); |
| 232 EXPECT_TRUE(info->cts_flow_control); | 233 EXPECT_TRUE(info->cts_flow_control); |
| 233 } | 234 } |
| 234 | 235 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 247 EXPECT_TRUE(signals_->dsr); | 248 EXPECT_TRUE(signals_->dsr); |
| 248 } | 249 } |
| 249 | 250 |
| 250 TEST_F(SerialConnectionTest, SetControlSignals) { | 251 TEST_F(SerialConnectionTest, SetControlSignals) { |
| 251 serial::HostControlSignalsPtr signals(serial::HostControlSignals::New()); | 252 serial::HostControlSignalsPtr signals(serial::HostControlSignals::New()); |
| 252 signals->has_dtr = true; | 253 signals->has_dtr = true; |
| 253 signals->dtr = true; | 254 signals->dtr = true; |
| 254 signals->has_rts = true; | 255 signals->has_rts = true; |
| 255 signals->rts = true; | 256 signals->rts = true; |
| 256 | 257 |
| 257 connection_->SetControlSignals(signals.Pass(), | 258 connection_->SetControlSignals( |
| 258 base::Bind(&SerialConnectionTest::StoreSuccess, | 259 std::move(signals), |
| 259 base::Unretained(this), | 260 base::Bind(&SerialConnectionTest::StoreSuccess, base::Unretained(this), |
| 260 EVENT_SET_CONTROL_SIGNALS)); | 261 EVENT_SET_CONTROL_SIGNALS)); |
| 261 WaitForEvent(EVENT_SET_CONTROL_SIGNALS); | 262 WaitForEvent(EVENT_SET_CONTROL_SIGNALS); |
| 262 ASSERT_TRUE(success_); | 263 ASSERT_TRUE(success_); |
| 263 EXPECT_TRUE(io_handler_->dtr()); | 264 EXPECT_TRUE(io_handler_->dtr()); |
| 264 EXPECT_TRUE(io_handler_->rts()); | 265 EXPECT_TRUE(io_handler_->rts()); |
| 265 } | 266 } |
| 266 | 267 |
| 267 TEST_F(SerialConnectionTest, Flush) { | 268 TEST_F(SerialConnectionTest, Flush) { |
| 268 ASSERT_EQ(0, io_handler_->flushes()); | 269 ASSERT_EQ(0, io_handler_->flushes()); |
| 269 connection_->Flush(base::Bind(&SerialConnectionTest::StoreSuccess, | 270 connection_->Flush(base::Bind(&SerialConnectionTest::StoreSuccess, |
| 270 base::Unretained(this), | 271 base::Unretained(this), |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 WaitForEvent(EVENT_DATA_SENT); | 329 WaitForEvent(EVENT_DATA_SENT); |
| 329 EXPECT_EQ(serial::SEND_ERROR_NONE, send_error_); | 330 EXPECT_EQ(serial::SEND_ERROR_NONE, send_error_); |
| 330 EXPECT_EQ(4, bytes_sent_); | 331 EXPECT_EQ(4, bytes_sent_); |
| 331 ASSERT_NO_FATAL_FAILURE(Receive()); | 332 ASSERT_NO_FATAL_FAILURE(Receive()); |
| 332 WaitForEvent(EVENT_DATA_RECEIVED); | 333 WaitForEvent(EVENT_DATA_RECEIVED); |
| 333 EXPECT_EQ("data", data_received_); | 334 EXPECT_EQ("data", data_received_); |
| 334 EXPECT_EQ(serial::RECEIVE_ERROR_NONE, receive_error_); | 335 EXPECT_EQ(serial::RECEIVE_ERROR_NONE, receive_error_); |
| 335 } | 336 } |
| 336 | 337 |
| 337 } // namespace device | 338 } // namespace device |
| OLD | NEW |