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

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

Issue 2589663003: mojo:: Rename mojo::GetProxy() to mojo::MakeRequest() (Closed)
Patch Set: Rebase Created 3 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 #include <utility>
7 7
8 #include "base/bind.h" 8 #include "base/bind.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"
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 void ExpectValueAndRunClosure(uint32_t expected_value, 214 void ExpectValueAndRunClosure(uint32_t expected_value,
215 const base::Closure& closure, 215 const base::Closure& closure,
216 uint32_t value) { 216 uint32_t value) {
217 EXPECT_EQ(expected_value, value); 217 EXPECT_EQ(expected_value, value);
218 closure.Run(); 218 closure.Run();
219 } 219 }
220 220
221 TEST_F(InterfacePtrTest, IsBound) { 221 TEST_F(InterfacePtrTest, IsBound) {
222 math::CalculatorPtr calc; 222 math::CalculatorPtr calc;
223 EXPECT_FALSE(calc.is_bound()); 223 EXPECT_FALSE(calc.is_bound());
224 MathCalculatorImpl calc_impl(GetProxy(&calc)); 224 MathCalculatorImpl calc_impl(MakeRequest(&calc));
225 EXPECT_TRUE(calc.is_bound()); 225 EXPECT_TRUE(calc.is_bound());
226 } 226 }
227 227
228 TEST_F(InterfacePtrTest, EndToEnd) { 228 TEST_F(InterfacePtrTest, EndToEnd) {
229 math::CalculatorPtr calc; 229 math::CalculatorPtr calc;
230 MathCalculatorImpl calc_impl(GetProxy(&calc)); 230 MathCalculatorImpl calc_impl(MakeRequest(&calc));
231 231
232 // Suppose this is instantiated in a process that has pipe1_. 232 // Suppose this is instantiated in a process that has pipe1_.
233 MathCalculatorUI calculator_ui(std::move(calc)); 233 MathCalculatorUI calculator_ui(std::move(calc));
234 234
235 base::RunLoop run_loop, run_loop2; 235 base::RunLoop run_loop, run_loop2;
236 calculator_ui.Add(2.0, run_loop.QuitClosure()); 236 calculator_ui.Add(2.0, run_loop.QuitClosure());
237 calculator_ui.Multiply(5.0, run_loop2.QuitClosure()); 237 calculator_ui.Multiply(5.0, run_loop2.QuitClosure());
238 run_loop.Run(); 238 run_loop.Run();
239 run_loop2.Run(); 239 run_loop2.Run();
240 240
241 EXPECT_EQ(10.0, calculator_ui.GetOutput()); 241 EXPECT_EQ(10.0, calculator_ui.GetOutput());
242 } 242 }
243 243
244 TEST_F(InterfacePtrTest, EndToEnd_Synchronous) { 244 TEST_F(InterfacePtrTest, EndToEnd_Synchronous) {
245 math::CalculatorPtr calc; 245 math::CalculatorPtr calc;
246 MathCalculatorImpl calc_impl(GetProxy(&calc)); 246 MathCalculatorImpl calc_impl(MakeRequest(&calc));
247 247
248 // Suppose this is instantiated in a process that has pipe1_. 248 // Suppose this is instantiated in a process that has pipe1_.
249 MathCalculatorUI calculator_ui(std::move(calc)); 249 MathCalculatorUI calculator_ui(std::move(calc));
250 250
251 EXPECT_EQ(0.0, calculator_ui.GetOutput()); 251 EXPECT_EQ(0.0, calculator_ui.GetOutput());
252 252
253 base::RunLoop run_loop; 253 base::RunLoop run_loop;
254 calculator_ui.Add(2.0, run_loop.QuitClosure()); 254 calculator_ui.Add(2.0, run_loop.QuitClosure());
255 EXPECT_EQ(0.0, calculator_ui.GetOutput()); 255 EXPECT_EQ(0.0, calculator_ui.GetOutput());
256 calc_impl.binding()->WaitForIncomingMethodCall(); 256 calc_impl.binding()->WaitForIncomingMethodCall();
257 run_loop.Run(); 257 run_loop.Run();
258 EXPECT_EQ(2.0, calculator_ui.GetOutput()); 258 EXPECT_EQ(2.0, calculator_ui.GetOutput());
259 259
260 base::RunLoop run_loop2; 260 base::RunLoop run_loop2;
261 calculator_ui.Multiply(5.0, run_loop2.QuitClosure()); 261 calculator_ui.Multiply(5.0, run_loop2.QuitClosure());
262 EXPECT_EQ(2.0, calculator_ui.GetOutput()); 262 EXPECT_EQ(2.0, calculator_ui.GetOutput());
263 calc_impl.binding()->WaitForIncomingMethodCall(); 263 calc_impl.binding()->WaitForIncomingMethodCall();
264 run_loop2.Run(); 264 run_loop2.Run();
265 EXPECT_EQ(10.0, calculator_ui.GetOutput()); 265 EXPECT_EQ(10.0, calculator_ui.GetOutput());
266 } 266 }
267 267
268 TEST_F(InterfacePtrTest, Movable) { 268 TEST_F(InterfacePtrTest, Movable) {
269 math::CalculatorPtr a; 269 math::CalculatorPtr a;
270 math::CalculatorPtr b; 270 math::CalculatorPtr b;
271 MathCalculatorImpl calc_impl(GetProxy(&b)); 271 MathCalculatorImpl calc_impl(MakeRequest(&b));
272 272
273 EXPECT_TRUE(!a); 273 EXPECT_TRUE(!a);
274 EXPECT_FALSE(!b); 274 EXPECT_FALSE(!b);
275 275
276 a = std::move(b); 276 a = std::move(b);
277 277
278 EXPECT_FALSE(!a); 278 EXPECT_FALSE(!a);
279 EXPECT_TRUE(!b); 279 EXPECT_TRUE(!b);
280 } 280 }
281 281
(...skipping 26 matching lines...) Expand all
308 EXPECT_FALSE(ptr.get()); 308 EXPECT_FALSE(ptr.get());
309 EXPECT_FALSE(ptr); 309 EXPECT_FALSE(ptr);
310 310
311 ptr.Bind(InterfacePtrInfo<math::Calculator>()); 311 ptr.Bind(InterfacePtrInfo<math::Calculator>());
312 EXPECT_FALSE(ptr.get()); 312 EXPECT_FALSE(ptr.get());
313 EXPECT_FALSE(ptr); 313 EXPECT_FALSE(ptr);
314 } 314 }
315 315
316 TEST_F(InterfacePtrTest, EncounteredError) { 316 TEST_F(InterfacePtrTest, EncounteredError) {
317 math::CalculatorPtr proxy; 317 math::CalculatorPtr proxy;
318 MathCalculatorImpl calc_impl(GetProxy(&proxy)); 318 MathCalculatorImpl calc_impl(MakeRequest(&proxy));
319 319
320 MathCalculatorUI calculator_ui(std::move(proxy)); 320 MathCalculatorUI calculator_ui(std::move(proxy));
321 321
322 base::RunLoop run_loop; 322 base::RunLoop run_loop;
323 calculator_ui.Add(2.0, run_loop.QuitClosure()); 323 calculator_ui.Add(2.0, run_loop.QuitClosure());
324 run_loop.Run(); 324 run_loop.Run();
325 EXPECT_EQ(2.0, calculator_ui.GetOutput()); 325 EXPECT_EQ(2.0, calculator_ui.GetOutput());
326 EXPECT_FALSE(calculator_ui.encountered_error()); 326 EXPECT_FALSE(calculator_ui.encountered_error());
327 327
328 calculator_ui.Multiply(5.0, base::Closure()); 328 calculator_ui.Multiply(5.0, base::Closure());
329 EXPECT_FALSE(calculator_ui.encountered_error()); 329 EXPECT_FALSE(calculator_ui.encountered_error());
330 330
331 // Close the server. 331 // Close the server.
332 calc_impl.binding()->Close(); 332 calc_impl.binding()->Close();
333 333
334 // The state change isn't picked up locally yet. 334 // The state change isn't picked up locally yet.
335 base::RunLoop run_loop2; 335 base::RunLoop run_loop2;
336 calculator_ui.set_connection_error_handler(run_loop2.QuitClosure()); 336 calculator_ui.set_connection_error_handler(run_loop2.QuitClosure());
337 EXPECT_FALSE(calculator_ui.encountered_error()); 337 EXPECT_FALSE(calculator_ui.encountered_error());
338 338
339 run_loop2.Run(); 339 run_loop2.Run();
340 340
341 // OK, now we see the error. 341 // OK, now we see the error.
342 EXPECT_TRUE(calculator_ui.encountered_error()); 342 EXPECT_TRUE(calculator_ui.encountered_error());
343 } 343 }
344 344
345 TEST_F(InterfacePtrTest, EncounteredErrorCallback) { 345 TEST_F(InterfacePtrTest, EncounteredErrorCallback) {
346 math::CalculatorPtr proxy; 346 math::CalculatorPtr proxy;
347 MathCalculatorImpl calc_impl(GetProxy(&proxy)); 347 MathCalculatorImpl calc_impl(MakeRequest(&proxy));
348 348
349 bool encountered_error = false; 349 bool encountered_error = false;
350 base::RunLoop run_loop; 350 base::RunLoop run_loop;
351 proxy.set_connection_error_handler( 351 proxy.set_connection_error_handler(
352 base::Bind(&SetFlagAndRunClosure, &encountered_error, 352 base::Bind(&SetFlagAndRunClosure, &encountered_error,
353 run_loop.QuitClosure())); 353 run_loop.QuitClosure()));
354 354
355 MathCalculatorUI calculator_ui(std::move(proxy)); 355 MathCalculatorUI calculator_ui(std::move(proxy));
356 356
357 base::RunLoop run_loop2; 357 base::RunLoop run_loop2;
(...skipping 16 matching lines...) Expand all
374 // OK, now we see the error. 374 // OK, now we see the error.
375 EXPECT_TRUE(calculator_ui.encountered_error()); 375 EXPECT_TRUE(calculator_ui.encountered_error());
376 376
377 // We should have also been able to observe the error through the error 377 // We should have also been able to observe the error through the error
378 // handler. 378 // handler.
379 EXPECT_TRUE(encountered_error); 379 EXPECT_TRUE(encountered_error);
380 } 380 }
381 381
382 TEST_F(InterfacePtrTest, DestroyInterfacePtrOnMethodResponse) { 382 TEST_F(InterfacePtrTest, DestroyInterfacePtrOnMethodResponse) {
383 math::CalculatorPtr proxy; 383 math::CalculatorPtr proxy;
384 MathCalculatorImpl calc_impl(GetProxy(&proxy)); 384 MathCalculatorImpl calc_impl(MakeRequest(&proxy));
385 385
386 EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances()); 386 EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances());
387 387
388 SelfDestructingMathCalculatorUI* impl = 388 SelfDestructingMathCalculatorUI* impl =
389 new SelfDestructingMathCalculatorUI(std::move(proxy)); 389 new SelfDestructingMathCalculatorUI(std::move(proxy));
390 base::RunLoop run_loop; 390 base::RunLoop run_loop;
391 impl->BeginTest(false, run_loop.QuitClosure()); 391 impl->BeginTest(false, run_loop.QuitClosure());
392 run_loop.Run(); 392 run_loop.Run();
393 393
394 EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances()); 394 EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances());
395 } 395 }
396 396
397 TEST_F(InterfacePtrTest, NestedDestroyInterfacePtrOnMethodResponse) { 397 TEST_F(InterfacePtrTest, NestedDestroyInterfacePtrOnMethodResponse) {
398 math::CalculatorPtr proxy; 398 math::CalculatorPtr proxy;
399 MathCalculatorImpl calc_impl(GetProxy(&proxy)); 399 MathCalculatorImpl calc_impl(MakeRequest(&proxy));
400 400
401 EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances()); 401 EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances());
402 402
403 SelfDestructingMathCalculatorUI* impl = 403 SelfDestructingMathCalculatorUI* impl =
404 new SelfDestructingMathCalculatorUI(std::move(proxy)); 404 new SelfDestructingMathCalculatorUI(std::move(proxy));
405 base::RunLoop run_loop; 405 base::RunLoop run_loop;
406 impl->BeginTest(true, run_loop.QuitClosure()); 406 impl->BeginTest(true, run_loop.QuitClosure());
407 run_loop.Run(); 407 run_loop.Run();
408 408
409 EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances()); 409 EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances());
410 } 410 }
411 411
412 TEST_F(InterfacePtrTest, ReentrantWaitForIncomingMethodCall) { 412 TEST_F(InterfacePtrTest, ReentrantWaitForIncomingMethodCall) {
413 sample::ServicePtr proxy; 413 sample::ServicePtr proxy;
414 ReentrantServiceImpl impl(GetProxy(&proxy)); 414 ReentrantServiceImpl impl(MakeRequest(&proxy));
415 415
416 base::RunLoop run_loop, run_loop2; 416 base::RunLoop run_loop, run_loop2;
417 proxy->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, 417 proxy->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr,
418 base::Bind(&IgnoreValueAndRunClosure, 418 base::Bind(&IgnoreValueAndRunClosure,
419 run_loop.QuitClosure())); 419 run_loop.QuitClosure()));
420 proxy->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, 420 proxy->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr,
421 base::Bind(&IgnoreValueAndRunClosure, 421 base::Bind(&IgnoreValueAndRunClosure,
422 run_loop2.QuitClosure())); 422 run_loop2.QuitClosure()));
423 423
424 run_loop.Run(); 424 run_loop.Run();
425 run_loop2.Run(); 425 run_loop2.Run();
426 426
427 EXPECT_EQ(2, impl.max_call_depth()); 427 EXPECT_EQ(2, impl.max_call_depth());
428 } 428 }
429 429
430 TEST_F(InterfacePtrTest, QueryVersion) { 430 TEST_F(InterfacePtrTest, QueryVersion) {
431 IntegerAccessorImpl impl; 431 IntegerAccessorImpl impl;
432 sample::IntegerAccessorPtr ptr; 432 sample::IntegerAccessorPtr ptr;
433 Binding<sample::IntegerAccessor> binding(&impl, GetProxy(&ptr)); 433 Binding<sample::IntegerAccessor> binding(&impl, MakeRequest(&ptr));
434 434
435 EXPECT_EQ(0u, ptr.version()); 435 EXPECT_EQ(0u, ptr.version());
436 436
437 base::RunLoop run_loop; 437 base::RunLoop run_loop;
438 ptr.QueryVersion(base::Bind(&ExpectValueAndRunClosure, 3u, 438 ptr.QueryVersion(base::Bind(&ExpectValueAndRunClosure, 3u,
439 run_loop.QuitClosure())); 439 run_loop.QuitClosure()));
440 run_loop.Run(); 440 run_loop.Run();
441 441
442 EXPECT_EQ(3u, ptr.version()); 442 EXPECT_EQ(3u, ptr.version());
443 } 443 }
444 444
445 TEST_F(InterfacePtrTest, RequireVersion) { 445 TEST_F(InterfacePtrTest, RequireVersion) {
446 IntegerAccessorImpl impl; 446 IntegerAccessorImpl impl;
447 sample::IntegerAccessorPtr ptr; 447 sample::IntegerAccessorPtr ptr;
448 Binding<sample::IntegerAccessor> binding(&impl, GetProxy(&ptr)); 448 Binding<sample::IntegerAccessor> binding(&impl, MakeRequest(&ptr));
449 449
450 EXPECT_EQ(0u, ptr.version()); 450 EXPECT_EQ(0u, ptr.version());
451 451
452 ptr.RequireVersion(1u); 452 ptr.RequireVersion(1u);
453 EXPECT_EQ(1u, ptr.version()); 453 EXPECT_EQ(1u, ptr.version());
454 base::RunLoop run_loop; 454 base::RunLoop run_loop;
455 impl.set_closure(run_loop.QuitClosure()); 455 impl.set_closure(run_loop.QuitClosure());
456 ptr->SetInteger(123, sample::Enum::VALUE); 456 ptr->SetInteger(123, sample::Enum::VALUE);
457 run_loop.Run(); 457 run_loop.Run();
458 EXPECT_FALSE(ptr.encountered_error()); 458 EXPECT_FALSE(ptr.encountered_error());
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
504 }; 504 };
505 505
506 TEST(StrongConnectorTest, Math) { 506 TEST(StrongConnectorTest, Math) {
507 base::MessageLoop loop; 507 base::MessageLoop loop;
508 508
509 bool error_received = false; 509 bool error_received = false;
510 bool destroyed = false; 510 bool destroyed = false;
511 math::CalculatorPtr calc; 511 math::CalculatorPtr calc;
512 base::RunLoop run_loop; 512 base::RunLoop run_loop;
513 513
514 auto binding = MakeStrongBinding( 514 auto binding =
515 base::MakeUnique<StrongMathCalculatorImpl>(&destroyed), GetProxy(&calc)); 515 MakeStrongBinding(base::MakeUnique<StrongMathCalculatorImpl>(&destroyed),
516 MakeRequest(&calc));
516 binding->set_connection_error_handler(base::Bind( 517 binding->set_connection_error_handler(base::Bind(
517 &SetFlagAndRunClosure, &error_received, run_loop.QuitClosure())); 518 &SetFlagAndRunClosure, &error_received, run_loop.QuitClosure()));
518 519
519 { 520 {
520 // Suppose this is instantiated in a process that has the other end of the 521 // Suppose this is instantiated in a process that has the other end of the
521 // message pipe. 522 // message pipe.
522 MathCalculatorUI calculator_ui(std::move(calc)); 523 MathCalculatorUI calculator_ui(std::move(calc));
523 524
524 base::RunLoop run_loop, run_loop2; 525 base::RunLoop run_loop, run_loop2;
525 calculator_ui.Add(2.0, run_loop.QuitClosure()); 526 calculator_ui.Add(2.0, run_loop.QuitClosure());
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
661 } 662 }
662 663
663 bool d_called_; 664 bool d_called_;
664 Binding<A> binding_; 665 Binding<A> binding_;
665 base::Closure closure_; 666 base::Closure closure_;
666 }; 667 };
667 668
668 TEST_F(InterfacePtrTest, Scoping) { 669 TEST_F(InterfacePtrTest, Scoping) {
669 APtr a; 670 APtr a;
670 base::RunLoop run_loop; 671 base::RunLoop run_loop;
671 AImpl a_impl(GetProxy(&a), run_loop.QuitClosure()); 672 AImpl a_impl(MakeRequest(&a), run_loop.QuitClosure());
672 673
673 EXPECT_FALSE(a_impl.d_called()); 674 EXPECT_FALSE(a_impl.d_called());
674 675
675 { 676 {
676 BPtr b; 677 BPtr b;
677 a->GetB(GetProxy(&b)); 678 a->GetB(MakeRequest(&b));
678 CPtr c; 679 CPtr c;
679 b->GetC(GetProxy(&c)); 680 b->GetC(MakeRequest(&c));
680 c->D(); 681 c->D();
681 } 682 }
682 683
683 // While B & C have fallen out of scope, the pipes will remain until they are 684 // While B & C have fallen out of scope, the pipes will remain until they are
684 // flushed. 685 // flushed.
685 EXPECT_FALSE(a_impl.d_called()); 686 EXPECT_FALSE(a_impl.d_called());
686 run_loop.Run(); 687 run_loop.Run();
687 EXPECT_TRUE(a_impl.d_called()); 688 EXPECT_TRUE(a_impl.d_called());
688 } 689 }
689 690
690 class PingTestImpl : public sample::PingTest { 691 class PingTestImpl : public sample::PingTest {
691 public: 692 public:
692 explicit PingTestImpl(InterfaceRequest<sample::PingTest> request) 693 explicit PingTestImpl(InterfaceRequest<sample::PingTest> request)
693 : binding_(this, std::move(request)) {} 694 : binding_(this, std::move(request)) {}
694 ~PingTestImpl() override {} 695 ~PingTestImpl() override {}
695 696
696 private: 697 private:
697 // sample::PingTest: 698 // sample::PingTest:
698 void Ping(const PingCallback& callback) override { callback.Run(); } 699 void Ping(const PingCallback& callback) override { callback.Run(); }
699 700
700 Binding<sample::PingTest> binding_; 701 Binding<sample::PingTest> binding_;
701 }; 702 };
702 703
703 // Tests that FuseProxy does what it's supposed to do. 704 // Tests that FuseProxy does what it's supposed to do.
704 TEST_F(InterfacePtrTest, Fusion) { 705 TEST_F(InterfacePtrTest, Fusion) {
705 sample::PingTestPtr proxy; 706 sample::PingTestPtr proxy;
706 PingTestImpl impl(GetProxy(&proxy)); 707 PingTestImpl impl(MakeRequest(&proxy));
707 708
708 // Create another PingTest pipe. 709 // Create another PingTest pipe.
709 sample::PingTestPtr ptr; 710 sample::PingTestPtr ptr;
710 sample::PingTestRequest request = GetProxy(&ptr); 711 sample::PingTestRequest request = MakeRequest(&ptr);
711 712
712 // Fuse the new pipe to the one hanging off |impl|. 713 // Fuse the new pipe to the one hanging off |impl|.
713 EXPECT_TRUE(FuseInterface(std::move(request), proxy.PassInterface())); 714 EXPECT_TRUE(FuseInterface(std::move(request), proxy.PassInterface()));
714 715
715 // Ping! 716 // Ping!
716 bool called = false; 717 bool called = false;
717 base::RunLoop loop; 718 base::RunLoop loop;
718 ptr->Ping(base::Bind(&SetFlagAndRunClosure, &called, loop.QuitClosure())); 719 ptr->Ping(base::Bind(&SetFlagAndRunClosure, &called, loop.QuitClosure()));
719 loop.Run(); 720 loop.Run();
720 EXPECT_TRUE(called); 721 EXPECT_TRUE(called);
721 } 722 }
722 723
723 void Fail() { 724 void Fail() {
724 FAIL() << "Unexpected connection error"; 725 FAIL() << "Unexpected connection error";
725 } 726 }
726 727
727 TEST_F(InterfacePtrTest, FlushForTesting) { 728 TEST_F(InterfacePtrTest, FlushForTesting) {
728 math::CalculatorPtr calc; 729 math::CalculatorPtr calc;
729 MathCalculatorImpl calc_impl(GetProxy(&calc)); 730 MathCalculatorImpl calc_impl(MakeRequest(&calc));
730 calc.set_connection_error_handler(base::Bind(&Fail)); 731 calc.set_connection_error_handler(base::Bind(&Fail));
731 732
732 MathCalculatorUI calculator_ui(std::move(calc)); 733 MathCalculatorUI calculator_ui(std::move(calc));
733 734
734 calculator_ui.Add(2.0, base::Bind(&base::DoNothing)); 735 calculator_ui.Add(2.0, base::Bind(&base::DoNothing));
735 calculator_ui.GetInterfacePtr().FlushForTesting(); 736 calculator_ui.GetInterfacePtr().FlushForTesting();
736 EXPECT_EQ(2.0, calculator_ui.GetOutput()); 737 EXPECT_EQ(2.0, calculator_ui.GetOutput());
737 738
738 calculator_ui.Multiply(5.0, base::Bind(&base::DoNothing)); 739 calculator_ui.Multiply(5.0, base::Bind(&base::DoNothing));
739 calculator_ui.GetInterfacePtr().FlushForTesting(); 740 calculator_ui.GetInterfacePtr().FlushForTesting();
740 741
741 EXPECT_EQ(10.0, calculator_ui.GetOutput()); 742 EXPECT_EQ(10.0, calculator_ui.GetOutput());
742 } 743 }
743 744
744 void SetBool(bool* value) { 745 void SetBool(bool* value) {
745 *value = true; 746 *value = true;
746 } 747 }
747 748
748 TEST_F(InterfacePtrTest, FlushForTestingWithClosedPeer) { 749 TEST_F(InterfacePtrTest, FlushForTestingWithClosedPeer) {
749 math::CalculatorPtr calc; 750 math::CalculatorPtr calc;
750 GetProxy(&calc); 751 MakeRequest(&calc);
751 bool called = false; 752 bool called = false;
752 calc.set_connection_error_handler(base::Bind(&SetBool, &called)); 753 calc.set_connection_error_handler(base::Bind(&SetBool, &called));
753 calc.FlushForTesting(); 754 calc.FlushForTesting();
754 EXPECT_TRUE(called); 755 EXPECT_TRUE(called);
755 calc.FlushForTesting(); 756 calc.FlushForTesting();
756 } 757 }
757 758
758 TEST_F(InterfacePtrTest, ConnectionErrorWithReason) { 759 TEST_F(InterfacePtrTest, ConnectionErrorWithReason) {
759 math::CalculatorPtr calc; 760 math::CalculatorPtr calc;
760 MathCalculatorImpl calc_impl(GetProxy(&calc)); 761 MathCalculatorImpl calc_impl(MakeRequest(&calc));
761 762
762 base::RunLoop run_loop; 763 base::RunLoop run_loop;
763 calc.set_connection_error_with_reason_handler(base::Bind( 764 calc.set_connection_error_with_reason_handler(base::Bind(
764 [](const base::Closure& quit_closure, uint32_t custom_reason, 765 [](const base::Closure& quit_closure, uint32_t custom_reason,
765 const std::string& description) { 766 const std::string& description) {
766 EXPECT_EQ(42u, custom_reason); 767 EXPECT_EQ(42u, custom_reason);
767 EXPECT_EQ("hey", description); 768 EXPECT_EQ("hey", description);
768 quit_closure.Run(); 769 quit_closure.Run();
769 }, 770 },
770 run_loop.QuitClosure())); 771 run_loop.QuitClosure()));
771 772
772 calc_impl.binding()->CloseWithReason(42u, "hey"); 773 calc_impl.binding()->CloseWithReason(42u, "hey");
773 774
774 run_loop.Run(); 775 run_loop.Run();
775 } 776 }
776 777
777 TEST_F(InterfacePtrTest, InterfaceRequestResetWithReason) { 778 TEST_F(InterfacePtrTest, InterfaceRequestResetWithReason) {
778 math::CalculatorPtr calc; 779 math::CalculatorPtr calc;
779 auto request = GetProxy(&calc); 780 auto request = MakeRequest(&calc);
780 781
781 base::RunLoop run_loop; 782 base::RunLoop run_loop;
782 calc.set_connection_error_with_reason_handler(base::Bind( 783 calc.set_connection_error_with_reason_handler(base::Bind(
783 [](const base::Closure& quit_closure, uint32_t custom_reason, 784 [](const base::Closure& quit_closure, uint32_t custom_reason,
784 const std::string& description) { 785 const std::string& description) {
785 EXPECT_EQ(88u, custom_reason); 786 EXPECT_EQ(88u, custom_reason);
786 EXPECT_EQ("greetings", description); 787 EXPECT_EQ("greetings", description);
787 quit_closure.Run(); 788 quit_closure.Run();
788 }, 789 },
789 run_loop.QuitClosure())); 790 run_loop.QuitClosure()));
790 791
791 request.ResetWithReason(88u, "greetings"); 792 request.ResetWithReason(88u, "greetings");
792 793
793 run_loop.Run(); 794 run_loop.Run();
794 } 795 }
795 796
796 TEST_F(InterfacePtrTest, CallbackOwnsInterfacePtr) { 797 TEST_F(InterfacePtrTest, CallbackOwnsInterfacePtr) {
797 sample::PingTestPtr ptr; 798 sample::PingTestPtr ptr;
798 sample::PingTestRequest request = GetProxy(&ptr); 799 sample::PingTestRequest request = MakeRequest(&ptr);
799 800
800 base::RunLoop run_loop; 801 base::RunLoop run_loop;
801 802
802 // Make a call with the proxy's lifetime bound to the response callback. 803 // Make a call with the proxy's lifetime bound to the response callback.
803 sample::PingTest* raw_proxy = ptr.get(); 804 sample::PingTest* raw_proxy = ptr.get();
804 ptr.set_connection_error_handler(run_loop.QuitClosure()); 805 ptr.set_connection_error_handler(run_loop.QuitClosure());
805 raw_proxy->Ping( 806 raw_proxy->Ping(
806 base::Bind([](sample::PingTestPtr ptr) {}, base::Passed(&ptr))); 807 base::Bind([](sample::PingTestPtr ptr) {}, base::Passed(&ptr)));
807 808
808 // Trigger an error on |ptr|. This will ultimately lead to the proxy's 809 // Trigger an error on |ptr|. This will ultimately lead to the proxy's
809 // response callbacks being destroyed, which will in turn lead to the proxy 810 // response callbacks being destroyed, which will in turn lead to the proxy
810 // being destroyed. This should not crash. 811 // being destroyed. This should not crash.
811 request.PassMessagePipe(); 812 request.PassMessagePipe();
812 run_loop.Run(); 813 run_loop.Run();
813 } 814 }
814 815
815 TEST_F(InterfacePtrTest, ThreadSafeInterfacePointer) { 816 TEST_F(InterfacePtrTest, ThreadSafeInterfacePointer) {
816 math::CalculatorPtr ptr; 817 math::CalculatorPtr ptr;
817 MathCalculatorImpl calc_impl(GetProxy(&ptr)); 818 MathCalculatorImpl calc_impl(MakeRequest(&ptr));
818 scoped_refptr<math::ThreadSafeCalculatorPtr> thread_safe_ptr = 819 scoped_refptr<math::ThreadSafeCalculatorPtr> thread_safe_ptr =
819 math::ThreadSafeCalculatorPtr::Create(std::move(ptr)); 820 math::ThreadSafeCalculatorPtr::Create(std::move(ptr));
820 821
821 base::RunLoop run_loop; 822 base::RunLoop run_loop;
822 823
823 // Create and start the thread from where we'll call the interface pointer. 824 // Create and start the thread from where we'll call the interface pointer.
824 base::Thread other_thread("service test thread"); 825 base::Thread other_thread("service test thread");
825 other_thread.Start(); 826 other_thread.Start();
826 827
827 auto run_method = base::Bind( 828 auto run_method = base::Bind(
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
865 MathCalculatorImpl* math_calc_impl = nullptr; 866 MathCalculatorImpl* math_calc_impl = nullptr;
866 { 867 {
867 base::RunLoop run_loop; 868 base::RunLoop run_loop;
868 auto run_method = base::Bind( 869 auto run_method = base::Bind(
869 [](const scoped_refptr<base::TaskRunner>& main_task_runner, 870 [](const scoped_refptr<base::TaskRunner>& main_task_runner,
870 const base::Closure& quit_closure, 871 const base::Closure& quit_closure,
871 const scoped_refptr<math::ThreadSafeCalculatorPtr>& thread_safe_ptr, 872 const scoped_refptr<math::ThreadSafeCalculatorPtr>& thread_safe_ptr,
872 MathCalculatorImpl** math_calc_impl) { 873 MathCalculatorImpl** math_calc_impl) {
873 math::CalculatorPtr ptr; 874 math::CalculatorPtr ptr;
874 // In real life, the implementation would have a legitimate owner. 875 // In real life, the implementation would have a legitimate owner.
875 *math_calc_impl = new MathCalculatorImpl(GetProxy(&ptr)); 876 *math_calc_impl = new MathCalculatorImpl(MakeRequest(&ptr));
876 thread_safe_ptr->Bind(std::move(ptr)); 877 thread_safe_ptr->Bind(std::move(ptr));
877 main_task_runner->PostTask(FROM_HERE, quit_closure); 878 main_task_runner->PostTask(FROM_HERE, quit_closure);
878 }, 879 },
879 base::SequencedTaskRunnerHandle::Get(), run_loop.QuitClosure(), 880 base::SequencedTaskRunnerHandle::Get(), run_loop.QuitClosure(),
880 thread_safe_ptr, &math_calc_impl); 881 thread_safe_ptr, &math_calc_impl);
881 other_thread.message_loop()->task_runner()->PostTask(FROM_HERE, run_method); 882 other_thread.message_loop()->task_runner()->PostTask(FROM_HERE, run_method);
882 run_loop.Run(); 883 run_loop.Run();
883 } 884 }
884 885
885 { 886 {
(...skipping 13 matching lines...) Expand all
899 other_thread_task_runner->DeleteSoon(FROM_HERE, math_calc_impl); 900 other_thread_task_runner->DeleteSoon(FROM_HERE, math_calc_impl);
900 901
901 // Reset the pointer now so the InterfacePtr associated resources can be 902 // Reset the pointer now so the InterfacePtr associated resources can be
902 // deleted before the background thread's message loop is invalidated. 903 // deleted before the background thread's message loop is invalidated.
903 thread_safe_ptr = nullptr; 904 thread_safe_ptr = nullptr;
904 } 905 }
905 906
906 } // namespace 907 } // namespace
907 } // namespace test 908 } // namespace test
908 } // namespace mojo 909 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/tests/handle_passing_unittest.cc ('k') | mojo/public/cpp/bindings/tests/pickle_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698