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 | |
| 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.org | a29af56 | 2013-07-18 08:00:30 +0900 | [diff] [blame] | 9 | #include "base/message_loop/message_loop.h" |
earthdok | 64401d7 | 2014-09-03 19:32:36 +0900 | [diff] [blame] | 10 | #include "base/run_loop.h" |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 11 | #include "dbus/message.h" |
| 12 | #include "dbus/mock_bus.h" |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 13 | #include "dbus/mock_exported_object.h" |
avi@chromium.org | a29af56 | 2013-07-18 08:00:30 +0900 | [diff] [blame] | 14 | #include "dbus/mock_object_proxy.h" |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 15 | #include "dbus/object_path.h" |
avakulenko | 1d8962b | 2014-09-17 10:44:09 +0900 | [diff] [blame] | 16 | #include "dbus/scoped_dbus_error.h" |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 17 | #include "testing/gmock/include/gmock/gmock.h" |
| 18 | #include "testing/gtest/include/gtest/gtest.h" |
| 19 | |
| 20 | using ::testing::_; |
| 21 | using ::testing::Invoke; |
| 22 | using ::testing::Return; |
| 23 | using ::testing::Unused; |
| 24 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 25 | namespace dbus { |
| 26 | |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 27 | class MockTest : public testing::Test { |
| 28 | public: |
| 29 | MockTest() { |
| 30 | } |
| 31 | |
dcheng | 7f5750d | 2014-12-30 03:30:17 +0900 | [diff] [blame] | 32 | void SetUp() override { |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 33 | // Create a mock bus. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 34 | Bus::Options options; |
| 35 | options.bus_type = Bus::SYSTEM; |
| 36 | mock_bus_ = new MockBus(options); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 37 | |
| 38 | // Create a mock proxy. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 39 | mock_proxy_ = new MockObjectProxy( |
keybuk@google.com | bf4649a | 2012-02-15 06:29:06 +0900 | [diff] [blame] | 40 | mock_bus_.get(), |
| 41 | "org.chromium.TestService", |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 42 | ObjectPath("/org/chromium/TestObject")); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 43 | |
| 44 | // Set an expectation so mock_proxy's CallMethodAndBlock() will use |
| 45 | // CreateMockProxyResponse() to return responses. |
rsleevi@chromium.org | c5cb859 | 2013-06-03 08:38:09 +0900 | [diff] [blame] | 46 | EXPECT_CALL(*mock_proxy_.get(), MockCallMethodAndBlock(_, _)) |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 47 | .WillRepeatedly(Invoke(this, &MockTest::CreateMockProxyResponse)); |
avakulenko | 1d8962b | 2014-09-17 10:44:09 +0900 | [diff] [blame] | 48 | EXPECT_CALL(*mock_proxy_.get(), |
| 49 | MockCallMethodAndBlockWithErrorDetails(_, _, _)) |
| 50 | .WillRepeatedly( |
| 51 | Invoke(this, &MockTest::CreateMockProxyResponseWithErrorDetails)); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 52 | |
| 53 | // Set an expectation so mock_proxy's CallMethod() will use |
| 54 | // HandleMockProxyResponseWithMessageLoop() to return responses. |
rsleevi@chromium.org | c5cb859 | 2013-06-03 08:38:09 +0900 | [diff] [blame] | 55 | EXPECT_CALL(*mock_proxy_.get(), CallMethod(_, _, _)).WillRepeatedly( |
| 56 | Invoke(this, &MockTest::HandleMockProxyResponseWithMessageLoop)); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 57 | |
| 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.org | c5cb859 | 2013-06-03 08:38:09 +0900 | [diff] [blame] | 60 | EXPECT_CALL(*mock_bus_.get(), |
| 61 | GetObjectProxy("org.chromium.TestService", |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 62 | ObjectPath("/org/chromium/TestObject"))) |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 63 | .WillOnce(Return(mock_proxy_.get())); |
satorux@chromium.org | 326a6f8 | 2011-08-27 16:26:34 +0900 | [diff] [blame] | 64 | |
| 65 | // ShutdownAndBlock() will be called in TearDown(). |
rsleevi@chromium.org | c5cb859 | 2013-06-03 08:38:09 +0900 | [diff] [blame] | 66 | EXPECT_CALL(*mock_bus_.get(), ShutdownAndBlock()).WillOnce(Return()); |
satorux@chromium.org | 326a6f8 | 2011-08-27 16:26:34 +0900 | [diff] [blame] | 67 | } |
| 68 | |
dcheng | 7f5750d | 2014-12-30 03:30:17 +0900 | [diff] [blame] | 69 | void TearDown() override { mock_bus_->ShutdownAndBlock(); } |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 70 | |
| 71 | // Called when the response is received. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 72 | void OnResponse(Response* response) { |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 73 | // |response| will be deleted on exit of the function. Copy the |
| 74 | // payload to |response_string_|. |
| 75 | if (response) { |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 76 | MessageReader reader(response); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 77 | ASSERT_TRUE(reader.PopString(&response_string_)); |
| 78 | } |
earthdok | 64401d7 | 2014-09-03 19:32:36 +0900 | [diff] [blame] | 79 | run_loop_->Quit(); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | protected: |
| 83 | std::string response_string_; |
xhwang@chromium.org | dff6b13 | 2013-05-02 01:10:30 +0900 | [diff] [blame] | 84 | base::MessageLoop message_loop_; |
earthdok | 64401d7 | 2014-09-03 19:32:36 +0900 | [diff] [blame] | 85 | scoped_ptr<base::RunLoop> run_loop_; |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 86 | scoped_refptr<MockBus> mock_bus_; |
| 87 | scoped_refptr<MockObjectProxy> mock_proxy_; |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 88 | |
| 89 | private: |
| 90 | // Returns a response for the given method call. Used to implement |
| 91 | // CallMethodAndBlock() for |mock_proxy_|. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 92 | Response* CreateMockProxyResponse(MethodCall* method_call, |
| 93 | int timeout_ms) { |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 94 | if (method_call->GetInterface() == "org.chromium.TestInterface" && |
| 95 | method_call->GetMember() == "Echo") { |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 96 | MessageReader reader(method_call); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 97 | std::string text_message; |
| 98 | if (reader.PopString(&text_message)) { |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 99 | scoped_ptr<Response> response = Response::CreateEmpty(); |
| 100 | MessageWriter writer(response.get()); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 101 | writer.AppendString(text_message); |
yuki@chromium.org | d4eedf8 | 2013-02-07 18:46:24 +0900 | [diff] [blame] | 102 | return response.release(); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | LOG(ERROR) << "Unexpected method call: " << method_call->ToString(); |
| 107 | return NULL; |
| 108 | } |
| 109 | |
avakulenko | 1d8962b | 2014-09-17 10:44:09 +0900 | [diff] [blame] | 110 | 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.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 116 | // 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.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 119 | MethodCall* method_call, |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 120 | int timeout_ms, |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 121 | ObjectProxy::ResponseCallback response_callback) { |
| 122 | Response* response = CreateMockProxyResponse(method_call, timeout_ms); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 123 | 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.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 132 | ObjectProxy::ResponseCallback response_callback, |
| 133 | Response* response) { |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 134 | response_callback.Run(response); |
| 135 | delete response; |
| 136 | } |
| 137 | }; |
| 138 | |
avakulenko | 1d8962b | 2014-09-17 10:44:09 +0900 | [diff] [blame] | 139 | // 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] | 140 | // mock classes. |
| 141 | TEST_F(MockTest, CallMethodAndBlock) { |
| 142 | const char kHello[] = "Hello"; |
| 143 | // Get an object proxy from the mock bus. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 144 | ObjectProxy* proxy = mock_bus_->GetObjectProxy( |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 145 | "org.chromium.TestService", |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 146 | ObjectPath("/org/chromium/TestObject")); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 147 | |
| 148 | // Create a method call. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 149 | MethodCall method_call("org.chromium.TestInterface", "Echo"); |
| 150 | MessageWriter writer(&method_call); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 151 | writer.AppendString(kHello); |
| 152 | |
| 153 | // Call the method. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 154 | scoped_ptr<Response> response( |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 155 | proxy->CallMethodAndBlock(&method_call, |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 156 | 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. |
| 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.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 190 | // mock classes. |
| 191 | TEST_F(MockTest, CallMethod) { |
| 192 | const char kHello[] = "hello"; |
| 193 | |
| 194 | // Get an object proxy from the mock bus. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 195 | ObjectProxy* proxy = mock_bus_->GetObjectProxy( |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 196 | "org.chromium.TestService", |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 197 | ObjectPath("/org/chromium/TestObject")); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 198 | |
| 199 | // Create a method call. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 200 | MethodCall method_call("org.chromium.TestInterface", "Echo"); |
| 201 | MessageWriter writer(&method_call); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 202 | writer.AppendString(kHello); |
| 203 | |
| 204 | // Call the method. |
earthdok | 64401d7 | 2014-09-03 19:32:36 +0900 | [diff] [blame] | 205 | run_loop_.reset(new base::RunLoop); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 206 | proxy->CallMethod(&method_call, |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 207 | ObjectProxy::TIMEOUT_USE_DEFAULT, |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 208 | base::Bind(&MockTest::OnResponse, |
| 209 | base::Unretained(this))); |
| 210 | // Run the message loop to let OnResponse be called. |
earthdok | 64401d7 | 2014-09-03 19:32:36 +0900 | [diff] [blame] | 211 | run_loop_->Run(); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 212 | |
| 213 | EXPECT_EQ(kHello, response_string_); |
| 214 | } |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 215 | |
| 216 | } // namespace dbus |