This source file includes following definitions.
- SetUp
- TearDown
- OnResponse
- CreateMockProxyResponse
- HandleMockProxyResponseWithMessageLoop
- RunResponseCallback
- TEST_F
- TEST_F
#include "base/bind.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "dbus/message.h"
#include "dbus/mock_bus.h"
#include "dbus/mock_exported_object.h"
#include "dbus/mock_object_proxy.h"
#include "dbus/object_path.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::_;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::Unused;
namespace dbus {
class MockTest : public testing::Test {
public:
MockTest() {
}
virtual void SetUp() {
Bus::Options options;
options.bus_type = Bus::SYSTEM;
mock_bus_ = new MockBus(options);
mock_proxy_ = new MockObjectProxy(
mock_bus_.get(),
"org.chromium.TestService",
ObjectPath("/org/chromium/TestObject"));
EXPECT_CALL(*mock_proxy_.get(), MockCallMethodAndBlock(_, _))
.WillRepeatedly(Invoke(this, &MockTest::CreateMockProxyResponse));
EXPECT_CALL(*mock_proxy_.get(), CallMethod(_, _, _)).WillRepeatedly(
Invoke(this, &MockTest::HandleMockProxyResponseWithMessageLoop));
EXPECT_CALL(*mock_bus_.get(),
GetObjectProxy("org.chromium.TestService",
ObjectPath("/org/chromium/TestObject")))
.WillOnce(Return(mock_proxy_.get()));
EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()).WillOnce(Return());
}
virtual void TearDown() {
mock_bus_->ShutdownAndBlock();
}
void OnResponse(Response* response) {
if (response) {
MessageReader reader(response);
ASSERT_TRUE(reader.PopString(&response_string_));
}
message_loop_.Quit();
};
protected:
std::string response_string_;
base::MessageLoop message_loop_;
scoped_refptr<MockBus> mock_bus_;
scoped_refptr<MockObjectProxy> mock_proxy_;
private:
Response* CreateMockProxyResponse(MethodCall* method_call,
int timeout_ms) {
if (method_call->GetInterface() == "org.chromium.TestInterface" &&
method_call->GetMember() == "Echo") {
MessageReader reader(method_call);
std::string text_message;
if (reader.PopString(&text_message)) {
scoped_ptr<Response> response = Response::CreateEmpty();
MessageWriter writer(response.get());
writer.AppendString(text_message);
return response.release();
}
}
LOG(ERROR) << "Unexpected method call: " << method_call->ToString();
return NULL;
}
void HandleMockProxyResponseWithMessageLoop(
MethodCall* method_call,
int timeout_ms,
ObjectProxy::ResponseCallback response_callback) {
Response* response = CreateMockProxyResponse(method_call, timeout_ms);
message_loop_.PostTask(FROM_HERE,
base::Bind(&MockTest::RunResponseCallback,
base::Unretained(this),
response_callback,
response));
}
void RunResponseCallback(
ObjectProxy::ResponseCallback response_callback,
Response* response) {
response_callback.Run(response);
delete response;
}
};
TEST_F(MockTest, CallMethodAndBlock) {
const char kHello[] = "Hello";
ObjectProxy* proxy = mock_bus_->GetObjectProxy(
"org.chromium.TestService",
ObjectPath("/org/chromium/TestObject"));
MethodCall method_call("org.chromium.TestInterface", "Echo");
MessageWriter writer(&method_call);
writer.AppendString(kHello);
scoped_ptr<Response> response(
proxy->CallMethodAndBlock(&method_call,
ObjectProxy::TIMEOUT_USE_DEFAULT));
ASSERT_TRUE(response.get());
MessageReader reader(response.get());
std::string text_message;
ASSERT_TRUE(reader.PopString(&text_message));
EXPECT_EQ(kHello, text_message);
}
TEST_F(MockTest, CallMethod) {
const char kHello[] = "hello";
ObjectProxy* proxy = mock_bus_->GetObjectProxy(
"org.chromium.TestService",
ObjectPath("/org/chromium/TestObject"));
MethodCall method_call("org.chromium.TestInterface", "Echo");
MessageWriter writer(&method_call);
writer.AppendString(kHello);
proxy->CallMethod(&method_call,
ObjectProxy::TIMEOUT_USE_DEFAULT,
base::Bind(&MockTest::OnResponse,
base::Unretained(this)));
message_loop_.Run();
EXPECT_EQ(kHello, response_string_);
}
}