OLD | NEW |
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 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
474 ptr.set_connection_error_handler(run_loop3.QuitClosure()); | 474 ptr.set_connection_error_handler(run_loop3.QuitClosure()); |
475 ptr->SetInteger(789, sample::Enum::VALUE); | 475 ptr->SetInteger(789, sample::Enum::VALUE); |
476 run_loop3.Run(); | 476 run_loop3.Run(); |
477 EXPECT_TRUE(ptr.encountered_error()); | 477 EXPECT_TRUE(ptr.encountered_error()); |
478 // The call to SetInteger() after RequireVersion(4u) is ignored. | 478 // The call to SetInteger() after RequireVersion(4u) is ignored. |
479 EXPECT_EQ(456, impl.integer()); | 479 EXPECT_EQ(456, impl.integer()); |
480 } | 480 } |
481 | 481 |
482 class StrongMathCalculatorImpl : public math::Calculator { | 482 class StrongMathCalculatorImpl : public math::Calculator { |
483 public: | 483 public: |
484 StrongMathCalculatorImpl(ScopedMessagePipeHandle handle, | 484 StrongMathCalculatorImpl(bool* destroyed) : destroyed_(destroyed) {} |
485 bool* error_received, | |
486 bool* destroyed, | |
487 const base::Closure& closure) | |
488 : error_received_(error_received), | |
489 destroyed_(destroyed), | |
490 closure_(closure), | |
491 binding_(this, std::move(handle)) { | |
492 binding_.set_connection_error_handler( | |
493 base::Bind(&SetFlagAndRunClosure, error_received_, closure_)); | |
494 } | |
495 ~StrongMathCalculatorImpl() override { *destroyed_ = true; } | 485 ~StrongMathCalculatorImpl() override { *destroyed_ = true; } |
496 | 486 |
497 // math::Calculator implementation. | 487 // math::Calculator implementation. |
498 void Clear(const CalcCallback& callback) override { callback.Run(total_); } | 488 void Clear(const CalcCallback& callback) override { callback.Run(total_); } |
499 | 489 |
500 void Add(double value, const CalcCallback& callback) override { | 490 void Add(double value, const CalcCallback& callback) override { |
501 total_ += value; | 491 total_ += value; |
502 callback.Run(total_); | 492 callback.Run(total_); |
503 } | 493 } |
504 | 494 |
505 void Multiply(double value, const CalcCallback& callback) override { | 495 void Multiply(double value, const CalcCallback& callback) override { |
506 total_ *= value; | 496 total_ *= value; |
507 callback.Run(total_); | 497 callback.Run(total_); |
508 } | 498 } |
509 | 499 |
510 private: | 500 private: |
511 double total_ = 0.0; | 501 double total_ = 0.0; |
512 bool* error_received_; | |
513 bool* destroyed_; | 502 bool* destroyed_; |
514 base::Closure closure_; | |
515 | |
516 StrongBinding<math::Calculator> binding_; | |
517 }; | 503 }; |
518 | 504 |
519 TEST(StrongConnectorTest, Math) { | 505 TEST(StrongConnectorTest, Math) { |
520 base::MessageLoop loop; | 506 base::MessageLoop loop; |
521 | 507 |
522 bool error_received = false; | 508 bool error_received = false; |
523 bool destroyed = false; | 509 bool destroyed = false; |
524 MessagePipe pipe; | 510 math::CalculatorPtr calc; |
525 base::RunLoop run_loop; | 511 base::RunLoop run_loop; |
526 new StrongMathCalculatorImpl(std::move(pipe.handle0), &error_received, | |
527 &destroyed, run_loop.QuitClosure()); | |
528 | 512 |
529 math::CalculatorPtr calc; | 513 auto binding = MakeStrongBinding( |
530 calc.Bind(InterfacePtrInfo<math::Calculator>(std::move(pipe.handle1), 0u)); | 514 base::MakeUnique<StrongMathCalculatorImpl>(&destroyed), GetProxy(&calc)); |
| 515 binding->set_connection_error_handler(base::Bind( |
| 516 &SetFlagAndRunClosure, &error_received, run_loop.QuitClosure())); |
531 | 517 |
532 { | 518 { |
533 // Suppose this is instantiated in a process that has the other end of the | 519 // Suppose this is instantiated in a process that has the other end of the |
534 // message pipe. | 520 // message pipe. |
535 MathCalculatorUI calculator_ui(std::move(calc)); | 521 MathCalculatorUI calculator_ui(std::move(calc)); |
536 | 522 |
537 base::RunLoop run_loop, run_loop2; | 523 base::RunLoop run_loop, run_loop2; |
538 calculator_ui.Add(2.0, run_loop.QuitClosure()); | 524 calculator_ui.Add(2.0, run_loop.QuitClosure()); |
539 calculator_ui.Multiply(5.0, run_loop2.QuitClosure()); | 525 calculator_ui.Multiply(5.0, run_loop2.QuitClosure()); |
540 run_loop.Run(); | 526 run_loop.Run(); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
621 // end which will destroy the instance since it is strongly bound. | 607 // end which will destroy the instance since it is strongly bound. |
622 } | 608 } |
623 | 609 |
624 run_loop.Run(); | 610 run_loop.Run(); |
625 EXPECT_TRUE(error_received); | 611 EXPECT_TRUE(error_received); |
626 EXPECT_FALSE(destroyed); | 612 EXPECT_FALSE(destroyed); |
627 } | 613 } |
628 | 614 |
629 class CImpl : public C { | 615 class CImpl : public C { |
630 public: | 616 public: |
631 CImpl(bool* d_called, InterfaceRequest<C> request, | 617 CImpl(bool* d_called, const base::Closure& closure) |
632 const base::Closure& closure) | 618 : d_called_(d_called), closure_(closure) {} |
633 : d_called_(d_called), binding_(this, std::move(request)), | |
634 closure_(closure) {} | |
635 ~CImpl() override {} | 619 ~CImpl() override {} |
636 | 620 |
637 private: | 621 private: |
638 void D() override { | 622 void D() override { |
639 *d_called_ = true; | 623 *d_called_ = true; |
640 closure_.Run(); | 624 closure_.Run(); |
641 } | 625 } |
642 | 626 |
643 bool* d_called_; | 627 bool* d_called_; |
644 StrongBinding<C> binding_; | |
645 base::Closure closure_; | 628 base::Closure closure_; |
646 }; | 629 }; |
647 | 630 |
648 class BImpl : public B { | 631 class BImpl : public B { |
649 public: | 632 public: |
650 BImpl(bool* d_called, InterfaceRequest<B> request, | 633 BImpl(bool* d_called, const base::Closure& closure) |
651 const base::Closure& closure) | 634 : d_called_(d_called), closure_(closure) {} |
652 : d_called_(d_called), binding_(this, std::move(request)), | |
653 closure_(closure) {} | |
654 ~BImpl() override {} | 635 ~BImpl() override {} |
655 | 636 |
656 private: | 637 private: |
657 void GetC(InterfaceRequest<C> c) override { | 638 void GetC(InterfaceRequest<C> c) override { |
658 new CImpl(d_called_, std::move(c), closure_); | 639 MakeStrongBinding(base::MakeUnique<CImpl>(d_called_, closure_), |
| 640 std::move(c)); |
659 } | 641 } |
660 | 642 |
661 bool* d_called_; | 643 bool* d_called_; |
662 StrongBinding<B> binding_; | |
663 base::Closure closure_; | 644 base::Closure closure_; |
664 }; | 645 }; |
665 | 646 |
666 class AImpl : public A { | 647 class AImpl : public A { |
667 public: | 648 public: |
668 AImpl(InterfaceRequest<A> request, const base::Closure& closure) | 649 AImpl(InterfaceRequest<A> request, const base::Closure& closure) |
669 : d_called_(false), binding_(this, std::move(request)), | 650 : d_called_(false), binding_(this, std::move(request)), |
670 closure_(closure) {} | 651 closure_(closure) {} |
671 ~AImpl() override {} | 652 ~AImpl() override {} |
672 | 653 |
673 bool d_called() const { return d_called_; } | 654 bool d_called() const { return d_called_; } |
674 | 655 |
675 private: | 656 private: |
676 void GetB(InterfaceRequest<B> b) override { | 657 void GetB(InterfaceRequest<B> b) override { |
677 new BImpl(&d_called_, std::move(b), closure_); | 658 MakeStrongBinding(base::MakeUnique<BImpl>(&d_called_, closure_), |
| 659 std::move(b)); |
678 } | 660 } |
679 | 661 |
680 bool d_called_; | 662 bool d_called_; |
681 Binding<A> binding_; | 663 Binding<A> binding_; |
682 base::Closure closure_; | 664 base::Closure closure_; |
683 }; | 665 }; |
684 | 666 |
685 TEST_F(InterfacePtrTest, Scoping) { | 667 TEST_F(InterfacePtrTest, Scoping) { |
686 APtr a; | 668 APtr a; |
687 base::RunLoop run_loop; | 669 base::RunLoop run_loop; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
768 bool called = false; | 750 bool called = false; |
769 calc.set_connection_error_handler(base::Bind(&SetBool, &called)); | 751 calc.set_connection_error_handler(base::Bind(&SetBool, &called)); |
770 calc.FlushForTesting(); | 752 calc.FlushForTesting(); |
771 EXPECT_TRUE(called); | 753 EXPECT_TRUE(called); |
772 calc.FlushForTesting(); | 754 calc.FlushForTesting(); |
773 } | 755 } |
774 | 756 |
775 } // namespace | 757 } // namespace |
776 } // namespace test | 758 } // namespace test |
777 } // namespace mojo | 759 } // namespace mojo |
OLD | NEW |