| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "net/socket/socket.h" | 8 #include "net/socket/socket.h" |
| 9 #include "remoting/protocol/fake_session.h" | 9 #include "remoting/protocol/fake_session.h" |
| 10 #include "remoting/protocol/message_reader.h" | 10 #include "remoting/protocol/message_reader.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 | 52 |
| 53 socket_.AppendInputData(data.data(), data.size()); | 53 socket_.AppendInputData(data.data(), data.size()); |
| 54 } | 54 } |
| 55 | 55 |
| 56 bool CompareResult(CompoundBuffer* buffer, const std::string& expected) { | 56 bool CompareResult(CompoundBuffer* buffer, const std::string& expected) { |
| 57 std::string result(buffer->total_bytes(), ' '); | 57 std::string result(buffer->total_bytes(), ' '); |
| 58 buffer->CopyTo(const_cast<char*>(result.data()), result.size()); | 58 buffer->CopyTo(const_cast<char*>(result.data()), result.size()); |
| 59 return result == expected; | 59 return result == expected; |
| 60 } | 60 } |
| 61 | 61 |
| 62 void RunAndDeleteTask(Task* task) { |
| 63 task->Run(); |
| 64 delete task; |
| 65 } |
| 66 |
| 62 // MessageLoop must be first here, so that is is destroyed the last. | 67 // MessageLoop must be first here, so that is is destroyed the last. |
| 63 MessageLoop message_loop_; | 68 MessageLoop message_loop_; |
| 64 | 69 |
| 65 scoped_refptr<MessageReader> reader_; | 70 scoped_refptr<MessageReader> reader_; |
| 66 FakeSocket socket_; | 71 FakeSocket socket_; |
| 67 MockMessageReceivedCallback callback_; | 72 MockMessageReceivedCallback callback_; |
| 68 }; | 73 }; |
| 69 | 74 |
| 70 // Receive one message and process it with delay | 75 // Receive one message and process it with delay |
| 71 TEST_F(MessageReaderTest, OneMessage_Delay) { | 76 TEST_F(MessageReaderTest, OneMessage_Delay) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 83 | 88 |
| 84 Mock::VerifyAndClearExpectations(&callback_); | 89 Mock::VerifyAndClearExpectations(&callback_); |
| 85 Mock::VerifyAndClearExpectations(&socket_); | 90 Mock::VerifyAndClearExpectations(&socket_); |
| 86 | 91 |
| 87 EXPECT_TRUE(CompareResult(buffer, kTestMessage1)); | 92 EXPECT_TRUE(CompareResult(buffer, kTestMessage1)); |
| 88 | 93 |
| 89 // Verify that the reader starts reading again only after we've | 94 // Verify that the reader starts reading again only after we've |
| 90 // finished processing the previous message. | 95 // finished processing the previous message. |
| 91 EXPECT_FALSE(socket_.read_pending()); | 96 EXPECT_FALSE(socket_.read_pending()); |
| 92 | 97 |
| 93 done_task->Run(); | 98 RunAndDeleteTask(done_task); |
| 94 | 99 |
| 95 EXPECT_TRUE(socket_.read_pending()); | 100 EXPECT_TRUE(socket_.read_pending()); |
| 96 } | 101 } |
| 97 | 102 |
| 98 // Receive one message and process it instantly. | 103 // Receive one message and process it instantly. |
| 99 TEST_F(MessageReaderTest, OneMessage_Instant) { | 104 TEST_F(MessageReaderTest, OneMessage_Instant) { |
| 100 AddMessage(kTestMessage1); | 105 AddMessage(kTestMessage1); |
| 101 | 106 |
| 102 EXPECT_CALL(callback_, OnMessage(_, _)) | 107 EXPECT_CALL(callback_, OnMessage(_, _)) |
| 103 .Times(1) | 108 .Times(1) |
| (...skipping 26 matching lines...) Expand all Loading... |
| 130 Mock::VerifyAndClearExpectations(&callback_); | 135 Mock::VerifyAndClearExpectations(&callback_); |
| 131 Mock::VerifyAndClearExpectations(&socket_); | 136 Mock::VerifyAndClearExpectations(&socket_); |
| 132 | 137 |
| 133 EXPECT_TRUE(CompareResult(buffer1, kTestMessage1)); | 138 EXPECT_TRUE(CompareResult(buffer1, kTestMessage1)); |
| 134 EXPECT_TRUE(CompareResult(buffer2, kTestMessage2)); | 139 EXPECT_TRUE(CompareResult(buffer2, kTestMessage2)); |
| 135 | 140 |
| 136 // Verify that the reader starts reading again only after we've | 141 // Verify that the reader starts reading again only after we've |
| 137 // finished processing the previous message. | 142 // finished processing the previous message. |
| 138 EXPECT_FALSE(socket_.read_pending()); | 143 EXPECT_FALSE(socket_.read_pending()); |
| 139 | 144 |
| 140 done_task1->Run(); | 145 RunAndDeleteTask(done_task1); |
| 141 | 146 |
| 142 EXPECT_FALSE(socket_.read_pending()); | 147 EXPECT_FALSE(socket_.read_pending()); |
| 143 | 148 |
| 144 done_task2->Run(); | 149 RunAndDeleteTask(done_task2); |
| 145 | 150 |
| 146 EXPECT_TRUE(socket_.read_pending()); | 151 EXPECT_TRUE(socket_.read_pending()); |
| 147 } | 152 } |
| 148 | 153 |
| 149 // Receive two messages in one packet, and process the first one | 154 // Receive two messages in one packet, and process the first one |
| 150 // instantly. | 155 // instantly. |
| 151 TEST_F(MessageReaderTest, TwoMessages_Instant) { | 156 TEST_F(MessageReaderTest, TwoMessages_Instant) { |
| 152 CompoundBuffer* buffer2; | 157 CompoundBuffer* buffer2; |
| 153 Task* done_task2; | 158 Task* done_task2; |
| 154 | 159 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 165 | 170 |
| 166 Mock::VerifyAndClearExpectations(&callback_); | 171 Mock::VerifyAndClearExpectations(&callback_); |
| 167 Mock::VerifyAndClearExpectations(&socket_); | 172 Mock::VerifyAndClearExpectations(&socket_); |
| 168 | 173 |
| 169 EXPECT_TRUE(CompareResult(buffer2, kTestMessage2)); | 174 EXPECT_TRUE(CompareResult(buffer2, kTestMessage2)); |
| 170 | 175 |
| 171 // Verify that the reader starts reading again only after we've | 176 // Verify that the reader starts reading again only after we've |
| 172 // finished processing the second message. | 177 // finished processing the second message. |
| 173 EXPECT_FALSE(socket_.read_pending()); | 178 EXPECT_FALSE(socket_.read_pending()); |
| 174 | 179 |
| 175 done_task2->Run(); | 180 RunAndDeleteTask(done_task2); |
| 176 | 181 |
| 177 EXPECT_TRUE(socket_.read_pending()); | 182 EXPECT_TRUE(socket_.read_pending()); |
| 178 } | 183 } |
| 179 | 184 |
| 180 // Receive two messages in one packet, and process both of them | 185 // Receive two messages in one packet, and process both of them |
| 181 // instantly. | 186 // instantly. |
| 182 TEST_F(MessageReaderTest, TwoMessages_Instant2) { | 187 TEST_F(MessageReaderTest, TwoMessages_Instant2) { |
| 183 AddMessage(kTestMessage1); | 188 AddMessage(kTestMessage1); |
| 184 AddMessage(kTestMessage2); | 189 AddMessage(kTestMessage2); |
| 185 | 190 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 209 | 214 |
| 210 Mock::VerifyAndClearExpectations(&callback_); | 215 Mock::VerifyAndClearExpectations(&callback_); |
| 211 Mock::VerifyAndClearExpectations(&socket_); | 216 Mock::VerifyAndClearExpectations(&socket_); |
| 212 | 217 |
| 213 EXPECT_TRUE(CompareResult(buffer, kTestMessage1)); | 218 EXPECT_TRUE(CompareResult(buffer, kTestMessage1)); |
| 214 | 219 |
| 215 // Verify that the reader starts reading again only after we've | 220 // Verify that the reader starts reading again only after we've |
| 216 // finished processing the previous message. | 221 // finished processing the previous message. |
| 217 EXPECT_FALSE(socket_.read_pending()); | 222 EXPECT_FALSE(socket_.read_pending()); |
| 218 | 223 |
| 219 done_task->Run(); | 224 RunAndDeleteTask(done_task); |
| 220 | 225 |
| 221 EXPECT_TRUE(socket_.read_pending()); | 226 EXPECT_TRUE(socket_.read_pending()); |
| 222 | 227 |
| 223 // Write another message and verify that we receive it. | 228 // Write another message and verify that we receive it. |
| 224 EXPECT_CALL(callback_, OnMessage(_, _)) | 229 EXPECT_CALL(callback_, OnMessage(_, _)) |
| 225 .Times(1) | 230 .Times(1) |
| 226 .WillOnce(DoAll(SaveArg<0>(&buffer), | 231 .WillOnce(DoAll(SaveArg<0>(&buffer), |
| 227 SaveArg<1>(&done_task))); | 232 SaveArg<1>(&done_task))); |
| 228 AddMessage(kTestMessage2); | 233 AddMessage(kTestMessage2); |
| 229 | 234 |
| 230 EXPECT_TRUE(CompareResult(buffer, kTestMessage2)); | 235 EXPECT_TRUE(CompareResult(buffer, kTestMessage2)); |
| 231 | 236 |
| 232 // Verify that the reader starts reading again only after we've | 237 // Verify that the reader starts reading again only after we've |
| 233 // finished processing the previous message. | 238 // finished processing the previous message. |
| 234 EXPECT_FALSE(socket_.read_pending()); | 239 EXPECT_FALSE(socket_.read_pending()); |
| 235 | 240 |
| 236 done_task->Run(); | 241 RunAndDeleteTask(done_task); |
| 237 | 242 |
| 238 EXPECT_TRUE(socket_.read_pending()); | 243 EXPECT_TRUE(socket_.read_pending()); |
| 239 } | 244 } |
| 240 | 245 |
| 241 } // namespace protocol | 246 } // namespace protocol |
| 242 } // namespace remoting | 247 } // namespace remoting |
| OLD | NEW |