OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
13 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
14 #include "mojo/message_pump/message_pump_mojo.h" | 14 #include "mojo/message_pump/message_pump_mojo.h" |
15 #include "mojo/public/cpp/bindings/associated_binding.h" | 15 #include "mojo/public/cpp/bindings/associated_binding.h" |
16 #include "mojo/public/cpp/bindings/associated_group.h" | 16 #include "mojo/public/cpp/bindings/associated_group.h" |
17 #include "mojo/public/cpp/bindings/associated_interface_ptr.h" | 17 #include "mojo/public/cpp/bindings/associated_interface_ptr.h" |
18 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" | 18 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" |
19 #include "mojo/public/cpp/bindings/associated_interface_request.h" | 19 #include "mojo/public/cpp/bindings/associated_interface_request.h" |
20 #include "mojo/public/cpp/bindings/binding.h" | 20 #include "mojo/public/cpp/bindings/binding.h" |
21 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" | 21 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" |
| 22 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" |
22 #include "mojo/public/interfaces/bindings/tests/test_associated_interfaces.mojom
.h" | 23 #include "mojo/public/interfaces/bindings/tests/test_associated_interfaces.mojom
.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
24 | 25 |
25 namespace mojo { | 26 namespace mojo { |
26 namespace test { | 27 namespace test { |
27 namespace { | 28 namespace { |
28 | 29 |
29 using mojo::internal::AssociatedInterfacePtrInfoHelper; | 30 using mojo::internal::AssociatedInterfacePtrInfoHelper; |
30 using mojo::internal::AssociatedInterfaceRequestHelper; | 31 using mojo::internal::AssociatedInterfaceRequestHelper; |
31 using mojo::internal::MultiplexRouter; | 32 using mojo::internal::MultiplexRouter; |
(...skipping 543 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
575 // There are no associated interfaces running on the pipe yet. It is okay to | 576 // There are no associated interfaces running on the pipe yet. It is okay to |
576 // wait. | 577 // wait. |
577 EXPECT_TRUE(connection.binding()->WaitForIncomingMethodCall()); | 578 EXPECT_TRUE(connection.binding()->WaitForIncomingMethodCall()); |
578 | 579 |
579 // The previous wait has dispatched the GetSender request message, therefore | 580 // The previous wait has dispatched the GetSender request message, therefore |
580 // an associated interface has been set up on the pipe. It is not allowed to | 581 // an associated interface has been set up on the pipe. It is not allowed to |
581 // wait or pause. | 582 // wait or pause. |
582 EXPECT_TRUE(connection.binding()->HasAssociatedInterfaces()); | 583 EXPECT_TRUE(connection.binding()->HasAssociatedInterfaces()); |
583 } | 584 } |
584 | 585 |
| 586 class SingleMethodImpl : public sample::SingleMethod { |
| 587 public: |
| 588 explicit SingleMethodImpl(const Closure& on_call, |
| 589 sample::SingleMethodAssociatedRequest request) |
| 590 : on_call_(on_call), binding_(this, std::move(request)) { |
| 591 } |
| 592 ~SingleMethodImpl() override {} |
| 593 |
| 594 void set_connection_error_handler(const Closure& handler) { |
| 595 binding_.set_connection_error_handler(handler); |
| 596 } |
| 597 |
| 598 private: |
| 599 // sample::SingleMethod: |
| 600 void Call() override { on_call_.Run(); } |
| 601 |
| 602 const Closure on_call_; |
| 603 AssociatedBinding<SingleMethod> binding_; |
| 604 }; |
| 605 |
| 606 class SingleMethodVendorImpl : public sample::SingleMethodVendor { |
| 607 public: |
| 608 SingleMethodVendorImpl(sample::SingleMethodVendorRequest request) |
| 609 : binding_(this, std::move(request)) { |
| 610 binding_.SetAllowSyncDispatch(true); |
| 611 } |
| 612 ~SingleMethodVendorImpl() override {} |
| 613 |
| 614 void SetCallerCallback(const Closure& callback) { |
| 615 caller_callback_ = callback; |
| 616 } |
| 617 |
| 618 void GetCaller(sample::SingleMethodAssociatedRequest request) override { |
| 619 SingleMethodImpl* impl = |
| 620 new SingleMethodImpl(caller_callback_, std::move(request)); |
| 621 impl->set_connection_error_handler([impl] { delete impl; }); |
| 622 } |
| 623 |
| 624 private: |
| 625 Binding<SingleMethodVendor> binding_; |
| 626 Closure caller_callback_; |
| 627 }; |
| 628 |
| 629 TEST_F(AssociatedInterfaceTest, SyncDispatch) { |
| 630 sample::SingleMethodVendorPtr ptr; |
| 631 SingleMethodVendorImpl vendor(GetProxy(&ptr)); |
| 632 |
| 633 bool called1 = false, called2 = false; |
| 634 sample::SingleMethodAssociatedPtr caller1, caller2; |
| 635 |
| 636 vendor.SetCallerCallback([&called1] { |
| 637 EXPECT_FALSE(called1); |
| 638 called1 = true; |
| 639 }); |
| 640 ptr->GetCaller(GetProxy(&caller1, ptr.associated_group())); |
| 641 vendor.SetCallerCallback([&called2] { |
| 642 EXPECT_FALSE(called2); |
| 643 called2 = true; |
| 644 }); |
| 645 ptr->GetCaller(GetProxy(&caller2, ptr.associated_group())); |
| 646 |
| 647 caller1->Call(); |
| 648 caller2->Call(); |
| 649 |
| 650 EXPECT_TRUE(called1); |
| 651 EXPECT_TRUE(called2); |
| 652 |
| 653 caller1.reset(); |
| 654 caller2.reset(); |
| 655 } |
| 656 |
585 } // namespace | 657 } // namespace |
586 } // namespace test | 658 } // namespace test |
587 } // namespace mojo | 659 } // namespace mojo |
OLD | NEW |