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/async_waiter.h"
10 #include "device/serial/buffer.h"
11 #include "device/serial/data_receiver.h"
12 #include "device/serial/data_source_sender.h"
13 #include "device/serial/data_stream.mojom.h"
14 #include "mojo/public/cpp/bindings/interface_ptr.h"
15 #include "testing/gtest/include/gtest/gtest.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 virtual void SetUp() OVERRIDE
{
32 message_loop_
.reset(new base::MessageLoop
);
33 mojo::InterfacePtr
<serial::DataSource
> source_sender_handle
;
34 source_sender_
= mojo::WeakBindToProxy(
36 base::Bind(&DataSourceTest::CanWriteData
, base::Unretained(this)),
37 base::Bind(&DataSourceTest::OnError
, base::Unretained(this))),
38 &source_sender_handle
);
39 receiver_
= new DataReceiver(source_sender_handle
.Pass(), 100, kFatalError
);
42 virtual void TearDown() OVERRIDE
{
43 write_buffer_
.reset();
45 message_loop_
.reset();
49 seen_connection_error_
= true;
50 EventReceived(EVENT_ERROR
);
53 void WaitForEvent(Event event
) {
54 expected_event_
= event
;
55 base::RunLoop run_loop
;
56 stop_run_loop_
= run_loop
.QuitClosure();
60 void EventReceived(Event event
) {
61 if (event
== expected_event_
&& !stop_run_loop_
.is_null())
66 return receiver_
->Receive(
67 base::Bind(&DataSourceTest::OnDataReceived
, base::Unretained(this)),
68 base::Bind(&DataSourceTest::OnReceiveError
, base::Unretained(this)));
71 void FillWriteBuffer(const base::StringPiece
& data
, int32_t error
) {
73 // Run the message loop until CanWriteData is called.
74 WaitForEvent(EVENT_WRITE_BUFFER_READY
);
76 ASSERT_TRUE(write_buffer_
);
77 ASSERT_GE(write_buffer_
->GetSize(), static_cast<uint32_t>(data
.size()));
78 memcpy(write_buffer_
->GetData(), data
.data(), data
.size());
80 write_buffer_
->DoneWithError(static_cast<uint32_t>(data
.size()), error
);
82 write_buffer_
->Done(static_cast<uint32_t>(data
.size()));
83 write_buffer_
.reset();
86 void ReceiveAndWait() {
87 ASSERT_TRUE(Receive());
88 // Run the message loop until OnDataReceived or OnReceiveError is called.
89 WaitForEvent(EVENT_RECEIVE_COMPLETE
);
92 void OnDataReceived(scoped_ptr
<ReadOnlyBuffer
> buffer
) {
95 buffer_
= buffer
.Pass();
96 buffer_contents_
= std::string(buffer_
->GetData(), buffer_
->GetSize());
97 EventReceived(EVENT_RECEIVE_COMPLETE
);
100 void OnReceiveError(int32_t error
) {
101 buffer_contents_
.clear();
103 EventReceived(EVENT_RECEIVE_COMPLETE
);
106 void CanWriteData(scoped_ptr
<WritableBuffer
> buffer
) {
107 write_buffer_
= buffer
.Pass();
108 EventReceived(EVENT_WRITE_BUFFER_READY
);
112 static const int32_t kFatalError
;
113 scoped_ptr
<base::MessageLoop
> message_loop_
;
114 base::Closure stop_run_loop_
;
116 scoped_refptr
<DataSourceSender
> source_sender_
;
117 scoped_refptr
<DataReceiver
> receiver_
;
119 scoped_ptr
<ReadOnlyBuffer
> buffer_
;
120 std::string buffer_contents_
;
122 scoped_ptr
<WritableBuffer
> write_buffer_
;
124 bool seen_connection_error_
;
126 Event expected_event_
;
129 DISALLOW_COPY_AND_ASSIGN(DataSourceTest
);
132 const int32_t DataSourceTest::kFatalError
= -10;
134 // Test that data is successfully transmitted from the source to the receiver.
135 TEST_F(DataSourceTest
, Basic
) {
136 ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("a", 0));
138 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
139 EXPECT_EQ(0, error_
);
140 ASSERT_TRUE(buffer_
);
141 EXPECT_EQ("a", buffer_contents_
);
144 ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("b", 0));
146 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
147 EXPECT_EQ(0, error_
);
148 ASSERT_TRUE(buffer_
);
149 EXPECT_EQ("b", buffer_contents_
);
152 // Test that the receiver does not discard any data that is not read by the
154 TEST_F(DataSourceTest
, PartialReceive
) {
155 ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("ab", 0));
157 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
158 EXPECT_EQ(0, error_
);
159 ASSERT_TRUE(buffer_
);
160 EXPECT_EQ("ab", buffer_contents_
);
163 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
164 EXPECT_EQ(0, error_
);
165 ASSERT_TRUE(buffer_
);
166 EXPECT_EQ("b", buffer_contents_
);
169 // Test that an error is correctly reported to the Receive() call immediately
170 // after the data has been read by the client.
171 TEST_F(DataSourceTest
, ErrorAndData
) {
172 ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("abc", -1));
174 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
175 ASSERT_TRUE(buffer_
);
176 EXPECT_EQ("abc", buffer_contents_
);
178 EXPECT_EQ(0, error_
);
179 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
180 ASSERT_TRUE(buffer_
);
181 EXPECT_EQ("bc", buffer_contents_
);
183 EXPECT_EQ(0, error_
);
184 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
185 ASSERT_TRUE(buffer_
);
186 EXPECT_EQ("c", buffer_contents_
);
188 EXPECT_EQ(0, error_
);
189 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
190 EXPECT_EQ(-1, error_
);
191 ASSERT_FALSE(write_buffer_
);
193 ASSERT_TRUE(Receive());
194 ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("d", 0));
196 WaitForEvent(EVENT_RECEIVE_COMPLETE
);
197 ASSERT_TRUE(buffer_
);
198 EXPECT_EQ("d", buffer_contents_
);
200 EXPECT_EQ(0, error_
);
202 ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("e", -2));
203 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
204 ASSERT_TRUE(buffer_
);
205 EXPECT_EQ("e", buffer_contents_
);
207 EXPECT_EQ(0, error_
);
209 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
210 EXPECT_EQ(-2, error_
);
213 // Test that an error is correctly reported when the source encounters an error
214 // without sending any data.
215 TEST_F(DataSourceTest
, ErrorOnly
) {
216 ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("", -1));
218 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
219 EXPECT_FALSE(buffer_
);
220 EXPECT_EQ(-1, error_
);
221 ASSERT_FALSE(write_buffer_
);
223 ASSERT_TRUE(Receive());
224 ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("", -2));
226 WaitForEvent(EVENT_RECEIVE_COMPLETE
);
227 EXPECT_FALSE(buffer_
);
228 EXPECT_EQ(-2, error_
);
229 ASSERT_FALSE(write_buffer_
);
232 // Test that the source shutting down is correctly reported to the client.
233 TEST_F(DataSourceTest
, SourceShutdown
) {
234 source_sender_
->ShutDown();
235 source_sender_
= NULL
;
236 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
237 EXPECT_FALSE(buffer_
);
238 EXPECT_EQ(kFatalError
, error_
);
239 ASSERT_FALSE(write_buffer_
);
240 ASSERT_FALSE(Receive());
243 // Test that the receiver shutting down is correctly reported to the source.
244 TEST_F(DataSourceTest
, ReceiverShutdown
) {
247 EXPECT_EQ(kFatalError
, error_
);
248 WaitForEvent(EVENT_ERROR
);
249 EXPECT_TRUE(seen_connection_error_
);
252 } // namespace device