| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/logging.h" | 6 #include "base/logging.h" |
| 7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "dbus/message.h" | 11 #include "dbus/message.h" |
| 12 #include "dbus/mock_bus.h" | 12 #include "dbus/mock_bus.h" |
| 13 #include "dbus/mock_exported_object.h" | 13 #include "dbus/mock_exported_object.h" |
| 14 #include "dbus/mock_object_proxy.h" | 14 #include "dbus/mock_object_proxy.h" |
| 15 #include "dbus/object_path.h" | 15 #include "dbus/object_path.h" |
| 16 #include "dbus/scoped_dbus_error.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| 19 using ::testing::_; | 20 using ::testing::_; |
| 20 using ::testing::Invoke; | 21 using ::testing::Invoke; |
| 21 using ::testing::Return; | 22 using ::testing::Return; |
| 22 using ::testing::Unused; | 23 using ::testing::Unused; |
| 23 | 24 |
| 24 namespace dbus { | 25 namespace dbus { |
| 25 | 26 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 37 // Create a mock proxy. | 38 // Create a mock proxy. |
| 38 mock_proxy_ = new MockObjectProxy( | 39 mock_proxy_ = new MockObjectProxy( |
| 39 mock_bus_.get(), | 40 mock_bus_.get(), |
| 40 "org.chromium.TestService", | 41 "org.chromium.TestService", |
| 41 ObjectPath("/org/chromium/TestObject")); | 42 ObjectPath("/org/chromium/TestObject")); |
| 42 | 43 |
| 43 // Set an expectation so mock_proxy's CallMethodAndBlock() will use | 44 // Set an expectation so mock_proxy's CallMethodAndBlock() will use |
| 44 // CreateMockProxyResponse() to return responses. | 45 // CreateMockProxyResponse() to return responses. |
| 45 EXPECT_CALL(*mock_proxy_.get(), MockCallMethodAndBlock(_, _)) | 46 EXPECT_CALL(*mock_proxy_.get(), MockCallMethodAndBlock(_, _)) |
| 46 .WillRepeatedly(Invoke(this, &MockTest::CreateMockProxyResponse)); | 47 .WillRepeatedly(Invoke(this, &MockTest::CreateMockProxyResponse)); |
| 48 EXPECT_CALL(*mock_proxy_.get(), |
| 49 MockCallMethodAndBlockWithErrorDetails(_, _, _)) |
| 50 .WillRepeatedly( |
| 51 Invoke(this, &MockTest::CreateMockProxyResponseWithErrorDetails)); |
| 47 | 52 |
| 48 // Set an expectation so mock_proxy's CallMethod() will use | 53 // Set an expectation so mock_proxy's CallMethod() will use |
| 49 // HandleMockProxyResponseWithMessageLoop() to return responses. | 54 // HandleMockProxyResponseWithMessageLoop() to return responses. |
| 50 EXPECT_CALL(*mock_proxy_.get(), CallMethod(_, _, _)).WillRepeatedly( | 55 EXPECT_CALL(*mock_proxy_.get(), CallMethod(_, _, _)).WillRepeatedly( |
| 51 Invoke(this, &MockTest::HandleMockProxyResponseWithMessageLoop)); | 56 Invoke(this, &MockTest::HandleMockProxyResponseWithMessageLoop)); |
| 52 | 57 |
| 53 // Set an expectation so mock_bus's GetObjectProxy() for the given | 58 // Set an expectation so mock_bus's GetObjectProxy() for the given |
| 54 // service name and the object path will return mock_proxy_. | 59 // service name and the object path will return mock_proxy_. |
| 55 EXPECT_CALL(*mock_bus_.get(), | 60 EXPECT_CALL(*mock_bus_.get(), |
| 56 GetObjectProxy("org.chromium.TestService", | 61 GetObjectProxy("org.chromium.TestService", |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 MessageWriter writer(response.get()); | 102 MessageWriter writer(response.get()); |
| 98 writer.AppendString(text_message); | 103 writer.AppendString(text_message); |
| 99 return response.release(); | 104 return response.release(); |
| 100 } | 105 } |
| 101 } | 106 } |
| 102 | 107 |
| 103 LOG(ERROR) << "Unexpected method call: " << method_call->ToString(); | 108 LOG(ERROR) << "Unexpected method call: " << method_call->ToString(); |
| 104 return NULL; | 109 return NULL; |
| 105 } | 110 } |
| 106 | 111 |
| 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 |
| 107 // Creates a response and runs the given response callback in the | 118 // Creates a response and runs the given response callback in the |
| 108 // message loop with the response. Used to implement for |mock_proxy_|. | 119 // message loop with the response. Used to implement for |mock_proxy_|. |
| 109 void HandleMockProxyResponseWithMessageLoop( | 120 void HandleMockProxyResponseWithMessageLoop( |
| 110 MethodCall* method_call, | 121 MethodCall* method_call, |
| 111 int timeout_ms, | 122 int timeout_ms, |
| 112 ObjectProxy::ResponseCallback response_callback) { | 123 ObjectProxy::ResponseCallback response_callback) { |
| 113 Response* response = CreateMockProxyResponse(method_call, timeout_ms); | 124 Response* response = CreateMockProxyResponse(method_call, timeout_ms); |
| 114 message_loop_.PostTask(FROM_HERE, | 125 message_loop_.PostTask(FROM_HERE, |
| 115 base::Bind(&MockTest::RunResponseCallback, | 126 base::Bind(&MockTest::RunResponseCallback, |
| 116 base::Unretained(this), | 127 base::Unretained(this), |
| 117 response_callback, | 128 response_callback, |
| 118 response)); | 129 response)); |
| 119 } | 130 } |
| 120 | 131 |
| 121 // Runs the given response callback with the given response. | 132 // Runs the given response callback with the given response. |
| 122 void RunResponseCallback( | 133 void RunResponseCallback( |
| 123 ObjectProxy::ResponseCallback response_callback, | 134 ObjectProxy::ResponseCallback response_callback, |
| 124 Response* response) { | 135 Response* response) { |
| 125 response_callback.Run(response); | 136 response_callback.Run(response); |
| 126 delete response; | 137 delete response; |
| 127 } | 138 } |
| 128 }; | 139 }; |
| 129 | 140 |
| 130 // This test demonstrates how to mock a synchronos method call using the | 141 // This test demonstrates how to mock a synchronous method call using the |
| 131 // mock classes. | 142 // mock classes. |
| 132 TEST_F(MockTest, CallMethodAndBlock) { | 143 TEST_F(MockTest, CallMethodAndBlock) { |
| 133 const char kHello[] = "Hello"; | 144 const char kHello[] = "Hello"; |
| 134 // Get an object proxy from the mock bus. | 145 // Get an object proxy from the mock bus. |
| 135 ObjectProxy* proxy = mock_bus_->GetObjectProxy( | 146 ObjectProxy* proxy = mock_bus_->GetObjectProxy( |
| 136 "org.chromium.TestService", | 147 "org.chromium.TestService", |
| 137 ObjectPath("/org/chromium/TestObject")); | 148 ObjectPath("/org/chromium/TestObject")); |
| 138 | 149 |
| 139 // Create a method call. | 150 // Create a method call. |
| 140 MethodCall method_call("org.chromium.TestInterface", "Echo"); | 151 MethodCall method_call("org.chromium.TestInterface", "Echo"); |
| 141 MessageWriter writer(&method_call); | 152 MessageWriter writer(&method_call); |
| 142 writer.AppendString(kHello); | 153 writer.AppendString(kHello); |
| 143 | 154 |
| 144 // Call the method. | 155 // Call the method. |
| 145 scoped_ptr<Response> response( | 156 scoped_ptr<Response> response( |
| 146 proxy->CallMethodAndBlock(&method_call, | 157 proxy->CallMethodAndBlock(&method_call, |
| 147 ObjectProxy::TIMEOUT_USE_DEFAULT)); | 158 ObjectProxy::TIMEOUT_USE_DEFAULT)); |
| 148 | 159 |
| 149 // Check the response. | 160 // Check the response. |
| 150 ASSERT_TRUE(response.get()); | 161 ASSERT_TRUE(response.get()); |
| 151 MessageReader reader(response.get()); | 162 MessageReader reader(response.get()); |
| 152 std::string text_message; | 163 std::string text_message; |
| 153 ASSERT_TRUE(reader.PopString(&text_message)); | 164 ASSERT_TRUE(reader.PopString(&text_message)); |
| 154 // The text message should be echo'ed back. | 165 // The text message should be echo'ed back. |
| 155 EXPECT_EQ(kHello, text_message); | 166 EXPECT_EQ(kHello, text_message); |
| 156 } | 167 } |
| 157 | 168 |
| 158 // This test demonstrates how to mock an asynchronos method call using the | 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 |
| 159 // mock classes. | 192 // mock classes. |
| 160 TEST_F(MockTest, CallMethod) { | 193 TEST_F(MockTest, CallMethod) { |
| 161 const char kHello[] = "hello"; | 194 const char kHello[] = "hello"; |
| 162 | 195 |
| 163 // Get an object proxy from the mock bus. | 196 // Get an object proxy from the mock bus. |
| 164 ObjectProxy* proxy = mock_bus_->GetObjectProxy( | 197 ObjectProxy* proxy = mock_bus_->GetObjectProxy( |
| 165 "org.chromium.TestService", | 198 "org.chromium.TestService", |
| 166 ObjectPath("/org/chromium/TestObject")); | 199 ObjectPath("/org/chromium/TestObject")); |
| 167 | 200 |
| 168 // Create a method call. | 201 // Create a method call. |
| 169 MethodCall method_call("org.chromium.TestInterface", "Echo"); | 202 MethodCall method_call("org.chromium.TestInterface", "Echo"); |
| 170 MessageWriter writer(&method_call); | 203 MessageWriter writer(&method_call); |
| 171 writer.AppendString(kHello); | 204 writer.AppendString(kHello); |
| 172 | 205 |
| 173 // Call the method. | 206 // Call the method. |
| 174 run_loop_.reset(new base::RunLoop); | 207 run_loop_.reset(new base::RunLoop); |
| 175 proxy->CallMethod(&method_call, | 208 proxy->CallMethod(&method_call, |
| 176 ObjectProxy::TIMEOUT_USE_DEFAULT, | 209 ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 177 base::Bind(&MockTest::OnResponse, | 210 base::Bind(&MockTest::OnResponse, |
| 178 base::Unretained(this))); | 211 base::Unretained(this))); |
| 179 // Run the message loop to let OnResponse be called. | 212 // Run the message loop to let OnResponse be called. |
| 180 run_loop_->Run(); | 213 run_loop_->Run(); |
| 181 | 214 |
| 182 EXPECT_EQ(kHello, response_string_); | 215 EXPECT_EQ(kHello, response_string_); |
| 183 } | 216 } |
| 184 | 217 |
| 185 } // namespace dbus | 218 } // namespace dbus |
| OLD | NEW |