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 <memory> |
| 6 |
5 #include "base/bind.h" | 7 #include "base/bind.h" |
6 #include "base/logging.h" | 8 #include "base/logging.h" |
7 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
10 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
11 #include "dbus/message.h" | 12 #include "dbus/message.h" |
12 #include "dbus/mock_bus.h" | 13 #include "dbus/mock_bus.h" |
13 #include "dbus/mock_exported_object.h" | 14 #include "dbus/mock_exported_object.h" |
14 #include "dbus/mock_object_proxy.h" | 15 #include "dbus/mock_object_proxy.h" |
15 #include "dbus/object_path.h" | 16 #include "dbus/object_path.h" |
16 #include "dbus/scoped_dbus_error.h" | 17 #include "dbus/scoped_dbus_error.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 if (response) { | 76 if (response) { |
76 MessageReader reader(response); | 77 MessageReader reader(response); |
77 ASSERT_TRUE(reader.PopString(&response_string_)); | 78 ASSERT_TRUE(reader.PopString(&response_string_)); |
78 } | 79 } |
79 run_loop_->Quit(); | 80 run_loop_->Quit(); |
80 }; | 81 }; |
81 | 82 |
82 protected: | 83 protected: |
83 std::string response_string_; | 84 std::string response_string_; |
84 base::MessageLoop message_loop_; | 85 base::MessageLoop message_loop_; |
85 scoped_ptr<base::RunLoop> run_loop_; | 86 std::unique_ptr<base::RunLoop> run_loop_; |
86 scoped_refptr<MockBus> mock_bus_; | 87 scoped_refptr<MockBus> mock_bus_; |
87 scoped_refptr<MockObjectProxy> mock_proxy_; | 88 scoped_refptr<MockObjectProxy> mock_proxy_; |
88 | 89 |
89 private: | 90 private: |
90 // Returns a response for the given method call. Used to implement | 91 // Returns a response for the given method call. Used to implement |
91 // CallMethodAndBlock() for |mock_proxy_|. | 92 // CallMethodAndBlock() for |mock_proxy_|. |
92 Response* CreateMockProxyResponse(MethodCall* method_call, | 93 Response* CreateMockProxyResponse(MethodCall* method_call, |
93 int timeout_ms) { | 94 int timeout_ms) { |
94 if (method_call->GetInterface() == "org.chromium.TestInterface" && | 95 if (method_call->GetInterface() == "org.chromium.TestInterface" && |
95 method_call->GetMember() == "Echo") { | 96 method_call->GetMember() == "Echo") { |
96 MessageReader reader(method_call); | 97 MessageReader reader(method_call); |
97 std::string text_message; | 98 std::string text_message; |
98 if (reader.PopString(&text_message)) { | 99 if (reader.PopString(&text_message)) { |
99 scoped_ptr<Response> response = Response::CreateEmpty(); | 100 std::unique_ptr<Response> response = Response::CreateEmpty(); |
100 MessageWriter writer(response.get()); | 101 MessageWriter writer(response.get()); |
101 writer.AppendString(text_message); | 102 writer.AppendString(text_message); |
102 return response.release(); | 103 return response.release(); |
103 } | 104 } |
104 } | 105 } |
105 | 106 |
106 LOG(ERROR) << "Unexpected method call: " << method_call->ToString(); | 107 LOG(ERROR) << "Unexpected method call: " << method_call->ToString(); |
107 return NULL; | 108 return NULL; |
108 } | 109 } |
109 | 110 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 ObjectProxy* proxy = mock_bus_->GetObjectProxy( | 145 ObjectProxy* proxy = mock_bus_->GetObjectProxy( |
145 "org.chromium.TestService", | 146 "org.chromium.TestService", |
146 ObjectPath("/org/chromium/TestObject")); | 147 ObjectPath("/org/chromium/TestObject")); |
147 | 148 |
148 // Create a method call. | 149 // Create a method call. |
149 MethodCall method_call("org.chromium.TestInterface", "Echo"); | 150 MethodCall method_call("org.chromium.TestInterface", "Echo"); |
150 MessageWriter writer(&method_call); | 151 MessageWriter writer(&method_call); |
151 writer.AppendString(kHello); | 152 writer.AppendString(kHello); |
152 | 153 |
153 // Call the method. | 154 // Call the method. |
154 scoped_ptr<Response> response( | 155 std::unique_ptr<Response> response(proxy->CallMethodAndBlock( |
155 proxy->CallMethodAndBlock(&method_call, | 156 &method_call, ObjectProxy::TIMEOUT_USE_DEFAULT)); |
156 ObjectProxy::TIMEOUT_USE_DEFAULT)); | |
157 | 157 |
158 // Check the response. | 158 // Check the response. |
159 ASSERT_TRUE(response.get()); | 159 ASSERT_TRUE(response.get()); |
160 MessageReader reader(response.get()); | 160 MessageReader reader(response.get()); |
161 std::string text_message; | 161 std::string text_message; |
162 ASSERT_TRUE(reader.PopString(&text_message)); | 162 ASSERT_TRUE(reader.PopString(&text_message)); |
163 // The text message should be echo'ed back. | 163 // The text message should be echo'ed back. |
164 EXPECT_EQ(kHello, text_message); | 164 EXPECT_EQ(kHello, text_message); |
165 } | 165 } |
166 | 166 |
167 TEST_F(MockTest, CallMethodAndBlockWithErrorDetails) { | 167 TEST_F(MockTest, CallMethodAndBlockWithErrorDetails) { |
168 // Get an object proxy from the mock bus. | 168 // Get an object proxy from the mock bus. |
169 ObjectProxy* proxy = mock_bus_->GetObjectProxy( | 169 ObjectProxy* proxy = mock_bus_->GetObjectProxy( |
170 "org.chromium.TestService", | 170 "org.chromium.TestService", |
171 ObjectPath("/org/chromium/TestObject")); | 171 ObjectPath("/org/chromium/TestObject")); |
172 | 172 |
173 // Create a method call. | 173 // Create a method call. |
174 MethodCall method_call("org.chromium.TestInterface", "Echo"); | 174 MethodCall method_call("org.chromium.TestInterface", "Echo"); |
175 | 175 |
176 ScopedDBusError error; | 176 ScopedDBusError error; |
177 // Call the method. | 177 // Call the method. |
178 scoped_ptr<Response> response( | 178 std::unique_ptr<Response> response(proxy->CallMethodAndBlockWithErrorDetails( |
179 proxy->CallMethodAndBlockWithErrorDetails( | 179 &method_call, ObjectProxy::TIMEOUT_USE_DEFAULT, &error)); |
180 &method_call, ObjectProxy::TIMEOUT_USE_DEFAULT, &error)); | |
181 | 180 |
182 // Check the response. | 181 // Check the response. |
183 ASSERT_FALSE(response.get()); | 182 ASSERT_FALSE(response.get()); |
184 ASSERT_TRUE(error.is_set()); | 183 ASSERT_TRUE(error.is_set()); |
185 EXPECT_STREQ(DBUS_ERROR_NOT_SUPPORTED, error.name()); | 184 EXPECT_STREQ(DBUS_ERROR_NOT_SUPPORTED, error.name()); |
186 EXPECT_STREQ("Not implemented", error.message()); | 185 EXPECT_STREQ("Not implemented", error.message()); |
187 } | 186 } |
188 | 187 |
189 // This test demonstrates how to mock an asynchronous method call using the | 188 // This test demonstrates how to mock an asynchronous method call using the |
190 // mock classes. | 189 // mock classes. |
(...skipping 16 matching lines...) Expand all Loading... |
207 ObjectProxy::TIMEOUT_USE_DEFAULT, | 206 ObjectProxy::TIMEOUT_USE_DEFAULT, |
208 base::Bind(&MockTest::OnResponse, | 207 base::Bind(&MockTest::OnResponse, |
209 base::Unretained(this))); | 208 base::Unretained(this))); |
210 // Run the message loop to let OnResponse be called. | 209 // Run the message loop to let OnResponse be called. |
211 run_loop_->Run(); | 210 run_loop_->Run(); |
212 | 211 |
213 EXPECT_EQ(kHello, response_string_); | 212 EXPECT_EQ(kHello, response_string_); |
214 } | 213 } |
215 | 214 |
216 } // namespace dbus | 215 } // namespace dbus |
OLD | NEW |