blob: bfdc5a30c28a3988a56a36f021367755a38f432d [file] [log] [blame]
keybuk@google.combf4649a2012-02-15 06:29:06 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
satorux@chromium.orgf77861f2011-08-25 14:18:29 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/bind.h"
6#include "base/logging.h"
7#include "base/memory/ref_counted.h"
8#include "base/memory/scoped_ptr.h"
avi@chromium.orga29af562013-07-18 08:00:30 +09009#include "base/message_loop/message_loop.h"
earthdok64401d72014-09-03 19:32:36 +090010#include "base/run_loop.h"
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090011#include "dbus/message.h"
12#include "dbus/mock_bus.h"
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090013#include "dbus/mock_exported_object.h"
avi@chromium.orga29af562013-07-18 08:00:30 +090014#include "dbus/mock_object_proxy.h"
keybuk@google.combf4649a2012-02-15 06:29:06 +090015#include "dbus/object_path.h"
avakulenko1d8962b2014-09-17 10:44:09 +090016#include "dbus/scoped_dbus_error.h"
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090017#include "testing/gmock/include/gmock/gmock.h"
18#include "testing/gtest/include/gtest/gtest.h"
19
20using ::testing::_;
21using ::testing::Invoke;
22using ::testing::Return;
23using ::testing::Unused;
24
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090025namespace dbus {
26
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090027class MockTest : public testing::Test {
28 public:
29 MockTest() {
30 }
31
satorux@chromium.org326a6f82011-08-27 16:26:34 +090032 virtual void SetUp() {
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090033 // Create a mock bus.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090034 Bus::Options options;
35 options.bus_type = Bus::SYSTEM;
36 mock_bus_ = new MockBus(options);
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090037
38 // Create a mock proxy.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090039 mock_proxy_ = new MockObjectProxy(
keybuk@google.combf4649a2012-02-15 06:29:06 +090040 mock_bus_.get(),
41 "org.chromium.TestService",
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090042 ObjectPath("/org/chromium/TestObject"));
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090043
44 // Set an expectation so mock_proxy's CallMethodAndBlock() will use
45 // CreateMockProxyResponse() to return responses.
rsleevi@chromium.orgc5cb8592013-06-03 08:38:09 +090046 EXPECT_CALL(*mock_proxy_.get(), MockCallMethodAndBlock(_, _))
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090047 .WillRepeatedly(Invoke(this, &MockTest::CreateMockProxyResponse));
avakulenko1d8962b2014-09-17 10:44:09 +090048 EXPECT_CALL(*mock_proxy_.get(),
49 MockCallMethodAndBlockWithErrorDetails(_, _, _))
50 .WillRepeatedly(
51 Invoke(this, &MockTest::CreateMockProxyResponseWithErrorDetails));
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090052
53 // Set an expectation so mock_proxy's CallMethod() will use
54 // HandleMockProxyResponseWithMessageLoop() to return responses.
rsleevi@chromium.orgc5cb8592013-06-03 08:38:09 +090055 EXPECT_CALL(*mock_proxy_.get(), CallMethod(_, _, _)).WillRepeatedly(
56 Invoke(this, &MockTest::HandleMockProxyResponseWithMessageLoop));
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090057
58 // Set an expectation so mock_bus's GetObjectProxy() for the given
59 // service name and the object path will return mock_proxy_.
rsleevi@chromium.orgc5cb8592013-06-03 08:38:09 +090060 EXPECT_CALL(*mock_bus_.get(),
61 GetObjectProxy("org.chromium.TestService",
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090062 ObjectPath("/org/chromium/TestObject")))
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090063 .WillOnce(Return(mock_proxy_.get()));
satorux@chromium.org326a6f82011-08-27 16:26:34 +090064
65 // ShutdownAndBlock() will be called in TearDown().
rsleevi@chromium.orgc5cb8592013-06-03 08:38:09 +090066 EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()).WillOnce(Return());
satorux@chromium.org326a6f82011-08-27 16:26:34 +090067 }
68
69 virtual void TearDown() {
70 mock_bus_->ShutdownAndBlock();
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090071 }
72
73 // Called when the response is received.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090074 void OnResponse(Response* response) {
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090075 // |response| will be deleted on exit of the function. Copy the
76 // payload to |response_string_|.
77 if (response) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090078 MessageReader reader(response);
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090079 ASSERT_TRUE(reader.PopString(&response_string_));
80 }
earthdok64401d72014-09-03 19:32:36 +090081 run_loop_->Quit();
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090082 };
83
84 protected:
85 std::string response_string_;
xhwang@chromium.orgdff6b132013-05-02 01:10:30 +090086 base::MessageLoop message_loop_;
earthdok64401d72014-09-03 19:32:36 +090087 scoped_ptr<base::RunLoop> run_loop_;
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090088 scoped_refptr<MockBus> mock_bus_;
89 scoped_refptr<MockObjectProxy> mock_proxy_;
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090090
91 private:
92 // Returns a response for the given method call. Used to implement
93 // CallMethodAndBlock() for |mock_proxy_|.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090094 Response* CreateMockProxyResponse(MethodCall* method_call,
95 int timeout_ms) {
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090096 if (method_call->GetInterface() == "org.chromium.TestInterface" &&
97 method_call->GetMember() == "Echo") {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090098 MessageReader reader(method_call);
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090099 std::string text_message;
100 if (reader.PopString(&text_message)) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900101 scoped_ptr<Response> response = Response::CreateEmpty();
102 MessageWriter writer(response.get());
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900103 writer.AppendString(text_message);
yuki@chromium.orgd4eedf82013-02-07 18:46:24 +0900104 return response.release();
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900105 }
106 }
107
108 LOG(ERROR) << "Unexpected method call: " << method_call->ToString();
109 return NULL;
110 }
111
avakulenko1d8962b2014-09-17 10:44:09 +0900112 Response* CreateMockProxyResponseWithErrorDetails(
113 MethodCall* method_call, int timeout_ms, ScopedDBusError* error) {
114 dbus_set_error(error->get(), DBUS_ERROR_NOT_SUPPORTED, "Not implemented");
115 return NULL;
116 }
117
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900118 // Creates a response and runs the given response callback in the
119 // message loop with the response. Used to implement for |mock_proxy_|.
120 void HandleMockProxyResponseWithMessageLoop(
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900121 MethodCall* method_call,
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900122 int timeout_ms,
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900123 ObjectProxy::ResponseCallback response_callback) {
124 Response* response = CreateMockProxyResponse(method_call, timeout_ms);
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900125 message_loop_.PostTask(FROM_HERE,
126 base::Bind(&MockTest::RunResponseCallback,
127 base::Unretained(this),
128 response_callback,
129 response));
130 }
131
132 // Runs the given response callback with the given response.
133 void RunResponseCallback(
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900134 ObjectProxy::ResponseCallback response_callback,
135 Response* response) {
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900136 response_callback.Run(response);
137 delete response;
138 }
139};
140
avakulenko1d8962b2014-09-17 10:44:09 +0900141// This test demonstrates how to mock a synchronous method call using the
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900142// mock classes.
143TEST_F(MockTest, CallMethodAndBlock) {
144 const char kHello[] = "Hello";
145 // Get an object proxy from the mock bus.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900146 ObjectProxy* proxy = mock_bus_->GetObjectProxy(
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900147 "org.chromium.TestService",
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900148 ObjectPath("/org/chromium/TestObject"));
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900149
150 // Create a method call.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900151 MethodCall method_call("org.chromium.TestInterface", "Echo");
152 MessageWriter writer(&method_call);
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900153 writer.AppendString(kHello);
154
155 // Call the method.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900156 scoped_ptr<Response> response(
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900157 proxy->CallMethodAndBlock(&method_call,
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900158 ObjectProxy::TIMEOUT_USE_DEFAULT));
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900159
160 // Check the response.
161 ASSERT_TRUE(response.get());
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900162 MessageReader reader(response.get());
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900163 std::string text_message;
164 ASSERT_TRUE(reader.PopString(&text_message));
165 // The text message should be echo'ed back.
166 EXPECT_EQ(kHello, text_message);
167}
168
avakulenko1d8962b2014-09-17 10:44:09 +0900169TEST_F(MockTest, CallMethodAndBlockWithErrorDetails) {
170 // Get an object proxy from the mock bus.
171 ObjectProxy* proxy = mock_bus_->GetObjectProxy(
172 "org.chromium.TestService",
173 ObjectPath("/org/chromium/TestObject"));
174
175 // Create a method call.
176 MethodCall method_call("org.chromium.TestInterface", "Echo");
177
178 ScopedDBusError error;
179 // Call the method.
180 scoped_ptr<Response> response(
181 proxy->CallMethodAndBlockWithErrorDetails(
182 &method_call, ObjectProxy::TIMEOUT_USE_DEFAULT, &error));
183
184 // Check the response.
185 ASSERT_FALSE(response.get());
186 ASSERT_TRUE(error.is_set());
187 EXPECT_STREQ(DBUS_ERROR_NOT_SUPPORTED, error.name());
188 EXPECT_STREQ("Not implemented", error.message());
189}
190
191// This test demonstrates how to mock an asynchronous method call using the
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900192// mock classes.
193TEST_F(MockTest, CallMethod) {
194 const char kHello[] = "hello";
195
196 // Get an object proxy from the mock bus.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900197 ObjectProxy* proxy = mock_bus_->GetObjectProxy(
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900198 "org.chromium.TestService",
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900199 ObjectPath("/org/chromium/TestObject"));
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900200
201 // Create a method call.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900202 MethodCall method_call("org.chromium.TestInterface", "Echo");
203 MessageWriter writer(&method_call);
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900204 writer.AppendString(kHello);
205
206 // Call the method.
earthdok64401d72014-09-03 19:32:36 +0900207 run_loop_.reset(new base::RunLoop);
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900208 proxy->CallMethod(&method_call,
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900209 ObjectProxy::TIMEOUT_USE_DEFAULT,
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900210 base::Bind(&MockTest::OnResponse,
211 base::Unretained(this)));
212 // Run the message loop to let OnResponse be called.
earthdok64401d72014-09-03 19:32:36 +0900213 run_loop_->Run();
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900214
215 EXPECT_EQ(kHello, response_string_);
216}
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900217
218} // namespace dbus