Index: mojo/public/cpp/bindings/tests/sync_method_unittest.cc |
diff --git a/mojo/public/cpp/bindings/tests/sync_method_unittest.cc b/mojo/public/cpp/bindings/tests/sync_method_unittest.cc |
index 65a098ad626e8bac1eac286353f59388b943d826..10a8bda205667ba6cd80ba98eb724829df4da1d0 100644 |
--- a/mojo/public/cpp/bindings/tests/sync_method_unittest.cc |
+++ b/mojo/public/cpp/bindings/tests/sync_method_unittest.cc |
@@ -2,6 +2,8 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
+#include <utility> |
+ |
#include "base/bind.h" |
#include "base/logging.h" |
#include "base/macros.h" |
@@ -17,30 +19,59 @@ namespace mojo { |
namespace test { |
namespace { |
+template <typename... Args> |
+struct LambdaBinder { |
+ using CallbackType = base::Callback<void(Args...)>; |
+ |
+ template <typename Func> |
+ static void RunLambda(Func func, Args... args) { |
+ func(std::move(args)...); |
+ } |
+ |
+ template <typename Func> |
+ static CallbackType BindLambda(Func func) { |
+ return base::Bind(&LambdaBinder::RunLambda<Func>, func); |
+ } |
+}; |
+ |
class TestSyncCommonImpl { |
public: |
TestSyncCommonImpl() {} |
using PingHandler = Callback<void(const Callback<void()>&)>; |
- void set_ping_handler(const PingHandler& handler) { ping_handler_ = handler; } |
+ using PingBinder = LambdaBinder<const Callback<void()>&>; |
+ template <typename Func> |
+ void set_ping_handler(Func handler) { |
+ ping_handler_ = PingBinder::BindLambda(handler); |
+ } |
using EchoHandler = Callback<void(int32_t, const Callback<void(int32_t)>&)>; |
- void set_echo_handler(const EchoHandler& handler) { echo_handler_ = handler; } |
+ using EchoBinder = LambdaBinder<int32_t, const Callback<void(int32_t)>&>; |
+ template <typename Func> |
+ void set_echo_handler(Func handler) { |
+ echo_handler_ = EchoBinder::BindLambda(handler); |
+ } |
using AsyncEchoHandler = |
Callback<void(int32_t, const Callback<void(int32_t)>&)>; |
- void set_async_echo_handler(const AsyncEchoHandler& handler) { |
- async_echo_handler_ = handler; |
+ using AsyncEchoBinder = LambdaBinder<int32_t, const Callback<void(int32_t)>&>; |
+ template <typename Func> |
+ void set_async_echo_handler(Func handler) { |
+ async_echo_handler_ = AsyncEchoBinder::BindLambda(handler); |
} |
using SendInterfaceHandler = Callback<void(TestSyncAssociatedPtrInfo)>; |
- void set_send_interface_handler(const SendInterfaceHandler& handler) { |
- send_interface_handler_ = handler; |
+ using SendInterfaceBinder = LambdaBinder<TestSyncAssociatedPtrInfo>; |
+ template <typename Func> |
+ void set_send_interface_handler(Func handler) { |
+ send_interface_handler_ = SendInterfaceBinder::BindLambda(handler); |
} |
using SendRequestHandler = Callback<void(TestSyncAssociatedRequest)>; |
- void set_send_request_handler(const SendRequestHandler& handler) { |
- send_request_handler_ = handler; |
+ using SendRequestBinder = LambdaBinder<TestSyncAssociatedRequest>; |
+ template <typename Func> |
+ void set_send_request_handler(Func handler) { |
+ send_request_handler_ = SendRequestBinder::BindLambda(handler); |
} |
void PingImpl(const Callback<void()>& callback) { |
@@ -278,6 +309,28 @@ class SyncMethodAssociatedTest : public SyncMethodTest { |
TestSyncAssociatedRequest opposite_asso_request_; |
}; |
+void SetFlagAndRunClosure(bool* flag, const base::Closure& closure) { |
+ *flag = true; |
+ closure.Run(); |
+} |
+ |
+void ExpectValueAndRunClosure(int32_t expected_value, |
+ const base::Closure& closure, |
+ int32_t value) { |
+ EXPECT_EQ(expected_value, value); |
+ closure.Run(); |
+} |
+ |
+template <typename Func> |
+void CallAsyncEchoCallback(Func func, int32_t value) { |
+ func(value); |
+} |
+ |
+template <typename Func> |
+TestSync::AsyncEchoCallback BindAsyncEchoCallback(Func func) { |
+ return base::Bind(&CallAsyncEchoCallback<Func>, func); |
+} |
+ |
// TestSync and TestSyncMaster exercise Router and MultiplexRouter, |
// respectively. |
using InterfaceTypes = testing::Types<TestSync, TestSyncMaster>; |
@@ -288,10 +341,8 @@ TYPED_TEST(SyncMethodCommonTest, CallSyncMethodAsynchronously) { |
typename ImplTraits<TypeParam>::Type impl(GetProxy(&ptr)); |
base::RunLoop run_loop; |
- ptr->Echo(123, [&run_loop](int32_t result) { |
- EXPECT_EQ(123, result); |
- run_loop.Quit(); |
- }); |
+ ptr->Echo(123, base::Bind(&ExpectValueAndRunClosure, 123, |
+ run_loop.QuitClosure())); |
run_loop.Run(); |
} |
@@ -429,12 +480,14 @@ TYPED_TEST(SyncMethodCommonTest, AsyncResponseQueuedDuringSyncCall) { |
bool async_echo_response_dispatched = false; |
base::RunLoop run_loop2; |
- ptr->AsyncEcho(123, |
- [&async_echo_response_dispatched, &run_loop2](int32_t result) { |
- async_echo_response_dispatched = true; |
- EXPECT_EQ(123, result); |
- run_loop2.Quit(); |
- }); |
+ ptr->AsyncEcho( |
+ 123, |
+ BindAsyncEchoCallback( |
+ [&async_echo_response_dispatched, &run_loop2](int32_t result) { |
+ async_echo_response_dispatched = true; |
+ EXPECT_EQ(123, result); |
+ run_loop2.Quit(); |
+ })); |
// Run until the AsyncEcho request reaches the service side. |
run_loop1.Run(); |
@@ -479,12 +532,14 @@ TYPED_TEST(SyncMethodCommonTest, AsyncRequestQueuedDuringSyncCall) { |
bool async_echo_response_dispatched = false; |
base::RunLoop run_loop; |
- ptr->AsyncEcho(123, |
- [&async_echo_response_dispatched, &run_loop](int32_t result) { |
- async_echo_response_dispatched = true; |
- EXPECT_EQ(123, result); |
- run_loop.Quit(); |
- }); |
+ ptr->AsyncEcho( |
+ 123, |
+ BindAsyncEchoCallback( |
+ [&async_echo_response_dispatched, &run_loop](int32_t result) { |
+ async_echo_response_dispatched = true; |
+ EXPECT_EQ(123, result); |
+ run_loop.Quit(); |
+ })); |
impl.set_echo_handler([&async_echo_request_dispatched]( |
int32_t value, const TestSync::EchoCallback& callback) { |
@@ -534,16 +589,18 @@ TYPED_TEST(SyncMethodCommonTest, |
bool connection_error_dispatched = false; |
base::RunLoop run_loop2; |
ptr->AsyncEcho( |
- 123, [&async_echo_response_dispatched, &connection_error_dispatched, &ptr, |
- &run_loop2](int32_t result) { |
- async_echo_response_dispatched = true; |
- // At this point, error notification should not be dispatched |
- // yet. |
- EXPECT_FALSE(connection_error_dispatched); |
- EXPECT_FALSE(ptr.encountered_error()); |
- EXPECT_EQ(123, result); |
- run_loop2.Quit(); |
- }); |
+ 123, |
+ BindAsyncEchoCallback( |
+ [&async_echo_response_dispatched, &connection_error_dispatched, &ptr, |
+ &run_loop2](int32_t result) { |
+ async_echo_response_dispatched = true; |
+ // At this point, error notification should not be dispatched |
+ // yet. |
+ EXPECT_FALSE(connection_error_dispatched); |
+ EXPECT_FALSE(ptr.encountered_error()); |
+ EXPECT_EQ(123, result); |
+ run_loop2.Quit(); |
+ })); |
// Run until the AsyncEcho request reaches the service side. |
run_loop1.Run(); |
@@ -559,10 +616,8 @@ TYPED_TEST(SyncMethodCommonTest, |
base::RunLoop run_loop3; |
ptr.set_connection_error_handler( |
- [&connection_error_dispatched, &run_loop3]() { |
- connection_error_dispatched = true; |
- run_loop3.Quit(); |
- }); |
+ base::Bind(&SetFlagAndRunClosure, &connection_error_dispatched, |
+ run_loop3.QuitClosure())); |
int32_t result_value = -1; |
ASSERT_FALSE(ptr->Echo(456, &result_value)); |
@@ -614,10 +669,9 @@ TYPED_TEST(SyncMethodCommonTest, InvalidMessageDuringSyncCall) { |
bool connection_error_dispatched = false; |
base::RunLoop run_loop; |
- ptr.set_connection_error_handler([&connection_error_dispatched, &run_loop]() { |
- connection_error_dispatched = true; |
- run_loop.Quit(); |
- }); |
+ ptr.set_connection_error_handler( |
+ base::Bind(&SetFlagAndRunClosure, &connection_error_dispatched, |
+ run_loop.QuitClosure())); |
int32_t result_value = -1; |
ASSERT_FALSE(ptr->Echo(456, &result_value)); |