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 "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
20 using ::testing::Invoke
;
21 using ::testing::Return
;
22 using ::testing::Unused
;
26 class MockTest
: public testing::Test
{
31 virtual void SetUp() {
34 options
.bus_type
= Bus::SYSTEM
;
35 mock_bus_
= new MockBus(options
);
37 // Create a mock proxy.
38 mock_proxy_
= new MockObjectProxy(
40 "org.chromium.TestService",
41 ObjectPath("/org/chromium/TestObject"));
43 // Set an expectation so mock_proxy's CallMethodAndBlock() will use
44 // CreateMockProxyResponse() to return responses.
45 EXPECT_CALL(*mock_proxy_
.get(), MockCallMethodAndBlock(_
, _
))
46 .WillRepeatedly(Invoke(this, &MockTest::CreateMockProxyResponse
));
48 // Set an expectation so mock_proxy's CallMethod() will use
49 // HandleMockProxyResponseWithMessageLoop() to return responses.
50 EXPECT_CALL(*mock_proxy_
.get(), CallMethod(_
, _
, _
)).WillRepeatedly(
51 Invoke(this, &MockTest::HandleMockProxyResponseWithMessageLoop
));
53 // Set an expectation so mock_bus's GetObjectProxy() for the given
54 // service name and the object path will return mock_proxy_.
55 EXPECT_CALL(*mock_bus_
.get(),
56 GetObjectProxy("org.chromium.TestService",
57 ObjectPath("/org/chromium/TestObject")))
58 .WillOnce(Return(mock_proxy_
.get()));
60 // ShutdownAndBlock() will be called in TearDown().
61 EXPECT_CALL(*mock_bus_
.get(), ShutdownAndBlock()).WillOnce(Return());
64 virtual void TearDown() {
65 mock_bus_
->ShutdownAndBlock();
68 // Called when the response is received.
69 void OnResponse(Response
* response
) {
70 // |response| will be deleted on exit of the function. Copy the
71 // payload to |response_string_|.
73 MessageReader
reader(response
);
74 ASSERT_TRUE(reader
.PopString(&response_string_
));
80 std::string response_string_
;
81 base::MessageLoop message_loop_
;
82 scoped_ptr
<base::RunLoop
> run_loop_
;
83 scoped_refptr
<MockBus
> mock_bus_
;
84 scoped_refptr
<MockObjectProxy
> mock_proxy_
;
87 // Returns a response for the given method call. Used to implement
88 // CallMethodAndBlock() for |mock_proxy_|.
89 Response
* CreateMockProxyResponse(MethodCall
* method_call
,
91 if (method_call
->GetInterface() == "org.chromium.TestInterface" &&
92 method_call
->GetMember() == "Echo") {
93 MessageReader
reader(method_call
);
94 std::string text_message
;
95 if (reader
.PopString(&text_message
)) {
96 scoped_ptr
<Response
> response
= Response::CreateEmpty();
97 MessageWriter
writer(response
.get());
98 writer
.AppendString(text_message
);
99 return response
.release();
103 LOG(ERROR
) << "Unexpected method call: " << method_call
->ToString();
107 // Creates a response and runs the given response callback in the
108 // message loop with the response. Used to implement for |mock_proxy_|.
109 void HandleMockProxyResponseWithMessageLoop(
110 MethodCall
* method_call
,
112 ObjectProxy::ResponseCallback response_callback
) {
113 Response
* response
= CreateMockProxyResponse(method_call
, timeout_ms
);
114 message_loop_
.PostTask(FROM_HERE
,
115 base::Bind(&MockTest::RunResponseCallback
,
116 base::Unretained(this),
121 // Runs the given response callback with the given response.
122 void RunResponseCallback(
123 ObjectProxy::ResponseCallback response_callback
,
124 Response
* response
) {
125 response_callback
.Run(response
);
130 // This test demonstrates how to mock a synchronos method call using the
132 TEST_F(MockTest
, CallMethodAndBlock
) {
133 const char kHello
[] = "Hello";
134 // Get an object proxy from the mock bus.
135 ObjectProxy
* proxy
= mock_bus_
->GetObjectProxy(
136 "org.chromium.TestService",
137 ObjectPath("/org/chromium/TestObject"));
139 // Create a method call.
140 MethodCall
method_call("org.chromium.TestInterface", "Echo");
141 MessageWriter
writer(&method_call
);
142 writer
.AppendString(kHello
);
145 scoped_ptr
<Response
> response(
146 proxy
->CallMethodAndBlock(&method_call
,
147 ObjectProxy::TIMEOUT_USE_DEFAULT
));
149 // Check the response.
150 ASSERT_TRUE(response
.get());
151 MessageReader
reader(response
.get());
152 std::string text_message
;
153 ASSERT_TRUE(reader
.PopString(&text_message
));
154 // The text message should be echo'ed back.
155 EXPECT_EQ(kHello
, text_message
);
158 // This test demonstrates how to mock an asynchronos method call using the
160 TEST_F(MockTest
, CallMethod
) {
161 const char kHello
[] = "hello";
163 // Get an object proxy from the mock bus.
164 ObjectProxy
* proxy
= mock_bus_
->GetObjectProxy(
165 "org.chromium.TestService",
166 ObjectPath("/org/chromium/TestObject"));
168 // Create a method call.
169 MethodCall
method_call("org.chromium.TestInterface", "Echo");
170 MessageWriter
writer(&method_call
);
171 writer
.AppendString(kHello
);
174 run_loop_
.reset(new base::RunLoop
);
175 proxy
->CallMethod(&method_call
,
176 ObjectProxy::TIMEOUT_USE_DEFAULT
,
177 base::Bind(&MockTest::OnResponse
,
178 base::Unretained(this)));
179 // Run the message loop to let OnResponse be called.
182 EXPECT_EQ(kHello
, response_string_
);