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

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

Issue 2280483002: Add FlushForTesting to InterfacePtr and Binding. (Closed)
Patch Set: Created 4 years, 3 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 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
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
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
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.
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, AssociatedPtrFlushForTestingWhenClosed) {
815 IntegerSenderAssociatedPtr sender;
816 sender.FlushForTesting();
817 }
818
819 TEST_F(AssociatedInterfaceTest, AssociatedBindingFlushForTesting) {
820 // Bind to the same pipe two associated interfaces, whose implementation lives
821 // at different ends. Test that the two don't interfere with each other.
822
823 MessagePipe pipe;
824 scoped_refptr<MultiplexRouter> router0(new MultiplexRouter(
825 true, std::move(pipe.handle0), base::ThreadTaskRunnerHandle::Get()));
826 scoped_refptr<MultiplexRouter> router1(new MultiplexRouter(
827 false, std::move(pipe.handle1), base::ThreadTaskRunnerHandle::Get()));
828
829 AssociatedInterfaceRequest<IntegerSender> request;
830 IntegerSenderAssociatedPtrInfo ptr_info;
831
832 router0->CreateAssociatedGroup()->CreateAssociatedInterface(
833 AssociatedGroup::WILL_PASS_PTR, &ptr_info, &request);
834 ptr_info = EmulatePassingAssociatedPtrInfo(std::move(ptr_info), router1);
835
836 IntegerSenderImpl impl0(std::move(request));
837 impl0.set_connection_error_handler(base::Bind(&Fail));
838 AssociatedInterfacePtr<IntegerSender> ptr0;
839 ptr0.Bind(std::move(ptr_info));
840
841 bool ptr0_callback_run = false;
842 ptr0->Echo(123, ExpectValueSetFlagAndRunClosure(
843 123, &ptr0_callback_run, base::Bind(&base::DoNothing)));
844 impl0.binding()->FlushForTesting();
845 EXPECT_TRUE(ptr0_callback_run);
846 }
847
848 TEST_F(AssociatedInterfaceTest,
849 AssociatedBindingFlushForTestingWithClosedPeer) {
850 // Bind to the same pipe two associated interfaces, whose implementation lives
851 // at different ends. Test that the two don't interfere with each other.
852
853 MessagePipe pipe;
854 scoped_refptr<MultiplexRouter> router0(new MultiplexRouter(
855 true, std::move(pipe.handle0), base::ThreadTaskRunnerHandle::Get()));
856 scoped_refptr<MultiplexRouter> router1(new MultiplexRouter(
857 false, std::move(pipe.handle1), base::ThreadTaskRunnerHandle::Get()));
858
859 AssociatedInterfaceRequest<IntegerSender> request;
860 {
861 IntegerSenderAssociatedPtrInfo ptr_info;
862
863 router0->CreateAssociatedGroup()->CreateAssociatedInterface(
864 AssociatedGroup::WILL_PASS_PTR, &ptr_info, &request);
865 }
866
867 IntegerSenderImpl impl(std::move(request));
868 bool called = false;
869 impl.set_connection_error_handler(base::Bind(&SetBool, &called));
870 impl.binding()->FlushForTesting();
871 EXPECT_TRUE(called);
872 impl.binding()->FlushForTesting();
873 }
874
875 TEST_F(AssociatedInterfaceTest, AssociatedBindingFlushForTestingWhenClosed) {
876 IntegerSenderImpl impl(IntegerSenderAssociatedRequest{});
877 impl.binding()->FlushForTesting();
878 }
879
880 TEST_F(AssociatedInterfaceTest, BindingFlushForTesting) {
881 IntegerSenderConnectionPtr ptr;
882 IntegerSenderConnectionImpl impl(GetProxy(&ptr));
883 bool called = false;
884 ptr->AsyncGetSender(base::Bind(
885 &SetBoolWithUnusedParameter<IntegerSenderAssociatedPtrInfo>, &called));
886 EXPECT_FALSE(called);
887 impl.binding()->set_connection_error_handler(base::Bind(&Fail));
888 impl.binding()->FlushForTesting();
889 impl.binding()->FlushForTesting();
890 EXPECT_TRUE(called);
891 }
892
893 TEST_F(AssociatedInterfaceTest, BindingFlushForTestingWithClosedPeer) {
894 IntegerSenderConnectionPtr ptr;
895 IntegerSenderConnectionImpl impl(GetProxy(&ptr));
896 bool called = false;
897 impl.binding()->set_connection_error_handler(base::Bind(&SetBool, &called));
898 ptr.reset();
899 EXPECT_FALSE(called);
900 impl.binding()->FlushForTesting();
901 EXPECT_TRUE(called);
902 impl.binding()->FlushForTesting();
903 }
904
905 TEST_F(AssociatedInterfaceTest, BindingFlushForTestingWhenClosed) {
906 IntegerSenderConnectionImpl impl(IntegerSenderConnectionRequest{});
907 Binding<IntegerSenderConnection> binding(&impl);
908 binding.FlushForTesting();
909 }
910
911 TEST_F(AssociatedInterfaceTest, StrongBindingFlushForTesting) {
912 IntegerSenderConnectionPtr ptr;
913 IntegerSenderConnectionImpl impl(IntegerSenderConnectionRequest{});
914 mojo::StrongBinding<IntegerSenderConnection> binding(&impl, GetProxy(&ptr));
915 binding.set_connection_error_handler(base::Bind(&Fail));
916 bool called = false;
917 IntegerSenderAssociatedPtr sender_ptr;
918 ptr->GetSender(GetProxy(&sender_ptr, ptr.associated_group()));
919 sender_ptr->Echo(1, base::Bind(&SetBoolWithUnusedParameter<int>, &called));
920 EXPECT_FALSE(called);
921 binding.FlushForTesting();
922 binding.FlushForTesting();
923 EXPECT_TRUE(called);
924 }
925
926 TEST_F(AssociatedInterfaceTest, StrongBindingFlushForTestingWithClosedPeer) {
927 IntegerSenderConnectionPtr ptr;
928 mojo::StrongBinding<IntegerSenderConnection> binding(
929 new IntegerSenderConnectionImpl(IntegerSenderConnectionRequest{}),
930 GetProxy(&ptr));
931 bool called = false;
932 binding.set_connection_error_handler(base::Bind(&SetBool, &called));
933 ptr.reset();
934 EXPECT_FALSE(called);
935 binding.FlushForTesting();
936 EXPECT_TRUE(called);
937 binding.FlushForTesting();
938 }
939
940 TEST_F(AssociatedInterfaceTest, StrongBindingFlushForTestingWhenClosed) {
941 IntegerSenderConnectionImpl impl(IntegerSenderConnectionRequest{});
942 StrongBinding<IntegerSenderConnection> binding(&impl);
943 binding.FlushForTesting();
944 }
945
946 TEST_F(AssociatedInterfaceTest, PtrFlushForTesting) {
947 IntegerSenderConnectionPtr ptr;
948 IntegerSenderConnectionImpl impl(GetProxy(&ptr));
949 bool called = false;
950 ptr.set_connection_error_handler(base::Bind(&Fail));
951 ptr->AsyncGetSender(base::Bind(
952 &SetBoolWithUnusedParameter<IntegerSenderAssociatedPtrInfo>, &called));
953 EXPECT_FALSE(called);
954 ptr.FlushForTesting();
955 EXPECT_TRUE(called);
956 }
957
958 TEST_F(AssociatedInterfaceTest, PtrFlushForTestingWithClosedPeer) {
959 IntegerSenderConnectionPtr ptr;
960 GetProxy(&ptr);
961 bool called = false;
962 ptr.set_connection_error_handler(base::Bind(&SetBool, &called));
963 EXPECT_FALSE(called);
964 ptr.FlushForTesting();
965 EXPECT_TRUE(called);
966 ptr.FlushForTesting();
967 }
968
969 TEST_F(AssociatedInterfaceTest, PtrFlushForTestingWhenClosed) {
970 IntegerSenderConnectionPtr ptr;
971 ptr.FlushForTesting();
972 }
973
743 } // namespace 974 } // namespace
744 } // namespace test 975 } // namespace test
745 } // namespace mojo 976 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698