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" | 5 #include "device/serial/serial_connection.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 EVENT_CANCEL_COMPLETE, | 54 EVENT_CANCEL_COMPLETE, |
55 EVENT_ERROR, | 55 EVENT_ERROR, |
56 }; | 56 }; |
57 | 57 |
58 static const uint32_t kBufferSize; | 58 static const uint32_t kBufferSize; |
59 | 59 |
60 SerialConnectionTest() | 60 SerialConnectionTest() |
61 : connected_(false), | 61 : connected_(false), |
62 success_(false), | 62 success_(false), |
63 bytes_sent_(0), | 63 bytes_sent_(0), |
64 send_error_(serial::SEND_ERROR_NONE), | 64 send_error_(serial::SendError::NONE), |
65 receive_error_(serial::RECEIVE_ERROR_NONE), | 65 receive_error_(serial::ReceiveError::NONE), |
66 expected_event_(EVENT_NONE) {} | 66 expected_event_(EVENT_NONE) {} |
67 | 67 |
68 void SetUp() override { | 68 void SetUp() override { |
69 message_loop_.reset(new base::MessageLoop); | 69 message_loop_.reset(new base::MessageLoop); |
70 mojo::InterfacePtr<serial::SerialService> service; | 70 mojo::InterfacePtr<serial::SerialService> service; |
71 new SerialServiceImpl( | 71 new SerialServiceImpl( |
72 new SerialConnectionFactory( | 72 new SerialConnectionFactory( |
73 base::Bind(&SerialConnectionTest::CreateIoHandler, | 73 base::Bind(&SerialConnectionTest::CreateIoHandler, |
74 base::Unretained(this)), | 74 base::Unretained(this)), |
75 base::ThreadTaskRunnerHandle::Get()), | 75 base::ThreadTaskRunnerHandle::Get()), |
76 scoped_ptr<SerialDeviceEnumerator>(new FakeSerialDeviceEnumerator), | 76 scoped_ptr<SerialDeviceEnumerator>(new FakeSerialDeviceEnumerator), |
77 mojo::GetProxy(&service)); | 77 mojo::GetProxy(&service)); |
78 service.set_connection_error_handler(base::Bind( | 78 service.set_connection_error_handler(base::Bind( |
79 &SerialConnectionTest::OnConnectionError, base::Unretained(this))); | 79 &SerialConnectionTest::OnConnectionError, base::Unretained(this))); |
80 mojo::InterfacePtr<serial::DataSink> sink; | 80 mojo::InterfacePtr<serial::DataSink> sink; |
81 mojo::InterfacePtr<serial::DataSource> source; | 81 mojo::InterfacePtr<serial::DataSource> source; |
82 mojo::InterfacePtr<serial::DataSourceClient> source_client; | 82 mojo::InterfacePtr<serial::DataSourceClient> source_client; |
83 mojo::InterfaceRequest<serial::DataSourceClient> source_client_request = | 83 mojo::InterfaceRequest<serial::DataSourceClient> source_client_request = |
84 mojo::GetProxy(&source_client); | 84 mojo::GetProxy(&source_client); |
85 service->Connect("device", serial::ConnectionOptions::New(), | 85 service->Connect("device", serial::ConnectionOptions::New(), |
86 mojo::GetProxy(&connection_), mojo::GetProxy(&sink), | 86 mojo::GetProxy(&connection_), mojo::GetProxy(&sink), |
87 mojo::GetProxy(&source), std::move(source_client)); | 87 mojo::GetProxy(&source), std::move(source_client)); |
88 sender_.reset(new DataSender(std::move(sink), kBufferSize, | 88 sender_.reset( |
89 serial::SEND_ERROR_DISCONNECTED)); | 89 new DataSender(std::move(sink), kBufferSize, |
90 receiver_ = | 90 static_cast<int32_t>(serial::SendError::DISCONNECTED))); |
91 new DataReceiver(std::move(source), std::move(source_client_request), | 91 receiver_ = new DataReceiver( |
92 kBufferSize, serial::RECEIVE_ERROR_DISCONNECTED); | 92 std::move(source), std::move(source_client_request), kBufferSize, |
| 93 static_cast<int32_t>(serial::ReceiveError::DISCONNECTED)); |
93 connection_.set_connection_error_handler(base::Bind( | 94 connection_.set_connection_error_handler(base::Bind( |
94 &SerialConnectionTest::OnConnectionError, base::Unretained(this))); | 95 &SerialConnectionTest::OnConnectionError, base::Unretained(this))); |
95 connection_->GetInfo( | 96 connection_->GetInfo( |
96 base::Bind(&SerialConnectionTest::StoreInfo, base::Unretained(this))); | 97 base::Bind(&SerialConnectionTest::StoreInfo, base::Unretained(this))); |
97 WaitForEvent(EVENT_GOT_INFO); | 98 WaitForEvent(EVENT_GOT_INFO); |
98 ASSERT_TRUE(io_handler_.get()); | 99 ASSERT_TRUE(io_handler_.get()); |
99 } | 100 } |
100 | 101 |
101 void StoreInfo(serial::ConnectionInfoPtr options) { | 102 void StoreInfo(serial::ConnectionInfoPtr options) { |
102 info_ = std::move(options); | 103 info_ = std::move(options); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 message_loop_->PostTask(FROM_HERE, stop_run_loop_); | 146 message_loop_->PostTask(FROM_HERE, stop_run_loop_); |
146 } | 147 } |
147 | 148 |
148 scoped_refptr<SerialIoHandler> CreateIoHandler() { | 149 scoped_refptr<SerialIoHandler> CreateIoHandler() { |
149 io_handler_ = new TestSerialIoHandler; | 150 io_handler_ = new TestSerialIoHandler; |
150 return io_handler_; | 151 return io_handler_; |
151 } | 152 } |
152 | 153 |
153 void OnDataSent(uint32_t bytes_sent) { | 154 void OnDataSent(uint32_t bytes_sent) { |
154 bytes_sent_ += bytes_sent; | 155 bytes_sent_ += bytes_sent; |
155 send_error_ = serial::SEND_ERROR_NONE; | 156 send_error_ = serial::SendError::NONE; |
156 EventReceived(EVENT_DATA_SENT); | 157 EventReceived(EVENT_DATA_SENT); |
157 } | 158 } |
158 | 159 |
159 void OnSendError(uint32_t bytes_sent, int32_t error) { | 160 void OnSendError(uint32_t bytes_sent, int32_t error) { |
160 bytes_sent_ += bytes_sent; | 161 bytes_sent_ += bytes_sent; |
161 send_error_ = static_cast<serial::SendError>(error); | 162 send_error_ = static_cast<serial::SendError>(error); |
162 EventReceived(EVENT_SEND_ERROR); | 163 EventReceived(EVENT_SEND_ERROR); |
163 } | 164 } |
164 | 165 |
165 void OnDataReceived(scoped_ptr<ReadOnlyBuffer> buffer) { | 166 void OnDataReceived(scoped_ptr<ReadOnlyBuffer> buffer) { |
166 data_received_ += std::string(buffer->GetData(), buffer->GetSize()); | 167 data_received_ += std::string(buffer->GetData(), buffer->GetSize()); |
167 buffer->Done(buffer->GetSize()); | 168 buffer->Done(buffer->GetSize()); |
168 receive_error_ = serial::RECEIVE_ERROR_NONE; | 169 receive_error_ = serial::ReceiveError::NONE; |
169 EventReceived(EVENT_DATA_RECEIVED); | 170 EventReceived(EVENT_DATA_RECEIVED); |
170 } | 171 } |
171 | 172 |
172 void OnReceiveError(int32_t error) { | 173 void OnReceiveError(int32_t error) { |
173 receive_error_ = static_cast<serial::ReceiveError>(error); | 174 receive_error_ = static_cast<serial::ReceiveError>(error); |
174 EventReceived(EVENT_RECEIVE_ERROR); | 175 EventReceived(EVENT_RECEIVE_ERROR); |
175 } | 176 } |
176 | 177 |
177 void OnConnectionError() { | 178 void OnConnectionError() { |
178 EventReceived(EVENT_ERROR); | 179 EventReceived(EVENT_ERROR); |
(...skipping 21 matching lines...) Expand all Loading... |
200 private: | 201 private: |
201 DISALLOW_COPY_AND_ASSIGN(SerialConnectionTest); | 202 DISALLOW_COPY_AND_ASSIGN(SerialConnectionTest); |
202 }; | 203 }; |
203 | 204 |
204 const uint32_t SerialConnectionTest::kBufferSize = 10; | 205 const uint32_t SerialConnectionTest::kBufferSize = 10; |
205 | 206 |
206 TEST_F(SerialConnectionTest, GetInfo) { | 207 TEST_F(SerialConnectionTest, GetInfo) { |
207 // |info_| is filled in during SetUp(). | 208 // |info_| is filled in during SetUp(). |
208 ASSERT_TRUE(info_); | 209 ASSERT_TRUE(info_); |
209 EXPECT_EQ(9600u, info_->bitrate); | 210 EXPECT_EQ(9600u, info_->bitrate); |
210 EXPECT_EQ(serial::DATA_BITS_EIGHT, info_->data_bits); | 211 EXPECT_EQ(serial::DataBits::EIGHT, info_->data_bits); |
211 EXPECT_EQ(serial::PARITY_BIT_NO, info_->parity_bit); | 212 EXPECT_EQ(serial::ParityBit::NO, info_->parity_bit); |
212 EXPECT_EQ(serial::STOP_BITS_ONE, info_->stop_bits); | 213 EXPECT_EQ(serial::StopBits::ONE, info_->stop_bits); |
213 EXPECT_FALSE(info_->cts_flow_control); | 214 EXPECT_FALSE(info_->cts_flow_control); |
214 } | 215 } |
215 | 216 |
216 TEST_F(SerialConnectionTest, SetOptions) { | 217 TEST_F(SerialConnectionTest, SetOptions) { |
217 serial::ConnectionOptionsPtr options(serial::ConnectionOptions::New()); | 218 serial::ConnectionOptionsPtr options(serial::ConnectionOptions::New()); |
218 options->bitrate = 12345; | 219 options->bitrate = 12345; |
219 options->data_bits = serial::DATA_BITS_SEVEN; | 220 options->data_bits = serial::DataBits::SEVEN; |
220 options->has_cts_flow_control = true; | 221 options->has_cts_flow_control = true; |
221 options->cts_flow_control = true; | 222 options->cts_flow_control = true; |
222 connection_->SetOptions( | 223 connection_->SetOptions( |
223 std::move(options), | 224 std::move(options), |
224 base::Bind(&SerialConnectionTest::StoreSuccess, base::Unretained(this), | 225 base::Bind(&SerialConnectionTest::StoreSuccess, base::Unretained(this), |
225 EVENT_SET_OPTIONS)); | 226 EVENT_SET_OPTIONS)); |
226 WaitForEvent(EVENT_SET_OPTIONS); | 227 WaitForEvent(EVENT_SET_OPTIONS); |
227 ASSERT_TRUE(success_); | 228 ASSERT_TRUE(success_); |
228 serial::ConnectionInfo* info = io_handler_->connection_info(); | 229 serial::ConnectionInfo* info = io_handler_->connection_info(); |
229 EXPECT_EQ(12345u, info->bitrate); | 230 EXPECT_EQ(12345u, info->bitrate); |
230 EXPECT_EQ(serial::DATA_BITS_SEVEN, info->data_bits); | 231 EXPECT_EQ(serial::DataBits::SEVEN, info->data_bits); |
231 EXPECT_EQ(serial::PARITY_BIT_NO, info->parity_bit); | 232 EXPECT_EQ(serial::ParityBit::NO, info->parity_bit); |
232 EXPECT_EQ(serial::STOP_BITS_ONE, info->stop_bits); | 233 EXPECT_EQ(serial::StopBits::ONE, info->stop_bits); |
233 EXPECT_TRUE(info->cts_flow_control); | 234 EXPECT_TRUE(info->cts_flow_control); |
234 } | 235 } |
235 | 236 |
236 TEST_F(SerialConnectionTest, GetControlSignals) { | 237 TEST_F(SerialConnectionTest, GetControlSignals) { |
237 connection_->GetControlSignals(base::Bind( | 238 connection_->GetControlSignals(base::Bind( |
238 &SerialConnectionTest::StoreControlSignals, base::Unretained(this))); | 239 &SerialConnectionTest::StoreControlSignals, base::Unretained(this))); |
239 serial::DeviceControlSignals* signals = io_handler_->device_control_signals(); | 240 serial::DeviceControlSignals* signals = io_handler_->device_control_signals(); |
240 signals->dcd = true; | 241 signals->dcd = true; |
241 signals->dsr = true; | 242 signals->dsr = true; |
242 | 243 |
(...skipping 30 matching lines...) Expand all Loading... |
273 WaitForEvent(EVENT_FLUSHED); | 274 WaitForEvent(EVENT_FLUSHED); |
274 ASSERT_TRUE(success_); | 275 ASSERT_TRUE(success_); |
275 EXPECT_EQ(1, io_handler_->flushes()); | 276 EXPECT_EQ(1, io_handler_->flushes()); |
276 } | 277 } |
277 | 278 |
278 TEST_F(SerialConnectionTest, DisconnectWithSend) { | 279 TEST_F(SerialConnectionTest, DisconnectWithSend) { |
279 connection_.reset(); | 280 connection_.reset(); |
280 io_handler_->set_send_callback(base::Bind(base::DoNothing)); | 281 io_handler_->set_send_callback(base::Bind(base::DoNothing)); |
281 ASSERT_NO_FATAL_FAILURE(Send("data")); | 282 ASSERT_NO_FATAL_FAILURE(Send("data")); |
282 WaitForEvent(EVENT_SEND_ERROR); | 283 WaitForEvent(EVENT_SEND_ERROR); |
283 EXPECT_EQ(serial::SEND_ERROR_DISCONNECTED, send_error_); | 284 EXPECT_EQ(serial::SendError::DISCONNECTED, send_error_); |
284 EXPECT_EQ(0, bytes_sent_); | 285 EXPECT_EQ(0, bytes_sent_); |
285 EXPECT_TRUE(io_handler_->HasOneRef()); | 286 EXPECT_TRUE(io_handler_->HasOneRef()); |
286 } | 287 } |
287 | 288 |
288 TEST_F(SerialConnectionTest, DisconnectWithReceive) { | 289 TEST_F(SerialConnectionTest, DisconnectWithReceive) { |
289 connection_.reset(); | 290 connection_.reset(); |
290 ASSERT_NO_FATAL_FAILURE(Receive()); | 291 ASSERT_NO_FATAL_FAILURE(Receive()); |
291 WaitForEvent(EVENT_RECEIVE_ERROR); | 292 WaitForEvent(EVENT_RECEIVE_ERROR); |
292 EXPECT_EQ(serial::RECEIVE_ERROR_DISCONNECTED, receive_error_); | 293 EXPECT_EQ(serial::ReceiveError::DISCONNECTED, receive_error_); |
293 EXPECT_EQ("", data_received_); | 294 EXPECT_EQ("", data_received_); |
294 EXPECT_TRUE(io_handler_->HasOneRef()); | 295 EXPECT_TRUE(io_handler_->HasOneRef()); |
295 } | 296 } |
296 | 297 |
297 TEST_F(SerialConnectionTest, Echo) { | 298 TEST_F(SerialConnectionTest, Echo) { |
298 ASSERT_NO_FATAL_FAILURE(Send("data")); | 299 ASSERT_NO_FATAL_FAILURE(Send("data")); |
299 WaitForEvent(EVENT_DATA_SENT); | 300 WaitForEvent(EVENT_DATA_SENT); |
300 EXPECT_EQ(serial::SEND_ERROR_NONE, send_error_); | 301 EXPECT_EQ(serial::SendError::NONE, send_error_); |
301 EXPECT_EQ(4, bytes_sent_); | 302 EXPECT_EQ(4, bytes_sent_); |
302 ASSERT_NO_FATAL_FAILURE(Receive()); | 303 ASSERT_NO_FATAL_FAILURE(Receive()); |
303 WaitForEvent(EVENT_DATA_RECEIVED); | 304 WaitForEvent(EVENT_DATA_RECEIVED); |
304 EXPECT_EQ("data", data_received_); | 305 EXPECT_EQ("data", data_received_); |
305 EXPECT_EQ(serial::RECEIVE_ERROR_NONE, receive_error_); | 306 EXPECT_EQ(serial::ReceiveError::NONE, receive_error_); |
306 } | 307 } |
307 | 308 |
308 TEST_F(SerialConnectionTest, Cancel) { | 309 TEST_F(SerialConnectionTest, Cancel) { |
309 // To test that cancels are correctly passed to the IoHandler, we need a send | 310 // To test that cancels are correctly passed to the IoHandler, we need a send |
310 // to be in progress because otherwise, the DataSinkReceiver would handle the | 311 // to be in progress because otherwise, the DataSinkReceiver would handle the |
311 // cancel internally. | 312 // cancel internally. |
312 io_handler_->set_send_callback( | 313 io_handler_->set_send_callback( |
313 base::Bind(&SerialConnectionTest::EventReceived, | 314 base::Bind(&SerialConnectionTest::EventReceived, |
314 base::Unretained(this), | 315 base::Unretained(this), |
315 EVENT_DATA_AT_IO_HANDLER)); | 316 EVENT_DATA_AT_IO_HANDLER)); |
316 ASSERT_NO_FATAL_FAILURE(Send("something else")); | 317 ASSERT_NO_FATAL_FAILURE(Send("something else")); |
317 WaitForEvent(EVENT_DATA_AT_IO_HANDLER); | 318 WaitForEvent(EVENT_DATA_AT_IO_HANDLER); |
318 EXPECT_EQ(0, bytes_sent_); | 319 EXPECT_EQ(0, bytes_sent_); |
319 | 320 |
320 ASSERT_TRUE(sender_->Cancel(serial::SEND_ERROR_TIMEOUT, | 321 ASSERT_TRUE(sender_->Cancel( |
321 base::Bind(&SerialConnectionTest::EventReceived, | 322 static_cast<int32_t>(serial::SendError::TIMEOUT), |
322 base::Unretained(this), | 323 base::Bind(&SerialConnectionTest::EventReceived, base::Unretained(this), |
323 EVENT_CANCEL_COMPLETE))); | 324 EVENT_CANCEL_COMPLETE))); |
324 | 325 |
325 WaitForEvent(EVENT_CANCEL_COMPLETE); | 326 WaitForEvent(EVENT_CANCEL_COMPLETE); |
326 EXPECT_EQ(serial::SEND_ERROR_TIMEOUT, send_error_); | 327 EXPECT_EQ(serial::SendError::TIMEOUT, send_error_); |
327 | 328 |
328 ASSERT_NO_FATAL_FAILURE(Send("data")); | 329 ASSERT_NO_FATAL_FAILURE(Send("data")); |
329 WaitForEvent(EVENT_DATA_SENT); | 330 WaitForEvent(EVENT_DATA_SENT); |
330 EXPECT_EQ(serial::SEND_ERROR_NONE, send_error_); | 331 EXPECT_EQ(serial::SendError::NONE, send_error_); |
331 EXPECT_EQ(4, bytes_sent_); | 332 EXPECT_EQ(4, bytes_sent_); |
332 ASSERT_NO_FATAL_FAILURE(Receive()); | 333 ASSERT_NO_FATAL_FAILURE(Receive()); |
333 WaitForEvent(EVENT_DATA_RECEIVED); | 334 WaitForEvent(EVENT_DATA_RECEIVED); |
334 EXPECT_EQ("data", data_received_); | 335 EXPECT_EQ("data", data_received_); |
335 EXPECT_EQ(serial::RECEIVE_ERROR_NONE, receive_error_); | 336 EXPECT_EQ(serial::ReceiveError::NONE, receive_error_); |
336 } | 337 } |
337 | 338 |
338 } // namespace device | 339 } // namespace device |
OLD | NEW |