| 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/sequenced_task_runner_handle.h" |
| 16 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
| 17 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
| 18 #include "mojo/public/cpp/bindings/associated_binding.h" | 19 #include "mojo/public/cpp/bindings/associated_binding.h" |
| 19 #include "mojo/public/cpp/bindings/associated_group.h" | 20 #include "mojo/public/cpp/bindings/associated_group.h" |
| 20 #include "mojo/public/cpp/bindings/associated_interface_ptr.h" | 21 #include "mojo/public/cpp/bindings/associated_interface_ptr.h" |
| 21 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" | 22 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h" |
| 22 #include "mojo/public/cpp/bindings/associated_interface_request.h" | 23 #include "mojo/public/cpp/bindings/associated_interface_request.h" |
| 23 #include "mojo/public/cpp/bindings/binding.h" | 24 #include "mojo/public/cpp/bindings/binding.h" |
| 24 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" | 25 #include "mojo/public/cpp/bindings/lib/multiplex_router.h" |
| 25 #include "mojo/public/cpp/bindings/strong_binding.h" | 26 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 27 #include "mojo/public/cpp/bindings/thread_safe_interface_ptr.h" |
| 26 #include "mojo/public/interfaces/bindings/tests/ping_service.mojom.h" | 28 #include "mojo/public/interfaces/bindings/tests/ping_service.mojom.h" |
| 27 #include "mojo/public/interfaces/bindings/tests/test_associated_interfaces.mojom
.h" | 29 #include "mojo/public/interfaces/bindings/tests/test_associated_interfaces.mojom
.h" |
| 28 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
| 29 | 31 |
| 30 namespace mojo { | 32 namespace mojo { |
| 31 namespace test { | 33 namespace test { |
| 32 namespace { | 34 namespace { |
| 33 | 35 |
| 34 using mojo::internal::MultiplexRouter; | 36 using mojo::internal::MultiplexRouter; |
| 35 | 37 |
| (...skipping 950 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 986 EXPECT_EQ("long time no see", description); | 988 EXPECT_EQ("long time no see", description); |
| 987 quit_closure.Run(); | 989 quit_closure.Run(); |
| 988 }, | 990 }, |
| 989 run_loop.QuitClosure())); | 991 run_loop.QuitClosure())); |
| 990 | 992 |
| 991 request.ResetWithReason(789u, "long time no see"); | 993 request.ResetWithReason(789u, "long time no see"); |
| 992 | 994 |
| 993 run_loop.Run(); | 995 run_loop.Run(); |
| 994 } | 996 } |
| 995 | 997 |
| 998 TEST_F(AssociatedInterfaceTest, ThreadSafeAssociatedInterfacePtr) { |
| 999 IntegerSenderConnectionPtr connection_ptr; |
| 1000 IntegerSenderConnectionImpl connection(GetProxy(&connection_ptr)); |
| 1001 |
| 1002 IntegerSenderAssociatedPtr sender; |
| 1003 connection_ptr->GetSender( |
| 1004 GetProxy(&sender, connection_ptr.associated_group())); |
| 1005 |
| 1006 scoped_refptr<ThreadSafeIntegerSenderAssociatedPtr> thread_safe_sender = |
| 1007 ThreadSafeIntegerSenderAssociatedPtr::Create(std::move(sender)); |
| 1008 |
| 1009 { |
| 1010 // Test the thread safe pointer can be used from the interface ptr thread. |
| 1011 int32_t echoed_value = 0; |
| 1012 base::RunLoop run_loop; |
| 1013 (*thread_safe_sender) |
| 1014 ->Echo(123, base::Bind(&CaptureInt32, &echoed_value, |
| 1015 run_loop.QuitClosure())); |
| 1016 run_loop.Run(); |
| 1017 EXPECT_EQ(123, echoed_value); |
| 1018 } |
| 1019 |
| 1020 // Test the thread safe pointer can be used from another thread. |
| 1021 base::RunLoop run_loop; |
| 1022 base::Thread other_thread("service test thread"); |
| 1023 other_thread.Start(); |
| 1024 |
| 1025 auto run_method = base::Bind( |
| 1026 [](const scoped_refptr<base::TaskRunner>& main_task_runner, |
| 1027 const base::Closure& quit_closure, |
| 1028 const scoped_refptr<ThreadSafeIntegerSenderAssociatedPtr>& |
| 1029 thread_safe_sender) { |
| 1030 auto done_callback = base::Bind( |
| 1031 [](const scoped_refptr<base::TaskRunner>& main_task_runner, |
| 1032 const base::Closure& quit_closure, |
| 1033 base::PlatformThreadId thread_id, int32_t result) { |
| 1034 EXPECT_EQ(123, result); |
| 1035 // Validate the callback is invoked on the calling thread. |
| 1036 EXPECT_EQ(thread_id, base::PlatformThread::CurrentId()); |
| 1037 // Notify the run_loop to quit. |
| 1038 main_task_runner->PostTask(FROM_HERE, quit_closure); |
| 1039 }); |
| 1040 (*thread_safe_sender) |
| 1041 ->Echo(123, |
| 1042 base::Bind(done_callback, main_task_runner, quit_closure, |
| 1043 base::PlatformThread::CurrentId())); |
| 1044 }, |
| 1045 base::SequencedTaskRunnerHandle::Get(), run_loop.QuitClosure(), |
| 1046 thread_safe_sender); |
| 1047 other_thread.message_loop()->task_runner()->PostTask(FROM_HERE, run_method); |
| 1048 |
| 1049 // Block until the method callback is called on the background thread. |
| 1050 run_loop.Run(); |
| 1051 } |
| 1052 |
| 996 } // namespace | 1053 } // namespace |
| 997 } // namespace test | 1054 } // namespace test |
| 998 } // namespace mojo | 1055 } // namespace mojo |
| OLD | NEW |