Commit 2a363a8c authored by Ryo Hashimoto's avatar Ryo Hashimoto Committed by Commit Bot

dbus: Let ObjectProxy::CallMethod() take OnceCallback

Currently, ObjectProxy::CallMethod is implemented by creating ErrorCallback from ResponseCallback.
This does not work with OnceCallback because it's not allowed to create two callbacks from one OnceCallback.
To solve this, this change introduces CallMethodCallbackInternal to ObjectProxy which takes either Response* or ErrorResponse*, and dispatches the response to the appropriate callback.

Also, Gmock does not allow directly mocking functions which take move-only
arguments.
https://github.com/google/googletest/blob/master/googlemock/docs/CookBook.md#mocking-methods-that-use-move-only-types
To workaround this, add DoCallMethod to MockObjetProxy.

BUG=740015
TEST=build

Change-Id: I1a98b5fd1d6d70f69674102a3d66bcec0c13214c
Reviewed-on: https://chromium-review.googlesource.com/578541
Commit-Queue: Ryo Hashimoto <hashimoto@chromium.org>
Reviewed-by: default avatarHidehiko Abe <hidehiko@chromium.org>
Reviewed-by: default avatarSatoru Takabayashi <satorux@chromium.org>
Cr-Commit-Position: refs/heads/master@{#488615}
parent c342ef57
...@@ -51,7 +51,7 @@ MATCHER_P(HasMember, name, "") { ...@@ -51,7 +51,7 @@ MATCHER_P(HasMember, name, "") {
// bare pointer rather than an std::unique_ptr. // bare pointer rather than an std::unique_ptr.
void RunResponseCallback(dbus::ObjectProxy::ResponseCallback callback, void RunResponseCallback(dbus::ObjectProxy::ResponseCallback callback,
std::unique_ptr<dbus::Response> response) { std::unique_ptr<dbus::Response> response) {
callback.Run(response.get()); std::move(callback).Run(response.get());
} }
} // namespace } // namespace
...@@ -98,7 +98,7 @@ class BiodClientTest : public testing::Test { ...@@ -98,7 +98,7 @@ class BiodClientTest : public testing::Test {
std::unique_ptr<dbus::Response> response) { std::unique_ptr<dbus::Response> response) {
ASSERT_FALSE(pending_method_calls_.count(method_name)); ASSERT_FALSE(pending_method_calls_.count(method_name));
pending_method_calls_[method_name] = std::move(response); pending_method_calls_[method_name] = std::move(response);
EXPECT_CALL(*proxy_.get(), CallMethod(HasMember(method_name), _, _)) EXPECT_CALL(*proxy_.get(), DoCallMethod(HasMember(method_name), _, _))
.WillOnce(Invoke(this, &BiodClientTest::OnCallMethod)); .WillOnce(Invoke(this, &BiodClientTest::OnCallMethod));
} }
...@@ -187,15 +187,15 @@ class BiodClientTest : public testing::Test { ...@@ -187,15 +187,15 @@ class BiodClientTest : public testing::Test {
// Handles calls to |proxy_|'s CallMethod(). // Handles calls to |proxy_|'s CallMethod().
void OnCallMethod(dbus::MethodCall* method_call, void OnCallMethod(dbus::MethodCall* method_call,
int timeout_ms, int timeout_ms,
const dbus::ObjectProxy::ResponseCallback& callback) { dbus::ObjectProxy::ResponseCallback* callback) {
auto it = pending_method_calls_.find(method_call->GetMember()); auto it = pending_method_calls_.find(method_call->GetMember());
ASSERT_TRUE(it != pending_method_calls_.end()); ASSERT_TRUE(it != pending_method_calls_.end());
auto pending_response = std::move(it->second); auto pending_response = std::move(it->second);
pending_method_calls_.erase(it); pending_method_calls_.erase(it);
message_loop_.task_runner()->PostTask( message_loop_.task_runner()->PostTask(
FROM_HERE, base::Bind(&RunResponseCallback, callback, FROM_HERE, base::BindOnce(&RunResponseCallback, std::move(*callback),
base::Passed(&pending_response))); base::Passed(&pending_response)));
} }
DISALLOW_COPY_AND_ASSIGN(BiodClientTest); DISALLOW_COPY_AND_ASSIGN(BiodClientTest);
......
...@@ -267,13 +267,13 @@ class CrasAudioClientTest : public testing::Test { ...@@ -267,13 +267,13 @@ class CrasAudioClientTest : public testing::Test {
// Set an expectation so mock_cras_proxy's CallMethod() will use // Set an expectation so mock_cras_proxy's CallMethod() will use
// OnCallMethod() to return responses. // OnCallMethod() to return responses.
EXPECT_CALL(*mock_cras_proxy_.get(), CallMethod(_, _, _)) EXPECT_CALL(*mock_cras_proxy_.get(), DoCallMethod(_, _, _))
.WillRepeatedly(Invoke(this, &CrasAudioClientTest::OnCallMethod)); .WillRepeatedly(Invoke(this, &CrasAudioClientTest::OnCallMethod));
// Set an expectation so mock_cras_proxy's CallMethodWithErrorCallback() // Set an expectation so mock_cras_proxy's CallMethodWithErrorCallback()
// will use OnCallMethodWithErrorCallback() to return responses. // will use OnCallMethodWithErrorCallback() to return responses.
EXPECT_CALL(*mock_cras_proxy_.get(), EXPECT_CALL(*mock_cras_proxy_.get(),
CallMethodWithErrorCallback(_, _, _, _)) DoCallMethodWithErrorCallback(_, _, _, _))
.WillRepeatedly( .WillRepeatedly(
Invoke(this, &CrasAudioClientTest::OnCallMethodWithErrorCallback)); Invoke(this, &CrasAudioClientTest::OnCallMethodWithErrorCallback));
...@@ -532,13 +532,13 @@ class CrasAudioClientTest : public testing::Test { ...@@ -532,13 +532,13 @@ class CrasAudioClientTest : public testing::Test {
// Used to implement the mock cras proxy. // Used to implement the mock cras proxy.
void OnCallMethod(dbus::MethodCall* method_call, void OnCallMethod(dbus::MethodCall* method_call,
int timeout_ms, int timeout_ms,
const dbus::ObjectProxy::ResponseCallback& response) { dbus::ObjectProxy::ResponseCallback* response) {
EXPECT_EQ(interface_name_, method_call->GetInterface()); EXPECT_EQ(interface_name_, method_call->GetInterface());
EXPECT_EQ(expected_method_name_, method_call->GetMember()); EXPECT_EQ(expected_method_name_, method_call->GetMember());
dbus::MessageReader reader(method_call); dbus::MessageReader reader(method_call);
argument_checker_.Run(&reader); argument_checker_.Run(&reader);
message_loop_.task_runner()->PostTask( message_loop_.task_runner()->PostTask(
FROM_HERE, base::Bind(response, response_)); FROM_HERE, base::BindOnce(std::move(*response), response_));
} }
// Checks the content of the method call and returns the response. // Checks the content of the method call and returns the response.
...@@ -546,8 +546,8 @@ class CrasAudioClientTest : public testing::Test { ...@@ -546,8 +546,8 @@ class CrasAudioClientTest : public testing::Test {
void OnCallMethodWithErrorCallback( void OnCallMethodWithErrorCallback(
dbus::MethodCall* method_call, dbus::MethodCall* method_call,
int timeout_ms, int timeout_ms,
const dbus::ObjectProxy::ResponseCallback& response_callback, dbus::ObjectProxy::ResponseCallback* response_callback,
const dbus::ObjectProxy::ErrorCallback& error_callback) { dbus::ObjectProxy::ErrorCallback* error_callback) {
OnCallMethod(method_call, timeout_ms, response_callback); OnCallMethod(method_call, timeout_ms, response_callback);
} }
}; };
......
...@@ -100,7 +100,7 @@ class GsmSMSClientTest : public testing::Test { ...@@ -100,7 +100,7 @@ class GsmSMSClientTest : public testing::Test {
// Handles Delete method call. // Handles Delete method call.
void OnDelete(dbus::MethodCall* method_call, void OnDelete(dbus::MethodCall* method_call,
int timeout_ms, int timeout_ms,
const dbus::ObjectProxy::ResponseCallback& callback) { dbus::ObjectProxy::ResponseCallback* callback) {
EXPECT_EQ(modemmanager::kModemManagerSMSInterface, EXPECT_EQ(modemmanager::kModemManagerSMSInterface,
method_call->GetInterface()); method_call->GetInterface());
EXPECT_EQ(modemmanager::kSMSDeleteFunction, method_call->GetMember()); EXPECT_EQ(modemmanager::kSMSDeleteFunction, method_call->GetMember());
...@@ -110,14 +110,14 @@ class GsmSMSClientTest : public testing::Test { ...@@ -110,14 +110,14 @@ class GsmSMSClientTest : public testing::Test {
EXPECT_EQ(expected_index_, index); EXPECT_EQ(expected_index_, index);
EXPECT_FALSE(reader.HasMoreData()); EXPECT_FALSE(reader.HasMoreData());
message_loop_.task_runner()->PostTask(FROM_HERE, message_loop_.task_runner()->PostTask(
base::Bind(callback, response_)); FROM_HERE, base::BindOnce(std::move(*callback), response_));
} }
// Handles Get method call. // Handles Get method call.
void OnGet(dbus::MethodCall* method_call, void OnGet(dbus::MethodCall* method_call,
int timeout_ms, int timeout_ms,
const dbus::ObjectProxy::ResponseCallback& callback) { dbus::ObjectProxy::ResponseCallback* callback) {
EXPECT_EQ(modemmanager::kModemManagerSMSInterface, EXPECT_EQ(modemmanager::kModemManagerSMSInterface,
method_call->GetInterface()); method_call->GetInterface());
EXPECT_EQ(modemmanager::kSMSGetFunction, method_call->GetMember()); EXPECT_EQ(modemmanager::kSMSGetFunction, method_call->GetMember());
...@@ -127,22 +127,22 @@ class GsmSMSClientTest : public testing::Test { ...@@ -127,22 +127,22 @@ class GsmSMSClientTest : public testing::Test {
EXPECT_EQ(expected_index_, index); EXPECT_EQ(expected_index_, index);
EXPECT_FALSE(reader.HasMoreData()); EXPECT_FALSE(reader.HasMoreData());
message_loop_.task_runner()->PostTask(FROM_HERE, message_loop_.task_runner()->PostTask(
base::Bind(callback, response_)); FROM_HERE, base::BindOnce(std::move(*callback), response_));
} }
// Handles List method call. // Handles List method call.
void OnList(dbus::MethodCall* method_call, void OnList(dbus::MethodCall* method_call,
int timeout_ms, int timeout_ms,
const dbus::ObjectProxy::ResponseCallback& callback) { dbus::ObjectProxy::ResponseCallback* callback) {
EXPECT_EQ(modemmanager::kModemManagerSMSInterface, EXPECT_EQ(modemmanager::kModemManagerSMSInterface,
method_call->GetInterface()); method_call->GetInterface());
EXPECT_EQ(modemmanager::kSMSListFunction, method_call->GetMember()); EXPECT_EQ(modemmanager::kSMSListFunction, method_call->GetMember());
dbus::MessageReader reader(method_call); dbus::MessageReader reader(method_call);
EXPECT_FALSE(reader.HasMoreData()); EXPECT_FALSE(reader.HasMoreData());
message_loop_.task_runner()->PostTask(FROM_HERE, message_loop_.task_runner()->PostTask(
base::Bind(callback, response_)); FROM_HERE, base::BindOnce(std::move(*callback), response_));
} }
// Checks the results of Get and List. // Checks the results of Get and List.
...@@ -214,7 +214,7 @@ TEST_F(GsmSMSClientTest, Delete) { ...@@ -214,7 +214,7 @@ TEST_F(GsmSMSClientTest, Delete) {
// Set expectations. // Set expectations.
const uint32_t kIndex = 42; const uint32_t kIndex = 42;
expected_index_ = kIndex; expected_index_ = kIndex;
EXPECT_CALL(*mock_proxy_.get(), CallMethod(_, _, _)) EXPECT_CALL(*mock_proxy_.get(), DoCallMethod(_, _, _))
.WillOnce(Invoke(this, &GsmSMSClientTest::OnDelete)); .WillOnce(Invoke(this, &GsmSMSClientTest::OnDelete));
base::MockCallback<GsmSMSClient::DeleteCallback> callback; base::MockCallback<GsmSMSClient::DeleteCallback> callback;
EXPECT_CALL(callback, Run()).Times(1); EXPECT_CALL(callback, Run()).Times(1);
...@@ -233,7 +233,7 @@ TEST_F(GsmSMSClientTest, Get) { ...@@ -233,7 +233,7 @@ TEST_F(GsmSMSClientTest, Get) {
// Set expectations. // Set expectations.
const uint32_t kIndex = 42; const uint32_t kIndex = 42;
expected_index_ = kIndex; expected_index_ = kIndex;
EXPECT_CALL(*mock_proxy_.get(), CallMethod(_, _, _)) EXPECT_CALL(*mock_proxy_.get(), DoCallMethod(_, _, _))
.WillOnce(Invoke(this, &GsmSMSClientTest::OnGet)); .WillOnce(Invoke(this, &GsmSMSClientTest::OnGet));
base::MockCallback<GsmSMSClient::GetCallback> callback; base::MockCallback<GsmSMSClient::GetCallback> callback;
EXPECT_CALL(callback, Run(_)) EXPECT_CALL(callback, Run(_))
...@@ -269,7 +269,7 @@ TEST_F(GsmSMSClientTest, Get) { ...@@ -269,7 +269,7 @@ TEST_F(GsmSMSClientTest, Get) {
TEST_F(GsmSMSClientTest, List) { TEST_F(GsmSMSClientTest, List) {
// Set expectations. // Set expectations.
EXPECT_CALL(*mock_proxy_.get(), CallMethod(_, _, _)) EXPECT_CALL(*mock_proxy_.get(), DoCallMethod(_, _, _))
.WillOnce(Invoke(this, &GsmSMSClientTest::OnList)); .WillOnce(Invoke(this, &GsmSMSClientTest::OnList));
base::MockCallback<GsmSMSClient::ListCallback> callback; base::MockCallback<GsmSMSClient::ListCallback> callback;
EXPECT_CALL(callback, Run(_)) EXPECT_CALL(callback, Run(_))
......
...@@ -97,7 +97,7 @@ class ModemMessagingClientTest : public testing::Test { ...@@ -97,7 +97,7 @@ class ModemMessagingClientTest : public testing::Test {
// Handles Delete method call. // Handles Delete method call.
void OnDelete(dbus::MethodCall* method_call, void OnDelete(dbus::MethodCall* method_call,
int timeout_ms, int timeout_ms,
const dbus::ObjectProxy::ResponseCallback& callback) { dbus::ObjectProxy::ResponseCallback* callback) {
EXPECT_EQ(modemmanager::kModemManager1MessagingInterface, EXPECT_EQ(modemmanager::kModemManager1MessagingInterface,
method_call->GetInterface()); method_call->GetInterface());
EXPECT_EQ(modemmanager::kSMSDeleteFunction, method_call->GetMember()); EXPECT_EQ(modemmanager::kSMSDeleteFunction, method_call->GetMember());
...@@ -107,22 +107,22 @@ class ModemMessagingClientTest : public testing::Test { ...@@ -107,22 +107,22 @@ class ModemMessagingClientTest : public testing::Test {
EXPECT_EQ(expected_sms_path_, sms_path); EXPECT_EQ(expected_sms_path_, sms_path);
EXPECT_FALSE(reader.HasMoreData()); EXPECT_FALSE(reader.HasMoreData());
message_loop_.task_runner()->PostTask(FROM_HERE, message_loop_.task_runner()->PostTask(
base::Bind(callback, response_)); FROM_HERE, base::BindOnce(std::move(*callback), response_));
} }
// Handles List method call. // Handles List method call.
void OnList(dbus::MethodCall* method_call, void OnList(dbus::MethodCall* method_call,
int timeout_ms, int timeout_ms,
const dbus::ObjectProxy::ResponseCallback& callback) { dbus::ObjectProxy::ResponseCallback* callback) {
EXPECT_EQ(modemmanager::kModemManager1MessagingInterface, EXPECT_EQ(modemmanager::kModemManager1MessagingInterface,
method_call->GetInterface()); method_call->GetInterface());
EXPECT_EQ(modemmanager::kSMSListFunction, method_call->GetMember()); EXPECT_EQ(modemmanager::kSMSListFunction, method_call->GetMember());
dbus::MessageReader reader(method_call); dbus::MessageReader reader(method_call);
EXPECT_FALSE(reader.HasMoreData()); EXPECT_FALSE(reader.HasMoreData());
message_loop_.task_runner()->PostTask(FROM_HERE, message_loop_.task_runner()->PostTask(
base::Bind(callback, response_)); FROM_HERE, base::BindOnce(std::move(*callback), response_));
} }
// Checks the results of List. // Checks the results of List.
...@@ -195,7 +195,7 @@ TEST_F(ModemMessagingClientTest, Delete) { ...@@ -195,7 +195,7 @@ TEST_F(ModemMessagingClientTest, Delete) {
// Set expectations. // Set expectations.
const dbus::ObjectPath kSmsPath("/SMS/0"); const dbus::ObjectPath kSmsPath("/SMS/0");
expected_sms_path_ = kSmsPath; expected_sms_path_ = kSmsPath;
EXPECT_CALL(*mock_proxy_.get(), CallMethod(_, _, _)) EXPECT_CALL(*mock_proxy_.get(), DoCallMethod(_, _, _))
.WillOnce(Invoke(this, &ModemMessagingClientTest::OnDelete)); .WillOnce(Invoke(this, &ModemMessagingClientTest::OnDelete));
MockDeleteCallback callback; MockDeleteCallback callback;
EXPECT_CALL(callback, Run()).Times(1); EXPECT_CALL(callback, Run()).Times(1);
...@@ -213,7 +213,7 @@ TEST_F(ModemMessagingClientTest, Delete) { ...@@ -213,7 +213,7 @@ TEST_F(ModemMessagingClientTest, Delete) {
TEST_F(ModemMessagingClientTest, List) { TEST_F(ModemMessagingClientTest, List) {
// Set expectations. // Set expectations.
EXPECT_CALL(*mock_proxy_.get(), CallMethod(_, _, _)) EXPECT_CALL(*mock_proxy_.get(), DoCallMethod(_, _, _))
.WillOnce(Invoke(this, &ModemMessagingClientTest::OnList)); .WillOnce(Invoke(this, &ModemMessagingClientTest::OnList));
MockListCallback callback; MockListCallback callback;
EXPECT_CALL(callback, Run(_)) EXPECT_CALL(callback, Run(_))
......
...@@ -812,8 +812,9 @@ class PowerManagerClientImpl : public PowerManagerClient { ...@@ -812,8 +812,9 @@ class PowerManagerClientImpl : public PowerManagerClient {
return; return;
} }
power_manager_proxy_->CallMethod( power_manager_proxy_->CallMethod(&method_call,
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, callback); dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
std::move(callback));
} }
// Registers suspend delays with the power manager. This is usually only // Registers suspend delays with the power manager. This is usually only
......
...@@ -75,7 +75,7 @@ MATCHER_P3(IsSuspendReadiness, method_name, suspend_id, delay_id, "") { ...@@ -75,7 +75,7 @@ MATCHER_P3(IsSuspendReadiness, method_name, suspend_id, delay_id, "") {
// bare pointer rather than an std::unique_ptr. // bare pointer rather than an std::unique_ptr.
void RunResponseCallback(dbus::ObjectProxy::ResponseCallback callback, void RunResponseCallback(dbus::ObjectProxy::ResponseCallback callback,
std::unique_ptr<dbus::Response> response) { std::unique_ptr<dbus::Response> response) {
callback.Run(response.get()); std::move(callback).Run(response.get());
} }
// Stub implementation of PowerManagerClient::Observer. // Stub implementation of PowerManagerClient::Observer.
...@@ -213,13 +213,14 @@ class PowerManagerClientTest : public testing::Test { ...@@ -213,13 +213,14 @@ class PowerManagerClientTest : public testing::Test {
// delays. // delays.
EXPECT_CALL( EXPECT_CALL(
*proxy_.get(), *proxy_.get(),
CallMethod(HasMember(power_manager::kRegisterSuspendDelayMethod), _, _)) DoCallMethod(HasMember(power_manager::kRegisterSuspendDelayMethod), _,
_))
.WillRepeatedly( .WillRepeatedly(
Invoke(this, &PowerManagerClientTest::RegisterSuspendDelay)); Invoke(this, &PowerManagerClientTest::RegisterSuspendDelay));
EXPECT_CALL( EXPECT_CALL(
*proxy_.get(), *proxy_.get(),
CallMethod(HasMember(power_manager::kRegisterDarkSuspendDelayMethod), _, DoCallMethod(HasMember(power_manager::kRegisterDarkSuspendDelayMethod),
_)) _, _))
.WillRepeatedly( .WillRepeatedly(
Invoke(this, &PowerManagerClientTest::RegisterSuspendDelay)); Invoke(this, &PowerManagerClientTest::RegisterSuspendDelay));
...@@ -269,8 +270,8 @@ class PowerManagerClientTest : public testing::Test { ...@@ -269,8 +270,8 @@ class PowerManagerClientTest : public testing::Test {
int delay_id) { int delay_id) {
EXPECT_CALL( EXPECT_CALL(
*proxy_.get(), *proxy_.get(),
CallMethod(IsSuspendReadiness(method_name, suspend_id, delay_id), _, DoCallMethod(IsSuspendReadiness(method_name, suspend_id, delay_id), _,
_)); _));
} }
// Arbitrary delay IDs returned to |client_|. // Arbitrary delay IDs returned to |client_|.
...@@ -311,7 +312,7 @@ class PowerManagerClientTest : public testing::Test { ...@@ -311,7 +312,7 @@ class PowerManagerClientTest : public testing::Test {
// Handles calls to |proxy_|'s CallMethod() method to register suspend delays. // Handles calls to |proxy_|'s CallMethod() method to register suspend delays.
void RegisterSuspendDelay(dbus::MethodCall* method_call, void RegisterSuspendDelay(dbus::MethodCall* method_call,
int timeout_ms, int timeout_ms,
dbus::ObjectProxy::ResponseCallback callback) { dbus::ObjectProxy::ResponseCallback* callback) {
power_manager::RegisterSuspendDelayReply proto; power_manager::RegisterSuspendDelayReply proto;
proto.set_delay_id(method_call->GetMember() == proto.set_delay_id(method_call->GetMember() ==
power_manager::kRegisterDarkSuspendDelayMethod power_manager::kRegisterDarkSuspendDelayMethod
...@@ -324,8 +325,8 @@ class PowerManagerClientTest : public testing::Test { ...@@ -324,8 +325,8 @@ class PowerManagerClientTest : public testing::Test {
CHECK(dbus::MessageWriter(response.get()).AppendProtoAsArrayOfBytes(proto)); CHECK(dbus::MessageWriter(response.get()).AppendProtoAsArrayOfBytes(proto));
message_loop_.task_runner()->PostTask( message_loop_.task_runner()->PostTask(
FROM_HERE, FROM_HERE, base::BindOnce(&RunResponseCallback, std::move(*callback),
base::Bind(&RunResponseCallback, callback, base::Passed(&response))); base::Passed(&response)));
} }
DISALLOW_COPY_AND_ASSIGN(PowerManagerClientTest); DISALLOW_COPY_AND_ASSIGN(PowerManagerClientTest);
......
...@@ -106,14 +106,14 @@ void ShillClientUnittestBase::SetUp() { ...@@ -106,14 +106,14 @@ void ShillClientUnittestBase::SetUp() {
// Set an expectation so mock_proxy's CallMethod() will use OnCallMethod() // Set an expectation so mock_proxy's CallMethod() will use OnCallMethod()
// to return responses. // to return responses.
EXPECT_CALL(*mock_proxy_.get(), CallMethod(_, _, _)) EXPECT_CALL(*mock_proxy_.get(), DoCallMethod(_, _, _))
.WillRepeatedly(Invoke(this, &ShillClientUnittestBase::OnCallMethod)); .WillRepeatedly(Invoke(this, &ShillClientUnittestBase::OnCallMethod));
// Set an expectation so mock_proxy's CallMethodWithErrorCallback() will use // Set an expectation so mock_proxy's CallMethodWithErrorCallback() will use
// OnCallMethodWithErrorCallback() to return responses. // OnCallMethodWithErrorCallback() to return responses.
EXPECT_CALL(*mock_proxy_.get(), CallMethodWithErrorCallback(_, _, _, _)) EXPECT_CALL(*mock_proxy_.get(), DoCallMethodWithErrorCallback(_, _, _, _))
.WillRepeatedly(Invoke( .WillRepeatedly(Invoke(
this, &ShillClientUnittestBase::OnCallMethodWithErrorCallback)); this, &ShillClientUnittestBase::OnCallMethodWithErrorCallback));
// Set an expectation so mock_proxy's ConnectToSignal() will use // Set an expectation so mock_proxy's ConnectToSignal() will use
// OnConnectToPropertyChanged() to run the callback. // OnConnectToPropertyChanged() to run the callback.
...@@ -422,20 +422,20 @@ void ShillClientUnittestBase::OnConnectToPropertyChanged( ...@@ -422,20 +422,20 @@ void ShillClientUnittestBase::OnConnectToPropertyChanged(
void ShillClientUnittestBase::OnCallMethod( void ShillClientUnittestBase::OnCallMethod(
dbus::MethodCall* method_call, dbus::MethodCall* method_call,
int timeout_ms, int timeout_ms,
const dbus::ObjectProxy::ResponseCallback& response_callback) { dbus::ObjectProxy::ResponseCallback* response_callback) {
EXPECT_EQ(interface_name_, method_call->GetInterface()); EXPECT_EQ(interface_name_, method_call->GetInterface());
EXPECT_EQ(expected_method_name_, method_call->GetMember()); EXPECT_EQ(expected_method_name_, method_call->GetMember());
dbus::MessageReader reader(method_call); dbus::MessageReader reader(method_call);
argument_checker_.Run(&reader); argument_checker_.Run(&reader);
message_loop_.task_runner()->PostTask( message_loop_.task_runner()->PostTask(
FROM_HERE, base::Bind(response_callback, response_)); FROM_HERE, base::BindOnce(std::move(*response_callback), response_));
} }
void ShillClientUnittestBase::OnCallMethodWithErrorCallback( void ShillClientUnittestBase::OnCallMethodWithErrorCallback(
dbus::MethodCall* method_call, dbus::MethodCall* method_call,
int timeout_ms, int timeout_ms,
const dbus::ObjectProxy::ResponseCallback& response_callback, dbus::ObjectProxy::ResponseCallback* response_callback,
const dbus::ObjectProxy::ErrorCallback& error_callback) { dbus::ObjectProxy::ErrorCallback* error_callback) {
OnCallMethod(method_call, timeout_ms, response_callback); OnCallMethod(method_call, timeout_ms, response_callback);
} }
......
...@@ -207,18 +207,17 @@ class ShillClientUnittestBase : public testing::Test { ...@@ -207,18 +207,17 @@ class ShillClientUnittestBase : public testing::Test {
// Checks the content of the method call and returns the response. // Checks the content of the method call and returns the response.
// Used to implement the mock proxy. // Used to implement the mock proxy.
void OnCallMethod( void OnCallMethod(dbus::MethodCall* method_call,
dbus::MethodCall* method_call, int timeout_ms,
int timeout_ms, dbus::ObjectProxy::ResponseCallback* response_callback);
const dbus::ObjectProxy::ResponseCallback& response_callback);
// Checks the content of the method call and returns the response. // Checks the content of the method call and returns the response.
// Used to implement the mock proxy. // Used to implement the mock proxy.
void OnCallMethodWithErrorCallback( void OnCallMethodWithErrorCallback(
dbus::MethodCall* method_call, dbus::MethodCall* method_call,
int timeout_ms, int timeout_ms,
const dbus::ObjectProxy::ResponseCallback& response_callback, dbus::ObjectProxy::ResponseCallback* response_callback,
const dbus::ObjectProxy::ErrorCallback& error_callback); dbus::ObjectProxy::ErrorCallback* error_callback);
// The interface name. // The interface name.
const std::string interface_name_; const std::string interface_name_;
......
...@@ -15,4 +15,19 @@ MockObjectProxy::MockObjectProxy(Bus* bus, ...@@ -15,4 +15,19 @@ MockObjectProxy::MockObjectProxy(Bus* bus,
MockObjectProxy::~MockObjectProxy() { MockObjectProxy::~MockObjectProxy() {
} }
void MockObjectProxy::CallMethod(MethodCall* method_call,
int timeout_ms,
ResponseCallback callback) {
DoCallMethod(method_call, timeout_ms, &callback);
}
void MockObjectProxy::CallMethodWithErrorCallback(
MethodCall* method_call,
int timeout_ms,
ResponseCallback callback,
ErrorCallback error_callback) {
DoCallMethodWithErrorCallback(method_call, timeout_ms, &callback,
&error_callback);
}
} // namespace dbus } // namespace dbus
...@@ -29,13 +29,30 @@ class MockObjectProxy : public ObjectProxy { ...@@ -29,13 +29,30 @@ class MockObjectProxy : public ObjectProxy {
MOCK_METHOD2(CallMethodAndBlock, MOCK_METHOD2(CallMethodAndBlock,
std::unique_ptr<Response>(MethodCall* method_call, std::unique_ptr<Response>(MethodCall* method_call,
int timeout_ms)); int timeout_ms));
MOCK_METHOD3(CallMethod, void(MethodCall* method_call,
int timeout_ms, // This method is not mockable because it takes a move-only argument. To work
ResponseCallback callback)); // around this, CallMethod() implementation here calls DoCallMethod() which is
MOCK_METHOD4(CallMethodWithErrorCallback, void(MethodCall* method_call, // mockable.
int timeout_ms, void CallMethod(MethodCall* method_call,
ResponseCallback callback, int timeout_ms,
ErrorCallback error_callback)); ResponseCallback callback) override;
MOCK_METHOD3(DoCallMethod,
void(MethodCall* method_call,
int timeout_ms,
ResponseCallback* callback));
// This method is not mockable because it takes a move-only argument. To work
// around this, CallMethodWithErrorCallback() implementation here calls
// DoCallMethodWithErrorCallback() which is mockable.
void CallMethodWithErrorCallback(MethodCall* method_call,
int timeout_ms,
ResponseCallback callback,
ErrorCallback error_callback) override;
MOCK_METHOD4(DoCallMethodWithErrorCallback,
void(MethodCall* method_call,
int timeout_ms,
ResponseCallback* callback,
ErrorCallback* error_callback));
MOCK_METHOD4(ConnectToSignal, MOCK_METHOD4(ConnectToSignal,
void(const std::string& interface_name, void(const std::string& interface_name,
const std::string& signal_name, const std::string& signal_name,
......
...@@ -54,8 +54,9 @@ class MockTest : public testing::Test { ...@@ -54,8 +54,9 @@ class MockTest : public testing::Test {
// Set an expectation so mock_proxy's CallMethod() will use // Set an expectation so mock_proxy's CallMethod() will use
// HandleMockProxyResponseWithMessageLoop() to return responses. // HandleMockProxyResponseWithMessageLoop() to return responses.
EXPECT_CALL(*mock_proxy_.get(), CallMethod(_, _, _)).WillRepeatedly( EXPECT_CALL(*mock_proxy_.get(), DoCallMethod(_, _, _))
Invoke(this, &MockTest::HandleMockProxyResponseWithMessageLoop)); .WillRepeatedly(
Invoke(this, &MockTest::HandleMockProxyResponseWithMessageLoop));
// Set an expectation so mock_bus's GetObjectProxy() for the given // Set an expectation so mock_bus's GetObjectProxy() for the given
// service name and the object path will return mock_proxy_. // service name and the object path will return mock_proxy_.
...@@ -120,20 +121,20 @@ class MockTest : public testing::Test { ...@@ -120,20 +121,20 @@ class MockTest : public testing::Test {
void HandleMockProxyResponseWithMessageLoop( void HandleMockProxyResponseWithMessageLoop(
MethodCall* method_call, MethodCall* method_call,
int timeout_ms, int timeout_ms,
ObjectProxy::ResponseCallback response_callback) { ObjectProxy::ResponseCallback* response_callback) {
std::unique_ptr<Response> response = std::unique_ptr<Response> response =
CreateMockProxyResponse(method_call, timeout_ms); CreateMockProxyResponse(method_call, timeout_ms);
message_loop_.task_runner()->PostTask( message_loop_.task_runner()->PostTask(
FROM_HERE, FROM_HERE,
base::Bind(&MockTest::RunResponseCallback, base::Unretained(this), base::BindOnce(&MockTest::RunResponseCallback, base::Unretained(this),
response_callback, base::Passed(&response))); std::move(*response_callback), base::Passed(&response)));
} }
// Runs the given response callback with the given response. // Runs the given response callback with the given response.
void RunResponseCallback( void RunResponseCallback(
ObjectProxy::ResponseCallback response_callback, ObjectProxy::ResponseCallback response_callback,
std::unique_ptr<Response> response) { std::unique_ptr<Response> response) {
response_callback.Run(response.get()); std::move(response_callback).Run(response.get());
} }
}; };
......
This diff is collapsed.
...@@ -68,30 +68,30 @@ class CHROME_DBUS_EXPORT ObjectProxy ...@@ -68,30 +68,30 @@ class CHROME_DBUS_EXPORT ObjectProxy
// Called when an error response is returned or no response is returned. // Called when an error response is returned or no response is returned.
// Used for CallMethodWithErrorCallback(). // Used for CallMethodWithErrorCallback().
typedef base::Callback<void(ErrorResponse*)> ErrorCallback; using ErrorCallback = base::OnceCallback<void(ErrorResponse*)>;
// Called when the response is returned. Used for CallMethod(). // Called when the response is returned. Used for CallMethod().
typedef base::Callback<void(Response*)> ResponseCallback; using ResponseCallback = base::OnceCallback<void(Response*)>;
// Called when a signal is received. Signal* is the incoming signal. // Called when a signal is received. Signal* is the incoming signal.
typedef base::Callback<void (Signal*)> SignalCallback; using SignalCallback = base::Callback<void(Signal*)>;
// Called when NameOwnerChanged signal is received. // Called when NameOwnerChanged signal is received.
typedef base::Callback<void( using NameOwnerChangedCallback =
const std::string& old_owner, base::Callback<void(const std::string& old_owner,
const std::string& new_owner)> NameOwnerChangedCallback; const std::string& new_owner)>;
// Called when the service becomes available. // Called when the service becomes available.
typedef base::Callback<void( using WaitForServiceToBeAvailableCallback =
bool service_is_available)> WaitForServiceToBeAvailableCallback; base::Callback<void(bool service_is_available)>;
// Called when the object proxy is connected to the signal. // Called when the object proxy is connected to the signal.
// Parameters: // Parameters:
// - the interface name. // - the interface name.
// - the signal name. // - the signal name.
// - whether it was successful or not. // - whether it was successful or not.
typedef base::Callback<void (const std::string&, const std::string&, bool)> using OnConnectedCallback =
OnConnectedCallback; base::Callback<void(const std::string&, const std::string&, bool)>;
// Calls the method of the remote object and blocks until the response // Calls the method of the remote object and blocks until the response
// is returned. Returns NULL on error with the error details specified // is returned. Returns NULL on error with the error details specified
...@@ -201,40 +201,45 @@ class CHROME_DBUS_EXPORT ObjectProxy ...@@ -201,40 +201,45 @@ class CHROME_DBUS_EXPORT ObjectProxy
private: private:
friend class base::RefCountedThreadSafe<ObjectProxy>; friend class base::RefCountedThreadSafe<ObjectProxy>;
using CallMethodInternalCallback =
base::OnceCallback<void(Response* response,
ErrorResponse* error_response)>;
// Struct of data we'll be passing from StartAsyncMethodCall() to // Struct of data we'll be passing from StartAsyncMethodCall() to
// OnPendingCallIsCompleteThunk(). // OnPendingCallIsCompleteThunk().
struct OnPendingCallIsCompleteData { struct OnPendingCallIsCompleteData {
OnPendingCallIsCompleteData(ObjectProxy* in_object_proxy, OnPendingCallIsCompleteData(ObjectProxy* in_object_proxy,
ResponseCallback in_response_callback, CallMethodInternalCallback callback,
ErrorCallback error_callback,
base::TimeTicks start_time); base::TimeTicks start_time);
~OnPendingCallIsCompleteData(); ~OnPendingCallIsCompleteData();
ObjectProxy* object_proxy; ObjectProxy* object_proxy;
ResponseCallback response_callback; CallMethodInternalCallback callback;
ErrorCallback error_callback;
base::TimeTicks start_time; base::TimeTicks start_time;
}; };
// This is a helpr function to implement CallMethod() and
// CallMethodWithErrorCallback().
void CallMethodInternal(MethodCall* method_call,
int timeout_ms,
CallMethodInternalCallback callback);
// Starts the async method call. This is a helper function to implement // Starts the async method call. This is a helper function to implement
// CallMethod(). // CallMethod().
void StartAsyncMethodCall(int timeout_ms, void StartAsyncMethodCall(int timeout_ms,
DBusMessage* request_message, DBusMessage* request_message,
ResponseCallback response_callback, CallMethodInternalCallback callback,
ErrorCallback error_callback,
base::TimeTicks start_time); base::TimeTicks start_time);
// Called when the pending call is complete. // Called when the pending call is complete.
void OnPendingCallIsComplete(DBusPendingCall* pending_call, void OnPendingCallIsComplete(DBusPendingCall* pending_call,
ResponseCallback response_callback, CallMethodInternalCallback callback,
ErrorCallback error_callback,
base::TimeTicks start_time); base::TimeTicks start_time);
// Runs the response callback with the given response object. // Runs the CallMethodInternalCallback with the given response object.
void RunResponseCallback(ResponseCallback response_callback, void RunCallMethodInternalCallback(CallMethodInternalCallback callback,
ErrorCallback error_callback, base::TimeTicks start_time,
base::TimeTicks start_time, DBusMessage* response_message);
DBusMessage* response_message);
// Redirects the function call to OnPendingCallIsComplete(). // Redirects the function call to OnPendingCallIsComplete().
static void OnPendingCallIsCompleteThunk(DBusPendingCall* pending_call, static void OnPendingCallIsCompleteThunk(DBusPendingCall* pending_call,
...@@ -272,11 +277,14 @@ class CHROME_DBUS_EXPORT ObjectProxy ...@@ -272,11 +277,14 @@ class CHROME_DBUS_EXPORT ObjectProxy
const base::StringPiece& error_name, const base::StringPiece& error_name,
const base::StringPiece& error_message) const; const base::StringPiece& error_message) const;
// Used as ErrorCallback by CallMethod(). // Used as CallMethodInternalCallback by CallMethod(). (i.e. ErrorCallback
void OnCallMethodError(const std::string& interface_name, // wasn't provided, hence response_callback will be used for handling the
const std::string& method_name, // error response).
ResponseCallback response_callback, void OnCallMethod(const std::string& interface_name,
ErrorResponse* error_response); const std::string& method_name,
ResponseCallback response_callback,
Response* response,
ErrorResponse* error_response);
// Adds the match rule to the bus and associate the callback with the signal. // Adds the match rule to the bus and associate the callback with the signal.
bool AddMatchRuleWithCallback(const std::string& match_rule, bool AddMatchRuleWithCallback(const std::string& match_rule,
...@@ -310,7 +318,7 @@ class CHROME_DBUS_EXPORT ObjectProxy ...@@ -310,7 +318,7 @@ class CHROME_DBUS_EXPORT ObjectProxy
// The method table where keys are absolute signal names (i.e. interface // The method table where keys are absolute signal names (i.e. interface
// name + signal name), and values are lists of the corresponding callbacks. // name + signal name), and values are lists of the corresponding callbacks.
typedef std::map<std::string, std::vector<SignalCallback> > MethodTable; using MethodTable = std::map<std::string, std::vector<SignalCallback>>;
MethodTable method_table_; MethodTable method_table_;
// The callback called when NameOwnerChanged signal is received. // The callback called when NameOwnerChanged signal is received.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment