keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
dcheng | 30c5a17 | 2016-04-09 07:55:04 +0900 | [diff] [blame] | 5 | #include <memory> |
| 6 | |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 7 | #include "base/bind.h" |
| 8 | #include "base/logging.h" |
| 9 | #include "base/memory/ref_counted.h" |
avi@chromium.org | a29af56 | 2013-07-18 08:00:30 +0900 | [diff] [blame] | 10 | #include "base/message_loop/message_loop.h" |
earthdok | 64401d7 | 2014-09-03 19:32:36 +0900 | [diff] [blame] | 11 | #include "base/run_loop.h" |
fdoray | 851719c | 2016-08-26 00:36:37 +0900 | [diff] [blame] | 12 | #include "base/single_thread_task_runner.h" |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 13 | #include "dbus/message.h" |
| 14 | #include "dbus/mock_bus.h" |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 15 | #include "dbus/mock_exported_object.h" |
avi@chromium.org | a29af56 | 2013-07-18 08:00:30 +0900 | [diff] [blame] | 16 | #include "dbus/mock_object_proxy.h" |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 17 | #include "dbus/object_path.h" |
avakulenko | 1d8962b | 2014-09-17 10:44:09 +0900 | [diff] [blame] | 18 | #include "dbus/scoped_dbus_error.h" |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 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 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 27 | namespace dbus { |
| 28 | |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 29 | class MockTest : public testing::Test { |
| 30 | public: |
| 31 | MockTest() { |
| 32 | } |
| 33 | |
dcheng | 7f5750d | 2014-12-30 03:30:17 +0900 | [diff] [blame] | 34 | void SetUp() override { |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 35 | // Create a mock bus. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 36 | Bus::Options options; |
| 37 | options.bus_type = Bus::SYSTEM; |
| 38 | mock_bus_ = new MockBus(options); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 39 | |
| 40 | // Create a mock proxy. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 41 | mock_proxy_ = new MockObjectProxy( |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 42 | mock_bus_.get(), |
| 43 | "org.chromium.TestService", |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 44 | ObjectPath("/org/chromium/TestObject")); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 45 | |
| 46 | // Set an expectation so mock_proxy's CallMethodAndBlock() will use |
| 47 | // CreateMockProxyResponse() to return responses. |
Hidehiko Abe | 8422130 | 2017-06-27 14:45:55 +0900 | [diff] [blame^] | 48 | EXPECT_CALL(*mock_proxy_.get(), CallMethodAndBlock(_, _)) |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 49 | .WillRepeatedly(Invoke(this, &MockTest::CreateMockProxyResponse)); |
avakulenko | 1d8962b | 2014-09-17 10:44:09 +0900 | [diff] [blame] | 50 | EXPECT_CALL(*mock_proxy_.get(), |
Hidehiko Abe | 8422130 | 2017-06-27 14:45:55 +0900 | [diff] [blame^] | 51 | CallMethodAndBlockWithErrorDetails(_, _, _)) |
avakulenko | 1d8962b | 2014-09-17 10:44:09 +0900 | [diff] [blame] | 52 | .WillRepeatedly( |
| 53 | Invoke(this, &MockTest::CreateMockProxyResponseWithErrorDetails)); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 54 | |
| 55 | // Set an expectation so mock_proxy's CallMethod() will use |
| 56 | // HandleMockProxyResponseWithMessageLoop() to return responses. |
rsleevi@chromium.org | c5cb859 | 2013-06-03 08:38:09 +0900 | [diff] [blame] | 57 | EXPECT_CALL(*mock_proxy_.get(), CallMethod(_, _, _)).WillRepeatedly( |
| 58 | Invoke(this, &MockTest::HandleMockProxyResponseWithMessageLoop)); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 59 | |
| 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.org | c5cb859 | 2013-06-03 08:38:09 +0900 | [diff] [blame] | 62 | EXPECT_CALL(*mock_bus_.get(), |
| 63 | GetObjectProxy("org.chromium.TestService", |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 64 | ObjectPath("/org/chromium/TestObject"))) |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 65 | .WillOnce(Return(mock_proxy_.get())); |
satorux@chromium.org | 326a6f8 | 2011-08-27 16:26:34 +0900 | [diff] [blame] | 66 | |
| 67 | // ShutdownAndBlock() will be called in TearDown(). |
rsleevi@chromium.org | c5cb859 | 2013-06-03 08:38:09 +0900 | [diff] [blame] | 68 | EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()).WillOnce(Return()); |
satorux@chromium.org | 326a6f8 | 2011-08-27 16:26:34 +0900 | [diff] [blame] | 69 | } |
| 70 | |
dcheng | 7f5750d | 2014-12-30 03:30:17 +0900 | [diff] [blame] | 71 | void TearDown() override { mock_bus_->ShutdownAndBlock(); } |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 72 | |
| 73 | // Called when the response is received. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 74 | void OnResponse(Response* response) { |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 75 | // |response| will be deleted on exit of the function. Copy the |
| 76 | // payload to |response_string_|. |
| 77 | if (response) { |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 78 | MessageReader reader(response); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 79 | ASSERT_TRUE(reader.PopString(&response_string_)); |
| 80 | } |
earthdok | 64401d7 | 2014-09-03 19:32:36 +0900 | [diff] [blame] | 81 | run_loop_->Quit(); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | protected: |
| 85 | std::string response_string_; |
xhwang@chromium.org | dff6b13 | 2013-05-02 01:10:30 +0900 | [diff] [blame] | 86 | base::MessageLoop message_loop_; |
dcheng | 30c5a17 | 2016-04-09 07:55:04 +0900 | [diff] [blame] | 87 | std::unique_ptr<base::RunLoop> run_loop_; |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 88 | scoped_refptr<MockBus> mock_bus_; |
| 89 | scoped_refptr<MockObjectProxy> mock_proxy_; |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 90 | |
| 91 | private: |
| 92 | // Returns a response for the given method call. Used to implement |
| 93 | // CallMethodAndBlock() for |mock_proxy_|. |
Hidehiko Abe | 8422130 | 2017-06-27 14:45:55 +0900 | [diff] [blame^] | 94 | std::unique_ptr<Response> CreateMockProxyResponse(MethodCall* method_call, |
| 95 | int timeout_ms) { |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 96 | if (method_call->GetInterface() == "org.chromium.TestInterface" && |
| 97 | method_call->GetMember() == "Echo") { |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 98 | MessageReader reader(method_call); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 99 | std::string text_message; |
| 100 | if (reader.PopString(&text_message)) { |
dcheng | 30c5a17 | 2016-04-09 07:55:04 +0900 | [diff] [blame] | 101 | std::unique_ptr<Response> response = Response::CreateEmpty(); |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 102 | MessageWriter writer(response.get()); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 103 | writer.AppendString(text_message); |
Hidehiko Abe | 8422130 | 2017-06-27 14:45:55 +0900 | [diff] [blame^] | 104 | return response; |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | LOG(ERROR) << "Unexpected method call: " << method_call->ToString(); |
Hidehiko Abe | 8422130 | 2017-06-27 14:45:55 +0900 | [diff] [blame^] | 109 | return nullptr; |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 110 | } |
| 111 | |
Hidehiko Abe | 8422130 | 2017-06-27 14:45:55 +0900 | [diff] [blame^] | 112 | std::unique_ptr<Response> CreateMockProxyResponseWithErrorDetails( |
avakulenko | 1d8962b | 2014-09-17 10:44:09 +0900 | [diff] [blame] | 113 | MethodCall* method_call, int timeout_ms, ScopedDBusError* error) { |
| 114 | dbus_set_error(error->get(), DBUS_ERROR_NOT_SUPPORTED, "Not implemented"); |
Hidehiko Abe | 8422130 | 2017-06-27 14:45:55 +0900 | [diff] [blame^] | 115 | return nullptr; |
avakulenko | 1d8962b | 2014-09-17 10:44:09 +0900 | [diff] [blame] | 116 | } |
| 117 | |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 118 | // 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.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 121 | MethodCall* method_call, |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 122 | int timeout_ms, |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 123 | ObjectProxy::ResponseCallback response_callback) { |
Hidehiko Abe | 8422130 | 2017-06-27 14:45:55 +0900 | [diff] [blame^] | 124 | std::unique_ptr<Response> response = |
| 125 | CreateMockProxyResponse(method_call, timeout_ms); |
fdoray | 851719c | 2016-08-26 00:36:37 +0900 | [diff] [blame] | 126 | message_loop_.task_runner()->PostTask( |
| 127 | FROM_HERE, |
| 128 | base::Bind(&MockTest::RunResponseCallback, base::Unretained(this), |
Hidehiko Abe | 8422130 | 2017-06-27 14:45:55 +0900 | [diff] [blame^] | 129 | response_callback, base::Passed(&response))); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | // Runs the given response callback with the given response. |
| 133 | void RunResponseCallback( |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 134 | ObjectProxy::ResponseCallback response_callback, |
Hidehiko Abe | 8422130 | 2017-06-27 14:45:55 +0900 | [diff] [blame^] | 135 | std::unique_ptr<Response> response) { |
| 136 | response_callback.Run(response.get()); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 137 | } |
| 138 | }; |
| 139 | |
avakulenko | 1d8962b | 2014-09-17 10:44:09 +0900 | [diff] [blame] | 140 | // This test demonstrates how to mock a synchronous method call using the |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 141 | // mock classes. |
| 142 | TEST_F(MockTest, CallMethodAndBlock) { |
| 143 | const char kHello[] = "Hello"; |
| 144 | // Get an object proxy from the mock bus. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 145 | ObjectProxy* proxy = mock_bus_->GetObjectProxy( |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 146 | "org.chromium.TestService", |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 147 | ObjectPath("/org/chromium/TestObject")); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 148 | |
| 149 | // Create a method call. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 150 | MethodCall method_call("org.chromium.TestInterface", "Echo"); |
| 151 | MessageWriter writer(&method_call); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 152 | writer.AppendString(kHello); |
| 153 | |
| 154 | // Call the method. |
dcheng | 30c5a17 | 2016-04-09 07:55:04 +0900 | [diff] [blame] | 155 | std::unique_ptr<Response> response(proxy->CallMethodAndBlock( |
| 156 | &method_call, ObjectProxy::TIMEOUT_USE_DEFAULT)); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 157 | |
| 158 | // Check the response. |
| 159 | ASSERT_TRUE(response.get()); |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 160 | MessageReader reader(response.get()); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 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 | |
avakulenko | 1d8962b | 2014-09-17 10:44:09 +0900 | [diff] [blame] | 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. |
dcheng | 30c5a17 | 2016-04-09 07:55:04 +0900 | [diff] [blame] | 178 | std::unique_ptr<Response> response(proxy->CallMethodAndBlockWithErrorDetails( |
| 179 | &method_call, ObjectProxy::TIMEOUT_USE_DEFAULT, &error)); |
avakulenko | 1d8962b | 2014-09-17 10:44:09 +0900 | [diff] [blame] | 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 |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 189 | // mock classes. |
| 190 | TEST_F(MockTest, CallMethod) { |
| 191 | const char kHello[] = "hello"; |
| 192 | |
| 193 | // Get an object proxy from the mock bus. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 194 | ObjectProxy* proxy = mock_bus_->GetObjectProxy( |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 195 | "org.chromium.TestService", |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 196 | ObjectPath("/org/chromium/TestObject")); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 197 | |
| 198 | // Create a method call. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 199 | MethodCall method_call("org.chromium.TestInterface", "Echo"); |
| 200 | MessageWriter writer(&method_call); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 201 | writer.AppendString(kHello); |
| 202 | |
| 203 | // Call the method. |
earthdok | 64401d7 | 2014-09-03 19:32:36 +0900 | [diff] [blame] | 204 | run_loop_.reset(new base::RunLoop); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 205 | proxy->CallMethod(&method_call, |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 206 | ObjectProxy::TIMEOUT_USE_DEFAULT, |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 207 | base::Bind(&MockTest::OnResponse, |
| 208 | base::Unretained(this))); |
| 209 | // Run the message loop to let OnResponse be called. |
earthdok | 64401d7 | 2014-09-03 19:32:36 +0900 | [diff] [blame] | 210 | run_loop_->Run(); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 211 | |
| 212 | EXPECT_EQ(kHello, response_string_); |
| 213 | } |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 214 | |
| 215 | } // namespace dbus |