Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / device / serial / data_source_unittest.cc
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.
4
5 #include "base/bind.h"
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"
16
17 namespace device {
18
19 class DataSourceTest : public testing::Test {
20  public:
21   enum Event {
22     EVENT_NONE,
23     EVENT_WRITE_BUFFER_READY,
24     EVENT_RECEIVE_COMPLETE,
25     EVENT_ERROR,
26   };
27
28   DataSourceTest()
29       : error_(0), seen_connection_error_(false), expected_event_(EVENT_NONE) {}
30
31   void SetUp() override {
32     message_loop_.reset(new base::MessageLoop);
33     mojo::InterfacePtr<serial::DataSource> source_sender_handle;
34     source_sender_ = mojo::WeakBindToProxy(
35         new DataSourceSender(
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);
40   }
41
42   void TearDown() override {
43     write_buffer_.reset();
44     buffer_.reset();
45     message_loop_.reset();
46   }
47
48   void OnError() {
49     seen_connection_error_ = true;
50     EventReceived(EVENT_ERROR);
51   }
52
53   void WaitForEvent(Event event) {
54     expected_event_ = event;
55     base::RunLoop run_loop;
56     stop_run_loop_ = run_loop.QuitClosure();
57     run_loop.Run();
58   }
59
60   void EventReceived(Event event) {
61     if (event == expected_event_ && !stop_run_loop_.is_null())
62       stop_run_loop_.Run();
63   }
64
65   bool Receive() {
66     return receiver_->Receive(
67         base::Bind(&DataSourceTest::OnDataReceived, base::Unretained(this)),
68         base::Bind(&DataSourceTest::OnReceiveError, base::Unretained(this)));
69   }
70
71   void FillWriteBuffer(const base::StringPiece& data, int32_t error) {
72     if (!write_buffer_) {
73       // Run the message loop until CanWriteData is called.
74       WaitForEvent(EVENT_WRITE_BUFFER_READY);
75     }
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());
79     if (error)
80       write_buffer_->DoneWithError(static_cast<uint32_t>(data.size()), error);
81     else
82       write_buffer_->Done(static_cast<uint32_t>(data.size()));
83     write_buffer_.reset();
84   }
85
86   void ReceiveAndWait() {
87     ASSERT_TRUE(Receive());
88     // Run the message loop until OnDataReceived or OnReceiveError is called.
89     WaitForEvent(EVENT_RECEIVE_COMPLETE);
90   }
91
92   void OnDataReceived(scoped_ptr<ReadOnlyBuffer> buffer) {
93     ASSERT_TRUE(buffer);
94     error_ = 0;
95     buffer_ = buffer.Pass();
96     buffer_contents_ = std::string(buffer_->GetData(), buffer_->GetSize());
97     EventReceived(EVENT_RECEIVE_COMPLETE);
98   }
99
100   void OnReceiveError(int32_t error) {
101     buffer_contents_.clear();
102     error_ = error;
103     EventReceived(EVENT_RECEIVE_COMPLETE);
104   }
105
106   void CanWriteData(scoped_ptr<WritableBuffer> buffer) {
107     write_buffer_ = buffer.Pass();
108     EventReceived(EVENT_WRITE_BUFFER_READY);
109   }
110
111  protected:
112   static const int32_t kFatalError;
113   scoped_ptr<base::MessageLoop> message_loop_;
114   base::Closure stop_run_loop_;
115
116   scoped_refptr<DataSourceSender> source_sender_;
117   scoped_refptr<DataReceiver> receiver_;
118
119   scoped_ptr<ReadOnlyBuffer> buffer_;
120   std::string buffer_contents_;
121   int32_t error_;
122   scoped_ptr<WritableBuffer> write_buffer_;
123
124   bool seen_connection_error_;
125
126   Event expected_event_;
127
128  private:
129   DISALLOW_COPY_AND_ASSIGN(DataSourceTest);
130 };
131
132 const int32_t DataSourceTest::kFatalError = -10;
133
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));
137
138   ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
139   EXPECT_EQ(0, error_);
140   ASSERT_TRUE(buffer_);
141   EXPECT_EQ("a", buffer_contents_);
142   buffer_->Done(1);
143
144   ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("b", 0));
145
146   ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
147   EXPECT_EQ(0, error_);
148   ASSERT_TRUE(buffer_);
149   EXPECT_EQ("b", buffer_contents_);
150 }
151
152 // Test that the receiver does not discard any data that is not read by the
153 // client.
154 TEST_F(DataSourceTest, PartialReceive) {
155   ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("ab", 0));
156
157   ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
158   EXPECT_EQ(0, error_);
159   ASSERT_TRUE(buffer_);
160   EXPECT_EQ("ab", buffer_contents_);
161   buffer_->Done(1);
162
163   ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
164   EXPECT_EQ(0, error_);
165   ASSERT_TRUE(buffer_);
166   EXPECT_EQ("b", buffer_contents_);
167 }
168
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));
173
174   ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
175   ASSERT_TRUE(buffer_);
176   EXPECT_EQ("abc", buffer_contents_);
177   buffer_->Done(1);
178   EXPECT_EQ(0, error_);
179   ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
180   ASSERT_TRUE(buffer_);
181   EXPECT_EQ("bc", buffer_contents_);
182   buffer_->Done(1);
183   EXPECT_EQ(0, error_);
184   ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
185   ASSERT_TRUE(buffer_);
186   EXPECT_EQ("c", buffer_contents_);
187   buffer_->Done(1);
188   EXPECT_EQ(0, error_);
189   ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
190   EXPECT_EQ(-1, error_);
191   ASSERT_FALSE(write_buffer_);
192
193   ASSERT_TRUE(Receive());
194   ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("d", 0));
195
196   WaitForEvent(EVENT_RECEIVE_COMPLETE);
197   ASSERT_TRUE(buffer_);
198   EXPECT_EQ("d", buffer_contents_);
199   buffer_->Done(1);
200   EXPECT_EQ(0, error_);
201
202   ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("e", -2));
203   ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
204   ASSERT_TRUE(buffer_);
205   EXPECT_EQ("e", buffer_contents_);
206   buffer_->Done(1);
207   EXPECT_EQ(0, error_);
208
209   ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
210   EXPECT_EQ(-2, error_);
211 }
212
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));
217
218   ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
219   EXPECT_FALSE(buffer_);
220   EXPECT_EQ(-1, error_);
221   ASSERT_FALSE(write_buffer_);
222
223   ASSERT_TRUE(Receive());
224   ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("", -2));
225
226   WaitForEvent(EVENT_RECEIVE_COMPLETE);
227   EXPECT_FALSE(buffer_);
228   EXPECT_EQ(-2, error_);
229   ASSERT_FALSE(write_buffer_);
230 }
231
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());
241 }
242
243 // Test that the receiver shutting down is correctly reported to the source.
244 TEST_F(DataSourceTest, ReceiverShutdown) {
245   Receive();
246   receiver_ = NULL;
247   EXPECT_EQ(kFatalError, error_);
248   WaitForEvent(EVENT_ERROR);
249   EXPECT_TRUE(seen_connection_error_);
250 }
251
252 }  // namespace device