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

Side by Side Diff: blimp/net/test_common.h

Issue 1452823011: Make PacketReader/PacketWriter interfaces async-only. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 5 years 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 | « blimp/net/stream_packet_writer_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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 #ifndef BLIMP_NET_TEST_COMMON_H_ 5 #ifndef BLIMP_NET_TEST_COMMON_H_
6 #define BLIMP_NET_TEST_COMMON_H_ 6 #define BLIMP_NET_TEST_COMMON_H_
7 7
8 #include <string> 8 #include <string>
9 9
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
(...skipping 17 matching lines...) Expand all
28 // Using this matcher for inequality checks will result in undefined behavior, 28 // Using this matcher for inequality checks will result in undefined behavior,
29 // due to IOBuffer's lack of a size field. 29 // due to IOBuffer's lack of a size field.
30 // 30 //
31 // arg (type: IOBuffer*) The buffer to check. 31 // arg (type: IOBuffer*) The buffer to check.
32 // data (type: std::string) The string to compare with |arg|. 32 // data (type: std::string) The string to compare with |arg|.
33 MATCHER_P(BufferEquals, expected, "") { 33 MATCHER_P(BufferEquals, expected, "") {
34 return expected == std::string(arg->data(), expected.size()); 34 return expected == std::string(arg->data(), expected.size());
35 } 35 }
36 36
37 // Checks if two proto messages are the same. 37 // Checks if two proto messages are the same.
38 // TODO(kmarshall): promote to a shared testing library.
39 MATCHER_P(EqualsProto, message, "") { 38 MATCHER_P(EqualsProto, message, "") {
40 std::string expected_serialized; 39 std::string expected_serialized;
41 std::string actual_serialized; 40 std::string actual_serialized;
42 message.SerializeToString(&expected_serialized); 41 message.SerializeToString(&expected_serialized);
43 arg.SerializeToString(&actual_serialized); 42 arg.SerializeToString(&actual_serialized);
44 return expected_serialized == actual_serialized; 43 return expected_serialized == actual_serialized;
45 } 44 }
46 45
47 // Checks if the contents of a buffer are an exact match with BlimpMessage. 46 // Checks if the contents of a buffer are an exact match with BlimpMessage.
48 // arg (type: net::DrainableIOBuffer*) The buffer to check. 47 // arg (type: net::DrainableIOBuffer*) The buffer to check.
49 // message (type: BlimpMessage) The message to compare with |arg|. 48 // message (type: BlimpMessage) The message to compare with |arg|.
50 MATCHER_P(BufferEqualsProto, message, "") { 49 MATCHER_P(BufferEqualsProto, message, "") {
51 BlimpMessage actual_message; 50 BlimpMessage actual_message;
52 actual_message.ParseFromArray(arg->data(), arg->BytesRemaining()); 51 actual_message.ParseFromArray(arg->data(), message.ByteSize());
53 std::string expected_serialized; 52 std::string expected_serialized;
54 std::string actual_serialized; 53 std::string actual_serialized;
55 message.SerializeToString(&expected_serialized); 54 message.SerializeToString(&expected_serialized);
56 actual_message.SerializeToString(&actual_serialized); 55 actual_message.SerializeToString(&actual_serialized);
57 return expected_serialized == actual_serialized; 56 return expected_serialized == actual_serialized;
58 } 57 }
59 58
60 // GMock action that writes data from a string to an IOBuffer. 59 // GMock action that writes data from a string to an IOBuffer.
61 // 60 //
62 // buf_idx (template parameter 0): 0-based index of the IOBuffer arg. 61 // buf_idx (template parameter 0): 0-based index of the IOBuffer arg.
63 // str: the string containing data to be written to the IOBuffer. 62 // str: the string containing data to be written to the IOBuffer.
64 ACTION_TEMPLATE(FillBufferFromString, 63 ACTION_TEMPLATE(FillBufferFromString,
65 HAS_1_TEMPLATE_PARAMS(int, buf_idx), 64 HAS_1_TEMPLATE_PARAMS(int, buf_idx),
66 AND_1_VALUE_PARAMS(str)) { 65 AND_1_VALUE_PARAMS(str)) {
67 memcpy(testing::get<buf_idx>(args)->data(), str.data(), str.size()); 66 memcpy(testing::get<buf_idx>(args)->data(), str.data(), str.size());
68 } 67 }
69 68
70 // Returns true if |buf| has a prefix of |str|. 69 // Returns true if |buf| has a prefix of |str|.
71 // Behavior is undefined if len(buf) < len(str). 70 // Behavior is undefined if len(buf) < len(str).
72 bool BufferStartsWith(net::GrowableIOBuffer* buf, const std::string& str); 71 bool BufferStartsWith(net::GrowableIOBuffer* buf, const std::string& str);
73 72
74 // GMock action that writes data from a blimp message to an IOBuffer . 73 // GMock action that writes data from a BlimpMessage to a GrowableIOBuffer.
74 // Advances the buffer's |offset| to the end of the message.
75 // 75 //
76 // buf_idx (template parameter 0): 0-based index of the IOBuffer arg. 76 // buf_idx (template parameter 0): 0-based index of the IOBuffer arg.
77 // message: the blimp message containing data to be written to the IOBuffer 77 // message: the blimp message containing data to be written to the IOBuffer
78 ACTION_TEMPLATE(FillBufferFromMessage, 78 ACTION_TEMPLATE(FillBufferFromMessage,
79 HAS_1_TEMPLATE_PARAMS(int, buf_idx), 79 HAS_1_TEMPLATE_PARAMS(int, buf_idx),
80 AND_1_VALUE_PARAMS(message)) { 80 AND_1_VALUE_PARAMS(message)) {
81 message->SerializeToArray(testing::get<buf_idx>(args)->data(), 81 message->SerializeToArray(testing::get<buf_idx>(args)->data(),
82 message->ByteSize()); 82 message->ByteSize());
83 testing::get<buf_idx>(args)->set_offset(message->ByteSize());
84 }
85
86 // Calls |set_offset()| for a GrowableIOBuffer.
87 ACTION_TEMPLATE(SetBufferOffset,
88 HAS_1_TEMPLATE_PARAMS(int, buf_idx),
89 AND_1_VALUE_PARAMS(offset)) {
90 testing::get<buf_idx>(args)->set_offset(offset);
83 } 91 }
84 92
85 // Formats a string-based representation of a BlimpMessage header. 93 // Formats a string-based representation of a BlimpMessage header.
86 std::string EncodeHeader(size_t size); 94 std::string EncodeHeader(size_t size);
87 95
88 class MockStreamSocket : public net::StreamSocket { 96 class MockStreamSocket : public net::StreamSocket {
89 public: 97 public:
90 MockStreamSocket(); 98 MockStreamSocket();
91 virtual ~MockStreamSocket(); 99 virtual ~MockStreamSocket();
92 100
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 MOCK_METHOD1(HandleConnectionPtr, void(BlimpConnection* connection)); 144 MOCK_METHOD1(HandleConnectionPtr, void(BlimpConnection* connection));
137 void HandleConnection(scoped_ptr<BlimpConnection> connection) override; 145 void HandleConnection(scoped_ptr<BlimpConnection> connection) override;
138 }; 146 };
139 147
140 class MockPacketReader : public PacketReader { 148 class MockPacketReader : public PacketReader {
141 public: 149 public:
142 MockPacketReader(); 150 MockPacketReader();
143 ~MockPacketReader() override; 151 ~MockPacketReader() override;
144 152
145 MOCK_METHOD2(ReadPacket, 153 MOCK_METHOD2(ReadPacket,
146 int(const scoped_refptr<net::GrowableIOBuffer>&, 154 void(const scoped_refptr<net::GrowableIOBuffer>&,
147 const net::CompletionCallback&)); 155 const net::CompletionCallback&));
148 }; 156 };
149 157
150 class MockPacketWriter : public PacketWriter { 158 class MockPacketWriter : public PacketWriter {
151 public: 159 public:
152 MockPacketWriter(); 160 MockPacketWriter();
153 ~MockPacketWriter() override; 161 ~MockPacketWriter() override;
154 162
155 MOCK_METHOD2(WritePacket, 163 MOCK_METHOD2(WritePacket,
156 int(scoped_refptr<net::DrainableIOBuffer>, 164 void(scoped_refptr<net::DrainableIOBuffer>,
157 const net::CompletionCallback&)); 165 const net::CompletionCallback&));
158 }; 166 };
159 167
160 class MockConnectionErrorObserver : public ConnectionErrorObserver { 168 class MockConnectionErrorObserver : public ConnectionErrorObserver {
161 public: 169 public:
162 MockConnectionErrorObserver(); 170 MockConnectionErrorObserver();
163 ~MockConnectionErrorObserver() override; 171 ~MockConnectionErrorObserver() override;
164 172
165 MOCK_METHOD1(OnConnectionError, void(int error)); 173 MOCK_METHOD1(OnConnectionError, void(int error));
166 }; 174 };
167 175
168 class MockBlimpMessageProcessor : public BlimpMessageProcessor { 176 class MockBlimpMessageProcessor : public BlimpMessageProcessor {
169 public: 177 public:
170 MockBlimpMessageProcessor(); 178 MockBlimpMessageProcessor();
171 179
172 ~MockBlimpMessageProcessor() override; 180 ~MockBlimpMessageProcessor() override;
173 181
174 // Adapts calls from ProcessMessage to MockableProcessMessage by 182 // Adapts calls from ProcessMessage to MockableProcessMessage by
175 // unboxing the |message| scoped_ptr for GMock compatibility. 183 // unboxing the |message| scoped_ptr for GMock compatibility.
176 void ProcessMessage(scoped_ptr<BlimpMessage> message, 184 void ProcessMessage(scoped_ptr<BlimpMessage> message,
177 const net::CompletionCallback& callback) override; 185 const net::CompletionCallback& callback) override;
178 186
179 MOCK_METHOD2(MockableProcessMessage, 187 MOCK_METHOD2(MockableProcessMessage,
180 void(const BlimpMessage& message, 188 void(const BlimpMessage& message,
181 const net::CompletionCallback& callback)); 189 const net::CompletionCallback& callback));
182 }; 190 };
183 191
184 } // namespace blimp 192 } // namespace blimp
185 193
186 #endif // BLIMP_NET_TEST_COMMON_H_ 194 #endif // BLIMP_NET_TEST_COMMON_H_
OLDNEW
« no previous file with comments | « blimp/net/stream_packet_writer_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698