blob: 34327cded689057bd45bc380521569c4d39bb13f [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
dcheng30c5a172016-04-09 07:55:04 +09005#include <memory>
6
satorux@chromium.orgf77861f2011-08-25 14:18:29 +09007#include "base/bind.h"
8#include "base/logging.h"
9#include "base/memory/ref_counted.h"
avi@chromium.orga29af562013-07-18 08:00:30 +090010#include "base/message_loop/message_loop.h"
earthdok64401d72014-09-03 19:32:36 +090011#include "base/run_loop.h"
fdoray851719c2016-08-26 00:36:37 +090012#include "base/single_thread_task_runner.h"
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090013#include "dbus/message.h"
14#include "dbus/mock_bus.h"
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090015#include "dbus/mock_exported_object.h"
avi@chromium.orga29af562013-07-18 08:00:30 +090016#include "dbus/mock_object_proxy.h"
keybuk@google.combf4649a2012-02-15 06:29:06 +090017#include "dbus/object_path.h"
avakulenko1d8962b2014-09-17 10:44:09 +090018#include "dbus/scoped_dbus_error.h"
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090019#include "testing/gmock/include/gmock/gmock.h"
20#include "testing/gtest/include/gtest/gtest.h"
21
22using ::testing::_;
23using ::testing::Invoke;
24using ::testing::Return;
25using ::testing::Unused;
26
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090027namespace dbus {
28
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090029class MockTest : public testing::Test {
30 public:
Chris Watkins635e8902017-11-29 16:44:11 +090031 MockTest() = default;
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090032
dcheng7f5750d2014-12-30 03:30:17 +090033 void SetUp() override {
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090034 // Create a mock bus.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090035 Bus::Options options;
36 options.bus_type = Bus::SYSTEM;
37 mock_bus_ = new MockBus(options);
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090038
39 // Create a mock proxy.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090040 mock_proxy_ = new MockObjectProxy(
keybuk@google.combf4649a2012-02-15 06:29:06 +090041 mock_bus_.get(),
42 "org.chromium.TestService",
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090043 ObjectPath("/org/chromium/TestObject"));
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090044
45 // Set an expectation so mock_proxy's CallMethodAndBlock() will use
46 // CreateMockProxyResponse() to return responses.
Hidehiko Abe84221302017-06-27 14:45:55 +090047 EXPECT_CALL(*mock_proxy_.get(), CallMethodAndBlock(_, _))
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090048 .WillRepeatedly(Invoke(this, &MockTest::CreateMockProxyResponse));
avakulenko1d8962b2014-09-17 10:44:09 +090049 EXPECT_CALL(*mock_proxy_.get(),
Hidehiko Abe84221302017-06-27 14:45:55 +090050 CallMethodAndBlockWithErrorDetails(_, _, _))
avakulenko1d8962b2014-09-17 10:44:09 +090051 .WillRepeatedly(
52 Invoke(this, &MockTest::CreateMockProxyResponseWithErrorDetails));
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090053
54 // Set an expectation so mock_proxy's CallMethod() will use
55 // HandleMockProxyResponseWithMessageLoop() to return responses.
Ryo Hashimoto0102c162017-07-21 17:11:14 +090056 EXPECT_CALL(*mock_proxy_.get(), DoCallMethod(_, _, _))
57 .WillRepeatedly(
58 Invoke(this, &MockTest::HandleMockProxyResponseWithMessageLoop));
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090059
60 // Set an expectation so mock_bus's GetObjectProxy() for the given
61 // service name and the object path will return mock_proxy_.
rsleevi@chromium.orgc5cb8592013-06-03 08:38:09 +090062 EXPECT_CALL(*mock_bus_.get(),
63 GetObjectProxy("org.chromium.TestService",
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +090064 ObjectPath("/org/chromium/TestObject")))
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090065 .WillOnce(Return(mock_proxy_.get()));
satorux@chromium.org326a6f82011-08-27 16:26:34 +090066
67 // ShutdownAndBlock() will be called in TearDown().
rsleevi@chromium.orgc5cb8592013-06-03 08:38:09 +090068 EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()).WillOnce(Return());
satorux@chromium.org326a6f82011-08-27 16:26:34 +090069 }
70
dcheng7f5750d2014-12-30 03:30:17 +090071 void TearDown() override { mock_bus_->ShutdownAndBlock(); }
satorux@chromium.orgf77861f2011-08-25 14:18:29 +090072
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_;
dcheng30c5a172016-04-09 07:55:04 +090087 std::unique_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_|.
Hidehiko Abe84221302017-06-27 14:45:55 +090094 std::unique_ptr<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)) {
dcheng30c5a172016-04-09 07:55:04 +0900101 std::unique_ptr<Response> response = Response::CreateEmpty();
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900102 MessageWriter writer(response.get());
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900103 writer.AppendString(text_message);
Hidehiko Abe84221302017-06-27 14:45:55 +0900104 return response;
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900105 }
106 }
107
108 LOG(ERROR) << "Unexpected method call: " << method_call->ToString();
Hidehiko Abe84221302017-06-27 14:45:55 +0900109 return nullptr;
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900110 }
111
Hidehiko Abe84221302017-06-27 14:45:55 +0900112 std::unique_ptr<Response> CreateMockProxyResponseWithErrorDetails(
avakulenko1d8962b2014-09-17 10:44:09 +0900113 MethodCall* method_call, int timeout_ms, ScopedDBusError* error) {
114 dbus_set_error(error->get(), DBUS_ERROR_NOT_SUPPORTED, "Not implemented");
Hidehiko Abe84221302017-06-27 14:45:55 +0900115 return nullptr;
avakulenko1d8962b2014-09-17 10:44:09 +0900116 }
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,
Ryo Hashimoto0102c162017-07-21 17:11:14 +0900123 ObjectProxy::ResponseCallback* response_callback) {
Hidehiko Abe84221302017-06-27 14:45:55 +0900124 std::unique_ptr<Response> response =
125 CreateMockProxyResponse(method_call, timeout_ms);
fdoray851719c2016-08-26 00:36:37 +0900126 message_loop_.task_runner()->PostTask(
127 FROM_HERE,
Ryo Hashimoto0102c162017-07-21 17:11:14 +0900128 base::BindOnce(&MockTest::RunResponseCallback, base::Unretained(this),
129 std::move(*response_callback), base::Passed(&response)));
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900130 }
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,
Hidehiko Abe84221302017-06-27 14:45:55 +0900135 std::unique_ptr<Response> response) {
Ryo Hashimoto0102c162017-07-21 17:11:14 +0900136 std::move(response_callback).Run(response.get());
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900137 }
138};
139
avakulenko1d8962b2014-09-17 10:44:09 +0900140// This test demonstrates how to mock a synchronous method call using the
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900141// mock classes.
142TEST_F(MockTest, CallMethodAndBlock) {
143 const char kHello[] = "Hello";
144 // Get an object proxy from the mock bus.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900145 ObjectProxy* proxy = mock_bus_->GetObjectProxy(
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900146 "org.chromium.TestService",
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900147 ObjectPath("/org/chromium/TestObject"));
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900148
149 // Create a method call.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900150 MethodCall method_call("org.chromium.TestInterface", "Echo");
151 MessageWriter writer(&method_call);
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900152 writer.AppendString(kHello);
153
154 // Call the method.
dcheng30c5a172016-04-09 07:55:04 +0900155 std::unique_ptr<Response> response(proxy->CallMethodAndBlock(
156 &method_call, 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.
dcheng30c5a172016-04-09 07:55:04 +0900178 std::unique_ptr<Response> response(proxy->CallMethodAndBlockWithErrorDetails(
179 &method_call, ObjectProxy::TIMEOUT_USE_DEFAULT, &error));
avakulenko1d8962b2014-09-17 10:44:09 +0900180
181 // Check the response.
182 ASSERT_FALSE(response.get());
183 ASSERT_TRUE(error.is_set());
184 EXPECT_STREQ(DBUS_ERROR_NOT_SUPPORTED, error.name());
185 EXPECT_STREQ("Not implemented", error.message());
186}
187
188// This test demonstrates how to mock an asynchronous method call using the
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900189// mock classes.
190TEST_F(MockTest, CallMethod) {
191 const char kHello[] = "hello";
192
193 // Get an object proxy from the mock bus.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900194 ObjectProxy* proxy = mock_bus_->GetObjectProxy(
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900195 "org.chromium.TestService",
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900196 ObjectPath("/org/chromium/TestObject"));
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900197
198 // Create a method call.
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900199 MethodCall method_call("org.chromium.TestInterface", "Echo");
200 MessageWriter writer(&method_call);
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900201 writer.AppendString(kHello);
202
203 // Call the method.
earthdok64401d72014-09-03 19:32:36 +0900204 run_loop_.reset(new base::RunLoop);
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900205 proxy->CallMethod(&method_call,
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900206 ObjectProxy::TIMEOUT_USE_DEFAULT,
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900207 base::Bind(&MockTest::OnResponse,
208 base::Unretained(this)));
209 // Run the message loop to let OnResponse be called.
earthdok64401d72014-09-03 19:32:36 +0900210 run_loop_->Run();
satorux@chromium.orgf77861f2011-08-25 14:18:29 +0900211
212 EXPECT_EQ(kHello, response_string_);
213}
thestig@chromium.orgf0b7eac2013-06-13 15:37:19 +0900214
215} // namespace dbus