Add OWNERS to content/browser/quota
[chromium-blink-merge.git] / device / serial / serial_connection_unittest.cc
blob552bb3afda2681f4b7f5699c90cdd373d00d3bcd
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.
5 #include <string>
7 #include "base/bind.h"
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/interface_ptr.h"
20 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_request.h"
22 namespace device {
23 namespace {
25 class FakeSerialDeviceEnumerator : public SerialDeviceEnumerator {
26 mojo::Array<serial::DeviceInfoPtr> GetDevices() override {
27 mojo::Array<serial::DeviceInfoPtr> devices(1);
28 devices[0] = serial::DeviceInfo::New();
29 devices[0]->path = "device";
30 return devices.Pass();
34 } // namespace
36 class SerialConnectionTest : public testing::Test {
37 public:
38 enum Event {
39 EVENT_NONE,
40 EVENT_GOT_INFO,
41 EVENT_SET_OPTIONS,
42 EVENT_GOT_CONTROL_SIGNALS,
43 EVENT_SET_CONTROL_SIGNALS,
44 EVENT_FLUSHED,
45 EVENT_DATA_AT_IO_HANDLER,
46 EVENT_DATA_SENT,
47 EVENT_SEND_ERROR,
48 EVENT_DATA_RECEIVED,
49 EVENT_RECEIVE_ERROR,
50 EVENT_CANCEL_COMPLETE,
51 EVENT_ERROR,
54 static const uint32_t kBufferSize;
56 SerialConnectionTest()
57 : connected_(false),
58 success_(false),
59 bytes_sent_(0),
60 send_error_(serial::SEND_ERROR_NONE),
61 receive_error_(serial::RECEIVE_ERROR_NONE),
62 expected_event_(EVENT_NONE) {}
64 void SetUp() override {
65 message_loop_.reset(new base::MessageLoop);
66 mojo::InterfacePtr<serial::SerialService> service;
67 new SerialServiceImpl(
68 new SerialConnectionFactory(
69 base::Bind(&SerialConnectionTest::CreateIoHandler,
70 base::Unretained(this)),
71 base::ThreadTaskRunnerHandle::Get()),
72 scoped_ptr<SerialDeviceEnumerator>(new FakeSerialDeviceEnumerator),
73 mojo::GetProxy(&service));
74 service.set_connection_error_handler(base::Bind(
75 &SerialConnectionTest::OnConnectionError, base::Unretained(this)));
76 mojo::InterfacePtr<serial::DataSink> sink;
77 mojo::InterfacePtr<serial::DataSource> source;
78 mojo::InterfacePtr<serial::DataSourceClient> source_client;
79 mojo::InterfaceRequest<serial::DataSourceClient> source_client_request =
80 mojo::GetProxy(&source_client);
81 service->Connect("device", serial::ConnectionOptions::New(),
82 mojo::GetProxy(&connection_), mojo::GetProxy(&sink),
83 mojo::GetProxy(&source), source_client.Pass());
84 sender_.reset(new DataSender(sink.Pass(), kBufferSize,
85 serial::SEND_ERROR_DISCONNECTED));
86 receiver_ =
87 new DataReceiver(source.Pass(), source_client_request.Pass(),
88 kBufferSize, serial::RECEIVE_ERROR_DISCONNECTED);
89 connection_.set_connection_error_handler(base::Bind(
90 &SerialConnectionTest::OnConnectionError, base::Unretained(this)));
91 connection_->GetInfo(
92 base::Bind(&SerialConnectionTest::StoreInfo, base::Unretained(this)));
93 WaitForEvent(EVENT_GOT_INFO);
94 ASSERT_TRUE(io_handler_.get());
97 void StoreInfo(serial::ConnectionInfoPtr options) {
98 info_ = options.Pass();
99 EventReceived(EVENT_GOT_INFO);
102 void StoreControlSignals(serial::DeviceControlSignalsPtr signals) {
103 signals_ = signals.Pass();
104 EventReceived(EVENT_GOT_CONTROL_SIGNALS);
107 void StoreSuccess(Event event_to_report, bool success) {
108 success_ = success;
109 EventReceived(event_to_report);
112 void Send(const base::StringPiece& data) {
113 ASSERT_TRUE(sender_->Send(
114 data,
115 base::Bind(&SerialConnectionTest::OnDataSent, base::Unretained(this)),
116 base::Bind(&SerialConnectionTest::OnSendError,
117 base::Unretained(this))));
120 void Receive() {
121 ASSERT_TRUE(
122 receiver_->Receive(base::Bind(&SerialConnectionTest::OnDataReceived,
123 base::Unretained(this)),
124 base::Bind(&SerialConnectionTest::OnReceiveError,
125 base::Unretained(this))));
128 void WaitForEvent(Event event) {
129 expected_event_ = event;
130 base::RunLoop run_loop;
131 stop_run_loop_ = run_loop.QuitClosure();
132 run_loop.Run();
135 void EventReceived(Event event) {
136 if (event != expected_event_)
137 return;
138 expected_event_ = EVENT_NONE;
139 ASSERT_TRUE(message_loop_);
140 ASSERT_TRUE(!stop_run_loop_.is_null());
141 message_loop_->PostTask(FROM_HERE, stop_run_loop_);
144 scoped_refptr<SerialIoHandler> CreateIoHandler() {
145 io_handler_ = new TestSerialIoHandler;
146 return io_handler_;
149 void OnDataSent(uint32_t bytes_sent) {
150 bytes_sent_ += bytes_sent;
151 send_error_ = serial::SEND_ERROR_NONE;
152 EventReceived(EVENT_DATA_SENT);
155 void OnSendError(uint32_t bytes_sent, int32_t error) {
156 bytes_sent_ += bytes_sent;
157 send_error_ = static_cast<serial::SendError>(error);
158 EventReceived(EVENT_SEND_ERROR);
161 void OnDataReceived(scoped_ptr<ReadOnlyBuffer> buffer) {
162 data_received_ += std::string(buffer->GetData(), buffer->GetSize());
163 buffer->Done(buffer->GetSize());
164 receive_error_ = serial::RECEIVE_ERROR_NONE;
165 EventReceived(EVENT_DATA_RECEIVED);
168 void OnReceiveError(int32_t error) {
169 receive_error_ = static_cast<serial::ReceiveError>(error);
170 EventReceived(EVENT_RECEIVE_ERROR);
173 void OnConnectionError() {
174 EventReceived(EVENT_ERROR);
175 FAIL() << "Connection error";
178 mojo::Array<serial::DeviceInfoPtr> devices_;
179 serial::ConnectionInfoPtr info_;
180 serial::DeviceControlSignalsPtr signals_;
181 bool connected_;
182 bool success_;
183 int bytes_sent_;
184 serial::SendError send_error_;
185 serial::ReceiveError receive_error_;
186 std::string data_received_;
187 Event expected_event_;
189 scoped_ptr<base::MessageLoop> message_loop_;
190 base::Closure stop_run_loop_;
191 mojo::InterfacePtr<serial::Connection> connection_;
192 scoped_ptr<DataSender> sender_;
193 scoped_refptr<DataReceiver> receiver_;
194 scoped_refptr<TestSerialIoHandler> io_handler_;
196 private:
197 DISALLOW_COPY_AND_ASSIGN(SerialConnectionTest);
200 const uint32_t SerialConnectionTest::kBufferSize = 10;
202 TEST_F(SerialConnectionTest, GetInfo) {
203 // |info_| is filled in during SetUp().
204 ASSERT_TRUE(info_);
205 EXPECT_EQ(9600u, info_->bitrate);
206 EXPECT_EQ(serial::DATA_BITS_EIGHT, info_->data_bits);
207 EXPECT_EQ(serial::PARITY_BIT_NO, info_->parity_bit);
208 EXPECT_EQ(serial::STOP_BITS_ONE, info_->stop_bits);
209 EXPECT_FALSE(info_->cts_flow_control);
212 TEST_F(SerialConnectionTest, SetOptions) {
213 serial::ConnectionOptionsPtr options(serial::ConnectionOptions::New());
214 options->bitrate = 12345;
215 options->data_bits = serial::DATA_BITS_SEVEN;
216 options->has_cts_flow_control = true;
217 options->cts_flow_control = true;
218 connection_->SetOptions(options.Pass(),
219 base::Bind(&SerialConnectionTest::StoreSuccess,
220 base::Unretained(this),
221 EVENT_SET_OPTIONS));
222 WaitForEvent(EVENT_SET_OPTIONS);
223 ASSERT_TRUE(success_);
224 serial::ConnectionInfo* info = io_handler_->connection_info();
225 EXPECT_EQ(12345u, info->bitrate);
226 EXPECT_EQ(serial::DATA_BITS_SEVEN, info->data_bits);
227 EXPECT_EQ(serial::PARITY_BIT_NO, info->parity_bit);
228 EXPECT_EQ(serial::STOP_BITS_ONE, info->stop_bits);
229 EXPECT_TRUE(info->cts_flow_control);
232 TEST_F(SerialConnectionTest, GetControlSignals) {
233 connection_->GetControlSignals(base::Bind(
234 &SerialConnectionTest::StoreControlSignals, base::Unretained(this)));
235 serial::DeviceControlSignals* signals = io_handler_->device_control_signals();
236 signals->dcd = true;
237 signals->dsr = true;
239 WaitForEvent(EVENT_GOT_CONTROL_SIGNALS);
240 ASSERT_TRUE(signals_);
241 EXPECT_TRUE(signals_->dcd);
242 EXPECT_FALSE(signals_->cts);
243 EXPECT_FALSE(signals_->ri);
244 EXPECT_TRUE(signals_->dsr);
247 TEST_F(SerialConnectionTest, SetControlSignals) {
248 serial::HostControlSignalsPtr signals(serial::HostControlSignals::New());
249 signals->has_dtr = true;
250 signals->dtr = true;
251 signals->has_rts = true;
252 signals->rts = true;
254 connection_->SetControlSignals(signals.Pass(),
255 base::Bind(&SerialConnectionTest::StoreSuccess,
256 base::Unretained(this),
257 EVENT_SET_CONTROL_SIGNALS));
258 WaitForEvent(EVENT_SET_CONTROL_SIGNALS);
259 ASSERT_TRUE(success_);
260 EXPECT_TRUE(io_handler_->dtr());
261 EXPECT_TRUE(io_handler_->rts());
264 TEST_F(SerialConnectionTest, Flush) {
265 ASSERT_EQ(0, io_handler_->flushes());
266 connection_->Flush(base::Bind(&SerialConnectionTest::StoreSuccess,
267 base::Unretained(this),
268 EVENT_FLUSHED));
269 WaitForEvent(EVENT_FLUSHED);
270 ASSERT_TRUE(success_);
271 EXPECT_EQ(1, io_handler_->flushes());
274 TEST_F(SerialConnectionTest, DisconnectWithSend) {
275 connection_.reset();
276 io_handler_->set_send_callback(base::Bind(base::DoNothing));
277 ASSERT_NO_FATAL_FAILURE(Send("data"));
278 WaitForEvent(EVENT_SEND_ERROR);
279 EXPECT_EQ(serial::SEND_ERROR_DISCONNECTED, send_error_);
280 EXPECT_EQ(0, bytes_sent_);
281 EXPECT_TRUE(io_handler_->HasOneRef());
284 TEST_F(SerialConnectionTest, DisconnectWithReceive) {
285 connection_.reset();
286 ASSERT_NO_FATAL_FAILURE(Receive());
287 WaitForEvent(EVENT_RECEIVE_ERROR);
288 EXPECT_EQ(serial::RECEIVE_ERROR_DISCONNECTED, receive_error_);
289 EXPECT_EQ("", data_received_);
290 EXPECT_TRUE(io_handler_->HasOneRef());
293 TEST_F(SerialConnectionTest, Echo) {
294 ASSERT_NO_FATAL_FAILURE(Send("data"));
295 WaitForEvent(EVENT_DATA_SENT);
296 EXPECT_EQ(serial::SEND_ERROR_NONE, send_error_);
297 EXPECT_EQ(4, bytes_sent_);
298 ASSERT_NO_FATAL_FAILURE(Receive());
299 WaitForEvent(EVENT_DATA_RECEIVED);
300 EXPECT_EQ("data", data_received_);
301 EXPECT_EQ(serial::RECEIVE_ERROR_NONE, receive_error_);
304 TEST_F(SerialConnectionTest, Cancel) {
305 // To test that cancels are correctly passed to the IoHandler, we need a send
306 // to be in progress because otherwise, the DataSinkReceiver would handle the
307 // cancel internally.
308 io_handler_->set_send_callback(
309 base::Bind(&SerialConnectionTest::EventReceived,
310 base::Unretained(this),
311 EVENT_DATA_AT_IO_HANDLER));
312 ASSERT_NO_FATAL_FAILURE(Send("something else"));
313 WaitForEvent(EVENT_DATA_AT_IO_HANDLER);
314 EXPECT_EQ(0, bytes_sent_);
316 ASSERT_TRUE(sender_->Cancel(serial::SEND_ERROR_TIMEOUT,
317 base::Bind(&SerialConnectionTest::EventReceived,
318 base::Unretained(this),
319 EVENT_CANCEL_COMPLETE)));
321 WaitForEvent(EVENT_CANCEL_COMPLETE);
322 EXPECT_EQ(serial::SEND_ERROR_TIMEOUT, send_error_);
324 ASSERT_NO_FATAL_FAILURE(Send("data"));
325 WaitForEvent(EVENT_DATA_SENT);
326 EXPECT_EQ(serial::SEND_ERROR_NONE, send_error_);
327 EXPECT_EQ(4, bytes_sent_);
328 ASSERT_NO_FATAL_FAILURE(Receive());
329 WaitForEvent(EVENT_DATA_RECEIVED);
330 EXPECT_EQ("data", data_received_);
331 EXPECT_EQ(serial::RECEIVE_ERROR_NONE, receive_error_);
334 } // namespace device