blob: 6b2a521127aa1eca0e6dd4c17ef05131a65adb21 [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
dcheng7f5750d2014-12-30 03:30:17 +090032 void SetUp() override {
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
dcheng7f5750d2014-12-30 03:30:17 +090069 void TearDown() override { mock_bus_->ShutdownAndBlock(); }
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090070
71 // Called when the response is received.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090072 void OnResponse(Response* response) {
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090073 // |response| will be deleted on exit of the function. Copy the
74 // payload to |response_string_|.
75 if (response) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090076 MessageReader reader(response);
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090077 ASSERT_TRUE(reader.PopString(&response_string_));
78 }
earthdok64401d72014-09-03 19:32:36 +090079 run_loop_->Quit();
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090080 };
81
82 protected:
83 std::string response_string_;
xhwang@chromium.orgdff6b132013-05-02 01:10:30 +090084 base::MessageLoop message_loop_;
earthdok64401d72014-09-03 19:32:36 +090085 scoped_ptr<base::RunLoop> run_loop_;
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090086 scoped_refptr<MockBus> mock_bus_;
87 scoped_refptr<MockObjectProxy> mock_proxy_;
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090088
89 private:
90 // Returns a response for the given method call. Used to implement
91 // CallMethodAndBlock() for |mock_proxy_|.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090092 Response* CreateMockProxyResponse(MethodCall* method_call,
93 int timeout_ms) {
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090094 if (method_call->GetInterface() == "org.chromium.TestInterface" &&
95 method_call->GetMember() == "Echo") {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090096 MessageReader reader(method_call);
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090097 std::string text_message;
98 if (reader.PopString(&text_message)) {
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090099 scoped_ptr<Response> response = Response::CreateEmpty();
100 MessageWriter writer(response.get());
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900101 writer.AppendString(text_message);
yuki@chromium.orgd4eedf82013-02-07 18:46:24 +0900102 return response.release();
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900103 }
104 }
105
106 LOG(ERROR) << "Unexpected method call: " << method_call->ToString();
107 return NULL;
108 }
109
avakulenko1d8962b2014-09-17 10:44:09 +0900110 Response* CreateMockProxyResponseWithErrorDetails(
111 MethodCall* method_call, int timeout_ms, ScopedDBusError* error) {
112 dbus_set_error(error->get(), DBUS_ERROR_NOT_SUPPORTED, "Not implemented");
113 return NULL;
114 }
115
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900116 // 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(
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900119 MethodCall* method_call,
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900120 int timeout_ms,
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900121 ObjectProxy::ResponseCallback response_callback) {
122 Response* response = CreateMockProxyResponse(method_call, timeout_ms);
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900123 message_loop_.PostTask(FROM_HERE,
124 base::Bind(&MockTest::RunResponseCallback,
125 base::Unretained(this),
126 response_callback,
127 response));
128 }
129
130 // Runs the given response callback with the given response.
131 void RunResponseCallback(
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900132 ObjectProxy::ResponseCallback response_callback,
133 Response* response) {
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900134 response_callback.Run(response);
135 delete response;
136 }
137};
138
avakulenko1d8962b2014-09-17 10:44:09 +0900139// This test demonstrates how to mock a synchronous method call using the
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900140// mock classes.
141TEST_F(MockTest, CallMethodAndBlock) {
142 const char kHello[] = "Hello";
143 // Get an object proxy from the mock bus.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900144 ObjectProxy* proxy = mock_bus_->GetObjectProxy(
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900145 "org.chromium.TestService",
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900146 ObjectPath("/org/chromium/TestObject"));
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900147
148 // Create a method call.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900149 MethodCall method_call("org.chromium.TestInterface", "Echo");
150 MessageWriter writer(&method_call);
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900151 writer.AppendString(kHello);
152
153 // Call the method.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900154 scoped_ptr<Response> response(
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900155 proxy->CallMethodAndBlock(&method_call,
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900156 ObjectProxy::TIMEOUT_USE_DEFAULT));
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900157
158 // Check the response.
159 ASSERT_TRUE(response.get());
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900160 MessageReader reader(response.get());
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900161 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);
165}
166
avakulenko1d8962b2014-09-17 10:44:09 +0900167TEST_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"));
172
173 // Create a method call.
174 MethodCall method_call("org.chromium.TestInterface", "Echo");
175
176 ScopedDBusError error;
177 // Call the method.
178 scoped_ptr<Response> response(
179 proxy->CallMethodAndBlockWithErrorDetails(
180 &method_call, ObjectProxy::TIMEOUT_USE_DEFAULT, &error));
181
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());
187}
188
189// This test demonstrates how to mock an asynchronous method call using the
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900190// mock classes.
191TEST_F(MockTest, CallMethod) {
192 const char kHello[] = "hello";
193
194 // Get an object proxy from the mock bus.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900195 ObjectProxy* proxy = mock_bus_->GetObjectProxy(
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900196 "org.chromium.TestService",
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900197 ObjectPath("/org/chromium/TestObject"));
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900198
199 // Create a method call.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900200 MethodCall method_call("org.chromium.TestInterface", "Echo");
201 MessageWriter writer(&method_call);
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900202 writer.AppendString(kHello);
203
204 // Call the method.
earthdok64401d72014-09-03 19:32:36 +0900205 run_loop_.reset(new base::RunLoop);
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900206 proxy->CallMethod(&method_call,
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900207 ObjectProxy::TIMEOUT_USE_DEFAULT,
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900208 base::Bind(&MockTest::OnResponse,
209 base::Unretained(this)));
210 // Run the message loop to let OnResponse be called.
earthdok64401d72014-09-03 19:32:36 +0900211 run_loop_->Run();
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900212
213 EXPECT_EQ(kHello, response_string_);
214}
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900215
216} // namespace dbus