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.
6 #include "base/message_loop/message_loop.h"
7 #include "base/run_loop.h"
8 #include "base/strings/string_piece.h"
9 #include "device/serial/buffer.h"
10 #include "device/serial/data_receiver.h"
11 #include "device/serial/data_source_sender.h"
12 #include "device/serial/data_stream.mojom.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_ptr.h"
15 #include "third_party/mojo/src/mojo/public/cpp/environment/async_waiter.h"
19 class DataSourceTest
: public testing::Test
{
23 EVENT_WRITE_BUFFER_READY
,
24 EVENT_RECEIVE_COMPLETE
,
29 : error_(0), seen_connection_error_(false), expected_event_(EVENT_NONE
) {}
31 void SetUp() override
{
32 message_loop_
.reset(new base::MessageLoop
);
33 mojo::InterfacePtr
<serial::DataSource
> source_sender_handle
;
34 mojo::InterfacePtr
<serial::DataSourceClient
> source_sender_client_handle
;
35 mojo::InterfaceRequest
<serial::DataSourceClient
>
36 source_sender_client_request
=
37 mojo::GetProxy(&source_sender_client_handle
);
38 source_sender_
= new DataSourceSender(
39 mojo::GetProxy(&source_sender_handle
),
40 source_sender_client_handle
.Pass(),
41 base::Bind(&DataSourceTest::CanWriteData
, base::Unretained(this)),
42 base::Bind(&DataSourceTest::OnError
, base::Unretained(this)));
44 new DataReceiver(source_sender_handle
.Pass(),
45 source_sender_client_request
.Pass(), 100, kFatalError
);
48 void TearDown() override
{
49 write_buffer_
.reset();
51 message_loop_
.reset();
55 seen_connection_error_
= true;
56 EventReceived(EVENT_ERROR
);
59 void WaitForEvent(Event event
) {
60 expected_event_
= event
;
61 base::RunLoop run_loop
;
62 stop_run_loop_
= run_loop
.QuitClosure();
66 void EventReceived(Event event
) {
67 if (event
== expected_event_
&& !stop_run_loop_
.is_null())
72 return receiver_
->Receive(
73 base::Bind(&DataSourceTest::OnDataReceived
, base::Unretained(this)),
74 base::Bind(&DataSourceTest::OnReceiveError
, base::Unretained(this)));
77 void FillWriteBuffer(const base::StringPiece
& data
, int32_t error
) {
79 // Run the message loop until CanWriteData is called.
80 WaitForEvent(EVENT_WRITE_BUFFER_READY
);
82 ASSERT_TRUE(write_buffer_
);
83 ASSERT_GE(write_buffer_
->GetSize(), static_cast<uint32_t>(data
.size()));
84 memcpy(write_buffer_
->GetData(), data
.data(), data
.size());
86 write_buffer_
->DoneWithError(static_cast<uint32_t>(data
.size()), error
);
88 write_buffer_
->Done(static_cast<uint32_t>(data
.size()));
89 write_buffer_
.reset();
92 void ReceiveAndWait() {
93 ASSERT_TRUE(Receive());
94 // Run the message loop until OnDataReceived or OnReceiveError is called.
95 WaitForEvent(EVENT_RECEIVE_COMPLETE
);
98 void OnDataReceived(scoped_ptr
<ReadOnlyBuffer
> buffer
) {
101 buffer_
= buffer
.Pass();
102 buffer_contents_
= std::string(buffer_
->GetData(), buffer_
->GetSize());
103 EventReceived(EVENT_RECEIVE_COMPLETE
);
106 void OnReceiveError(int32_t error
) {
107 buffer_contents_
.clear();
109 EventReceived(EVENT_RECEIVE_COMPLETE
);
112 void CanWriteData(scoped_ptr
<WritableBuffer
> buffer
) {
113 write_buffer_
= buffer
.Pass();
114 EventReceived(EVENT_WRITE_BUFFER_READY
);
118 static const int32_t kFatalError
;
119 scoped_ptr
<base::MessageLoop
> message_loop_
;
120 base::Closure stop_run_loop_
;
122 scoped_refptr
<DataSourceSender
> source_sender_
;
123 scoped_refptr
<DataReceiver
> receiver_
;
125 scoped_ptr
<ReadOnlyBuffer
> buffer_
;
126 std::string buffer_contents_
;
128 scoped_ptr
<WritableBuffer
> write_buffer_
;
130 bool seen_connection_error_
;
132 Event expected_event_
;
135 DISALLOW_COPY_AND_ASSIGN(DataSourceTest
);
138 const int32_t DataSourceTest::kFatalError
= -10;
140 // Test that data is successfully transmitted from the source to the receiver.
141 TEST_F(DataSourceTest
, Basic
) {
142 ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("a", 0));
144 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
145 EXPECT_EQ(0, error_
);
146 ASSERT_TRUE(buffer_
);
147 EXPECT_EQ("a", buffer_contents_
);
150 ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("b", 0));
152 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
153 EXPECT_EQ(0, error_
);
154 ASSERT_TRUE(buffer_
);
155 EXPECT_EQ("b", buffer_contents_
);
158 // Test that the receiver does not discard any data that is not read by the
160 TEST_F(DataSourceTest
, PartialReceive
) {
161 ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("ab", 0));
163 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
164 EXPECT_EQ(0, error_
);
165 ASSERT_TRUE(buffer_
);
166 EXPECT_EQ("ab", buffer_contents_
);
169 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
170 EXPECT_EQ(0, error_
);
171 ASSERT_TRUE(buffer_
);
172 EXPECT_EQ("b", buffer_contents_
);
175 // Test that an error is correctly reported to the Receive() call immediately
176 // after the data has been read by the client.
177 TEST_F(DataSourceTest
, ErrorAndData
) {
178 ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("abc", -1));
180 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
181 ASSERT_TRUE(buffer_
);
182 EXPECT_EQ("abc", buffer_contents_
);
184 EXPECT_EQ(0, error_
);
185 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
186 ASSERT_TRUE(buffer_
);
187 EXPECT_EQ("bc", buffer_contents_
);
189 EXPECT_EQ(0, error_
);
190 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
191 ASSERT_TRUE(buffer_
);
192 EXPECT_EQ("c", buffer_contents_
);
194 EXPECT_EQ(0, error_
);
195 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
196 EXPECT_EQ(-1, error_
);
197 ASSERT_FALSE(write_buffer_
);
199 ASSERT_TRUE(Receive());
200 ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("d", 0));
202 WaitForEvent(EVENT_RECEIVE_COMPLETE
);
203 ASSERT_TRUE(buffer_
);
204 EXPECT_EQ("d", buffer_contents_
);
206 EXPECT_EQ(0, error_
);
208 ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("e", -2));
209 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
210 ASSERT_TRUE(buffer_
);
211 EXPECT_EQ("e", buffer_contents_
);
213 EXPECT_EQ(0, error_
);
215 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
216 EXPECT_EQ(-2, error_
);
219 // Test that an error is correctly reported when the source encounters an error
220 // without sending any data.
221 TEST_F(DataSourceTest
, ErrorOnly
) {
222 ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("", -1));
224 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
225 EXPECT_FALSE(buffer_
);
226 EXPECT_EQ(-1, error_
);
227 ASSERT_FALSE(write_buffer_
);
229 ASSERT_TRUE(Receive());
230 ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("", -2));
232 WaitForEvent(EVENT_RECEIVE_COMPLETE
);
233 EXPECT_FALSE(buffer_
);
234 EXPECT_EQ(-2, error_
);
235 ASSERT_FALSE(write_buffer_
);
238 // Test that the source shutting down is correctly reported to the client.
239 TEST_F(DataSourceTest
, SourceShutdown
) {
240 source_sender_
->ShutDown();
241 source_sender_
= NULL
;
242 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
243 EXPECT_FALSE(buffer_
);
244 EXPECT_EQ(kFatalError
, error_
);
245 ASSERT_FALSE(write_buffer_
);
246 ASSERT_FALSE(Receive());
249 // Test that the receiver shutting down is correctly reported to the source.
250 TEST_F(DataSourceTest
, ReceiverShutdown
) {
253 EXPECT_EQ(kFatalError
, error_
);
254 WaitForEvent(EVENT_ERROR
);
255 EXPECT_TRUE(seen_connection_error_
);
258 } // namespace device