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/message_loop.h"
10 #include "dbus/message.h"
11 #include "dbus/mock_bus.h"
12 #include "dbus/mock_exported_object.h"
13 #include "dbus/mock_object_proxy.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
;
25 class MockTest
: public testing::Test
{
30 virtual void SetUp() {
33 options
.bus_type
= Bus::SYSTEM
;
34 mock_bus_
= new MockBus(options
);
36 // Create a mock proxy.
37 mock_proxy_
= new MockObjectProxy(
39 "org.chromium.TestService",
40 ObjectPath("/org/chromium/TestObject"));
42 // Set an expectation so mock_proxy's CallMethodAndBlock() will use
43 // CreateMockProxyResponse() to return responses.
44 EXPECT_CALL(*mock_proxy_
.get(), MockCallMethodAndBlock(_
, _
))
45 .WillRepeatedly(Invoke(this, &MockTest::CreateMockProxyResponse
));
47 // Set an expectation so mock_proxy's CallMethod() will use
48 // HandleMockProxyResponseWithMessageLoop() to return responses.
49 EXPECT_CALL(*mock_proxy_
.get(), CallMethod(_
, _
, _
)).WillRepeatedly(
50 Invoke(this, &MockTest::HandleMockProxyResponseWithMessageLoop
));
52 // Set an expectation so mock_bus's GetObjectProxy() for the given
53 // service name and the object path will return mock_proxy_.
54 EXPECT_CALL(*mock_bus_
.get(),
55 GetObjectProxy("org.chromium.TestService",
56 ObjectPath("/org/chromium/TestObject")))
57 .WillOnce(Return(mock_proxy_
.get()));
59 // ShutdownAndBlock() will be called in TearDown().
60 EXPECT_CALL(*mock_bus_
.get(), ShutdownAndBlock()).WillOnce(Return());
63 virtual void TearDown() {
64 mock_bus_
->ShutdownAndBlock();
67 // Called when the response is received.
68 void OnResponse(Response
* response
) {
69 // |response| will be deleted on exit of the function. Copy the
70 // payload to |response_string_|.
72 MessageReader
reader(response
);
73 ASSERT_TRUE(reader
.PopString(&response_string_
));
79 std::string response_string_
;
80 base::MessageLoop message_loop_
;
81 scoped_refptr
<MockBus
> mock_bus_
;
82 scoped_refptr
<MockObjectProxy
> mock_proxy_
;
85 // Returns a response for the given method call. Used to implement
86 // CallMethodAndBlock() for |mock_proxy_|.
87 Response
* CreateMockProxyResponse(MethodCall
* method_call
,
89 if (method_call
->GetInterface() == "org.chromium.TestInterface" &&
90 method_call
->GetMember() == "Echo") {
91 MessageReader
reader(method_call
);
92 std::string text_message
;
93 if (reader
.PopString(&text_message
)) {
94 scoped_ptr
<Response
> response
= Response::CreateEmpty();
95 MessageWriter
writer(response
.get());
96 writer
.AppendString(text_message
);
97 return response
.release();
101 LOG(ERROR
) << "Unexpected method call: " << method_call
->ToString();
105 // Creates a response and runs the given response callback in the
106 // message loop with the response. Used to implement for |mock_proxy_|.
107 void HandleMockProxyResponseWithMessageLoop(
108 MethodCall
* method_call
,
110 ObjectProxy::ResponseCallback response_callback
) {
111 Response
* response
= CreateMockProxyResponse(method_call
, timeout_ms
);
112 message_loop_
.PostTask(FROM_HERE
,
113 base::Bind(&MockTest::RunResponseCallback
,
114 base::Unretained(this),
119 // Runs the given response callback with the given response.
120 void RunResponseCallback(
121 ObjectProxy::ResponseCallback response_callback
,
122 Response
* response
) {
123 response_callback
.Run(response
);
128 // This test demonstrates how to mock a synchronos method call using the
130 TEST_F(MockTest
, CallMethodAndBlock
) {
131 const char kHello
[] = "Hello";
132 // Get an object proxy from the mock bus.
133 ObjectProxy
* proxy
= mock_bus_
->GetObjectProxy(
134 "org.chromium.TestService",
135 ObjectPath("/org/chromium/TestObject"));
137 // Create a method call.
138 MethodCall
method_call("org.chromium.TestInterface", "Echo");
139 MessageWriter
writer(&method_call
);
140 writer
.AppendString(kHello
);
143 scoped_ptr
<Response
> response(
144 proxy
->CallMethodAndBlock(&method_call
,
145 ObjectProxy::TIMEOUT_USE_DEFAULT
));
147 // Check the response.
148 ASSERT_TRUE(response
.get());
149 MessageReader
reader(response
.get());
150 std::string text_message
;
151 ASSERT_TRUE(reader
.PopString(&text_message
));
152 // The text message should be echo'ed back.
153 EXPECT_EQ(kHello
, text_message
);
156 // This test demonstrates how to mock an asynchronos method call using the
158 TEST_F(MockTest
, CallMethod
) {
159 const char kHello
[] = "hello";
161 // Get an object proxy from the mock bus.
162 ObjectProxy
* proxy
= mock_bus_
->GetObjectProxy(
163 "org.chromium.TestService",
164 ObjectPath("/org/chromium/TestObject"));
166 // Create a method call.
167 MethodCall
method_call("org.chromium.TestInterface", "Echo");
168 MessageWriter
writer(&method_call
);
169 writer
.AppendString(kHello
);
172 proxy
->CallMethod(&method_call
,
173 ObjectProxy::TIMEOUT_USE_DEFAULT
,
174 base::Bind(&MockTest::OnResponse
,
175 base::Unretained(this)));
176 // Run the message loop to let OnResponse be called.
179 EXPECT_EQ(kHello
, response_string_
);