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
3 // found in the LICENSE file.
8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
10 #include "base/strings/string_piece.h"
11 #include "device/serial/data_receiver.h"
12 #include "device/serial/data_sender.h"
13 #include "device/serial/data_stream.mojom.h"
14 #include "device/serial/serial.mojom.h"
15 #include "device/serial/serial_connection.h"
16 #include "device/serial/serial_service_impl.h"
17 #include "device/serial/test_serial_io_handler.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h"
20 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_ptr.h"
21 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h"
26 class FakeSerialDeviceEnumerator
: public SerialDeviceEnumerator
{
27 mojo::Array
<serial::DeviceInfoPtr
> GetDevices() override
{
28 mojo::Array
<serial::DeviceInfoPtr
> devices(1);
29 devices
[0] = serial::DeviceInfo::New();
30 devices
[0]->path
= "device";
31 return devices
.Pass();
37 class SerialConnectionTest
: public testing::Test
, public mojo::ErrorHandler
{
43 EVENT_GOT_CONTROL_SIGNALS
,
44 EVENT_SET_CONTROL_SIGNALS
,
46 EVENT_DATA_AT_IO_HANDLER
,
51 EVENT_CANCEL_COMPLETE
,
55 static const uint32_t kBufferSize
;
57 SerialConnectionTest()
61 send_error_(serial::SEND_ERROR_NONE
),
62 receive_error_(serial::RECEIVE_ERROR_NONE
),
63 expected_event_(EVENT_NONE
) {}
65 void SetUp() override
{
66 message_loop_
.reset(new base::MessageLoop
);
67 mojo::InterfacePtr
<serial::SerialService
> service
;
69 new SerialServiceImpl(
70 new SerialConnectionFactory(
71 base::Bind(&SerialConnectionTest::CreateIoHandler
,
72 base::Unretained(this)),
73 base::MessageLoopProxy::current()),
74 scoped_ptr
<SerialDeviceEnumerator
>(new FakeSerialDeviceEnumerator
)),
76 service
.set_error_handler(this);
77 mojo::InterfacePtr
<serial::DataSink
> sink
;
78 mojo::InterfacePtr
<serial::DataSinkClient
> sink_client
;
79 mojo::InterfaceRequest
<serial::DataSinkClient
> sink_client_request
=
80 mojo::GetProxy(&sink_client
);
81 mojo::InterfacePtr
<serial::DataSource
> source
;
82 mojo::InterfacePtr
<serial::DataSourceClient
> source_client
;
83 mojo::InterfaceRequest
<serial::DataSourceClient
> source_client_request
=
84 mojo::GetProxy(&source_client
);
85 service
->Connect("device", serial::ConnectionOptions::New(),
86 mojo::GetProxy(&connection_
), mojo::GetProxy(&sink
),
87 sink_client
.Pass(), mojo::GetProxy(&source
),
88 source_client
.Pass());
89 sender_
.reset(new DataSender(sink
.Pass(), sink_client_request
.Pass(),
90 kBufferSize
, serial::SEND_ERROR_DISCONNECTED
));
92 new DataReceiver(source
.Pass(), source_client_request
.Pass(),
93 kBufferSize
, serial::RECEIVE_ERROR_DISCONNECTED
);
94 connection_
.set_error_handler(this);
96 base::Bind(&SerialConnectionTest::StoreInfo
, base::Unretained(this)));
97 WaitForEvent(EVENT_GOT_INFO
);
98 ASSERT_TRUE(io_handler_
.get());
101 void StoreInfo(serial::ConnectionInfoPtr options
) {
102 info_
= options
.Pass();
103 EventReceived(EVENT_GOT_INFO
);
106 void StoreControlSignals(serial::DeviceControlSignalsPtr signals
) {
107 signals_
= signals
.Pass();
108 EventReceived(EVENT_GOT_CONTROL_SIGNALS
);
111 void StoreSuccess(Event event_to_report
, bool success
) {
113 EventReceived(event_to_report
);
116 void Send(const base::StringPiece
& data
) {
117 ASSERT_TRUE(sender_
->Send(
119 base::Bind(&SerialConnectionTest::OnDataSent
, base::Unretained(this)),
120 base::Bind(&SerialConnectionTest::OnSendError
,
121 base::Unretained(this))));
126 receiver_
->Receive(base::Bind(&SerialConnectionTest::OnDataReceived
,
127 base::Unretained(this)),
128 base::Bind(&SerialConnectionTest::OnReceiveError
,
129 base::Unretained(this))));
132 void WaitForEvent(Event event
) {
133 expected_event_
= event
;
134 base::RunLoop run_loop
;
135 stop_run_loop_
= run_loop
.QuitClosure();
139 void EventReceived(Event event
) {
140 if (event
!= expected_event_
)
142 expected_event_
= EVENT_NONE
;
143 ASSERT_TRUE(message_loop_
);
144 ASSERT_TRUE(!stop_run_loop_
.is_null());
145 message_loop_
->PostTask(FROM_HERE
, stop_run_loop_
);
148 scoped_refptr
<SerialIoHandler
> CreateIoHandler() {
149 io_handler_
= new TestSerialIoHandler
;
153 void OnDataSent(uint32_t bytes_sent
) {
154 bytes_sent_
+= bytes_sent
;
155 send_error_
= serial::SEND_ERROR_NONE
;
156 EventReceived(EVENT_DATA_SENT
);
159 void OnSendError(uint32_t bytes_sent
, int32_t error
) {
160 bytes_sent_
+= bytes_sent
;
161 send_error_
= static_cast<serial::SendError
>(error
);
162 EventReceived(EVENT_SEND_ERROR
);
165 void OnDataReceived(scoped_ptr
<ReadOnlyBuffer
> buffer
) {
166 data_received_
+= std::string(buffer
->GetData(), buffer
->GetSize());
167 buffer
->Done(buffer
->GetSize());
168 receive_error_
= serial::RECEIVE_ERROR_NONE
;
169 EventReceived(EVENT_DATA_RECEIVED
);
172 void OnReceiveError(int32_t error
) {
173 receive_error_
= static_cast<serial::ReceiveError
>(error
);
174 EventReceived(EVENT_RECEIVE_ERROR
);
177 void OnConnectionError() override
{
178 EventReceived(EVENT_ERROR
);
179 FAIL() << "Connection error";
182 mojo::Array
<serial::DeviceInfoPtr
> devices_
;
183 serial::ConnectionInfoPtr info_
;
184 serial::DeviceControlSignalsPtr signals_
;
188 serial::SendError send_error_
;
189 serial::ReceiveError receive_error_
;
190 std::string data_received_
;
191 Event expected_event_
;
193 scoped_ptr
<base::MessageLoop
> message_loop_
;
194 base::Closure stop_run_loop_
;
195 mojo::InterfacePtr
<serial::Connection
> connection_
;
196 scoped_ptr
<DataSender
> sender_
;
197 scoped_refptr
<DataReceiver
> receiver_
;
198 scoped_refptr
<TestSerialIoHandler
> io_handler_
;
201 DISALLOW_COPY_AND_ASSIGN(SerialConnectionTest
);
204 const uint32_t SerialConnectionTest::kBufferSize
= 10;
206 TEST_F(SerialConnectionTest
, GetInfo
) {
207 // |info_| is filled in during SetUp().
209 EXPECT_EQ(9600u, info_
->bitrate
);
210 EXPECT_EQ(serial::DATA_BITS_EIGHT
, info_
->data_bits
);
211 EXPECT_EQ(serial::PARITY_BIT_NO
, info_
->parity_bit
);
212 EXPECT_EQ(serial::STOP_BITS_ONE
, info_
->stop_bits
);
213 EXPECT_FALSE(info_
->cts_flow_control
);
216 TEST_F(SerialConnectionTest
, SetOptions
) {
217 serial::ConnectionOptionsPtr
options(serial::ConnectionOptions::New());
218 options
->bitrate
= 12345;
219 options
->data_bits
= serial::DATA_BITS_SEVEN
;
220 options
->has_cts_flow_control
= true;
221 options
->cts_flow_control
= true;
222 connection_
->SetOptions(options
.Pass(),
223 base::Bind(&SerialConnectionTest::StoreSuccess
,
224 base::Unretained(this),
226 WaitForEvent(EVENT_SET_OPTIONS
);
227 ASSERT_TRUE(success_
);
228 serial::ConnectionInfo
* info
= io_handler_
->connection_info();
229 EXPECT_EQ(12345u, info
->bitrate
);
230 EXPECT_EQ(serial::DATA_BITS_SEVEN
, info
->data_bits
);
231 EXPECT_EQ(serial::PARITY_BIT_NO
, info
->parity_bit
);
232 EXPECT_EQ(serial::STOP_BITS_ONE
, info
->stop_bits
);
233 EXPECT_TRUE(info
->cts_flow_control
);
236 TEST_F(SerialConnectionTest
, GetControlSignals
) {
237 connection_
->GetControlSignals(base::Bind(
238 &SerialConnectionTest::StoreControlSignals
, base::Unretained(this)));
239 serial::DeviceControlSignals
* signals
= io_handler_
->device_control_signals();
243 WaitForEvent(EVENT_GOT_CONTROL_SIGNALS
);
244 ASSERT_TRUE(signals_
);
245 EXPECT_TRUE(signals_
->dcd
);
246 EXPECT_FALSE(signals_
->cts
);
247 EXPECT_FALSE(signals_
->ri
);
248 EXPECT_TRUE(signals_
->dsr
);
251 TEST_F(SerialConnectionTest
, SetControlSignals
) {
252 serial::HostControlSignalsPtr
signals(serial::HostControlSignals::New());
253 signals
->has_dtr
= true;
255 signals
->has_rts
= true;
258 connection_
->SetControlSignals(signals
.Pass(),
259 base::Bind(&SerialConnectionTest::StoreSuccess
,
260 base::Unretained(this),
261 EVENT_SET_CONTROL_SIGNALS
));
262 WaitForEvent(EVENT_SET_CONTROL_SIGNALS
);
263 ASSERT_TRUE(success_
);
264 EXPECT_TRUE(io_handler_
->dtr());
265 EXPECT_TRUE(io_handler_
->rts());
268 TEST_F(SerialConnectionTest
, Flush
) {
269 ASSERT_EQ(0, io_handler_
->flushes());
270 connection_
->Flush(base::Bind(&SerialConnectionTest::StoreSuccess
,
271 base::Unretained(this),
273 WaitForEvent(EVENT_FLUSHED
);
274 ASSERT_TRUE(success_
);
275 EXPECT_EQ(1, io_handler_
->flushes());
278 TEST_F(SerialConnectionTest
, DisconnectWithSend
) {
280 io_handler_
->set_send_callback(base::Bind(base::DoNothing
));
281 ASSERT_NO_FATAL_FAILURE(Send("data"));
282 WaitForEvent(EVENT_SEND_ERROR
);
283 EXPECT_EQ(serial::SEND_ERROR_DISCONNECTED
, send_error_
);
284 EXPECT_EQ(0, bytes_sent_
);
285 EXPECT_TRUE(io_handler_
->HasOneRef());
288 TEST_F(SerialConnectionTest
, DisconnectWithReceive
) {
290 ASSERT_NO_FATAL_FAILURE(Receive());
291 WaitForEvent(EVENT_RECEIVE_ERROR
);
292 EXPECT_EQ(serial::RECEIVE_ERROR_DISCONNECTED
, receive_error_
);
293 EXPECT_EQ("", data_received_
);
294 EXPECT_TRUE(io_handler_
->HasOneRef());
297 TEST_F(SerialConnectionTest
, Echo
) {
298 ASSERT_NO_FATAL_FAILURE(Send("data"));
299 WaitForEvent(EVENT_DATA_SENT
);
300 EXPECT_EQ(serial::SEND_ERROR_NONE
, send_error_
);
301 EXPECT_EQ(4, bytes_sent_
);
302 ASSERT_NO_FATAL_FAILURE(Receive());
303 WaitForEvent(EVENT_DATA_RECEIVED
);
304 EXPECT_EQ("data", data_received_
);
305 EXPECT_EQ(serial::RECEIVE_ERROR_NONE
, receive_error_
);
308 TEST_F(SerialConnectionTest
, Cancel
) {
309 // 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 // cancel internally.
312 io_handler_
->set_send_callback(
313 base::Bind(&SerialConnectionTest::EventReceived
,
314 base::Unretained(this),
315 EVENT_DATA_AT_IO_HANDLER
));
316 ASSERT_NO_FATAL_FAILURE(Send("something else"));
317 WaitForEvent(EVENT_DATA_AT_IO_HANDLER
);
318 EXPECT_EQ(0, bytes_sent_
);
320 ASSERT_TRUE(sender_
->Cancel(serial::SEND_ERROR_TIMEOUT
,
321 base::Bind(&SerialConnectionTest::EventReceived
,
322 base::Unretained(this),
323 EVENT_CANCEL_COMPLETE
)));
325 WaitForEvent(EVENT_CANCEL_COMPLETE
);
326 EXPECT_EQ(serial::SEND_ERROR_TIMEOUT
, send_error_
);
328 ASSERT_NO_FATAL_FAILURE(Send("data"));
329 WaitForEvent(EVENT_DATA_SENT
);
330 EXPECT_EQ(serial::SEND_ERROR_NONE
, send_error_
);
331 EXPECT_EQ(4, bytes_sent_
);
332 ASSERT_NO_FATAL_FAILURE(Receive());
333 WaitForEvent(EVENT_DATA_RECEIVED
);
334 EXPECT_EQ("data", data_received_
);
335 EXPECT_EQ(serial::RECEIVE_ERROR_NONE
, receive_error_
);
338 } // namespace device