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

Side by Side Diff: mojo/public/cpp/bindings/tests/handle_passing_unittest.cc

Issue 1535943002: Convert Pass()→std::move() in //mojo/public/ (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Regenerate correctly Created 4 years, 12 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 <stdint.h> 5 #include <stdint.h>
6 #include <utility>
6 7
7 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
8 #include "mojo/message_pump/message_pump_mojo.h" 9 #include "mojo/message_pump/message_pump_mojo.h"
9 #include "mojo/public/cpp/bindings/binding.h" 10 #include "mojo/public/cpp/bindings/binding.h"
10 #include "mojo/public/cpp/bindings/strong_binding.h" 11 #include "mojo/public/cpp/bindings/strong_binding.h"
11 #include "mojo/public/cpp/test_support/test_utils.h" 12 #include "mojo/public/cpp/test_support/test_utils.h"
12 #include "mojo/public/interfaces/bindings/tests/sample_factory.mojom.h" 13 #include "mojo/public/interfaces/bindings/tests/sample_factory.mojom.h"
13 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
14 15
15 namespace mojo { 16 namespace mojo {
16 namespace test { 17 namespace test {
17 namespace { 18 namespace {
18 19
19 const char kText1[] = "hello"; 20 const char kText1[] = "hello";
20 const char kText2[] = "world"; 21 const char kText2[] = "world";
21 22
22 class StringRecorder { 23 class StringRecorder {
23 public: 24 public:
24 explicit StringRecorder(std::string* buf) : buf_(buf) {} 25 explicit StringRecorder(std::string* buf) : buf_(buf) {}
25 void Run(const String& a) const { *buf_ = a.To<std::string>(); } 26 void Run(const String& a) const { *buf_ = a.To<std::string>(); }
26 27
27 private: 28 private:
28 std::string* buf_; 29 std::string* buf_;
29 }; 30 };
30 31
31 class ImportedInterfaceImpl : public imported::ImportedInterface { 32 class ImportedInterfaceImpl : public imported::ImportedInterface {
32 public: 33 public:
33 explicit ImportedInterfaceImpl( 34 explicit ImportedInterfaceImpl(
34 InterfaceRequest<imported::ImportedInterface> request) 35 InterfaceRequest<imported::ImportedInterface> request)
35 : binding_(this, request.Pass()) {} 36 : binding_(this, std::move(request)) {}
36 37
37 void DoSomething() override { do_something_count_++; } 38 void DoSomething() override { do_something_count_++; }
38 39
39 static int do_something_count() { return do_something_count_; } 40 static int do_something_count() { return do_something_count_; }
40 41
41 private: 42 private:
42 static int do_something_count_; 43 static int do_something_count_;
43 Binding<ImportedInterface> binding_; 44 Binding<ImportedInterface> binding_;
44 }; 45 };
45 int ImportedInterfaceImpl::do_something_count_ = 0; 46 int ImportedInterfaceImpl::do_something_count_ = 0;
46 47
47 class SampleNamedObjectImpl : public sample::NamedObject { 48 class SampleNamedObjectImpl : public sample::NamedObject {
48 public: 49 public:
49 explicit SampleNamedObjectImpl(InterfaceRequest<sample::NamedObject> request) 50 explicit SampleNamedObjectImpl(InterfaceRequest<sample::NamedObject> request)
50 : binding_(this, request.Pass()) {} 51 : binding_(this, std::move(request)) {}
51 void SetName(const mojo::String& name) override { name_ = name; } 52 void SetName(const mojo::String& name) override { name_ = name; }
52 53
53 void GetName(const mojo::Callback<void(mojo::String)>& callback) override { 54 void GetName(const mojo::Callback<void(mojo::String)>& callback) override {
54 callback.Run(name_); 55 callback.Run(name_);
55 } 56 }
56 57
57 private: 58 private:
58 std::string name_; 59 std::string name_;
59 StrongBinding<sample::NamedObject> binding_; 60 StrongBinding<sample::NamedObject> binding_;
60 }; 61 };
61 62
62 class SampleFactoryImpl : public sample::Factory { 63 class SampleFactoryImpl : public sample::Factory {
63 public: 64 public:
64 explicit SampleFactoryImpl(InterfaceRequest<sample::Factory> request) 65 explicit SampleFactoryImpl(InterfaceRequest<sample::Factory> request)
65 : binding_(this, request.Pass()) {} 66 : binding_(this, std::move(request)) {}
66 67
67 void DoStuff(sample::RequestPtr request, 68 void DoStuff(sample::RequestPtr request,
68 ScopedMessagePipeHandle pipe, 69 ScopedMessagePipeHandle pipe,
69 const DoStuffCallback& callback) override { 70 const DoStuffCallback& callback) override {
70 std::string text1; 71 std::string text1;
71 if (pipe.is_valid()) 72 if (pipe.is_valid())
72 EXPECT_TRUE(ReadTextMessage(pipe.get(), &text1)); 73 EXPECT_TRUE(ReadTextMessage(pipe.get(), &text1));
73 74
74 std::string text2; 75 std::string text2;
75 if (request->pipe.is_valid()) { 76 if (request->pipe.is_valid()) {
76 EXPECT_TRUE(ReadTextMessage(request->pipe.get(), &text2)); 77 EXPECT_TRUE(ReadTextMessage(request->pipe.get(), &text2));
77 78
78 // Ensure that simply accessing request->pipe does not close it. 79 // Ensure that simply accessing request->pipe does not close it.
79 EXPECT_TRUE(request->pipe.is_valid()); 80 EXPECT_TRUE(request->pipe.is_valid());
80 } 81 }
81 82
82 ScopedMessagePipeHandle pipe0; 83 ScopedMessagePipeHandle pipe0;
83 if (!text2.empty()) { 84 if (!text2.empty()) {
84 CreateMessagePipe(nullptr, &pipe0, &pipe1_); 85 CreateMessagePipe(nullptr, &pipe0, &pipe1_);
85 EXPECT_TRUE(WriteTextMessage(pipe1_.get(), text2)); 86 EXPECT_TRUE(WriteTextMessage(pipe1_.get(), text2));
86 } 87 }
87 88
88 sample::ResponsePtr response(sample::Response::New()); 89 sample::ResponsePtr response(sample::Response::New());
89 response->x = 2; 90 response->x = 2;
90 response->pipe = pipe0.Pass(); 91 response->pipe = std::move(pipe0);
91 callback.Run(response.Pass(), text1); 92 callback.Run(std::move(response), text1);
92 93
93 if (request->obj) 94 if (request->obj)
94 request->obj->DoSomething(); 95 request->obj->DoSomething();
95 } 96 }
96 97
97 void DoStuff2(ScopedDataPipeConsumerHandle pipe, 98 void DoStuff2(ScopedDataPipeConsumerHandle pipe,
98 const DoStuff2Callback& callback) override { 99 const DoStuff2Callback& callback) override {
99 // Read the data from the pipe, writing the response (as a string) to 100 // Read the data from the pipe, writing the response (as a string) to
100 // DidStuff2(). 101 // DidStuff2().
101 ASSERT_TRUE(pipe.is_valid()); 102 ASSERT_TRUE(pipe.is_valid());
102 uint32_t data_size = 0; 103 uint32_t data_size = 0;
103 ASSERT_EQ(MOJO_RESULT_OK, 104 ASSERT_EQ(MOJO_RESULT_OK,
104 ReadDataRaw( 105 ReadDataRaw(
105 pipe.get(), nullptr, &data_size, MOJO_READ_DATA_FLAG_QUERY)); 106 pipe.get(), nullptr, &data_size, MOJO_READ_DATA_FLAG_QUERY));
106 ASSERT_NE(0, static_cast<int>(data_size)); 107 ASSERT_NE(0, static_cast<int>(data_size));
107 char data[64]; 108 char data[64];
108 ASSERT_LT(static_cast<int>(data_size), 64); 109 ASSERT_LT(static_cast<int>(data_size), 64);
109 ASSERT_EQ( 110 ASSERT_EQ(
110 MOJO_RESULT_OK, 111 MOJO_RESULT_OK,
111 ReadDataRaw( 112 ReadDataRaw(
112 pipe.get(), data, &data_size, MOJO_READ_DATA_FLAG_ALL_OR_NONE)); 113 pipe.get(), data, &data_size, MOJO_READ_DATA_FLAG_ALL_OR_NONE));
113 114
114 callback.Run(data); 115 callback.Run(data);
115 } 116 }
116 117
117 void CreateNamedObject( 118 void CreateNamedObject(
118 InterfaceRequest<sample::NamedObject> object_request) override { 119 InterfaceRequest<sample::NamedObject> object_request) override {
119 EXPECT_TRUE(object_request.is_pending()); 120 EXPECT_TRUE(object_request.is_pending());
120 new SampleNamedObjectImpl(object_request.Pass()); 121 new SampleNamedObjectImpl(std::move(object_request));
121 } 122 }
122 123
123 // These aren't called or implemented, but exist here to test that the 124 // These aren't called or implemented, but exist here to test that the
124 // methods are generated with the correct argument types for imported 125 // methods are generated with the correct argument types for imported
125 // interfaces. 126 // interfaces.
126 void RequestImportedInterface( 127 void RequestImportedInterface(
127 InterfaceRequest<imported::ImportedInterface> imported, 128 InterfaceRequest<imported::ImportedInterface> imported,
128 const mojo::Callback<void(InterfaceRequest<imported::ImportedInterface>)>& 129 const mojo::Callback<void(InterfaceRequest<imported::ImportedInterface>)>&
129 callback) override {} 130 callback) override {}
130 void TakeImportedInterface( 131 void TakeImportedInterface(
(...skipping 28 matching lines...) Expand all
159 if (response->pipe.is_valid()) { 160 if (response->pipe.is_valid()) {
160 std::string text2; 161 std::string text2;
161 EXPECT_TRUE(ReadTextMessage(response->pipe.get(), &text2)); 162 EXPECT_TRUE(ReadTextMessage(response->pipe.get(), &text2));
162 163
163 // Ensure that simply accessing response.pipe does not close it. 164 // Ensure that simply accessing response.pipe does not close it.
164 EXPECT_TRUE(response->pipe.is_valid()); 165 EXPECT_TRUE(response->pipe.is_valid());
165 166
166 EXPECT_EQ(std::string(kText2), text2); 167 EXPECT_EQ(std::string(kText2), text2);
167 168
168 // Do some more tests of handle passing: 169 // Do some more tests of handle passing:
169 ScopedMessagePipeHandle p = response->pipe.Pass(); 170 ScopedMessagePipeHandle p = std::move(response->pipe);
170 EXPECT_TRUE(p.is_valid()); 171 EXPECT_TRUE(p.is_valid());
171 EXPECT_FALSE(response->pipe.is_valid()); 172 EXPECT_FALSE(response->pipe.is_valid());
172 } 173 }
173 174
174 *got_response = true; 175 *got_response = true;
175 } 176 }
176 177
177 bool* got_response; 178 bool* got_response;
178 std::string* got_text_reply; 179 std::string* got_text_reply;
179 }; 180 };
180 181
181 TEST_F(HandlePassingTest, Basic) { 182 TEST_F(HandlePassingTest, Basic) {
182 sample::FactoryPtr factory; 183 sample::FactoryPtr factory;
183 SampleFactoryImpl factory_impl(GetProxy(&factory)); 184 SampleFactoryImpl factory_impl(GetProxy(&factory));
184 185
185 MessagePipe pipe0; 186 MessagePipe pipe0;
186 EXPECT_TRUE(WriteTextMessage(pipe0.handle1.get(), kText1)); 187 EXPECT_TRUE(WriteTextMessage(pipe0.handle1.get(), kText1));
187 188
188 MessagePipe pipe1; 189 MessagePipe pipe1;
189 EXPECT_TRUE(WriteTextMessage(pipe1.handle1.get(), kText2)); 190 EXPECT_TRUE(WriteTextMessage(pipe1.handle1.get(), kText2));
190 191
191 imported::ImportedInterfacePtr imported; 192 imported::ImportedInterfacePtr imported;
192 ImportedInterfaceImpl imported_impl(GetProxy(&imported)); 193 ImportedInterfaceImpl imported_impl(GetProxy(&imported));
193 194
194 sample::RequestPtr request(sample::Request::New()); 195 sample::RequestPtr request(sample::Request::New());
195 request->x = 1; 196 request->x = 1;
196 request->pipe = pipe1.handle0.Pass(); 197 request->pipe = std::move(pipe1.handle0);
197 request->obj = imported.Pass(); 198 request->obj = std::move(imported);
198 bool got_response = false; 199 bool got_response = false;
199 std::string got_text_reply; 200 std::string got_text_reply;
200 DoStuffCallback cb(&got_response, &got_text_reply); 201 DoStuffCallback cb(&got_response, &got_text_reply);
201 factory->DoStuff(request.Pass(), pipe0.handle0.Pass(), cb); 202 factory->DoStuff(std::move(request), std::move(pipe0.handle0), cb);
202 203
203 EXPECT_FALSE(*cb.got_response); 204 EXPECT_FALSE(*cb.got_response);
204 int count_before = ImportedInterfaceImpl::do_something_count(); 205 int count_before = ImportedInterfaceImpl::do_something_count();
205 206
206 PumpMessages(); 207 PumpMessages();
207 208
208 EXPECT_TRUE(*cb.got_response); 209 EXPECT_TRUE(*cb.got_response);
209 EXPECT_EQ(kText1, *cb.got_text_reply); 210 EXPECT_EQ(kText1, *cb.got_text_reply);
210 EXPECT_EQ(1, ImportedInterfaceImpl::do_something_count() - count_before); 211 EXPECT_EQ(1, ImportedInterfaceImpl::do_something_count() - count_before);
211 } 212 }
212 213
213 TEST_F(HandlePassingTest, PassInvalid) { 214 TEST_F(HandlePassingTest, PassInvalid) {
214 sample::FactoryPtr factory; 215 sample::FactoryPtr factory;
215 SampleFactoryImpl factory_impl(GetProxy(&factory)); 216 SampleFactoryImpl factory_impl(GetProxy(&factory));
216 217
217 sample::RequestPtr request(sample::Request::New()); 218 sample::RequestPtr request(sample::Request::New());
218 request->x = 1; 219 request->x = 1;
219 bool got_response = false; 220 bool got_response = false;
220 std::string got_text_reply; 221 std::string got_text_reply;
221 DoStuffCallback cb(&got_response, &got_text_reply); 222 DoStuffCallback cb(&got_response, &got_text_reply);
222 factory->DoStuff(request.Pass(), ScopedMessagePipeHandle().Pass(), cb); 223 factory->DoStuff(std::move(request), ScopedMessagePipeHandle(), cb);
223 224
224 EXPECT_FALSE(*cb.got_response); 225 EXPECT_FALSE(*cb.got_response);
225 226
226 PumpMessages(); 227 PumpMessages();
227 228
228 EXPECT_TRUE(*cb.got_response); 229 EXPECT_TRUE(*cb.got_response);
229 } 230 }
230 231
231 struct DoStuff2Callback { 232 struct DoStuff2Callback {
232 DoStuff2Callback(bool* got_response, std::string* got_text_reply) 233 DoStuff2Callback(bool* got_response, std::string* got_text_reply)
(...skipping 28 matching lines...) Expand all
261 uint32_t data_size = static_cast<uint32_t>(expected_text_reply.size() + 1); 262 uint32_t data_size = static_cast<uint32_t>(expected_text_reply.size() + 1);
262 ASSERT_EQ(MOJO_RESULT_OK, 263 ASSERT_EQ(MOJO_RESULT_OK,
263 WriteDataRaw(producer_handle.get(), 264 WriteDataRaw(producer_handle.get(),
264 expected_text_reply.c_str(), 265 expected_text_reply.c_str(),
265 &data_size, 266 &data_size,
266 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); 267 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE));
267 268
268 bool got_response = false; 269 bool got_response = false;
269 std::string got_text_reply; 270 std::string got_text_reply;
270 DoStuff2Callback cb(&got_response, &got_text_reply); 271 DoStuff2Callback cb(&got_response, &got_text_reply);
271 factory->DoStuff2(consumer_handle.Pass(), cb); 272 factory->DoStuff2(std::move(consumer_handle), cb);
272 273
273 EXPECT_FALSE(*cb.got_response); 274 EXPECT_FALSE(*cb.got_response);
274 275
275 PumpMessages(); 276 PumpMessages();
276 277
277 EXPECT_TRUE(*cb.got_response); 278 EXPECT_TRUE(*cb.got_response);
278 EXPECT_EQ(expected_text_reply, *cb.got_text_reply); 279 EXPECT_EQ(expected_text_reply, *cb.got_text_reply);
279 } 280 }
280 281
281 TEST_F(HandlePassingTest, PipesAreClosed) { 282 TEST_F(HandlePassingTest, PipesAreClosed) {
282 sample::FactoryPtr factory; 283 sample::FactoryPtr factory;
283 SampleFactoryImpl factory_impl(GetProxy(&factory)); 284 SampleFactoryImpl factory_impl(GetProxy(&factory));
284 285
285 MessagePipe extra_pipe; 286 MessagePipe extra_pipe;
286 287
287 MojoHandle handle0_value = extra_pipe.handle0.get().value(); 288 MojoHandle handle0_value = extra_pipe.handle0.get().value();
288 MojoHandle handle1_value = extra_pipe.handle1.get().value(); 289 MojoHandle handle1_value = extra_pipe.handle1.get().value();
289 290
290 { 291 {
291 Array<ScopedMessagePipeHandle> pipes(2); 292 Array<ScopedMessagePipeHandle> pipes(2);
292 pipes[0] = extra_pipe.handle0.Pass(); 293 pipes[0] = std::move(extra_pipe.handle0);
293 pipes[1] = extra_pipe.handle1.Pass(); 294 pipes[1] = std::move(extra_pipe.handle1);
294 295
295 sample::RequestPtr request(sample::Request::New()); 296 sample::RequestPtr request(sample::Request::New());
296 request->more_pipes = pipes.Pass(); 297 request->more_pipes = std::move(pipes);
297 298
298 factory->DoStuff(request.Pass(), ScopedMessagePipeHandle(), 299 factory->DoStuff(std::move(request), ScopedMessagePipeHandle(),
299 sample::Factory::DoStuffCallback()); 300 sample::Factory::DoStuffCallback());
300 } 301 }
301 302
302 // We expect the pipes to have been closed. 303 // We expect the pipes to have been closed.
303 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(handle0_value)); 304 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(handle0_value));
304 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(handle1_value)); 305 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(handle1_value));
305 } 306 }
306 307
307 TEST_F(HandlePassingTest, IsHandle) { 308 TEST_F(HandlePassingTest, IsHandle) {
308 // Validate that mojo::internal::IsHandle<> works as expected since this. 309 // Validate that mojo::internal::IsHandle<> works as expected since this.
(...skipping 12 matching lines...) Expand all
321 322
322 TEST_F(HandlePassingTest, CreateNamedObject) { 323 TEST_F(HandlePassingTest, CreateNamedObject) {
323 sample::FactoryPtr factory; 324 sample::FactoryPtr factory;
324 SampleFactoryImpl factory_impl(GetProxy(&factory)); 325 SampleFactoryImpl factory_impl(GetProxy(&factory));
325 326
326 sample::NamedObjectPtr object1; 327 sample::NamedObjectPtr object1;
327 EXPECT_FALSE(object1); 328 EXPECT_FALSE(object1);
328 329
329 InterfaceRequest<sample::NamedObject> object1_request = GetProxy(&object1); 330 InterfaceRequest<sample::NamedObject> object1_request = GetProxy(&object1);
330 EXPECT_TRUE(object1_request.is_pending()); 331 EXPECT_TRUE(object1_request.is_pending());
331 factory->CreateNamedObject(object1_request.Pass()); 332 factory->CreateNamedObject(std::move(object1_request));
332 EXPECT_FALSE(object1_request.is_pending()); // We've passed the request. 333 EXPECT_FALSE(object1_request.is_pending()); // We've passed the request.
333 334
334 ASSERT_TRUE(object1); 335 ASSERT_TRUE(object1);
335 object1->SetName("object1"); 336 object1->SetName("object1");
336 337
337 sample::NamedObjectPtr object2; 338 sample::NamedObjectPtr object2;
338 factory->CreateNamedObject(GetProxy(&object2)); 339 factory->CreateNamedObject(GetProxy(&object2));
339 object2->SetName("object2"); 340 object2->SetName("object2");
340 341
341 std::string name1; 342 std::string name1;
342 object1->GetName(StringRecorder(&name1)); 343 object1->GetName(StringRecorder(&name1));
343 344
344 std::string name2; 345 std::string name2;
345 object2->GetName(StringRecorder(&name2)); 346 object2->GetName(StringRecorder(&name2));
346 347
347 PumpMessages(); // Yield for results. 348 PumpMessages(); // Yield for results.
348 349
349 EXPECT_EQ(std::string("object1"), name1); 350 EXPECT_EQ(std::string("object1"), name1);
350 EXPECT_EQ(std::string("object2"), name2); 351 EXPECT_EQ(std::string("object2"), name2);
351 } 352 }
352 353
353 } // namespace 354 } // namespace
354 } // namespace test 355 } // namespace test
355 } // namespace mojo 356 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/tests/equals_unittest.cc ('k') | mojo/public/cpp/bindings/tests/interface_ptr_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698