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 "base/run_loop.h"
11 #include "dbus/message.h"
12 #include "dbus/mock_bus.h"
13 #include "dbus/mock_exported_object.h"
14 #include "dbus/mock_object_proxy.h"
15 #include "dbus/object_path.h"
16 #include "dbus/scoped_dbus_error.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
21 using ::testing::Invoke
;
22 using ::testing::Return
;
23 using ::testing::Unused
;
27 class MockTest
: public testing::Test
{
32 void SetUp() override
{
35 options
.bus_type
= Bus::SYSTEM
;
36 mock_bus_
= new MockBus(options
);
38 // Create a mock proxy.
39 mock_proxy_
= new MockObjectProxy(
41 "org.chromium.TestService",
42 ObjectPath("/org/chromium/TestObject"));
44 // Set an expectation so mock_proxy's CallMethodAndBlock() will use
45 // CreateMockProxyResponse() to return responses.
46 EXPECT_CALL(*mock_proxy_
.get(), MockCallMethodAndBlock(_
, _
))
47 .WillRepeatedly(Invoke(this, &MockTest::CreateMockProxyResponse
));
48 EXPECT_CALL(*mock_proxy_
.get(),
49 MockCallMethodAndBlockWithErrorDetails(_
, _
, _
))
51 Invoke(this, &MockTest::CreateMockProxyResponseWithErrorDetails
));
53 // Set an expectation so mock_proxy's CallMethod() will use
54 // HandleMockProxyResponseWithMessageLoop() to return responses.
55 EXPECT_CALL(*mock_proxy_
.get(), CallMethod(_
, _
, _
)).WillRepeatedly(
56 Invoke(this, &MockTest::HandleMockProxyResponseWithMessageLoop
));
58 // Set an expectation so mock_bus's GetObjectProxy() for the given
59 // service name and the object path will return mock_proxy_.
60 EXPECT_CALL(*mock_bus_
.get(),
61 GetObjectProxy("org.chromium.TestService",
62 ObjectPath("/org/chromium/TestObject")))
63 .WillOnce(Return(mock_proxy_
.get()));
65 // ShutdownAndBlock() will be called in TearDown().
66 EXPECT_CALL(*mock_bus_
.get(), ShutdownAndBlock()).WillOnce(Return());
69 void TearDown() override
{ mock_bus_
->ShutdownAndBlock(); }
71 // Called when the response is received.
72 void OnResponse(Response
* response
) {
73 // |response| will be deleted on exit of the function. Copy the
74 // payload to |response_string_|.
76 MessageReader
reader(response
);
77 ASSERT_TRUE(reader
.PopString(&response_string_
));
83 std::string response_string_
;
84 base::MessageLoop message_loop_
;
85 scoped_ptr
<base::RunLoop
> run_loop_
;
86 scoped_refptr
<MockBus
> mock_bus_
;
87 scoped_refptr
<MockObjectProxy
> mock_proxy_
;
90 // Returns a response for the given method call. Used to implement
91 // CallMethodAndBlock() for |mock_proxy_|.
92 Response
* CreateMockProxyResponse(MethodCall
* method_call
,
94 if (method_call
->GetInterface() == "org.chromium.TestInterface" &&
95 method_call
->GetMember() == "Echo") {
96 MessageReader
reader(method_call
);
97 std::string text_message
;
98 if (reader
.PopString(&text_message
)) {
99 scoped_ptr
<Response
> response
= Response::CreateEmpty();
100 MessageWriter
writer(response
.get());
101 writer
.AppendString(text_message
);
102 return response
.release();
106 LOG(ERROR
) << "Unexpected method call: " << method_call
->ToString();
110 Response
* CreateMockProxyResponseWithErrorDetails(
111 MethodCall
* method_call
, int timeout_ms
, ScopedDBusError
* error
) {
112 dbus_set_error(error
->get(), DBUS_ERROR_NOT_SUPPORTED
, "Not implemented");
116 // Creates a response and runs the given response callback in the
117 // message loop with the response. Used to implement for |mock_proxy_|.
118 void HandleMockProxyResponseWithMessageLoop(
119 MethodCall
* method_call
,
121 ObjectProxy::ResponseCallback response_callback
) {
122 Response
* response
= CreateMockProxyResponse(method_call
, timeout_ms
);
123 message_loop_
.PostTask(FROM_HERE
,
124 base::Bind(&MockTest::RunResponseCallback
,
125 base::Unretained(this),
130 // Runs the given response callback with the given response.
131 void RunResponseCallback(
132 ObjectProxy::ResponseCallback response_callback
,
133 Response
* response
) {
134 response_callback
.Run(response
);
139 // This test demonstrates how to mock a synchronous method call using the
141 TEST_F(MockTest
, CallMethodAndBlock
) {
142 const char kHello
[] = "Hello";
143 // Get an object proxy from the mock bus.
144 ObjectProxy
* proxy
= mock_bus_
->GetObjectProxy(
145 "org.chromium.TestService",
146 ObjectPath("/org/chromium/TestObject"));
148 // Create a method call.
149 MethodCall
method_call("org.chromium.TestInterface", "Echo");
150 MessageWriter
writer(&method_call
);
151 writer
.AppendString(kHello
);
154 scoped_ptr
<Response
> response(
155 proxy
->CallMethodAndBlock(&method_call
,
156 ObjectProxy::TIMEOUT_USE_DEFAULT
));
158 // Check the response.
159 ASSERT_TRUE(response
.get());
160 MessageReader
reader(response
.get());
161 std::string text_message
;
162 ASSERT_TRUE(reader
.PopString(&text_message
));
163 // The text message should be echo'ed back.
164 EXPECT_EQ(kHello
, text_message
);
167 TEST_F(MockTest
, CallMethodAndBlockWithErrorDetails
) {
168 // Get an object proxy from the mock bus.
169 ObjectProxy
* proxy
= mock_bus_
->GetObjectProxy(
170 "org.chromium.TestService",
171 ObjectPath("/org/chromium/TestObject"));
173 // Create a method call.
174 MethodCall
method_call("org.chromium.TestInterface", "Echo");
176 ScopedDBusError error
;
178 scoped_ptr
<Response
> response(
179 proxy
->CallMethodAndBlockWithErrorDetails(
180 &method_call
, ObjectProxy::TIMEOUT_USE_DEFAULT
, &error
));
182 // Check the response.
183 ASSERT_FALSE(response
.get());
184 ASSERT_TRUE(error
.is_set());
185 EXPECT_STREQ(DBUS_ERROR_NOT_SUPPORTED
, error
.name());
186 EXPECT_STREQ("Not implemented", error
.message());
189 // This test demonstrates how to mock an asynchronous method call using the
191 TEST_F(MockTest
, CallMethod
) {
192 const char kHello
[] = "hello";
194 // Get an object proxy from the mock bus.
195 ObjectProxy
* proxy
= mock_bus_
->GetObjectProxy(
196 "org.chromium.TestService",
197 ObjectPath("/org/chromium/TestObject"));
199 // Create a method call.
200 MethodCall
method_call("org.chromium.TestInterface", "Echo");
201 MessageWriter
writer(&method_call
);
202 writer
.AppendString(kHello
);
205 run_loop_
.reset(new base::RunLoop
);
206 proxy
->CallMethod(&method_call
,
207 ObjectProxy::TIMEOUT_USE_DEFAULT
,
208 base::Bind(&MockTest::OnResponse
,
209 base::Unretained(this)));
210 // Run the message loop to let OnResponse be called.
213 EXPECT_EQ(kHello
, response_string_
);