Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(305)

Side by Side Diff: device/serial/data_source_unittest.cc

Issue 2410743002: Remove the mojo serial interfaces and related infrastructure. (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « device/serial/data_source_sender.cc ('k') | device/serial/data_stream.mojom » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 <stdint.h>
6
7 #include <memory>
8 #include <utility>
9
10 #include "base/bind.h"
11 #include "base/macros.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h"
14 #include "base/strings/string_piece.h"
15 #include "device/serial/buffer.h"
16 #include "device/serial/data_receiver.h"
17 #include "device/serial/data_source_sender.h"
18 #include "device/serial/data_stream.mojom.h"
19 #include "mojo/public/cpp/bindings/interface_ptr.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 namespace device {
23
24 class DataSourceTest : public testing::Test {
25 public:
26 enum Event {
27 EVENT_NONE,
28 EVENT_WRITE_BUFFER_READY,
29 EVENT_RECEIVE_COMPLETE,
30 EVENT_ERROR,
31 };
32
33 DataSourceTest()
34 : error_(0), seen_connection_error_(false), expected_event_(EVENT_NONE) {}
35
36 void SetUp() override {
37 message_loop_.reset(new base::MessageLoop);
38 mojo::InterfacePtr<serial::DataSource> source_sender_handle;
39 mojo::InterfacePtr<serial::DataSourceClient> source_sender_client_handle;
40 mojo::InterfaceRequest<serial::DataSourceClient>
41 source_sender_client_request =
42 mojo::GetProxy(&source_sender_client_handle);
43 source_sender_ = new DataSourceSender(
44 mojo::GetProxy(&source_sender_handle),
45 std::move(source_sender_client_handle),
46 base::Bind(&DataSourceTest::CanWriteData, base::Unretained(this)),
47 base::Bind(&DataSourceTest::OnError, base::Unretained(this)));
48 receiver_ = new DataReceiver(std::move(source_sender_handle),
49 std::move(source_sender_client_request), 100,
50 kFatalError);
51 }
52
53 void TearDown() override {
54 write_buffer_.reset();
55 buffer_.reset();
56 message_loop_.reset();
57 }
58
59 void OnError() {
60 seen_connection_error_ = true;
61 EventReceived(EVENT_ERROR);
62 }
63
64 void WaitForEvent(Event event) {
65 expected_event_ = event;
66 base::RunLoop run_loop;
67 stop_run_loop_ = run_loop.QuitClosure();
68 run_loop.Run();
69 }
70
71 void EventReceived(Event event) {
72 if (event == expected_event_ && !stop_run_loop_.is_null())
73 stop_run_loop_.Run();
74 }
75
76 bool Receive() {
77 return receiver_->Receive(
78 base::Bind(&DataSourceTest::OnDataReceived, base::Unretained(this)),
79 base::Bind(&DataSourceTest::OnReceiveError, base::Unretained(this)));
80 }
81
82 void FillWriteBuffer(const base::StringPiece& data, int32_t error) {
83 if (!write_buffer_) {
84 // Run the message loop until CanWriteData is called.
85 WaitForEvent(EVENT_WRITE_BUFFER_READY);
86 }
87 ASSERT_TRUE(write_buffer_);
88 ASSERT_GE(write_buffer_->GetSize(), static_cast<uint32_t>(data.size()));
89 memcpy(write_buffer_->GetData(), data.data(), data.size());
90 if (error)
91 write_buffer_->DoneWithError(static_cast<uint32_t>(data.size()), error);
92 else
93 write_buffer_->Done(static_cast<uint32_t>(data.size()));
94 write_buffer_.reset();
95 }
96
97 void ReceiveAndWait() {
98 ASSERT_TRUE(Receive());
99 // Run the message loop until OnDataReceived or OnReceiveError is called.
100 WaitForEvent(EVENT_RECEIVE_COMPLETE);
101 }
102
103 void OnDataReceived(std::unique_ptr<ReadOnlyBuffer> buffer) {
104 ASSERT_TRUE(buffer);
105 error_ = 0;
106 buffer_ = std::move(buffer);
107 buffer_contents_ = std::string(buffer_->GetData(), buffer_->GetSize());
108 EventReceived(EVENT_RECEIVE_COMPLETE);
109 }
110
111 void OnReceiveError(int32_t error) {
112 buffer_contents_.clear();
113 error_ = error;
114 EventReceived(EVENT_RECEIVE_COMPLETE);
115 }
116
117 void CanWriteData(std::unique_ptr<WritableBuffer> buffer) {
118 write_buffer_ = std::move(buffer);
119 EventReceived(EVENT_WRITE_BUFFER_READY);
120 }
121
122 protected:
123 static const int32_t kFatalError;
124 std::unique_ptr<base::MessageLoop> message_loop_;
125 base::Closure stop_run_loop_;
126
127 scoped_refptr<DataSourceSender> source_sender_;
128 scoped_refptr<DataReceiver> receiver_;
129
130 std::unique_ptr<ReadOnlyBuffer> buffer_;
131 std::string buffer_contents_;
132 int32_t error_;
133 std::unique_ptr<WritableBuffer> write_buffer_;
134
135 bool seen_connection_error_;
136
137 Event expected_event_;
138
139 private:
140 DISALLOW_COPY_AND_ASSIGN(DataSourceTest);
141 };
142
143 const int32_t DataSourceTest::kFatalError = -10;
144
145 // Test that data is successfully transmitted from the source to the receiver.
146 TEST_F(DataSourceTest, Basic) {
147 ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("a", 0));
148
149 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
150 EXPECT_EQ(0, error_);
151 ASSERT_TRUE(buffer_);
152 EXPECT_EQ("a", buffer_contents_);
153 buffer_->Done(1);
154
155 ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("b", 0));
156
157 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
158 EXPECT_EQ(0, error_);
159 ASSERT_TRUE(buffer_);
160 EXPECT_EQ("b", buffer_contents_);
161 }
162
163 // Test that the receiver does not discard any data that is not read by the
164 // client.
165 TEST_F(DataSourceTest, PartialReceive) {
166 ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("ab", 0));
167
168 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
169 EXPECT_EQ(0, error_);
170 ASSERT_TRUE(buffer_);
171 EXPECT_EQ("ab", buffer_contents_);
172 buffer_->Done(1);
173
174 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
175 EXPECT_EQ(0, error_);
176 ASSERT_TRUE(buffer_);
177 EXPECT_EQ("b", buffer_contents_);
178 }
179
180 // Test that an error is correctly reported to the Receive() call immediately
181 // after the data has been read by the client.
182 TEST_F(DataSourceTest, ErrorAndData) {
183 ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("abc", -1));
184
185 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
186 ASSERT_TRUE(buffer_);
187 EXPECT_EQ("abc", buffer_contents_);
188 buffer_->Done(1);
189 EXPECT_EQ(0, error_);
190 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
191 ASSERT_TRUE(buffer_);
192 EXPECT_EQ("bc", buffer_contents_);
193 buffer_->Done(1);
194 EXPECT_EQ(0, error_);
195 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
196 ASSERT_TRUE(buffer_);
197 EXPECT_EQ("c", buffer_contents_);
198 buffer_->Done(1);
199 EXPECT_EQ(0, error_);
200 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
201 EXPECT_EQ(-1, error_);
202 ASSERT_FALSE(write_buffer_);
203
204 ASSERT_TRUE(Receive());
205 ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("d", 0));
206
207 WaitForEvent(EVENT_RECEIVE_COMPLETE);
208 ASSERT_TRUE(buffer_);
209 EXPECT_EQ("d", buffer_contents_);
210 buffer_->Done(1);
211 EXPECT_EQ(0, error_);
212
213 ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("e", -2));
214 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
215 ASSERT_TRUE(buffer_);
216 EXPECT_EQ("e", buffer_contents_);
217 buffer_->Done(1);
218 EXPECT_EQ(0, error_);
219
220 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
221 EXPECT_EQ(-2, error_);
222 }
223
224 // Test that an error is correctly reported when the source encounters an error
225 // without sending any data.
226 TEST_F(DataSourceTest, ErrorOnly) {
227 ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("", -1));
228
229 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
230 EXPECT_FALSE(buffer_);
231 EXPECT_EQ(-1, error_);
232 ASSERT_FALSE(write_buffer_);
233
234 ASSERT_TRUE(Receive());
235 ASSERT_NO_FATAL_FAILURE(FillWriteBuffer("", -2));
236
237 WaitForEvent(EVENT_RECEIVE_COMPLETE);
238 EXPECT_FALSE(buffer_);
239 EXPECT_EQ(-2, error_);
240 ASSERT_FALSE(write_buffer_);
241 }
242
243 // Test that the source shutting down is correctly reported to the client.
244 TEST_F(DataSourceTest, SourceShutdown) {
245 source_sender_->ShutDown();
246 source_sender_ = NULL;
247 ASSERT_NO_FATAL_FAILURE(ReceiveAndWait());
248 EXPECT_FALSE(buffer_);
249 EXPECT_EQ(kFatalError, error_);
250 ASSERT_FALSE(write_buffer_);
251 ASSERT_FALSE(Receive());
252 }
253
254 // Test that the receiver shutting down is correctly reported to the source.
255 TEST_F(DataSourceTest, ReceiverShutdown) {
256 Receive();
257 receiver_ = NULL;
258 EXPECT_EQ(kFatalError, error_);
259 WaitForEvent(EVENT_ERROR);
260 EXPECT_TRUE(seen_connection_error_);
261 }
262
263 } // namespace device
OLDNEW
« no previous file with comments | « device/serial/data_source_sender.cc ('k') | device/serial/data_stream.mojom » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698