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

Side by Side Diff: remoting/protocol/message_reader_unittest.cc

Issue 2386733002: Remove stl_util's deletion functions from remoting/. (Closed)
Patch Set: fix 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 | « remoting/protocol/message_decoder_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h" 11 #include "base/run_loop.h"
12 #include "base/stl_util.h"
13 #include "base/synchronization/waitable_event.h" 12 #include "base/synchronization/waitable_event.h"
14 #include "net/base/net_errors.h" 13 #include "net/base/net_errors.h"
15 #include "net/socket/socket.h" 14 #include "net/socket/socket.h"
16 #include "remoting/protocol/fake_stream_socket.h" 15 #include "remoting/protocol/fake_stream_socket.h"
17 #include "remoting/protocol/message_reader.h" 16 #include "remoting/protocol/message_reader.h"
18 #include "testing/gmock/include/gmock/gmock.h" 17 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
20 #include "third_party/webrtc/base/byteorder.h" 19 #include "third_party/webrtc/base/byteorder.h"
21 20
22 using testing::_; 21 using testing::_;
(...skipping 20 matching lines...) Expand all
43 void AddSecondMessage() { AddMessage(kTestMessage2); } 42 void AddSecondMessage() { AddMessage(kTestMessage2); }
44 43
45 // Used by the DeleteFromCallback() test. 44 // Used by the DeleteFromCallback() test.
46 void DeleteReader() { reader_.reset(); } 45 void DeleteReader() { reader_.reset(); }
47 46
48 protected: 47 protected:
49 void SetUp() override { 48 void SetUp() override {
50 reader_.reset(new MessageReader()); 49 reader_.reset(new MessageReader());
51 } 50 }
52 51
53 void TearDown() override { base::STLDeleteElements(&messages_); }
54
55 void InitReader() { 52 void InitReader() {
56 reader_->StartReading( 53 reader_->StartReading(
57 &socket_, 54 &socket_,
58 base::Bind(&MessageReaderTest::OnMessage, base::Unretained(this)), 55 base::Bind(&MessageReaderTest::OnMessage, base::Unretained(this)),
59 base::Bind(&MessageReaderTest::OnReadError, base::Unretained(this))); 56 base::Bind(&MessageReaderTest::OnReadError, base::Unretained(this)));
60 } 57 }
61 58
62 void AddMessage(const std::string& message) { 59 void AddMessage(const std::string& message) {
63 std::string data = std::string(4, ' ') + message; 60 std::string data = std::string(4, ' ') + message;
64 rtc::SetBE32(const_cast<char*>(data.data()), message.size()); 61 rtc::SetBE32(const_cast<char*>(data.data()), message.size());
65 62
66 socket_.AppendInputData(data); 63 socket_.AppendInputData(data);
67 } 64 }
68 65
69 bool CompareResult(CompoundBuffer* buffer, const std::string& expected) { 66 bool CompareResult(CompoundBuffer* buffer, const std::string& expected) {
70 std::string result(buffer->total_bytes(), ' '); 67 std::string result(buffer->total_bytes(), ' ');
71 buffer->CopyTo(const_cast<char*>(result.data()), result.size()); 68 buffer->CopyTo(const_cast<char*>(result.data()), result.size());
72 return result == expected; 69 return result == expected;
73 } 70 }
74 71
75 void OnReadError(int error) { 72 void OnReadError(int error) {
76 read_error_ = error; 73 read_error_ = error;
77 reader_.reset(); 74 reader_.reset();
78 } 75 }
79 76
80 void OnMessage(std::unique_ptr<CompoundBuffer> buffer) { 77 void OnMessage(std::unique_ptr<CompoundBuffer> buffer) {
81 messages_.push_back(buffer.release()); 78 messages_.push_back(std::move(buffer));
82 callback_.OnMessage(); 79 callback_.OnMessage();
83 } 80 }
84 81
85 base::MessageLoop message_loop_; 82 base::MessageLoop message_loop_;
86 std::unique_ptr<MessageReader> reader_; 83 std::unique_ptr<MessageReader> reader_;
87 FakeStreamSocket socket_; 84 FakeStreamSocket socket_;
88 MockMessageReceivedCallback callback_; 85 MockMessageReceivedCallback callback_;
89 int read_error_ = 0; 86 int read_error_ = 0;
90 std::vector<CompoundBuffer*> messages_; 87 std::vector<std::unique_ptr<CompoundBuffer>> messages_;
91 }; 88 };
92 89
93 // Receive one message. 90 // Receive one message.
94 TEST_F(MessageReaderTest, OneMessage) { 91 TEST_F(MessageReaderTest, OneMessage) {
95 AddMessage(kTestMessage1); 92 AddMessage(kTestMessage1);
96 93
97 EXPECT_CALL(callback_, OnMessage()).Times(1); 94 EXPECT_CALL(callback_, OnMessage()).Times(1);
98 95
99 InitReader(); 96 InitReader();
100 base::RunLoop().RunUntilIdle(); 97 base::RunLoop().RunUntilIdle();
101 98
102 EXPECT_TRUE(socket_.read_pending()); 99 EXPECT_TRUE(socket_.read_pending());
103 EXPECT_EQ(1U, messages_.size()); 100 EXPECT_EQ(1U, messages_.size());
104 } 101 }
105 102
106 // Receive two messages in one packet. 103 // Receive two messages in one packet.
107 TEST_F(MessageReaderTest, TwoMessages_Together) { 104 TEST_F(MessageReaderTest, TwoMessages_Together) {
108 AddMessage(kTestMessage1); 105 AddMessage(kTestMessage1);
109 AddMessage(kTestMessage2); 106 AddMessage(kTestMessage2);
110 107
111 EXPECT_CALL(callback_, OnMessage()).Times(2); 108 EXPECT_CALL(callback_, OnMessage()).Times(2);
112 109
113 InitReader(); 110 InitReader();
114 base::RunLoop().RunUntilIdle(); 111 base::RunLoop().RunUntilIdle();
115 112
116 Mock::VerifyAndClearExpectations(&callback_); 113 Mock::VerifyAndClearExpectations(&callback_);
117 Mock::VerifyAndClearExpectations(&socket_); 114 Mock::VerifyAndClearExpectations(&socket_);
118 115
119 EXPECT_TRUE(CompareResult(messages_[0], kTestMessage1)); 116 EXPECT_TRUE(CompareResult(messages_[0].get(), kTestMessage1));
120 EXPECT_TRUE(CompareResult(messages_[1], kTestMessage2)); 117 EXPECT_TRUE(CompareResult(messages_[1].get(), kTestMessage2));
121 118
122 EXPECT_TRUE(socket_.read_pending()); 119 EXPECT_TRUE(socket_.read_pending());
123 } 120 }
124 121
125 // Receive two messages in separate packets. 122 // Receive two messages in separate packets.
126 TEST_F(MessageReaderTest, TwoMessages_Separately) { 123 TEST_F(MessageReaderTest, TwoMessages_Separately) {
127 AddMessage(kTestMessage1); 124 AddMessage(kTestMessage1);
128 125
129 EXPECT_CALL(callback_, OnMessage()) 126 EXPECT_CALL(callback_, OnMessage())
130 .Times(1); 127 .Times(1);
131 128
132 InitReader(); 129 InitReader();
133 base::RunLoop().RunUntilIdle(); 130 base::RunLoop().RunUntilIdle();
134 131
135 Mock::VerifyAndClearExpectations(&callback_); 132 Mock::VerifyAndClearExpectations(&callback_);
136 Mock::VerifyAndClearExpectations(&socket_); 133 Mock::VerifyAndClearExpectations(&socket_);
137 134
138 EXPECT_TRUE(CompareResult(messages_[0], kTestMessage1)); 135 EXPECT_TRUE(CompareResult(messages_[0].get(), kTestMessage1));
139 136
140 EXPECT_TRUE(socket_.read_pending()); 137 EXPECT_TRUE(socket_.read_pending());
141 138
142 // Write another message and verify that we receive it. 139 // Write another message and verify that we receive it.
143 EXPECT_CALL(callback_, OnMessage()) 140 EXPECT_CALL(callback_, OnMessage())
144 .Times(1); 141 .Times(1);
145 AddMessage(kTestMessage2); 142 AddMessage(kTestMessage2);
146 base::RunLoop().RunUntilIdle(); 143 base::RunLoop().RunUntilIdle();
147 144
148 EXPECT_TRUE(CompareResult(messages_[1], kTestMessage2)); 145 EXPECT_TRUE(CompareResult(messages_[1].get(), kTestMessage2));
149 146
150 EXPECT_TRUE(socket_.read_pending()); 147 EXPECT_TRUE(socket_.read_pending());
151 } 148 }
152 149
153 // Read() returns error. 150 // Read() returns error.
154 TEST_F(MessageReaderTest, ReadError) { 151 TEST_F(MessageReaderTest, ReadError) {
155 socket_.AppendReadError(net::ERR_FAILED); 152 socket_.AppendReadError(net::ERR_FAILED);
156 153
157 EXPECT_CALL(callback_, OnMessage()).Times(0); 154 EXPECT_CALL(callback_, OnMessage()).Times(0);
158 155
(...skipping 26 matching lines...) Expand all
185 EXPECT_CALL(callback_, OnMessage()) 182 EXPECT_CALL(callback_, OnMessage())
186 .Times(1) 183 .Times(1)
187 .WillOnce(Invoke(this, &MessageReaderTest::DeleteReader)); 184 .WillOnce(Invoke(this, &MessageReaderTest::DeleteReader));
188 185
189 InitReader(); 186 InitReader();
190 base::RunLoop().RunUntilIdle(); 187 base::RunLoop().RunUntilIdle();
191 } 188 }
192 189
193 } // namespace protocol 190 } // namespace protocol
194 } // namespace remoting 191 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/protocol/message_decoder_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698