Mandoline: Disable slimming paint
[chromium-blink-merge.git] / remoting / protocol / message_reader_unittest.cc
blobaf494488460fc06667c13933956d92f9b5c4780e
1 // Copyright (c) 2012 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/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "base/stl_util.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "net/base/net_errors.h"
15 #include "net/socket/socket.h"
16 #include "remoting/protocol/fake_stream_socket.h"
17 #include "remoting/protocol/message_reader.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "third_party/webrtc/base/byteorder.h"
22 using testing::_;
23 using testing::DoAll;
24 using testing::Mock;
25 using testing::SaveArg;
27 namespace remoting {
28 namespace protocol {
30 namespace {
31 const char kTestMessage1[] = "Message1";
32 const char kTestMessage2[] = "Message2";
34 ACTION(CallDoneTask) {
35 arg0.Run();
37 } // namespace
39 class MockMessageReceivedCallback {
40 public:
41 MOCK_METHOD1(OnMessage, void(const base::Closure&));
44 class MessageReaderTest : public testing::Test {
45 public:
46 MessageReaderTest()
47 : in_callback_(false) {
50 // Following two methods are used by the ReadFromCallback test.
51 void AddSecondMessage(const base::Closure& task) {
52 AddMessage(kTestMessage2);
53 in_callback_ = true;
54 task.Run();
55 in_callback_ = false;
58 void OnSecondMessage(const base::Closure& task) {
59 EXPECT_FALSE(in_callback_);
60 task.Run();
63 // Used by the DeleteFromCallback() test.
64 void DeleteReader(const base::Closure& task) {
65 reader_.reset();
66 task.Run();
69 protected:
70 void SetUp() override {
71 reader_.reset(new MessageReader());
74 void TearDown() override { STLDeleteElements(&messages_); }
76 void InitReader() {
77 reader_->SetMessageReceivedCallback(
78 base::Bind(&MessageReaderTest::OnMessage, base::Unretained(this)));
79 reader_->StartReading(&socket_, base::Bind(&MessageReaderTest::OnReadError,
80 base::Unretained(this)));
83 void AddMessage(const std::string& message) {
84 std::string data = std::string(4, ' ') + message;
85 rtc::SetBE32(const_cast<char*>(data.data()), message.size());
87 socket_.AppendInputData(data);
90 bool CompareResult(CompoundBuffer* buffer, const std::string& expected) {
91 std::string result(buffer->total_bytes(), ' ');
92 buffer->CopyTo(const_cast<char*>(result.data()), result.size());
93 return result == expected;
96 void OnReadError(int error) {
97 read_error_ = error;
98 reader_.reset();
101 void OnMessage(scoped_ptr<CompoundBuffer> buffer,
102 const base::Closure& done_callback) {
103 messages_.push_back(buffer.release());
104 callback_.OnMessage(done_callback);
107 base::MessageLoop message_loop_;
108 scoped_ptr<MessageReader> reader_;
109 FakeStreamSocket socket_;
110 MockMessageReceivedCallback callback_;
111 int read_error_ = 0;
112 std::vector<CompoundBuffer*> messages_;
113 bool in_callback_;
116 // Receive one message and process it with delay
117 TEST_F(MessageReaderTest, OneMessage_Delay) {
118 base::Closure done_task;
120 AddMessage(kTestMessage1);
122 EXPECT_CALL(callback_, OnMessage(_))
123 .Times(1)
124 .WillOnce(SaveArg<0>(&done_task));
126 InitReader();
127 base::RunLoop().RunUntilIdle();
129 Mock::VerifyAndClearExpectations(&callback_);
130 Mock::VerifyAndClearExpectations(&socket_);
132 EXPECT_TRUE(CompareResult(messages_[0], kTestMessage1));
134 // Verify that the reader starts reading again only after we've
135 // finished processing the previous message.
136 EXPECT_FALSE(socket_.read_pending());
138 done_task.Run();
140 EXPECT_TRUE(socket_.read_pending());
143 // Receive one message and process it instantly.
144 TEST_F(MessageReaderTest, OneMessage_Instant) {
145 AddMessage(kTestMessage1);
147 EXPECT_CALL(callback_, OnMessage(_))
148 .Times(1)
149 .WillOnce(CallDoneTask());
151 InitReader();
152 base::RunLoop().RunUntilIdle();
154 EXPECT_TRUE(socket_.read_pending());
155 EXPECT_EQ(1U, messages_.size());
158 // Receive two messages in one packet.
159 TEST_F(MessageReaderTest, TwoMessages_Together) {
160 base::Closure done_task1;
161 base::Closure done_task2;
163 AddMessage(kTestMessage1);
164 AddMessage(kTestMessage2);
166 EXPECT_CALL(callback_, OnMessage(_))
167 .Times(2)
168 .WillOnce(SaveArg<0>(&done_task1))
169 .WillOnce(SaveArg<0>(&done_task2));
171 InitReader();
172 base::RunLoop().RunUntilIdle();
174 Mock::VerifyAndClearExpectations(&callback_);
175 Mock::VerifyAndClearExpectations(&socket_);
177 EXPECT_TRUE(CompareResult(messages_[0], kTestMessage1));
178 EXPECT_TRUE(CompareResult(messages_[1], kTestMessage2));
180 // Verify that the reader starts reading again only after we've
181 // finished processing the previous message.
182 EXPECT_FALSE(socket_.read_pending());
184 done_task1.Run();
185 base::RunLoop().RunUntilIdle();
187 EXPECT_FALSE(socket_.read_pending());
189 done_task2.Run();
190 base::RunLoop().RunUntilIdle();
192 EXPECT_TRUE(socket_.read_pending());
195 // Receive two messages in one packet, and process the first one
196 // instantly.
197 TEST_F(MessageReaderTest, TwoMessages_Instant) {
198 base::Closure done_task2;
200 AddMessage(kTestMessage1);
201 AddMessage(kTestMessage2);
203 EXPECT_CALL(callback_, OnMessage(_))
204 .Times(2)
205 .WillOnce(CallDoneTask())
206 .WillOnce(SaveArg<0>(&done_task2));
208 InitReader();
209 base::RunLoop().RunUntilIdle();
211 Mock::VerifyAndClearExpectations(&callback_);
212 Mock::VerifyAndClearExpectations(&socket_);
214 EXPECT_TRUE(CompareResult(messages_[1], kTestMessage2));
216 // Verify that the reader starts reading again only after we've
217 // finished processing the second message.
218 EXPECT_FALSE(socket_.read_pending());
220 done_task2.Run();
222 EXPECT_TRUE(socket_.read_pending());
225 // Receive two messages in one packet, and process both of them
226 // instantly.
227 TEST_F(MessageReaderTest, TwoMessages_Instant2) {
228 AddMessage(kTestMessage1);
229 AddMessage(kTestMessage2);
231 EXPECT_CALL(callback_, OnMessage(_))
232 .Times(2)
233 .WillOnce(CallDoneTask())
234 .WillOnce(CallDoneTask());
236 InitReader();
237 base::RunLoop().RunUntilIdle();
239 EXPECT_TRUE(socket_.read_pending());
242 // Receive two messages in separate packets.
243 TEST_F(MessageReaderTest, TwoMessages_Separately) {
244 base::Closure done_task;
246 AddMessage(kTestMessage1);
248 EXPECT_CALL(callback_, OnMessage(_))
249 .Times(1)
250 .WillOnce(SaveArg<0>(&done_task));
252 InitReader();
253 base::RunLoop().RunUntilIdle();
255 Mock::VerifyAndClearExpectations(&callback_);
256 Mock::VerifyAndClearExpectations(&socket_);
258 EXPECT_TRUE(CompareResult(messages_[0], kTestMessage1));
260 // Verify that the reader starts reading again only after we've
261 // finished processing the previous message.
262 EXPECT_FALSE(socket_.read_pending());
264 done_task.Run();
265 base::RunLoop().RunUntilIdle();
267 EXPECT_TRUE(socket_.read_pending());
269 // Write another message and verify that we receive it.
270 EXPECT_CALL(callback_, OnMessage(_))
271 .Times(1)
272 .WillOnce(SaveArg<0>(&done_task));
273 AddMessage(kTestMessage2);
274 base::RunLoop().RunUntilIdle();
276 EXPECT_TRUE(CompareResult(messages_[1], kTestMessage2));
278 // Verify that the reader starts reading again only after we've
279 // finished processing the previous message.
280 EXPECT_FALSE(socket_.read_pending());
282 done_task.Run();
284 EXPECT_TRUE(socket_.read_pending());
287 // Read() returns error.
288 TEST_F(MessageReaderTest, ReadError) {
289 socket_.AppendReadError(net::ERR_FAILED);
291 EXPECT_CALL(callback_, OnMessage(_)).Times(0);
293 InitReader();
295 EXPECT_EQ(net::ERR_FAILED, read_error_);
296 EXPECT_FALSE(reader_);
299 // Verify that we the OnMessage callback is not reentered.
300 TEST_F(MessageReaderTest, ReadFromCallback) {
301 AddMessage(kTestMessage1);
303 EXPECT_CALL(callback_, OnMessage(_))
304 .Times(2)
305 .WillOnce(Invoke(this, &MessageReaderTest::AddSecondMessage))
306 .WillOnce(Invoke(this, &MessageReaderTest::OnSecondMessage));
308 InitReader();
309 base::RunLoop().RunUntilIdle();
311 EXPECT_TRUE(socket_.read_pending());
314 // Verify that we stop getting callbacks after deleting MessageReader.
315 TEST_F(MessageReaderTest, DeleteFromCallback) {
316 base::Closure done_task1;
317 base::Closure done_task2;
319 AddMessage(kTestMessage1);
320 AddMessage(kTestMessage2);
322 // OnMessage() should never be called for the second message.
323 EXPECT_CALL(callback_, OnMessage(_))
324 .Times(1)
325 .WillOnce(Invoke(this, &MessageReaderTest::DeleteReader));
327 InitReader();
328 base::RunLoop().RunUntilIdle();
331 } // namespace protocol
332 } // namespace remoting