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/callback.h" | 11 #include "base/callback.h" |
12 #include "base/callback_helpers.h" | 12 #include "base/callback_helpers.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
16 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
17 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
18 #include "mojo/public/cpp/bindings/associated_binding.h" | 18 #include "mojo/public/cpp/bindings/associated_binding.h" |
19 #include "mojo/public/cpp/bindings/associated_group.h" | 19 #include "mojo/public/cpp/bindings/associated_group.h" |
20 #include "mojo/public/cpp/bindings/associated_interface_ptr.h" | 20 #include "mojo/public/cpp/bindings/associated_interface_ptr.h" |
21 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" | 21 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" |
22 #include "mojo/public/cpp/bindings/associated_interface_request.h" | 22 #include "mojo/public/cpp/bindings/associated_interface_request.h" |
23 #include "mojo/public/cpp/bindings/binding.h" | 23 #include "mojo/public/cpp/bindings/binding.h" |
24 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" | 24 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" |
| 25 #include "mojo/public/cpp/bindings/strong_binding.h" |
25 #include "mojo/public/interfaces/bindings/tests/ping_service.mojom.h" | 26 #include "mojo/public/interfaces/bindings/tests/ping_service.mojom.h" |
26 #include "mojo/public/interfaces/bindings/tests/test_associated_interfaces.mojom
.h" | 27 #include "mojo/public/interfaces/bindings/tests/test_associated_interfaces.mojom
.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
28 | 29 |
29 namespace mojo { | 30 namespace mojo { |
30 namespace test { | 31 namespace test { |
31 namespace { | 32 namespace { |
32 | 33 |
33 using mojo::internal::MultiplexRouter; | 34 using mojo::internal::MultiplexRouter; |
34 | 35 |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 } | 154 } |
154 | 155 |
155 base::Callback<void(int32_t)> ExpectValueSetFlagAndRunClosure( | 156 base::Callback<void(int32_t)> ExpectValueSetFlagAndRunClosure( |
156 int32_t expected_value, | 157 int32_t expected_value, |
157 bool* flag, | 158 bool* flag, |
158 const base::Closure& closure) { | 159 const base::Closure& closure) { |
159 return base::Bind( | 160 return base::Bind( |
160 &DoExpectValueSetFlagAndRunClosure, expected_value, flag, closure); | 161 &DoExpectValueSetFlagAndRunClosure, expected_value, flag, closure); |
161 } | 162 } |
162 | 163 |
| 164 void Fail() { |
| 165 FAIL() << "Unexpected connection error"; |
| 166 } |
| 167 |
163 TEST_F(AssociatedInterfaceTest, InterfacesAtBothEnds) { | 168 TEST_F(AssociatedInterfaceTest, InterfacesAtBothEnds) { |
164 // Bind to the same pipe two associated interfaces, whose implementation lives | 169 // Bind to the same pipe two associated interfaces, whose implementation lives |
165 // at different ends. Test that the two don't interfere with each other. | 170 // at different ends. Test that the two don't interfere with each other. |
166 | 171 |
167 MessagePipe pipe; | 172 MessagePipe pipe; |
168 scoped_refptr<MultiplexRouter> router0(new MultiplexRouter( | 173 scoped_refptr<MultiplexRouter> router0(new MultiplexRouter( |
169 true, std::move(pipe.handle0), base::ThreadTaskRunnerHandle::Get())); | 174 true, std::move(pipe.handle0), base::ThreadTaskRunnerHandle::Get())); |
170 scoped_refptr<MultiplexRouter> router1(new MultiplexRouter( | 175 scoped_refptr<MultiplexRouter> router1(new MultiplexRouter( |
171 false, std::move(pipe.handle1), base::ThreadTaskRunnerHandle::Get())); | 176 false, std::move(pipe.handle1), base::ThreadTaskRunnerHandle::Get())); |
172 | 177 |
(...skipping 560 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
733 base::RunLoop loop; | 738 base::RunLoop loop; |
734 ping_b->Ping(loop.QuitClosure()); | 739 ping_b->Ping(loop.QuitClosure()); |
735 loop.Run(); | 740 loop.Run(); |
736 } | 741 } |
737 | 742 |
738 EXPECT_EQ(3, a_status); | 743 EXPECT_EQ(3, a_status); |
739 EXPECT_EQ(3, b_status); | 744 EXPECT_EQ(3, b_status); |
740 } | 745 } |
741 } | 746 } |
742 | 747 |
| 748 TEST_F(AssociatedInterfaceTest, AssociatedPtrFlushForTesting) { |
| 749 MessagePipe pipe; |
| 750 scoped_refptr<MultiplexRouter> router0(new MultiplexRouter( |
| 751 true, std::move(pipe.handle0), base::ThreadTaskRunnerHandle::Get())); |
| 752 scoped_refptr<MultiplexRouter> router1(new MultiplexRouter( |
| 753 false, std::move(pipe.handle1), base::ThreadTaskRunnerHandle::Get())); |
| 754 |
| 755 AssociatedInterfaceRequest<IntegerSender> request; |
| 756 IntegerSenderAssociatedPtrInfo ptr_info; |
| 757 |
| 758 router0->CreateAssociatedGroup()->CreateAssociatedInterface( |
| 759 AssociatedGroup::WILL_PASS_PTR, &ptr_info, &request); |
| 760 ptr_info = EmulatePassingAssociatedPtrInfo(std::move(ptr_info), router1); |
| 761 |
| 762 IntegerSenderImpl impl0(std::move(request)); |
| 763 AssociatedInterfacePtr<IntegerSender> ptr0; |
| 764 ptr0.Bind(std::move(ptr_info)); |
| 765 ptr0.set_connection_error_handler(base::Bind(&Fail)); |
| 766 |
| 767 bool ptr0_callback_run = false; |
| 768 ptr0->Echo(123, ExpectValueSetFlagAndRunClosure( |
| 769 123, &ptr0_callback_run, base::Bind(&base::DoNothing))); |
| 770 ptr0.FlushForTesting(); |
| 771 EXPECT_TRUE(ptr0_callback_run); |
| 772 } |
| 773 |
| 774 void SetBool(bool* value) { |
| 775 *value = true; |
| 776 } |
| 777 |
| 778 template <typename T> |
| 779 void SetBoolWithUnusedParameter(bool* value, T unused) { |
| 780 *value = true; |
| 781 } |
| 782 |
| 783 TEST_F(AssociatedInterfaceTest, AssociatedPtrFlushForTestingWithClosedPeer) { |
| 784 MessagePipe pipe; |
| 785 scoped_refptr<MultiplexRouter> router0(new MultiplexRouter( |
| 786 true, std::move(pipe.handle0), base::ThreadTaskRunnerHandle::Get())); |
| 787 scoped_refptr<MultiplexRouter> router1(new MultiplexRouter( |
| 788 false, std::move(pipe.handle1), base::ThreadTaskRunnerHandle::Get())); |
| 789 |
| 790 AssociatedInterfaceRequest<IntegerSender> request; |
| 791 IntegerSenderAssociatedPtrInfo ptr_info; |
| 792 |
| 793 router0->CreateAssociatedGroup()->CreateAssociatedInterface( |
| 794 AssociatedGroup::WILL_PASS_PTR, &ptr_info, &request); |
| 795 ptr_info = EmulatePassingAssociatedPtrInfo(std::move(ptr_info), router1); |
| 796 |
| 797 AssociatedInterfacePtr<IntegerSender> ptr0; |
| 798 ptr0.Bind(std::move(ptr_info)); |
| 799 bool called = false; |
| 800 ptr0.set_connection_error_handler(base::Bind(&SetBool, &called)); |
| 801 request = nullptr; |
| 802 |
| 803 ptr0.FlushForTesting(); |
| 804 EXPECT_TRUE(called); |
| 805 ptr0.FlushForTesting(); |
| 806 } |
| 807 |
| 808 TEST_F(AssociatedInterfaceTest, AssociatedBindingFlushForTesting) { |
| 809 MessagePipe pipe; |
| 810 scoped_refptr<MultiplexRouter> router0(new MultiplexRouter( |
| 811 true, std::move(pipe.handle0), base::ThreadTaskRunnerHandle::Get())); |
| 812 scoped_refptr<MultiplexRouter> router1(new MultiplexRouter( |
| 813 false, std::move(pipe.handle1), base::ThreadTaskRunnerHandle::Get())); |
| 814 |
| 815 AssociatedInterfaceRequest<IntegerSender> request; |
| 816 IntegerSenderAssociatedPtrInfo ptr_info; |
| 817 |
| 818 router0->CreateAssociatedGroup()->CreateAssociatedInterface( |
| 819 AssociatedGroup::WILL_PASS_PTR, &ptr_info, &request); |
| 820 ptr_info = EmulatePassingAssociatedPtrInfo(std::move(ptr_info), router1); |
| 821 |
| 822 IntegerSenderImpl impl0(std::move(request)); |
| 823 impl0.set_connection_error_handler(base::Bind(&Fail)); |
| 824 AssociatedInterfacePtr<IntegerSender> ptr0; |
| 825 ptr0.Bind(std::move(ptr_info)); |
| 826 |
| 827 bool ptr0_callback_run = false; |
| 828 ptr0->Echo(123, ExpectValueSetFlagAndRunClosure( |
| 829 123, &ptr0_callback_run, base::Bind(&base::DoNothing))); |
| 830 // Because the flush is sent from the binding, it only guarantees that the |
| 831 // request has been received, not the response. The second flush waits for the |
| 832 // response to be received. |
| 833 impl0.binding()->FlushForTesting(); |
| 834 impl0.binding()->FlushForTesting(); |
| 835 EXPECT_TRUE(ptr0_callback_run); |
| 836 } |
| 837 |
| 838 TEST_F(AssociatedInterfaceTest, |
| 839 AssociatedBindingFlushForTestingWithClosedPeer) { |
| 840 MessagePipe pipe; |
| 841 scoped_refptr<MultiplexRouter> router0(new MultiplexRouter( |
| 842 true, std::move(pipe.handle0), base::ThreadTaskRunnerHandle::Get())); |
| 843 scoped_refptr<MultiplexRouter> router1(new MultiplexRouter( |
| 844 false, std::move(pipe.handle1), base::ThreadTaskRunnerHandle::Get())); |
| 845 |
| 846 AssociatedInterfaceRequest<IntegerSender> request; |
| 847 { |
| 848 IntegerSenderAssociatedPtrInfo ptr_info; |
| 849 |
| 850 router0->CreateAssociatedGroup()->CreateAssociatedInterface( |
| 851 AssociatedGroup::WILL_PASS_PTR, &ptr_info, &request); |
| 852 } |
| 853 |
| 854 IntegerSenderImpl impl(std::move(request)); |
| 855 bool called = false; |
| 856 impl.set_connection_error_handler(base::Bind(&SetBool, &called)); |
| 857 impl.binding()->FlushForTesting(); |
| 858 EXPECT_TRUE(called); |
| 859 impl.binding()->FlushForTesting(); |
| 860 } |
| 861 |
| 862 TEST_F(AssociatedInterfaceTest, BindingFlushForTesting) { |
| 863 IntegerSenderConnectionPtr ptr; |
| 864 IntegerSenderConnectionImpl impl(GetProxy(&ptr)); |
| 865 bool called = false; |
| 866 ptr->AsyncGetSender(base::Bind( |
| 867 &SetBoolWithUnusedParameter<IntegerSenderAssociatedPtrInfo>, &called)); |
| 868 EXPECT_FALSE(called); |
| 869 impl.binding()->set_connection_error_handler(base::Bind(&Fail)); |
| 870 // Because the flush is sent from the binding, it only guarantees that the |
| 871 // request has been received, not the response. The second flush waits for the |
| 872 // response to be received. |
| 873 impl.binding()->FlushForTesting(); |
| 874 impl.binding()->FlushForTesting(); |
| 875 EXPECT_TRUE(called); |
| 876 } |
| 877 |
| 878 TEST_F(AssociatedInterfaceTest, BindingFlushForTestingWithClosedPeer) { |
| 879 IntegerSenderConnectionPtr ptr; |
| 880 IntegerSenderConnectionImpl impl(GetProxy(&ptr)); |
| 881 bool called = false; |
| 882 impl.binding()->set_connection_error_handler(base::Bind(&SetBool, &called)); |
| 883 ptr.reset(); |
| 884 EXPECT_FALSE(called); |
| 885 impl.binding()->FlushForTesting(); |
| 886 EXPECT_TRUE(called); |
| 887 impl.binding()->FlushForTesting(); |
| 888 } |
| 889 |
| 890 TEST_F(AssociatedInterfaceTest, StrongBindingFlushForTesting) { |
| 891 IntegerSenderConnectionPtr ptr; |
| 892 IntegerSenderConnectionImpl impl(IntegerSenderConnectionRequest{}); |
| 893 mojo::StrongBinding<IntegerSenderConnection> binding(&impl, GetProxy(&ptr)); |
| 894 binding.set_connection_error_handler(base::Bind(&Fail)); |
| 895 bool called = false; |
| 896 IntegerSenderAssociatedPtr sender_ptr; |
| 897 ptr->GetSender(GetProxy(&sender_ptr, ptr.associated_group())); |
| 898 sender_ptr->Echo(1, base::Bind(&SetBoolWithUnusedParameter<int>, &called)); |
| 899 EXPECT_FALSE(called); |
| 900 // Because the flush is sent from the binding, it only guarantees that the |
| 901 // request has been received, not the response. The second flush waits for the |
| 902 // response to be received. |
| 903 binding.FlushForTesting(); |
| 904 binding.FlushForTesting(); |
| 905 EXPECT_TRUE(called); |
| 906 } |
| 907 |
| 908 TEST_F(AssociatedInterfaceTest, StrongBindingFlushForTestingWithClosedPeer) { |
| 909 IntegerSenderConnectionPtr ptr; |
| 910 mojo::StrongBinding<IntegerSenderConnection> binding( |
| 911 new IntegerSenderConnectionImpl(IntegerSenderConnectionRequest{}), |
| 912 GetProxy(&ptr)); |
| 913 bool called = false; |
| 914 binding.set_connection_error_handler(base::Bind(&SetBool, &called)); |
| 915 ptr.reset(); |
| 916 EXPECT_FALSE(called); |
| 917 binding.FlushForTesting(); |
| 918 EXPECT_TRUE(called); |
| 919 binding.FlushForTesting(); |
| 920 } |
| 921 |
| 922 TEST_F(AssociatedInterfaceTest, PtrFlushForTesting) { |
| 923 IntegerSenderConnectionPtr ptr; |
| 924 IntegerSenderConnectionImpl impl(GetProxy(&ptr)); |
| 925 bool called = false; |
| 926 ptr.set_connection_error_handler(base::Bind(&Fail)); |
| 927 ptr->AsyncGetSender(base::Bind( |
| 928 &SetBoolWithUnusedParameter<IntegerSenderAssociatedPtrInfo>, &called)); |
| 929 EXPECT_FALSE(called); |
| 930 ptr.FlushForTesting(); |
| 931 EXPECT_TRUE(called); |
| 932 } |
| 933 |
| 934 TEST_F(AssociatedInterfaceTest, PtrFlushForTestingWithClosedPeer) { |
| 935 IntegerSenderConnectionPtr ptr; |
| 936 GetProxy(&ptr); |
| 937 bool called = false; |
| 938 ptr.set_connection_error_handler(base::Bind(&SetBool, &called)); |
| 939 EXPECT_FALSE(called); |
| 940 ptr.FlushForTesting(); |
| 941 EXPECT_TRUE(called); |
| 942 ptr.FlushForTesting(); |
| 943 } |
| 944 |
743 } // namespace | 945 } // namespace |
744 } // namespace test | 946 } // namespace test |
745 } // namespace mojo | 947 } // namespace mojo |
OLD | NEW |