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

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

Issue 1552983003: Fix a bunch of mojo_public_*_unittests with the new EDK. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review comments Created 4 years, 11 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 #include <utility>
7 7
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h"
9 #include "mojo/message_pump/message_pump_mojo.h" 10 #include "mojo/message_pump/message_pump_mojo.h"
10 #include "mojo/public/cpp/bindings/binding.h" 11 #include "mojo/public/cpp/bindings/binding.h"
11 #include "mojo/public/cpp/bindings/strong_binding.h" 12 #include "mojo/public/cpp/bindings/strong_binding.h"
12 #include "mojo/public/cpp/test_support/test_utils.h" 13 #include "mojo/public/cpp/test_support/test_utils.h"
13 #include "mojo/public/interfaces/bindings/tests/sample_factory.mojom.h" 14 #include "mojo/public/interfaces/bindings/tests/sample_factory.mojom.h"
14 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
15 16
16 namespace mojo { 17 namespace mojo {
17 namespace test { 18 namespace test {
18 namespace { 19 namespace {
19 20
20 const char kText1[] = "hello"; 21 const char kText1[] = "hello";
21 const char kText2[] = "world"; 22 const char kText2[] = "world";
22 23
23 class StringRecorder { 24 class StringRecorder {
24 public: 25 public:
25 explicit StringRecorder(std::string* buf) : buf_(buf) {} 26 StringRecorder(std::string* buf, const base::Closure& closure)
26 void Run(const String& a) const { *buf_ = a.To<std::string>(); } 27 : buf_(buf), closure_(closure) {}
28 void Run(const String& a) const {
29 *buf_ = a.To<std::string>();
30 closure_.Run();
31 }
27 32
28 private: 33 private:
29 std::string* buf_; 34 std::string* buf_;
35 base::Closure closure_;
30 }; 36 };
31 37
32 class ImportedInterfaceImpl : public imported::ImportedInterface { 38 class ImportedInterfaceImpl : public imported::ImportedInterface {
33 public: 39 public:
34 explicit ImportedInterfaceImpl( 40 ImportedInterfaceImpl(
35 InterfaceRequest<imported::ImportedInterface> request) 41 InterfaceRequest<imported::ImportedInterface> request,
36 : binding_(this, std::move(request)) {} 42 const base::Closure& closure)
43 : binding_(this, std::move(request)), closure_(closure) {}
37 44
38 void DoSomething() override { do_something_count_++; } 45 void DoSomething() override {
46 do_something_count_++;
47 closure_.Run();
48 }
39 49
40 static int do_something_count() { return do_something_count_; } 50 static int do_something_count() { return do_something_count_; }
41 51
42 private: 52 private:
43 static int do_something_count_; 53 static int do_something_count_;
44 Binding<ImportedInterface> binding_; 54 Binding<ImportedInterface> binding_;
55 base::Closure closure_;
45 }; 56 };
46 int ImportedInterfaceImpl::do_something_count_ = 0; 57 int ImportedInterfaceImpl::do_something_count_ = 0;
47 58
48 class SampleNamedObjectImpl : public sample::NamedObject { 59 class SampleNamedObjectImpl : public sample::NamedObject {
49 public: 60 public:
50 explicit SampleNamedObjectImpl(InterfaceRequest<sample::NamedObject> request) 61 explicit SampleNamedObjectImpl(InterfaceRequest<sample::NamedObject> request)
51 : binding_(this, std::move(request)) {} 62 : binding_(this, std::move(request)) {}
52 void SetName(const mojo::String& name) override { name_ = name; } 63 void SetName(const mojo::String& name) override { name_ = name; }
53 64
54 void GetName(const mojo::Callback<void(mojo::String)>& callback) override { 65 void GetName(const mojo::Callback<void(mojo::String)>& callback) override {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 if (request->obj) 105 if (request->obj)
95 request->obj->DoSomething(); 106 request->obj->DoSomething();
96 } 107 }
97 108
98 void DoStuff2(ScopedDataPipeConsumerHandle pipe, 109 void DoStuff2(ScopedDataPipeConsumerHandle pipe,
99 const DoStuff2Callback& callback) override { 110 const DoStuff2Callback& callback) override {
100 // Read the data from the pipe, writing the response (as a string) to 111 // Read the data from the pipe, writing the response (as a string) to
101 // DidStuff2(). 112 // DidStuff2().
102 ASSERT_TRUE(pipe.is_valid()); 113 ASSERT_TRUE(pipe.is_valid());
103 uint32_t data_size = 0; 114 uint32_t data_size = 0;
115
116 MojoHandleSignalsState state;
117 ASSERT_EQ(MOJO_RESULT_OK,
118 MojoWait(pipe.get().value(), MOJO_HANDLE_SIGNAL_READABLE,
119 MOJO_DEADLINE_INDEFINITE, &state));
120 ASSERT_EQ(MOJO_HANDLE_SIGNAL_READABLE, state.satisfied_signals);
104 ASSERT_EQ(MOJO_RESULT_OK, 121 ASSERT_EQ(MOJO_RESULT_OK,
105 ReadDataRaw( 122 ReadDataRaw(
106 pipe.get(), nullptr, &data_size, MOJO_READ_DATA_FLAG_QUERY)); 123 pipe.get(), nullptr, &data_size, MOJO_READ_DATA_FLAG_QUERY));
107 ASSERT_NE(0, static_cast<int>(data_size)); 124 ASSERT_NE(0, static_cast<int>(data_size));
108 char data[64]; 125 char data[64];
109 ASSERT_LT(static_cast<int>(data_size), 64); 126 ASSERT_LT(static_cast<int>(data_size), 64);
110 ASSERT_EQ( 127 ASSERT_EQ(
111 MOJO_RESULT_OK, 128 MOJO_RESULT_OK,
112 ReadDataRaw( 129 ReadDataRaw(
113 pipe.get(), data, &data_size, MOJO_READ_DATA_FLAG_ALL_OR_NONE)); 130 pipe.get(), data, &data_size, MOJO_READ_DATA_FLAG_ALL_OR_NONE));
(...skipping 30 matching lines...) Expand all
144 161
145 void TearDown() override { PumpMessages(); } 162 void TearDown() override { PumpMessages(); }
146 163
147 void PumpMessages() { loop_.RunUntilIdle(); } 164 void PumpMessages() { loop_.RunUntilIdle(); }
148 165
149 private: 166 private:
150 base::MessageLoop loop_; 167 base::MessageLoop loop_;
151 }; 168 };
152 169
153 struct DoStuffCallback { 170 struct DoStuffCallback {
154 DoStuffCallback(bool* got_response, std::string* got_text_reply) 171 DoStuffCallback(bool* got_response,
155 : got_response(got_response), got_text_reply(got_text_reply) {} 172 std::string* got_text_reply,
173 const base::Closure& closure)
174 : got_response(got_response),
175 got_text_reply(got_text_reply),
176 closure(closure) {}
156 177
157 void Run(sample::ResponsePtr response, const String& text_reply) const { 178 void Run(sample::ResponsePtr response, const String& text_reply) const {
158 *got_text_reply = text_reply; 179 *got_text_reply = text_reply;
159 180
160 if (response->pipe.is_valid()) { 181 if (response->pipe.is_valid()) {
161 std::string text2; 182 std::string text2;
162 EXPECT_TRUE(ReadTextMessage(response->pipe.get(), &text2)); 183 EXPECT_TRUE(ReadTextMessage(response->pipe.get(), &text2));
163 184
164 // Ensure that simply accessing response.pipe does not close it. 185 // Ensure that simply accessing response.pipe does not close it.
165 EXPECT_TRUE(response->pipe.is_valid()); 186 EXPECT_TRUE(response->pipe.is_valid());
166 187
167 EXPECT_EQ(std::string(kText2), text2); 188 EXPECT_EQ(std::string(kText2), text2);
168 189
169 // Do some more tests of handle passing: 190 // Do some more tests of handle passing:
170 ScopedMessagePipeHandle p = std::move(response->pipe); 191 ScopedMessagePipeHandle p = std::move(response->pipe);
171 EXPECT_TRUE(p.is_valid()); 192 EXPECT_TRUE(p.is_valid());
172 EXPECT_FALSE(response->pipe.is_valid()); 193 EXPECT_FALSE(response->pipe.is_valid());
173 } 194 }
174 195
175 *got_response = true; 196 *got_response = true;
197 closure.Run();
176 } 198 }
177 199
178 bool* got_response; 200 bool* got_response;
179 std::string* got_text_reply; 201 std::string* got_text_reply;
202 base::Closure closure;
180 }; 203 };
181 204
182 TEST_F(HandlePassingTest, Basic) { 205 TEST_F(HandlePassingTest, Basic) {
183 sample::FactoryPtr factory; 206 sample::FactoryPtr factory;
184 SampleFactoryImpl factory_impl(GetProxy(&factory)); 207 SampleFactoryImpl factory_impl(GetProxy(&factory));
185 208
186 MessagePipe pipe0; 209 MessagePipe pipe0;
187 EXPECT_TRUE(WriteTextMessage(pipe0.handle1.get(), kText1)); 210 EXPECT_TRUE(WriteTextMessage(pipe0.handle1.get(), kText1));
188 211
189 MessagePipe pipe1; 212 MessagePipe pipe1;
190 EXPECT_TRUE(WriteTextMessage(pipe1.handle1.get(), kText2)); 213 EXPECT_TRUE(WriteTextMessage(pipe1.handle1.get(), kText2));
191 214
192 imported::ImportedInterfacePtr imported; 215 imported::ImportedInterfacePtr imported;
193 ImportedInterfaceImpl imported_impl(GetProxy(&imported)); 216 base::RunLoop run_loop;
217 ImportedInterfaceImpl imported_impl(GetProxy(&imported),
218 run_loop.QuitClosure());
194 219
195 sample::RequestPtr request(sample::Request::New()); 220 sample::RequestPtr request(sample::Request::New());
196 request->x = 1; 221 request->x = 1;
197 request->pipe = std::move(pipe1.handle0); 222 request->pipe = std::move(pipe1.handle0);
198 request->obj = std::move(imported); 223 request->obj = std::move(imported);
199 bool got_response = false; 224 bool got_response = false;
200 std::string got_text_reply; 225 std::string got_text_reply;
201 DoStuffCallback cb(&got_response, &got_text_reply); 226 base::RunLoop run_loop2;
227 DoStuffCallback cb(&got_response, &got_text_reply, run_loop2.QuitClosure());
202 factory->DoStuff(std::move(request), std::move(pipe0.handle0), cb); 228 factory->DoStuff(std::move(request), std::move(pipe0.handle0), cb);
203 229
204 EXPECT_FALSE(*cb.got_response); 230 EXPECT_FALSE(*cb.got_response);
205 int count_before = ImportedInterfaceImpl::do_something_count(); 231 int count_before = ImportedInterfaceImpl::do_something_count();
206 232
207 PumpMessages(); 233 run_loop.Run();
234 run_loop2.Run();
208 235
209 EXPECT_TRUE(*cb.got_response); 236 EXPECT_TRUE(*cb.got_response);
210 EXPECT_EQ(kText1, *cb.got_text_reply); 237 EXPECT_EQ(kText1, *cb.got_text_reply);
211 EXPECT_EQ(1, ImportedInterfaceImpl::do_something_count() - count_before); 238 EXPECT_EQ(1, ImportedInterfaceImpl::do_something_count() - count_before);
212 } 239 }
213 240
214 TEST_F(HandlePassingTest, PassInvalid) { 241 TEST_F(HandlePassingTest, PassInvalid) {
215 sample::FactoryPtr factory; 242 sample::FactoryPtr factory;
216 SampleFactoryImpl factory_impl(GetProxy(&factory)); 243 SampleFactoryImpl factory_impl(GetProxy(&factory));
217 244
218 sample::RequestPtr request(sample::Request::New()); 245 sample::RequestPtr request(sample::Request::New());
219 request->x = 1; 246 request->x = 1;
220 bool got_response = false; 247 bool got_response = false;
221 std::string got_text_reply; 248 std::string got_text_reply;
222 DoStuffCallback cb(&got_response, &got_text_reply); 249 base::RunLoop run_loop;
250 DoStuffCallback cb(&got_response, &got_text_reply, run_loop.QuitClosure());
223 factory->DoStuff(std::move(request), ScopedMessagePipeHandle(), cb); 251 factory->DoStuff(std::move(request), ScopedMessagePipeHandle(), cb);
224 252
225 EXPECT_FALSE(*cb.got_response); 253 EXPECT_FALSE(*cb.got_response);
226 254
227 PumpMessages(); 255 run_loop.Run();
228 256
229 EXPECT_TRUE(*cb.got_response); 257 EXPECT_TRUE(*cb.got_response);
230 } 258 }
231 259
232 struct DoStuff2Callback { 260 struct DoStuff2Callback {
233 DoStuff2Callback(bool* got_response, std::string* got_text_reply) 261 DoStuff2Callback(bool* got_response,
234 : got_response(got_response), got_text_reply(got_text_reply) {} 262 std::string* got_text_reply,
263 const base::Closure& closure)
264 : got_response(got_response),
265 got_text_reply(got_text_reply),
266 closure(closure) {}
235 267
236 void Run(const String& text_reply) const { 268 void Run(const String& text_reply) const {
237 *got_response = true; 269 *got_response = true;
238 *got_text_reply = text_reply; 270 *got_text_reply = text_reply;
271 closure.Run();
239 } 272 }
240 273
241 bool* got_response; 274 bool* got_response;
242 std::string* got_text_reply; 275 std::string* got_text_reply;
276 base::Closure closure;
243 }; 277 };
244 278
245 // Verifies DataPipeConsumer can be passed and read from. 279 // Verifies DataPipeConsumer can be passed and read from.
246 TEST_F(HandlePassingTest, DataPipe) { 280 TEST_F(HandlePassingTest, DataPipe) {
247 sample::FactoryPtr factory; 281 sample::FactoryPtr factory;
248 SampleFactoryImpl factory_impl(GetProxy(&factory)); 282 SampleFactoryImpl factory_impl(GetProxy(&factory));
249 283
250 // Writes a string to a data pipe and passes the data pipe (consumer) to the 284 // Writes a string to a data pipe and passes the data pipe (consumer) to the
251 // factory. 285 // factory.
252 ScopedDataPipeProducerHandle producer_handle; 286 ScopedDataPipeProducerHandle producer_handle;
253 ScopedDataPipeConsumerHandle consumer_handle; 287 ScopedDataPipeConsumerHandle consumer_handle;
254 MojoCreateDataPipeOptions options = {sizeof(MojoCreateDataPipeOptions), 288 MojoCreateDataPipeOptions options = {sizeof(MojoCreateDataPipeOptions),
255 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, 289 MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE,
256 1, 290 1,
257 1024}; 291 1024};
258 ASSERT_EQ(MOJO_RESULT_OK, 292 ASSERT_EQ(MOJO_RESULT_OK,
259 CreateDataPipe(&options, &producer_handle, &consumer_handle)); 293 CreateDataPipe(&options, &producer_handle, &consumer_handle));
260 std::string expected_text_reply = "got it"; 294 std::string expected_text_reply = "got it";
261 // +1 for \0. 295 // +1 for \0.
262 uint32_t data_size = static_cast<uint32_t>(expected_text_reply.size() + 1); 296 uint32_t data_size = static_cast<uint32_t>(expected_text_reply.size() + 1);
263 ASSERT_EQ(MOJO_RESULT_OK, 297 ASSERT_EQ(MOJO_RESULT_OK,
264 WriteDataRaw(producer_handle.get(), 298 WriteDataRaw(producer_handle.get(),
265 expected_text_reply.c_str(), 299 expected_text_reply.c_str(),
266 &data_size, 300 &data_size,
267 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); 301 MOJO_WRITE_DATA_FLAG_ALL_OR_NONE));
268 302
269 bool got_response = false; 303 bool got_response = false;
270 std::string got_text_reply; 304 std::string got_text_reply;
271 DoStuff2Callback cb(&got_response, &got_text_reply); 305 base::RunLoop run_loop;
306 DoStuff2Callback cb(&got_response, &got_text_reply, run_loop.QuitClosure());
272 factory->DoStuff2(std::move(consumer_handle), cb); 307 factory->DoStuff2(std::move(consumer_handle), cb);
273 308
274 EXPECT_FALSE(*cb.got_response); 309 EXPECT_FALSE(*cb.got_response);
275 310
276 PumpMessages(); 311 run_loop.Run();
277 312
278 EXPECT_TRUE(*cb.got_response); 313 EXPECT_TRUE(*cb.got_response);
279 EXPECT_EQ(expected_text_reply, *cb.got_text_reply); 314 EXPECT_EQ(expected_text_reply, *cb.got_text_reply);
280 } 315 }
281 316
282 TEST_F(HandlePassingTest, PipesAreClosed) { 317 TEST_F(HandlePassingTest, PipesAreClosed) {
283 sample::FactoryPtr factory; 318 sample::FactoryPtr factory;
284 SampleFactoryImpl factory_impl(GetProxy(&factory)); 319 SampleFactoryImpl factory_impl(GetProxy(&factory));
285 320
286 MessagePipe extra_pipe; 321 MessagePipe extra_pipe;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 factory->CreateNamedObject(std::move(object1_request)); 367 factory->CreateNamedObject(std::move(object1_request));
333 EXPECT_FALSE(object1_request.is_pending()); // We've passed the request. 368 EXPECT_FALSE(object1_request.is_pending()); // We've passed the request.
334 369
335 ASSERT_TRUE(object1); 370 ASSERT_TRUE(object1);
336 object1->SetName("object1"); 371 object1->SetName("object1");
337 372
338 sample::NamedObjectPtr object2; 373 sample::NamedObjectPtr object2;
339 factory->CreateNamedObject(GetProxy(&object2)); 374 factory->CreateNamedObject(GetProxy(&object2));
340 object2->SetName("object2"); 375 object2->SetName("object2");
341 376
377 base::RunLoop run_loop, run_loop2;
342 std::string name1; 378 std::string name1;
343 object1->GetName(StringRecorder(&name1)); 379 object1->GetName(StringRecorder(&name1, run_loop.QuitClosure()));
344 380
345 std::string name2; 381 std::string name2;
346 object2->GetName(StringRecorder(&name2)); 382 object2->GetName(StringRecorder(&name2, run_loop2.QuitClosure()));
347 383
348 PumpMessages(); // Yield for results. 384 run_loop.Run();
385 run_loop2.Run();
349 386
350 EXPECT_EQ(std::string("object1"), name1); 387 EXPECT_EQ(std::string("object1"), name1);
351 EXPECT_EQ(std::string("object2"), name2); 388 EXPECT_EQ(std::string("object2"), name2);
352 } 389 }
353 390
354 } // namespace 391 } // namespace
355 } // namespace test 392 } // namespace test
356 } // namespace mojo 393 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/tests/connector_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