xref: /aosp_15_r20/external/libchrome/dbus/mock_unittest.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
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.
4 
5 #include <memory>
6 
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "base/single_thread_task_runner.h"
13 #include "dbus/message.h"
14 #include "dbus/mock_bus.h"
15 #include "dbus/mock_exported_object.h"
16 #include "dbus/mock_object_proxy.h"
17 #include "dbus/object_path.h"
18 #include "dbus/scoped_dbus_error.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 
22 using ::testing::_;
23 using ::testing::Invoke;
24 using ::testing::Return;
25 using ::testing::Unused;
26 
27 namespace dbus {
28 
29 class MockTest : public testing::Test {
30  public:
31   MockTest() = default;
32 
SetUp()33   void SetUp() override {
34     // Create a mock bus.
35     Bus::Options options;
36     options.bus_type = Bus::SYSTEM;
37     mock_bus_ = new MockBus(options);
38 
39     // Create a mock proxy.
40     mock_proxy_ = new MockObjectProxy(
41         mock_bus_.get(),
42         "org.chromium.TestService",
43         ObjectPath("/org/chromium/TestObject"));
44 
45     // Set an expectation so mock_proxy's CallMethodAndBlock() will use
46     // CreateMockProxyResponse() to return responses.
47     EXPECT_CALL(*mock_proxy_.get(), CallMethodAndBlock(_, _))
48         .WillRepeatedly(Invoke(this, &MockTest::CreateMockProxyResponse));
49     EXPECT_CALL(*mock_proxy_.get(),
50                 CallMethodAndBlockWithErrorDetails(_, _, _))
51         .WillRepeatedly(
52             Invoke(this, &MockTest::CreateMockProxyResponseWithErrorDetails));
53 
54     // Set an expectation so mock_proxy's CallMethod() will use
55     // HandleMockProxyResponseWithMessageLoop() to return responses.
56     EXPECT_CALL(*mock_proxy_.get(), DoCallMethod(_, _, _))
57         .WillRepeatedly(
58             Invoke(this, &MockTest::HandleMockProxyResponseWithMessageLoop));
59 
60     // Set an expectation so mock_bus's GetObjectProxy() for the given
61     // service name and the object path will return mock_proxy_.
62     EXPECT_CALL(*mock_bus_.get(),
63                 GetObjectProxy("org.chromium.TestService",
64                                ObjectPath("/org/chromium/TestObject")))
65         .WillOnce(Return(mock_proxy_.get()));
66 
67     // ShutdownAndBlock() will be called in TearDown().
68     EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()).WillOnce(Return());
69   }
70 
TearDown()71   void TearDown() override { mock_bus_->ShutdownAndBlock(); }
72 
73   // Called when the response is received.
OnResponse(Response * response)74   void OnResponse(Response* response) {
75     // |response| will be deleted on exit of the function. Copy the
76     // payload to |response_string_|.
77     if (response) {
78       MessageReader reader(response);
79       ASSERT_TRUE(reader.PopString(&response_string_));
80     }
81     run_loop_->Quit();
82   };
83 
84  protected:
85   std::string response_string_;
86   base::MessageLoop message_loop_;
87   std::unique_ptr<base::RunLoop> run_loop_;
88   scoped_refptr<MockBus> mock_bus_;
89   scoped_refptr<MockObjectProxy> mock_proxy_;
90 
91  private:
92   // Returns a response for the given method call. Used to implement
93   // CallMethodAndBlock() for |mock_proxy_|.
CreateMockProxyResponse(MethodCall * method_call,int timeout_ms)94   std::unique_ptr<Response> CreateMockProxyResponse(MethodCall* method_call,
95                                                     int timeout_ms) {
96     if (method_call->GetInterface() == "org.chromium.TestInterface" &&
97         method_call->GetMember() == "Echo") {
98       MessageReader reader(method_call);
99       std::string text_message;
100       if (reader.PopString(&text_message)) {
101         std::unique_ptr<Response> response = Response::CreateEmpty();
102         MessageWriter writer(response.get());
103         writer.AppendString(text_message);
104         return response;
105       }
106     }
107 
108     LOG(ERROR) << "Unexpected method call: " << method_call->ToString();
109     return nullptr;
110   }
111 
CreateMockProxyResponseWithErrorDetails(MethodCall * method_call,int timeout_ms,ScopedDBusError * error)112   std::unique_ptr<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 nullptr;
116   }
117 
118   // Creates a response and runs the given response callback in the
119   // message loop with the response. Used to implement for |mock_proxy_|.
HandleMockProxyResponseWithMessageLoop(MethodCall * method_call,int timeout_ms,ObjectProxy::ResponseCallback * response_callback)120   void HandleMockProxyResponseWithMessageLoop(
121       MethodCall* method_call,
122       int timeout_ms,
123       ObjectProxy::ResponseCallback* response_callback) {
124     std::unique_ptr<Response> response =
125         CreateMockProxyResponse(method_call, timeout_ms);
126     message_loop_.task_runner()->PostTask(
127         FROM_HERE,
128         base::BindOnce(&MockTest::RunResponseCallback, base::Unretained(this),
129                        std::move(*response_callback), std::move(response)));
130   }
131 
132   // Runs the given response callback with the given response.
RunResponseCallback(ObjectProxy::ResponseCallback response_callback,std::unique_ptr<Response> response)133   void RunResponseCallback(
134       ObjectProxy::ResponseCallback response_callback,
135       std::unique_ptr<Response> response) {
136     std::move(response_callback).Run(response.get());
137   }
138 };
139 
140 // This test demonstrates how to mock a synchronous method call using the
141 // mock classes.
TEST_F(MockTest,CallMethodAndBlock)142 TEST_F(MockTest, CallMethodAndBlock) {
143   const char kHello[] = "Hello";
144   // Get an object proxy from the mock bus.
145   ObjectProxy* proxy = mock_bus_->GetObjectProxy(
146       "org.chromium.TestService",
147       ObjectPath("/org/chromium/TestObject"));
148 
149   // Create a method call.
150   MethodCall method_call("org.chromium.TestInterface", "Echo");
151   MessageWriter writer(&method_call);
152   writer.AppendString(kHello);
153 
154   // Call the method.
155   std::unique_ptr<Response> response(proxy->CallMethodAndBlock(
156       &method_call, ObjectProxy::TIMEOUT_USE_DEFAULT));
157 
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);
165 }
166 
TEST_F(MockTest,CallMethodAndBlockWithErrorDetails)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"));
172 
173   // Create a method call.
174   MethodCall method_call("org.chromium.TestInterface", "Echo");
175 
176   ScopedDBusError error;
177   // Call the method.
178   std::unique_ptr<Response> response(proxy->CallMethodAndBlockWithErrorDetails(
179       &method_call, ObjectProxy::TIMEOUT_USE_DEFAULT, &error));
180 
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
189 // mock classes.
TEST_F(MockTest,CallMethod)190 TEST_F(MockTest, CallMethod) {
191   const char kHello[] = "hello";
192 
193   // Get an object proxy from the mock bus.
194   ObjectProxy* proxy = mock_bus_->GetObjectProxy(
195       "org.chromium.TestService",
196       ObjectPath("/org/chromium/TestObject"));
197 
198   // Create a method call.
199   MethodCall method_call("org.chromium.TestInterface", "Echo");
200   MessageWriter writer(&method_call);
201   writer.AppendString(kHello);
202 
203   // Call the method.
204   run_loop_.reset(new base::RunLoop);
205   proxy->CallMethod(&method_call,
206                     ObjectProxy::TIMEOUT_USE_DEFAULT,
207                     base::Bind(&MockTest::OnResponse,
208                                base::Unretained(this)));
209   // Run the message loop to let OnResponse be called.
210   run_loop_->Run();
211 
212   EXPECT_EQ(kHello, response_string_);
213 }
214 
215 }  // namespace dbus
216