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

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

Powered by Google App Engine
This is Rietveld 408576698