1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 #include "base/logging.h"
7 #include "base/memory/ref_counted.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop.h"
10 #include "dbus/message.h"
11 #include "dbus/mock_bus.h"
12 #include "dbus/mock_object_proxy.h"
13 #include "dbus/mock_exported_object.h"
14 #include "dbus/object_path.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
19 using ::testing::Invoke
;
20 using ::testing::Return
;
21 using ::testing::Unused
;
23 class MockTest
: public testing::Test
{
28 virtual void SetUp() {
30 dbus::Bus::Options options
;
31 options
.bus_type
= dbus::Bus::SYSTEM
;
32 mock_bus_
= new dbus::MockBus(options
);
34 // Create a mock proxy.
35 mock_proxy_
= new dbus::MockObjectProxy(
37 "org.chromium.TestService",
38 dbus::ObjectPath("/org/chromium/TestObject"));
40 // Set an expectation so mock_proxy's CallMethodAndBlock() will use
41 // CreateMockProxyResponse() to return responses.
42 EXPECT_CALL(*mock_proxy_
.get(), MockCallMethodAndBlock(_
, _
))
43 .WillRepeatedly(Invoke(this, &MockTest::CreateMockProxyResponse
));
45 // Set an expectation so mock_proxy's CallMethod() will use
46 // HandleMockProxyResponseWithMessageLoop() to return responses.
47 EXPECT_CALL(*mock_proxy_
.get(), CallMethod(_
, _
, _
)).WillRepeatedly(
48 Invoke(this, &MockTest::HandleMockProxyResponseWithMessageLoop
));
50 // Set an expectation so mock_bus's GetObjectProxy() for the given
51 // service name and the object path will return mock_proxy_.
52 EXPECT_CALL(*mock_bus_
.get(),
53 GetObjectProxy("org.chromium.TestService",
54 dbus::ObjectPath("/org/chromium/TestObject")))
55 .WillOnce(Return(mock_proxy_
.get()));
57 // ShutdownAndBlock() will be called in TearDown().
58 EXPECT_CALL(*mock_bus_
.get(), ShutdownAndBlock()).WillOnce(Return());
61 virtual void TearDown() {
62 mock_bus_
->ShutdownAndBlock();
65 // Called when the response is received.
66 void OnResponse(dbus::Response
* response
) {
67 // |response| will be deleted on exit of the function. Copy the
68 // payload to |response_string_|.
70 dbus::MessageReader
reader(response
);
71 ASSERT_TRUE(reader
.PopString(&response_string_
));
77 std::string response_string_
;
78 base::MessageLoop message_loop_
;
79 scoped_refptr
<dbus::MockBus
> mock_bus_
;
80 scoped_refptr
<dbus::MockObjectProxy
> mock_proxy_
;
83 // Returns a response for the given method call. Used to implement
84 // CallMethodAndBlock() for |mock_proxy_|.
85 dbus::Response
* CreateMockProxyResponse(dbus::MethodCall
* method_call
,
87 if (method_call
->GetInterface() == "org.chromium.TestInterface" &&
88 method_call
->GetMember() == "Echo") {
89 dbus::MessageReader
reader(method_call
);
90 std::string text_message
;
91 if (reader
.PopString(&text_message
)) {
92 scoped_ptr
<dbus::Response
> response
= dbus::Response::CreateEmpty();
93 dbus::MessageWriter
writer(response
.get());
94 writer
.AppendString(text_message
);
95 return response
.release();
99 LOG(ERROR
) << "Unexpected method call: " << method_call
->ToString();
103 // Creates a response and runs the given response callback in the
104 // message loop with the response. Used to implement for |mock_proxy_|.
105 void HandleMockProxyResponseWithMessageLoop(
106 dbus::MethodCall
* method_call
,
108 dbus::ObjectProxy::ResponseCallback response_callback
) {
109 dbus::Response
* response
= CreateMockProxyResponse(method_call
,
111 message_loop_
.PostTask(FROM_HERE
,
112 base::Bind(&MockTest::RunResponseCallback
,
113 base::Unretained(this),
118 // Runs the given response callback with the given response.
119 void RunResponseCallback(
120 dbus::ObjectProxy::ResponseCallback response_callback
,
121 dbus::Response
* response
) {
122 response_callback
.Run(response
);
127 // This test demonstrates how to mock a synchronos method call using the
129 TEST_F(MockTest
, CallMethodAndBlock
) {
130 const char kHello
[] = "Hello";
131 // Get an object proxy from the mock bus.
132 dbus::ObjectProxy
* proxy
= mock_bus_
->GetObjectProxy(
133 "org.chromium.TestService",
134 dbus::ObjectPath("/org/chromium/TestObject"));
136 // Create a method call.
137 dbus::MethodCall
method_call("org.chromium.TestInterface", "Echo");
138 dbus::MessageWriter
writer(&method_call
);
139 writer
.AppendString(kHello
);
142 scoped_ptr
<dbus::Response
> response(
143 proxy
->CallMethodAndBlock(&method_call
,
144 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
));
146 // Check the response.
147 ASSERT_TRUE(response
.get());
148 dbus::MessageReader
reader(response
.get());
149 std::string text_message
;
150 ASSERT_TRUE(reader
.PopString(&text_message
));
151 // The text message should be echo'ed back.
152 EXPECT_EQ(kHello
, text_message
);
155 // This test demonstrates how to mock an asynchronos method call using the
157 TEST_F(MockTest
, CallMethod
) {
158 const char kHello
[] = "hello";
160 // Get an object proxy from the mock bus.
161 dbus::ObjectProxy
* proxy
= mock_bus_
->GetObjectProxy(
162 "org.chromium.TestService",
163 dbus::ObjectPath("/org/chromium/TestObject"));
165 // Create a method call.
166 dbus::MethodCall
method_call("org.chromium.TestInterface", "Echo");
167 dbus::MessageWriter
writer(&method_call
);
168 writer
.AppendString(kHello
);
171 proxy
->CallMethod(&method_call
,
172 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
173 base::Bind(&MockTest::OnResponse
,
174 base::Unretained(this)));
175 // Run the message loop to let OnResponse be called.
178 EXPECT_EQ(kHello
, response_string_
);