Index: mojo/public/cpp/bindings/tests/binding_unittest.cc |
diff --git a/mojo/public/cpp/bindings/tests/binding_unittest.cc b/mojo/public/cpp/bindings/tests/binding_unittest.cc |
index 71f12ca2945cb6edb2219201d5089fc62517b6c2..7f7d75b7db52c78169991e4cf025c4cbb710fff8 100644 |
--- a/mojo/public/cpp/bindings/tests/binding_unittest.cc |
+++ b/mojo/public/cpp/bindings/tests/binding_unittest.cc |
@@ -58,6 +58,21 @@ class ServiceImpl : public sample::Service { |
DISALLOW_COPY_AND_ASSIGN(ServiceImpl); |
}; |
+template <typename... Args> |
+void DoSetFlagAndRunClosure(bool* flag, |
+ const base::Closure& closure, |
+ Args... args) { |
+ *flag = true; |
+ closure.Run(); |
+} |
+ |
+template <typename... Args> |
+base::Callback<void(Args...)> SetFlagAndRunClosure( |
+ bool* flag, |
+ const base::Closure& callback = base::Closure()) { |
+ return base::Bind(&DoSetFlagAndRunClosure<Args...>, flag, callback); |
+} |
+ |
// BindingTest ----------------------------------------------------------------- |
using BindingTest = BindingTestBase; |
@@ -67,10 +82,8 @@ TEST_F(BindingTest, Close) { |
sample::ServicePtr ptr; |
auto request = GetProxy(&ptr); |
base::RunLoop run_loop; |
- ptr.set_connection_error_handler([&called, &run_loop]() { |
- called = true; |
- run_loop.Quit(); |
- }); |
+ ptr.set_connection_error_handler( |
+ SetFlagAndRunClosure(&called, run_loop.QuitClosure())); |
ServiceImpl impl; |
Binding<sample::Service> binding(&impl, std::move(request)); |
@@ -88,20 +101,14 @@ TEST_F(BindingTest, DestroyClosesMessagePipe) { |
auto request = GetProxy(&ptr); |
base::RunLoop run_loop; |
ptr.set_connection_error_handler( |
- [&encountered_error, &run_loop]() { |
- encountered_error = true; |
- run_loop.Quit(); |
- }); |
+ SetFlagAndRunClosure(&encountered_error, run_loop.QuitClosure())); |
bool called = false; |
base::RunLoop run_loop2; |
- auto called_cb = [&called, &run_loop2](int32_t result) { |
- called = true; |
- run_loop2.Quit(); |
- }; |
{ |
Binding<sample::Service> binding(&impl, std::move(request)); |
ptr->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, |
- called_cb); |
+ SetFlagAndRunClosure<int32_t>(&called, |
+ run_loop2.QuitClosure())); |
run_loop2.Run(); |
EXPECT_TRUE(called); |
EXPECT_FALSE(encountered_error); |
@@ -114,7 +121,8 @@ TEST_F(BindingTest, DestroyClosesMessagePipe) { |
// And calls should fail. |
called = false; |
ptr->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, |
- called_cb); |
+ SetFlagAndRunClosure<int32_t>(&called, |
+ run_loop2.QuitClosure())); |
loop().RunUntilIdle(); |
EXPECT_FALSE(called); |
} |
@@ -128,10 +136,8 @@ TEST_F(BindingTest, ConnectionError) { |
sample::ServicePtr ptr; |
Binding<sample::Service> binding(&impl, GetProxy(&ptr)); |
base::RunLoop run_loop; |
- binding.set_connection_error_handler([&called, &run_loop]() { |
- called = true; |
- run_loop.Quit(); |
- }); |
+ binding.set_connection_error_handler( |
+ SetFlagAndRunClosure(&called, run_loop.QuitClosure())); |
ptr.reset(); |
EXPECT_FALSE(called); |
run_loop.Run(); |
@@ -149,7 +155,7 @@ TEST_F(BindingTest, CloseDoesntCallConnectionErrorHandler) { |
sample::ServicePtr ptr; |
Binding<sample::Service> binding(&impl, GetProxy(&ptr)); |
bool called = false; |
- binding.set_connection_error_handler([&called]() { called = true; }); |
+ binding.set_connection_error_handler(SetFlagAndRunClosure(&called)); |
binding.Close(); |
loop().RunUntilIdle(); |
EXPECT_FALSE(called); |
@@ -169,7 +175,9 @@ class ServiceImplWithBinding : public ServiceImpl { |
: ServiceImpl(was_deleted), |
binding_(this, std::move(request)), |
closure_(closure) { |
- binding_.set_connection_error_handler([this]() { delete this; }); |
+ binding_.set_connection_error_handler( |
+ base::Bind(&ServiceImplWithBinding::OnConnectionError, |
+ base::Unretained(this))); |
} |
private: |
@@ -177,6 +185,8 @@ class ServiceImplWithBinding : public ServiceImpl { |
closure_.Run(); |
} |
+ void OnConnectionError() { delete this; } |
+ |
Binding<sample::Service> binding_; |
base::Closure closure_; |
@@ -205,12 +215,9 @@ TEST_F(BindingTest, Unbind) { |
bool called = false; |
base::RunLoop run_loop; |
- auto called_cb = [&called, &run_loop](int32_t result) { |
- called = true; |
- run_loop.Quit(); |
- }; |
ptr->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, |
- called_cb); |
+ SetFlagAndRunClosure<int32_t>(&called, |
+ run_loop.QuitClosure())); |
run_loop.Run(); |
EXPECT_TRUE(called); |
@@ -219,7 +226,8 @@ TEST_F(BindingTest, Unbind) { |
EXPECT_FALSE(binding.is_bound()); |
// All calls should fail when not bound... |
ptr->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, |
- called_cb); |
+ SetFlagAndRunClosure<int32_t>(&called, |
+ run_loop.QuitClosure())); |
loop().RunUntilIdle(); |
EXPECT_FALSE(called); |
@@ -228,12 +236,9 @@ TEST_F(BindingTest, Unbind) { |
EXPECT_TRUE(binding.is_bound()); |
// ...and should succeed again when the rebound. |
base::RunLoop run_loop2; |
- auto called_cb2 = [&called, &run_loop2](int32_t result) { |
- called = true; |
- run_loop2.Quit(); |
- }; |
ptr->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, |
- called_cb2); |
+ SetFlagAndRunClosure<int32_t>(&called, |
+ run_loop2.QuitClosure())); |
run_loop2.Run(); |
EXPECT_TRUE(called); |
} |
@@ -263,17 +268,14 @@ TEST_F(BindingTest, SetInterfacePtrVersion) { |
TEST_F(BindingTest, PauseResume) { |
bool called = false; |
base::RunLoop run_loop; |
- auto called_cb = [&called, &run_loop](int32_t result) { |
- called = true; |
- run_loop.Quit(); |
- }; |
sample::ServicePtr ptr; |
auto request = GetProxy(&ptr); |
ServiceImpl impl; |
Binding<sample::Service> binding(&impl, std::move(request)); |
binding.PauseIncomingMethodCallProcessing(); |
ptr->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, |
- called_cb); |
+ SetFlagAndRunClosure<int32_t>(&called, |
+ run_loop.QuitClosure())); |
EXPECT_FALSE(called); |
loop().RunUntilIdle(); |
// Frobinate() should not be called as the binding is paused. |
@@ -293,10 +295,8 @@ TEST_F(BindingTest, ErrorHandleNotRunWhilePaused) { |
auto request = GetProxy(&ptr); |
ServiceImpl impl; |
Binding<sample::Service> binding(&impl, std::move(request)); |
- binding.set_connection_error_handler([&called, &run_loop]() { |
- called = true; |
- run_loop.Quit(); |
- }); |
+ binding.set_connection_error_handler( |
+ SetFlagAndRunClosure(&called, run_loop.QuitClosure())); |
binding.PauseIncomingMethodCallProcessing(); |
ptr.reset(); |
@@ -324,20 +324,14 @@ TEST_F(StrongBindingTest, DestroyClosesMessagePipe) { |
sample::ServicePtr ptr; |
auto request = GetProxy(&ptr); |
ptr.set_connection_error_handler( |
- [&encountered_error, &run_loop]() { |
- encountered_error = true; |
- run_loop.Quit(); |
- }); |
+ SetFlagAndRunClosure(&encountered_error, run_loop.QuitClosure())); |
bool called = false; |
base::RunLoop run_loop2; |
- auto called_cb = [&called, &run_loop2](int32_t result) { |
- called = true; |
- run_loop2.Quit(); |
- }; |
{ |
StrongBinding<sample::Service> binding(&impl, std::move(request)); |
ptr->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, |
- called_cb); |
+ SetFlagAndRunClosure<int32_t>(&called, |
+ run_loop2.QuitClosure())); |
run_loop2.Run(); |
EXPECT_TRUE(called); |
EXPECT_FALSE(encountered_error); |
@@ -393,18 +387,13 @@ TEST_F(StrongBindingTest, ExplicitDeleteImpl) { |
auto request = GetProxy(&ptr); |
base::RunLoop run_loop; |
ptr.set_connection_error_handler( |
- [&ptr_error_handler_called, &run_loop]() { |
- ptr_error_handler_called = true; |
- run_loop.Quit(); |
- }); |
+ SetFlagAndRunClosure(&ptr_error_handler_called, run_loop.QuitClosure())); |
bool was_deleted = false; |
ServiceImplWithStrongBinding* impl = |
new ServiceImplWithStrongBinding(&was_deleted, std::move(request)); |
bool binding_error_handler_called = false; |
impl->binding().set_connection_error_handler( |
- [&binding_error_handler_called]() { |
- binding_error_handler_called = true; |
- }); |
+ SetFlagAndRunClosure(&binding_error_handler_called)); |
loop().RunUntilIdle(); |
EXPECT_FALSE(ptr_error_handler_called); |