This source file includes following definitions.
- SetUp
- TearDown
- SetUpReturnSignal
- CallMethod
- MockExportMethod
- MockCallMethodAndBlock
- MockConnectToSignal
- MockSendSignal
- OnResponse
#include "chrome/browser/chromeos/dbus/service_provider_test_helper.h"
#include "base/bind.h"
#include "dbus/message.h"
#include "dbus/mock_bus.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
using ::testing::_;
using ::testing::AllOf;
using ::testing::Invoke;
using ::testing::ResultOf;
using ::testing::Return;
using ::testing::Unused;
namespace chromeos {
ServiceProviderTestHelper::ServiceProviderTestHelper()
: response_received_(false) {
}
ServiceProviderTestHelper::~ServiceProviderTestHelper() {
}
void ServiceProviderTestHelper::SetUp(
const std::string& exported_method_name,
CrosDBusService::ServiceProviderInterface* service_provider) {
dbus::Bus::Options options;
options.bus_type = dbus::Bus::SYSTEM;
mock_bus_ = new dbus::MockBus(options);
EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()).WillOnce(Return());
mock_exported_object_ =
new dbus::MockExportedObject(mock_bus_.get(),
dbus::ObjectPath(kLibCrosServicePath));
EXPECT_CALL(
*mock_exported_object_.get(),
ExportMethod(kLibCrosServiceInterface, exported_method_name, _, _))
.WillOnce(Invoke(this, &ServiceProviderTestHelper::MockExportMethod));
mock_object_proxy_ =
new dbus::MockObjectProxy(mock_bus_.get(),
kLibCrosServiceName,
dbus::ObjectPath(kLibCrosServicePath));
EXPECT_CALL(*mock_object_proxy_.get(),
MockCallMethodAndBlock(
AllOf(ResultOf(std::mem_fun(&dbus::MethodCall::GetInterface),
kLibCrosServiceInterface),
ResultOf(std::mem_fun(&dbus::MethodCall::GetMember),
exported_method_name)),
_))
.WillOnce(
Invoke(this, &ServiceProviderTestHelper::MockCallMethodAndBlock));
service_provider->Start(mock_exported_object_.get());
}
void ServiceProviderTestHelper::TearDown() {
mock_bus_->ShutdownAndBlock();
mock_exported_object_ = NULL;
mock_object_proxy_ = NULL;
mock_bus_ = NULL;
}
void ServiceProviderTestHelper::SetUpReturnSignal(
const std::string& interface_name,
const std::string& signal_name,
dbus::ObjectProxy::SignalCallback signal_callback,
dbus::ObjectProxy::OnConnectedCallback on_connected_callback) {
EXPECT_CALL(*mock_exported_object_.get(), SendSignal(_))
.WillOnce(Invoke(this, &ServiceProviderTestHelper::MockSendSignal));
EXPECT_CALL(*mock_object_proxy_.get(),
ConnectToSignal(interface_name, signal_name, _, _))
.WillOnce(Invoke(this, &ServiceProviderTestHelper::MockConnectToSignal));
mock_object_proxy_->ConnectToSignal(interface_name, signal_name,
signal_callback, on_connected_callback);
}
scoped_ptr<dbus::Response> ServiceProviderTestHelper::CallMethod(
dbus::MethodCall* method_call) {
return mock_object_proxy_->CallMethodAndBlock(
method_call,
dbus::ObjectProxy::TIMEOUT_USE_DEFAULT);
}
void ServiceProviderTestHelper::MockExportMethod(
const std::string& interface_name,
const std::string& method_name,
dbus::ExportedObject::MethodCallCallback method_callback,
dbus::ExportedObject::OnExportedCallback on_exported_callback) {
on_exported_callback.Run(interface_name, method_name, true);
method_callback_ = method_callback;
}
dbus::Response* ServiceProviderTestHelper::MockCallMethodAndBlock(
dbus::MethodCall* method_call,
Unused) {
method_call->SetSerial(1);
method_callback_.Run(method_call,
base::Bind(&ServiceProviderTestHelper::OnResponse,
base::Unretained(this)));
if (!response_received_)
message_loop_.Run();
return response_.release();
}
void ServiceProviderTestHelper::MockConnectToSignal(
const std::string& interface_name,
const std::string& signal_name,
dbus::ObjectProxy::SignalCallback signal_callback,
dbus::ObjectProxy::OnConnectedCallback connected_callback) {
connected_callback.Run(interface_name, signal_name, true);
on_signal_callback_ = signal_callback;
}
void ServiceProviderTestHelper::MockSendSignal(dbus::Signal* signal) {
on_signal_callback_.Run(signal);
}
void ServiceProviderTestHelper::OnResponse(
scoped_ptr<dbus::Response> response) {
response_ = response.Pass();
response_received_ = true;
if (message_loop_.is_running())
message_loop_.Quit();
}
}