Reland r247589: <webview>: Fix text selection features in mac.
[chromium-blink-merge.git] / remoting / protocol / message_reader_unittest.cc
blob497e7a550bf9ce6729cf9f09ee84107d5b5dbcf4
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_session.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/libjingle/source/talk/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 virtual void SetUp() OVERRIDE {
71 reader_.reset(new MessageReader());
74 virtual void TearDown() OVERRIDE {
75 STLDeleteElements(&messages_);
78 void InitReader() {
79 reader_->Init(&socket_, base::Bind(
80 &MessageReaderTest::OnMessage, base::Unretained(this)));
83 void AddMessage(const std::string& message) {
84 std::string data = std::string(4, ' ') + message;
85 talk_base::SetBE32(const_cast<char*>(data.data()), message.size());
87 socket_.AppendInputData(std::vector<char>(data.begin(), data.end()));
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 OnMessage(scoped_ptr<CompoundBuffer> buffer,
97 const base::Closure& done_callback) {
98 messages_.push_back(buffer.release());
99 callback_.OnMessage(done_callback);
102 base::MessageLoop message_loop_;
103 scoped_ptr<MessageReader> reader_;
104 FakeSocket socket_;
105 MockMessageReceivedCallback callback_;
106 std::vector<CompoundBuffer*> messages_;
107 bool in_callback_;
110 // Receive one message and process it with delay
111 TEST_F(MessageReaderTest, OneMessage_Delay) {
112 base::Closure done_task;
114 AddMessage(kTestMessage1);
116 EXPECT_CALL(callback_, OnMessage(_))
117 .Times(1)
118 .WillOnce(SaveArg<0>(&done_task));
120 InitReader();
121 base::RunLoop().RunUntilIdle();
123 Mock::VerifyAndClearExpectations(&callback_);
124 Mock::VerifyAndClearExpectations(&socket_);
126 EXPECT_TRUE(CompareResult(messages_[0], kTestMessage1));
128 // Verify that the reader starts reading again only after we've
129 // finished processing the previous message.
130 EXPECT_FALSE(socket_.read_pending());
132 done_task.Run();
134 EXPECT_TRUE(socket_.read_pending());
137 // Receive one message and process it instantly.
138 TEST_F(MessageReaderTest, OneMessage_Instant) {
139 AddMessage(kTestMessage1);
141 EXPECT_CALL(callback_, OnMessage(_))
142 .Times(1)
143 .WillOnce(CallDoneTask());
145 InitReader();
146 base::RunLoop().RunUntilIdle();
148 EXPECT_TRUE(socket_.read_pending());
149 EXPECT_EQ(1U, messages_.size());
152 // Receive two messages in one packet.
153 TEST_F(MessageReaderTest, TwoMessages_Together) {
154 base::Closure done_task1;
155 base::Closure done_task2;
157 AddMessage(kTestMessage1);
158 AddMessage(kTestMessage2);
160 EXPECT_CALL(callback_, OnMessage(_))
161 .Times(2)
162 .WillOnce(SaveArg<0>(&done_task1))
163 .WillOnce(SaveArg<0>(&done_task2));
165 InitReader();
166 base::RunLoop().RunUntilIdle();
168 Mock::VerifyAndClearExpectations(&callback_);
169 Mock::VerifyAndClearExpectations(&socket_);
171 EXPECT_TRUE(CompareResult(messages_[0], kTestMessage1));
172 EXPECT_TRUE(CompareResult(messages_[1], kTestMessage2));
174 // Verify that the reader starts reading again only after we've
175 // finished processing the previous message.
176 EXPECT_FALSE(socket_.read_pending());
178 done_task1.Run();
179 base::RunLoop().RunUntilIdle();
181 EXPECT_FALSE(socket_.read_pending());
183 done_task2.Run();
184 base::RunLoop().RunUntilIdle();
186 EXPECT_TRUE(socket_.read_pending());
189 // Receive two messages in one packet, and process the first one
190 // instantly.
191 TEST_F(MessageReaderTest, TwoMessages_Instant) {
192 base::Closure done_task2;
194 AddMessage(kTestMessage1);
195 AddMessage(kTestMessage2);
197 EXPECT_CALL(callback_, OnMessage(_))
198 .Times(2)
199 .WillOnce(CallDoneTask())
200 .WillOnce(SaveArg<0>(&done_task2));
202 InitReader();
203 base::RunLoop().RunUntilIdle();
205 Mock::VerifyAndClearExpectations(&callback_);
206 Mock::VerifyAndClearExpectations(&socket_);
208 EXPECT_TRUE(CompareResult(messages_[1], kTestMessage2));
210 // Verify that the reader starts reading again only after we've
211 // finished processing the second message.
212 EXPECT_FALSE(socket_.read_pending());
214 done_task2.Run();
216 EXPECT_TRUE(socket_.read_pending());
219 // Receive two messages in one packet, and process both of them
220 // instantly.
221 TEST_F(MessageReaderTest, TwoMessages_Instant2) {
222 AddMessage(kTestMessage1);
223 AddMessage(kTestMessage2);
225 EXPECT_CALL(callback_, OnMessage(_))
226 .Times(2)
227 .WillOnce(CallDoneTask())
228 .WillOnce(CallDoneTask());
230 InitReader();
231 base::RunLoop().RunUntilIdle();
233 EXPECT_TRUE(socket_.read_pending());
236 // Receive two messages in separate packets.
237 TEST_F(MessageReaderTest, TwoMessages_Separately) {
238 base::Closure done_task;
240 AddMessage(kTestMessage1);
242 EXPECT_CALL(callback_, OnMessage(_))
243 .Times(1)
244 .WillOnce(SaveArg<0>(&done_task));
246 InitReader();
247 base::RunLoop().RunUntilIdle();
249 Mock::VerifyAndClearExpectations(&callback_);
250 Mock::VerifyAndClearExpectations(&socket_);
252 EXPECT_TRUE(CompareResult(messages_[0], kTestMessage1));
254 // Verify that the reader starts reading again only after we've
255 // finished processing the previous message.
256 EXPECT_FALSE(socket_.read_pending());
258 done_task.Run();
259 base::RunLoop().RunUntilIdle();
261 EXPECT_TRUE(socket_.read_pending());
263 // Write another message and verify that we receive it.
264 EXPECT_CALL(callback_, OnMessage(_))
265 .Times(1)
266 .WillOnce(SaveArg<0>(&done_task));
267 AddMessage(kTestMessage2);
268 base::RunLoop().RunUntilIdle();
270 EXPECT_TRUE(CompareResult(messages_[1], kTestMessage2));
272 // Verify that the reader starts reading again only after we've
273 // finished processing the previous message.
274 EXPECT_FALSE(socket_.read_pending());
276 done_task.Run();
278 EXPECT_TRUE(socket_.read_pending());
281 // Read() returns error.
282 TEST_F(MessageReaderTest, ReadError) {
283 socket_.set_next_read_error(net::ERR_FAILED);
285 // Add a message. It should never be read after the error above.
286 AddMessage(kTestMessage1);
288 EXPECT_CALL(callback_, OnMessage(_))
289 .Times(0);
291 InitReader();
294 // Verify that we the OnMessage callback is not reentered.
295 TEST_F(MessageReaderTest, ReadFromCallback) {
296 AddMessage(kTestMessage1);
298 EXPECT_CALL(callback_, OnMessage(_))
299 .Times(2)
300 .WillOnce(Invoke(this, &MessageReaderTest::AddSecondMessage))
301 .WillOnce(Invoke(this, &MessageReaderTest::OnSecondMessage));
303 InitReader();
304 base::RunLoop().RunUntilIdle();
306 EXPECT_TRUE(socket_.read_pending());
309 // Verify that we stop getting callbacks after deleting MessageReader.
310 TEST_F(MessageReaderTest, DeleteFromCallback) {
311 base::Closure done_task1;
312 base::Closure done_task2;
314 AddMessage(kTestMessage1);
315 AddMessage(kTestMessage2);
317 // OnMessage() should never be called for the second message.
318 EXPECT_CALL(callback_, OnMessage(_))
319 .Times(1)
320 .WillOnce(Invoke(this, &MessageReaderTest::DeleteReader));
322 InitReader();
323 base::RunLoop().RunUntilIdle();
326 } // namespace protocol
327 } // namespace remoting