Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Side by Side Diff: chrome/browser/chromeos/dbus/proxy_resolution_service_provider_unittest.cc

Issue 8728020: chrome: dbus: support asynchronous method replies (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix comment Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/chromeos/dbus/proxy_resolution_service_provider.cc ('k') | dbus/bus.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 // This test is relatively complicated. Here's the summary of what it does: 5 // This test is relatively complicated. Here's the summary of what it does:
6 // 6 //
7 // - Set up mock D-Bus related objects to mock out D-Bus calls. 7 // - Set up mock D-Bus related objects to mock out D-Bus calls.
8 // - Set up a mock proxy resolver to mock out the proxy resolution. 8 // - Set up a mock proxy resolver to mock out the proxy resolution.
9 // - Create ProxyResolutionServiceProvider by injecting the mocks 9 // - Create ProxyResolutionServiceProvider by injecting the mocks
10 // - Start the service provider. 10 // - Start the service provider.
11 // - Request ProxyResolutionServiceProvider to resolve proxy for kSourceURL. 11 // - Request ProxyResolutionServiceProvider to resolve proxy for kSourceURL.
12 // - ProxyResolutionServiceProvider will return the result as a signal. 12 // - ProxyResolutionServiceProvider will return the result as a signal.
13 // - Confirm that we receive the signal and check the contents of the signal. 13 // - Confirm that we receive the signal and check the contents of the signal.
14 14
15 #include "chrome/browser/chromeos/dbus/proxy_resolution_service_provider.h" 15 #include "chrome/browser/chromeos/dbus/proxy_resolution_service_provider.h"
16 16
17 #include "base/bind.h" 17 #include "base/bind.h"
18 #include "base/logging.h" 18 #include "base/logging.h"
19 #include "base/memory/ref_counted.h" 19 #include "base/memory/ref_counted.h"
20 #include "base/message_loop.h"
20 #include "dbus/message.h" 21 #include "dbus/message.h"
21 #include "dbus/mock_bus.h" 22 #include "dbus/mock_bus.h"
22 #include "dbus/mock_exported_object.h" 23 #include "dbus/mock_exported_object.h"
23 #include "dbus/mock_object_proxy.h" 24 #include "dbus/mock_object_proxy.h"
24 #include "testing/gmock/include/gmock/gmock.h" 25 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
26 #include "third_party/cros_system_api/dbus/service_constants.h" 27 #include "third_party/cros_system_api/dbus/service_constants.h"
27 28
28 using ::testing::_; 29 using ::testing::_;
29 using ::testing::Invoke; 30 using ::testing::Invoke;
(...skipping 24 matching lines...) Expand all
54 void(const std::string& source_url, 55 void(const std::string& source_url,
55 const std::string& signal_interface, 56 const std::string& signal_interface,
56 const std::string& signal_name, 57 const std::string& signal_name,
57 scoped_refptr<dbus::ExportedObject> exported_object)); 58 scoped_refptr<dbus::ExportedObject> exported_object));
58 }; 59 };
59 60
60 class ProxyResolutionServiceProviderTest : public testing::Test { 61 class ProxyResolutionServiceProviderTest : public testing::Test {
61 public: 62 public:
62 ProxyResolutionServiceProviderTest() 63 ProxyResolutionServiceProviderTest()
63 : signal_received_successfully_(false), 64 : signal_received_successfully_(false),
64 mock_resolver_(NULL) { 65 mock_resolver_(NULL),
66 response_received_(false) {
65 } 67 }
66 68
67 virtual void SetUp() { 69 virtual void SetUp() {
68 // Create a mock bus. 70 // Create a mock bus.
69 dbus::Bus::Options options; 71 dbus::Bus::Options options;
70 options.bus_type = dbus::Bus::SYSTEM; 72 options.bus_type = dbus::Bus::SYSTEM;
71 mock_bus_ = new dbus::MockBus(options); 73 mock_bus_ = new dbus::MockBus(options);
72 74
73 // ShutdownAndBlock() will be called in TearDown(). 75 // ShutdownAndBlock() will be called in TearDown().
74 EXPECT_CALL(*mock_bus_, ShutdownAndBlock()).WillOnce(Return()); 76 EXPECT_CALL(*mock_bus_, ShutdownAndBlock()).WillOnce(Return());
(...skipping 20 matching lines...) Expand all
95 Invoke(this, 97 Invoke(this,
96 &ProxyResolutionServiceProviderTest::MockSendSignal)); 98 &ProxyResolutionServiceProviderTest::MockSendSignal));
97 99
98 // Create a mock object proxy, with which we call a method of 100 // Create a mock object proxy, with which we call a method of
99 // |mock_exported_object_|. 101 // |mock_exported_object_|.
100 mock_object_proxy_ = 102 mock_object_proxy_ =
101 new dbus::MockObjectProxy(mock_bus_.get(), 103 new dbus::MockObjectProxy(mock_bus_.get(),
102 kLibCrosServiceName, 104 kLibCrosServiceName,
103 kLibCrosServicePath); 105 kLibCrosServicePath);
104 // |mock_object_proxy_|'s CallMethodAndBlock() will use 106 // |mock_object_proxy_|'s CallMethodAndBlock() will use
105 // CreateResponse() to return responses. 107 // MockCallMethodAndBlock() to return responses.
106 EXPECT_CALL(*mock_object_proxy_, 108 EXPECT_CALL(*mock_object_proxy_,
107 CallMethodAndBlock(_, _)) 109 CallMethodAndBlock(_, _))
108 .WillOnce(Invoke( 110 .WillOnce(Invoke(
109 this, 111 this,
110 &ProxyResolutionServiceProviderTest::CreateResponse)); 112 &ProxyResolutionServiceProviderTest::MockCallMethodAndBlock));
111 // |mock_object_proxy_|'s ConnectToSignal will use 113 // |mock_object_proxy_|'s ConnectToSignal will use
112 // MockConnectToSignal(). 114 // MockConnectToSignal().
113 EXPECT_CALL(*mock_object_proxy_, 115 EXPECT_CALL(*mock_object_proxy_,
114 ConnectToSignal(kReturnSignalInterface, 116 ConnectToSignal(kReturnSignalInterface,
115 kReturnSignalName, 117 kReturnSignalName,
116 _, _)) 118 _, _))
117 .WillOnce(Invoke( 119 .WillOnce(Invoke(
118 this, 120 this,
119 &ProxyResolutionServiceProviderTest::MockConnectToSignal)); 121 &ProxyResolutionServiceProviderTest::MockConnectToSignal));
120 122
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 180
179 protected: 181 protected:
180 bool signal_received_successfully_; 182 bool signal_received_successfully_;
181 MockProxyResolver* mock_resolver_; 183 MockProxyResolver* mock_resolver_;
182 scoped_refptr<dbus::MockBus> mock_bus_; 184 scoped_refptr<dbus::MockBus> mock_bus_;
183 scoped_refptr<dbus::MockExportedObject> mock_exported_object_; 185 scoped_refptr<dbus::MockExportedObject> mock_exported_object_;
184 scoped_refptr<dbus::MockObjectProxy> mock_object_proxy_; 186 scoped_refptr<dbus::MockObjectProxy> mock_object_proxy_;
185 scoped_ptr<ProxyResolutionServiceProvider> proxy_resolution_service_; 187 scoped_ptr<ProxyResolutionServiceProvider> proxy_resolution_service_;
186 dbus::ExportedObject::MethodCallCallback resolve_network_proxy_; 188 dbus::ExportedObject::MethodCallCallback resolve_network_proxy_;
187 dbus::ObjectProxy::SignalCallback on_signal_callback_; 189 dbus::ObjectProxy::SignalCallback on_signal_callback_;
190 MessageLoop message_loop_;
191 bool response_received_;
192 scoped_ptr<dbus::Response> response_;
188 193
189 private: 194 private:
190 // Behaves as |mock_exported_object_|'s ExportMethod(). 195 // Behaves as |mock_exported_object_|'s ExportMethod().
191 void MockExportMethod( 196 void MockExportMethod(
192 const std::string& interface_name, 197 const std::string& interface_name,
193 const std::string& method_name, 198 const std::string& method_name,
194 dbus::ExportedObject::MethodCallCallback method_callback, 199 dbus::ExportedObject::MethodCallCallback method_callback,
195 dbus::ExportedObject::OnExportedCallback on_exported_callback) { 200 dbus::ExportedObject::OnExportedCallback on_exported_callback) {
196 if (interface_name == kLibCrosServiceInterface && 201 if (interface_name == kLibCrosServiceInterface &&
197 method_name == kResolveNetworkProxy) { 202 method_name == kResolveNetworkProxy) {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 writer.AppendString(kReturnEmptyErrorMessage); 235 writer.AppendString(kReturnEmptyErrorMessage);
231 // Send the signal back to the requested signal interface and the 236 // Send the signal back to the requested signal interface and the
232 // signal name. 237 // signal name.
233 exported_object->SendSignal(&signal); 238 exported_object->SendSignal(&signal);
234 return; 239 return;
235 } 240 }
236 241
237 LOG(ERROR) << "Unexpected source URL: " << source_url; 242 LOG(ERROR) << "Unexpected source URL: " << source_url;
238 } 243 }
239 244
240 // Creates a response for |mock_object_proxy_|. 245 // Calls exported method and waits for a response for |mock_object_proxy_|.
241 dbus::Response* CreateResponse( 246 dbus::Response* MockCallMethodAndBlock(
242 dbus::MethodCall* method_call, 247 dbus::MethodCall* method_call,
243 Unused) { 248 Unused) {
244 if (method_call->GetInterface() == 249 if (method_call->GetInterface() != kLibCrosServiceInterface ||
245 kLibCrosServiceInterface && 250 method_call->GetMember() != kResolveNetworkProxy) {
246 method_call->GetMember() == kResolveNetworkProxy) { 251 LOG(ERROR) << "Unexpected method call: " << method_call->ToString();
247 // Set the serial number to non-zero, so 252 return NULL;
248 // dbus_message_new_method_return() won't emit a warning.
249 method_call->SetSerial(1);
250 // Run the callback captured in MockExportMethod(). This will send
251 // a signal, which will be received by |on_signal_callback_|.
252 dbus::Response* response = resolve_network_proxy_.Run(method_call);
253 return response;
254 } 253 }
254 // Set the serial number to non-zero, so
255 // dbus_message_new_method_return() won't emit a warning.
256 method_call->SetSerial(1);
257 // Run the callback captured in MockExportMethod(). In addition to returning
258 // a response that the caller will ignore, this will send a signal, which
259 // will be received by |on_signal_callback_|.
260 resolve_network_proxy_.Run(
261 method_call,
262 base::Bind(&ProxyResolutionServiceProviderTest::OnResponse,
263 base::Unretained(this)));
264 // Wait for a response.
265 while (!response_received_) {
266 message_loop_.Run();
Paweł Hajdan Jr. 2011/11/30 08:46:11 This is generally an anti-pattern. Could you only
Vince Laviano 2011/11/30 23:47:48 http://codereview.chromium.org/8764006/
267 }
268 // Return response.
269 return response_.release();
270 }
255 271
256 LOG(ERROR) << "Unexpected method call: " << method_call->ToString(); 272 // Receives a response and makes it available to MockCallMethodAndBlock().
257 return NULL; 273 void OnResponse(dbus::Response* response) {
274 response_.reset(response);
275 response_received_ = true;
276 if (message_loop_.is_running()) {
277 message_loop_.Quit();
278 }
258 } 279 }
259 280
260 // Behaves as |mock_object_proxy_|'s ConnectToSignal(). 281 // Behaves as |mock_object_proxy_|'s ConnectToSignal().
261 void MockConnectToSignal( 282 void MockConnectToSignal(
262 const std::string& interface_name, 283 const std::string& interface_name,
263 const std::string& signal_name, 284 const std::string& signal_name,
264 dbus::ObjectProxy::SignalCallback signal_callback, 285 dbus::ObjectProxy::SignalCallback signal_callback,
265 dbus::ObjectProxy::OnConnectedCallback connected_callback) { 286 dbus::ObjectProxy::OnConnectedCallback connected_callback) {
266 // Tell the callback that the object proxy is connected to the signal. 287 // Tell the callback that the object proxy is connected to the signal.
267 connected_callback.Run(interface_name, signal_name, true); 288 connected_callback.Run(interface_name, signal_name, true);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 ASSERT_TRUE(response.get()); 323 ASSERT_TRUE(response.get());
303 dbus::MessageReader reader(response.get()); 324 dbus::MessageReader reader(response.get());
304 ASSERT_FALSE(reader.HasMoreData()); 325 ASSERT_FALSE(reader.HasMoreData());
305 326
306 // Confirm that the signal is received successfully. 327 // Confirm that the signal is received successfully.
307 // The contents of the signal are checked in OnSignalReceived(). 328 // The contents of the signal are checked in OnSignalReceived().
308 ASSERT_TRUE(signal_received_successfully_); 329 ASSERT_TRUE(signal_received_successfully_);
309 } 330 }
310 331
311 } // namespace chromeos 332 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/dbus/proxy_resolution_service_provider.cc ('k') | dbus/bus.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698