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 | |
satorux@chromium.org | 326a6f8 | 2011-08-27 16:26:34 +0900 | [diff] [blame] | 32 | virtual void SetUp() { |
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 | |
| 69 | virtual void TearDown() { |
| 70 | mock_bus_->ShutdownAndBlock(); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 71 | } |
| 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_; |
earthdok | 64401d7 | 2014-09-03 19:32:36 +0900 | [diff] [blame] | 87 | scoped_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_|. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 94 | 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)) { |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 101 | scoped_ptr<Response> response = Response::CreateEmpty(); |
| 102 | MessageWriter writer(response.get()); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 103 | writer.AppendString(text_message); |
yuki@chromium.org | d4eedf8 | 2013-02-07 18:46:24 +0900 | [diff] [blame] | 104 | return response.release(); |
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(); |
| 109 | return NULL; |
| 110 | } |
| 111 | |
avakulenko | 1d8962b | 2014-09-17 10:44:09 +0900 | [diff] [blame] | 112 | 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 NULL; |
| 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) { |
| 124 | Response* response = CreateMockProxyResponse(method_call, timeout_ms); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 125 | message_loop_.PostTask(FROM_HERE, |
| 126 | base::Bind(&MockTest::RunResponseCallback, |
| 127 | base::Unretained(this), |
| 128 | response_callback, |
| 129 | response)); |
| 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, |
| 135 | Response* response) { |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 136 | response_callback.Run(response); |
| 137 | delete response; |
| 138 | } |
| 139 | }; |
| 140 | |
avakulenko | 1d8962b | 2014-09-17 10:44:09 +0900 | [diff] [blame] | 141 | // 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] | 142 | // mock classes. |
| 143 | TEST_F(MockTest, CallMethodAndBlock) { |
| 144 | const char kHello[] = "Hello"; |
| 145 | // Get an object proxy from the mock bus. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 146 | ObjectProxy* proxy = mock_bus_->GetObjectProxy( |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 147 | "org.chromium.TestService", |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 148 | ObjectPath("/org/chromium/TestObject")); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 149 | |
| 150 | // Create a method call. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 151 | MethodCall method_call("org.chromium.TestInterface", "Echo"); |
| 152 | MessageWriter writer(&method_call); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 153 | writer.AppendString(kHello); |
| 154 | |
| 155 | // Call the method. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 156 | scoped_ptr<Response> response( |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 157 | proxy->CallMethodAndBlock(&method_call, |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 158 | ObjectProxy::TIMEOUT_USE_DEFAULT)); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 159 | |
| 160 | // Check the response. |
| 161 | ASSERT_TRUE(response.get()); |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 162 | MessageReader reader(response.get()); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 163 | std::string text_message; |
| 164 | ASSERT_TRUE(reader.PopString(&text_message)); |
| 165 | // The text message should be echo'ed back. |
| 166 | EXPECT_EQ(kHello, text_message); |
| 167 | } |
| 168 | |
avakulenko | 1d8962b | 2014-09-17 10:44:09 +0900 | [diff] [blame] | 169 | TEST_F(MockTest, CallMethodAndBlockWithErrorDetails) { |
| 170 | // Get an object proxy from the mock bus. |
| 171 | ObjectProxy* proxy = mock_bus_->GetObjectProxy( |
| 172 | "org.chromium.TestService", |
| 173 | ObjectPath("/org/chromium/TestObject")); |
| 174 | |
| 175 | // Create a method call. |
| 176 | MethodCall method_call("org.chromium.TestInterface", "Echo"); |
| 177 | |
| 178 | ScopedDBusError error; |
| 179 | // Call the method. |
| 180 | scoped_ptr<Response> response( |
| 181 | proxy->CallMethodAndBlockWithErrorDetails( |
| 182 | &method_call, ObjectProxy::TIMEOUT_USE_DEFAULT, &error)); |
| 183 | |
| 184 | // Check the response. |
| 185 | ASSERT_FALSE(response.get()); |
| 186 | ASSERT_TRUE(error.is_set()); |
| 187 | EXPECT_STREQ(DBUS_ERROR_NOT_SUPPORTED, error.name()); |
| 188 | EXPECT_STREQ("Not implemented", error.message()); |
| 189 | } |
| 190 | |
| 191 | // 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] | 192 | // mock classes. |
| 193 | TEST_F(MockTest, CallMethod) { |
| 194 | const char kHello[] = "hello"; |
| 195 | |
| 196 | // Get an object proxy from the mock bus. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 197 | ObjectProxy* proxy = mock_bus_->GetObjectProxy( |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 198 | "org.chromium.TestService", |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 199 | ObjectPath("/org/chromium/TestObject")); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 200 | |
| 201 | // Create a method call. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 202 | MethodCall method_call("org.chromium.TestInterface", "Echo"); |
| 203 | MessageWriter writer(&method_call); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 204 | writer.AppendString(kHello); |
| 205 | |
| 206 | // Call the method. |
earthdok | 64401d7 | 2014-09-03 19:32:36 +0900 | [diff] [blame] | 207 | run_loop_.reset(new base::RunLoop); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 208 | proxy->CallMethod(&method_call, |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 209 | ObjectProxy::TIMEOUT_USE_DEFAULT, |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 210 | base::Bind(&MockTest::OnResponse, |
| 211 | base::Unretained(this))); |
| 212 | // Run the message loop to let OnResponse be called. |
earthdok | 64401d7 | 2014-09-03 19:32:36 +0900 | [diff] [blame] | 213 | run_loop_->Run(); |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 214 | |
| 215 | EXPECT_EQ(kHello, response_string_); |
| 216 | } |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 217 | |
| 218 | } // namespace dbus |