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 // Bind to the same pipe two associated interfaces, whose implementation lives | |
yzshen1
2016/08/31 17:06:47
This comment doesn't apply.
Sam McNally
2016/09/01 00:44:37
Done.
| |
750 // at different ends. Test that the two don't interfere with each other. | |
751 | |
752 MessagePipe pipe; | |
753 scoped_refptr<MultiplexRouter> router0(new MultiplexRouter( | |
754 true, std::move(pipe.handle0), base::ThreadTaskRunnerHandle::Get())); | |
755 scoped_refptr<MultiplexRouter> router1(new MultiplexRouter( | |
756 false, std::move(pipe.handle1), base::ThreadTaskRunnerHandle::Get())); | |
757 | |
758 AssociatedInterfaceRequest<IntegerSender> request; | |
759 IntegerSenderAssociatedPtrInfo ptr_info; | |
760 | |
761 router0->CreateAssociatedGroup()->CreateAssociatedInterface( | |
762 AssociatedGroup::WILL_PASS_PTR, &ptr_info, &request); | |
763 ptr_info = EmulatePassingAssociatedPtrInfo(std::move(ptr_info), router1); | |
764 | |
765 IntegerSenderImpl impl0(std::move(request)); | |
766 AssociatedInterfacePtr<IntegerSender> ptr0; | |
767 ptr0.Bind(std::move(ptr_info)); | |
768 ptr0.set_connection_error_handler(base::Bind(&Fail)); | |
769 | |
770 bool ptr0_callback_run = false; | |
771 ptr0->Echo(123, ExpectValueSetFlagAndRunClosure( | |
772 123, &ptr0_callback_run, base::Bind(&base::DoNothing))); | |
773 ptr0.FlushForTesting(); | |
774 EXPECT_TRUE(ptr0_callback_run); | |
775 } | |
776 | |
777 void SetBool(bool* value) { | |
778 *value = true; | |
779 } | |
780 | |
781 template <typename T> | |
782 void SetBoolWithUnusedParameter(bool* value, T unused) { | |
783 *value = true; | |
784 } | |
785 | |
786 TEST_F(AssociatedInterfaceTest, AssociatedPtrFlushForTestingWithClosedPeer) { | |
787 // Bind to the same pipe two associated interfaces, whose implementation lives | |
788 // at different ends. Test that the two don't interfere with each other. | |
yzshen1
2016/08/31 17:06:47
ditto
Sam McNally
2016/09/01 00:44:37
Done.
| |
789 | |
790 MessagePipe pipe; | |
791 scoped_refptr<MultiplexRouter> router0(new MultiplexRouter( | |
792 true, std::move(pipe.handle0), base::ThreadTaskRunnerHandle::Get())); | |
793 scoped_refptr<MultiplexRouter> router1(new MultiplexRouter( | |
794 false, std::move(pipe.handle1), base::ThreadTaskRunnerHandle::Get())); | |
795 | |
796 AssociatedInterfaceRequest<IntegerSender> request; | |
797 IntegerSenderAssociatedPtrInfo ptr_info; | |
798 | |
799 router0->CreateAssociatedGroup()->CreateAssociatedInterface( | |
800 AssociatedGroup::WILL_PASS_PTR, &ptr_info, &request); | |
801 ptr_info = EmulatePassingAssociatedPtrInfo(std::move(ptr_info), router1); | |
802 | |
803 AssociatedInterfacePtr<IntegerSender> ptr0; | |
804 ptr0.Bind(std::move(ptr_info)); | |
805 bool called = false; | |
806 ptr0.set_connection_error_handler(base::Bind(&SetBool, &called)); | |
807 request = nullptr; | |
808 | |
809 ptr0.FlushForTesting(); | |
810 EXPECT_TRUE(called); | |
811 ptr0.FlushForTesting(); | |
812 } | |
813 | |
814 TEST_F(AssociatedInterfaceTest, AssociatedBindingFlushForTesting) { | |
815 // Bind to the same pipe two associated interfaces, whose implementation lives | |
yzshen1
2016/08/31 17:06:47
ditto
Sam McNally
2016/09/01 00:44:37
Done.
| |
816 // at different ends. Test that the two don't interfere with each other. | |
817 | |
818 MessagePipe pipe; | |
819 scoped_refptr<MultiplexRouter> router0(new MultiplexRouter( | |
820 true, std::move(pipe.handle0), base::ThreadTaskRunnerHandle::Get())); | |
821 scoped_refptr<MultiplexRouter> router1(new MultiplexRouter( | |
822 false, std::move(pipe.handle1), base::ThreadTaskRunnerHandle::Get())); | |
823 | |
824 AssociatedInterfaceRequest<IntegerSender> request; | |
825 IntegerSenderAssociatedPtrInfo ptr_info; | |
826 | |
827 router0->CreateAssociatedGroup()->CreateAssociatedInterface( | |
828 AssociatedGroup::WILL_PASS_PTR, &ptr_info, &request); | |
829 ptr_info = EmulatePassingAssociatedPtrInfo(std::move(ptr_info), router1); | |
830 | |
831 IntegerSenderImpl impl0(std::move(request)); | |
832 impl0.set_connection_error_handler(base::Bind(&Fail)); | |
833 AssociatedInterfacePtr<IntegerSender> ptr0; | |
834 ptr0.Bind(std::move(ptr_info)); | |
835 | |
836 bool ptr0_callback_run = false; | |
837 ptr0->Echo(123, ExpectValueSetFlagAndRunClosure( | |
838 123, &ptr0_callback_run, base::Bind(&base::DoNothing))); | |
839 impl0.binding()->FlushForTesting(); | |
840 EXPECT_TRUE(ptr0_callback_run); | |
841 } | |
842 | |
843 TEST_F(AssociatedInterfaceTest, | |
844 AssociatedBindingFlushForTestingWithClosedPeer) { | |
845 // Bind to the same pipe two associated interfaces, whose implementation lives | |
yzshen1
2016/08/31 17:06:47
ditto
Sam McNally
2016/09/01 00:44:37
Done.
| |
846 // at different ends. Test that the two don't interfere with each other. | |
847 | |
848 MessagePipe pipe; | |
849 scoped_refptr<MultiplexRouter> router0(new MultiplexRouter( | |
850 true, std::move(pipe.handle0), base::ThreadTaskRunnerHandle::Get())); | |
851 scoped_refptr<MultiplexRouter> router1(new MultiplexRouter( | |
852 false, std::move(pipe.handle1), base::ThreadTaskRunnerHandle::Get())); | |
853 | |
854 AssociatedInterfaceRequest<IntegerSender> request; | |
855 { | |
856 IntegerSenderAssociatedPtrInfo ptr_info; | |
857 | |
858 router0->CreateAssociatedGroup()->CreateAssociatedInterface( | |
859 AssociatedGroup::WILL_PASS_PTR, &ptr_info, &request); | |
860 } | |
861 | |
862 IntegerSenderImpl impl(std::move(request)); | |
863 bool called = false; | |
864 impl.set_connection_error_handler(base::Bind(&SetBool, &called)); | |
865 impl.binding()->FlushForTesting(); | |
866 EXPECT_TRUE(called); | |
867 impl.binding()->FlushForTesting(); | |
868 } | |
869 | |
870 TEST_F(AssociatedInterfaceTest, BindingFlushForTesting) { | |
871 IntegerSenderConnectionPtr ptr; | |
872 IntegerSenderConnectionImpl impl(GetProxy(&ptr)); | |
873 bool called = false; | |
874 ptr->AsyncGetSender(base::Bind( | |
875 &SetBoolWithUnusedParameter<IntegerSenderAssociatedPtrInfo>, &called)); | |
876 EXPECT_FALSE(called); | |
877 impl.binding()->set_connection_error_handler(base::Bind(&Fail)); | |
878 impl.binding()->FlushForTesting(); | |
879 impl.binding()->FlushForTesting(); | |
880 EXPECT_TRUE(called); | |
881 } | |
882 | |
883 TEST_F(AssociatedInterfaceTest, BindingFlushForTestingWithClosedPeer) { | |
884 IntegerSenderConnectionPtr ptr; | |
885 IntegerSenderConnectionImpl impl(GetProxy(&ptr)); | |
886 bool called = false; | |
887 impl.binding()->set_connection_error_handler(base::Bind(&SetBool, &called)); | |
888 ptr.reset(); | |
889 EXPECT_FALSE(called); | |
890 impl.binding()->FlushForTesting(); | |
891 EXPECT_TRUE(called); | |
892 impl.binding()->FlushForTesting(); | |
893 } | |
894 | |
895 TEST_F(AssociatedInterfaceTest, StrongBindingFlushForTesting) { | |
896 IntegerSenderConnectionPtr ptr; | |
897 IntegerSenderConnectionImpl impl(IntegerSenderConnectionRequest{}); | |
898 mojo::StrongBinding<IntegerSenderConnection> binding(&impl, GetProxy(&ptr)); | |
899 binding.set_connection_error_handler(base::Bind(&Fail)); | |
900 bool called = false; | |
901 IntegerSenderAssociatedPtr sender_ptr; | |
902 ptr->GetSender(GetProxy(&sender_ptr, ptr.associated_group())); | |
903 sender_ptr->Echo(1, base::Bind(&SetBoolWithUnusedParameter<int>, &called)); | |
904 EXPECT_FALSE(called); | |
905 binding.FlushForTesting(); | |
906 binding.FlushForTesting(); | |
yzshen1
2016/08/31 17:06:47
Out of curiosity: why flush twice here (and line 8
Sam McNally
2016/09/01 00:44:37
The flush and the request are sent from opposite e
| |
907 EXPECT_TRUE(called); | |
908 } | |
909 | |
910 TEST_F(AssociatedInterfaceTest, StrongBindingFlushForTestingWithClosedPeer) { | |
911 IntegerSenderConnectionPtr ptr; | |
912 mojo::StrongBinding<IntegerSenderConnection> binding( | |
913 new IntegerSenderConnectionImpl(IntegerSenderConnectionRequest{}), | |
914 GetProxy(&ptr)); | |
915 bool called = false; | |
916 binding.set_connection_error_handler(base::Bind(&SetBool, &called)); | |
917 ptr.reset(); | |
918 EXPECT_FALSE(called); | |
919 binding.FlushForTesting(); | |
920 EXPECT_TRUE(called); | |
921 binding.FlushForTesting(); | |
922 } | |
923 | |
924 TEST_F(AssociatedInterfaceTest, PtrFlushForTesting) { | |
925 IntegerSenderConnectionPtr ptr; | |
926 IntegerSenderConnectionImpl impl(GetProxy(&ptr)); | |
927 bool called = false; | |
928 ptr.set_connection_error_handler(base::Bind(&Fail)); | |
929 ptr->AsyncGetSender(base::Bind( | |
930 &SetBoolWithUnusedParameter<IntegerSenderAssociatedPtrInfo>, &called)); | |
931 EXPECT_FALSE(called); | |
932 ptr.FlushForTesting(); | |
933 EXPECT_TRUE(called); | |
934 } | |
935 | |
936 TEST_F(AssociatedInterfaceTest, PtrFlushForTestingWithClosedPeer) { | |
937 IntegerSenderConnectionPtr ptr; | |
938 GetProxy(&ptr); | |
939 bool called = false; | |
940 ptr.set_connection_error_handler(base::Bind(&SetBool, &called)); | |
941 EXPECT_FALSE(called); | |
942 ptr.FlushForTesting(); | |
943 EXPECT_TRUE(called); | |
944 ptr.FlushForTesting(); | |
945 } | |
946 | |
743 } // namespace | 947 } // namespace |
744 } // namespace test | 948 } // namespace test |
745 } // namespace mojo | 949 } // namespace mojo |
OLD | NEW |