Updating trunk VERSION from 1129.0 to 1130.0
[chromium-blink-merge.git] / dbus / end_to_end_async_unittest.cc
bloba4438e4ae521cf7834b6d66580da88a9bd3c5599
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
3 // found in the LICENSE file.
5 #include <algorithm>
6 #include <string>
7 #include <vector>
9 #include "base/bind.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop.h"
12 #include "base/stl_util.h"
13 #include "base/test/test_timeouts.h"
14 #include "base/threading/thread.h"
15 #include "base/threading/thread_restrictions.h"
16 #include "dbus/bus.h"
17 #include "dbus/message.h"
18 #include "dbus/object_path.h"
19 #include "dbus/object_proxy.h"
20 #include "dbus/test_service.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 // The end-to-end test exercises the asynchronous APIs in ObjectProxy and
24 // ExportedObject.
25 class EndToEndAsyncTest : public testing::Test {
26 public:
27 EndToEndAsyncTest() {
30 virtual void SetUp() {
31 // Make the main thread not to allow IO.
32 base::ThreadRestrictions::SetIOAllowed(false);
34 // Start the D-Bus thread.
35 dbus_thread_.reset(new base::Thread("D-Bus Thread"));
36 base::Thread::Options thread_options;
37 thread_options.message_loop_type = MessageLoop::TYPE_IO;
38 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options));
40 // Start the test service, using the D-Bus thread.
41 dbus::TestService::Options options;
42 options.dbus_thread_message_loop_proxy = dbus_thread_->message_loop_proxy();
43 test_service_.reset(new dbus::TestService(options));
44 ASSERT_TRUE(test_service_->StartService());
45 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted());
46 ASSERT_TRUE(test_service_->HasDBusThread());
48 // Create the client, using the D-Bus thread.
49 dbus::Bus::Options bus_options;
50 bus_options.bus_type = dbus::Bus::SESSION;
51 bus_options.connection_type = dbus::Bus::PRIVATE;
52 bus_options.dbus_thread_message_loop_proxy =
53 dbus_thread_->message_loop_proxy();
54 bus_ = new dbus::Bus(bus_options);
55 object_proxy_ = bus_->GetObjectProxy(
56 "org.chromium.TestService",
57 dbus::ObjectPath("/org/chromium/TestObject"));
58 ASSERT_TRUE(bus_->HasDBusThread());
60 // Connect to the "Test" signal of "org.chromium.TestInterface" from
61 // the remote object.
62 object_proxy_->ConnectToSignal(
63 "org.chromium.TestInterface",
64 "Test",
65 base::Bind(&EndToEndAsyncTest::OnTestSignal,
66 base::Unretained(this)),
67 base::Bind(&EndToEndAsyncTest::OnConnected,
68 base::Unretained(this)));
69 // Wait until the object proxy is connected to the signal.
70 message_loop_.Run();
72 // Connect to the "Test2" signal of "org.chromium.TestInterface" from
73 // the remote object. There was a bug where we were emitting error
74 // messages like "Requested to remove an unknown match rule: ..." at
75 // the shutdown of Bus when an object proxy is connected to more than
76 // one signal of the same interface. See crosbug.com/23382 for details.
77 object_proxy_->ConnectToSignal(
78 "org.chromium.TestInterface",
79 "Test2",
80 base::Bind(&EndToEndAsyncTest::OnTest2Signal,
81 base::Unretained(this)),
82 base::Bind(&EndToEndAsyncTest::OnConnected,
83 base::Unretained(this)));
84 // Wait until the object proxy is connected to the signal.
85 message_loop_.Run();
87 // Create a second object proxy for the root object.
88 root_object_proxy_ = bus_->GetObjectProxy(
89 "org.chromium.TestService",
90 dbus::ObjectPath("/"));
91 ASSERT_TRUE(bus_->HasDBusThread());
93 // Connect to the "Test" signal of "org.chromium.TestInterface" from
94 // the root remote object too.
95 root_object_proxy_->ConnectToSignal(
96 "org.chromium.TestInterface",
97 "Test",
98 base::Bind(&EndToEndAsyncTest::OnRootTestSignal,
99 base::Unretained(this)),
100 base::Bind(&EndToEndAsyncTest::OnConnected,
101 base::Unretained(this)));
102 // Wait until the root object proxy is connected to the signal.
103 message_loop_.Run();
106 virtual void TearDown() {
107 bus_->ShutdownOnDBusThreadAndBlock();
109 // Shut down the service.
110 test_service_->ShutdownAndBlock();
112 // Reset to the default.
113 base::ThreadRestrictions::SetIOAllowed(true);
115 // Stopping a thread is considered an IO operation, so do this after
116 // allowing IO.
117 test_service_->Stop();
120 protected:
121 // Replaces the bus with a broken one.
122 void SetUpBrokenBus() {
123 // Shut down the existing bus.
124 bus_->ShutdownOnDBusThreadAndBlock();
126 // Create new bus with invalid address.
127 const char kInvalidAddress[] = "";
128 dbus::Bus::Options bus_options;
129 bus_options.bus_type = dbus::Bus::CUSTOM_ADDRESS;
130 bus_options.address = kInvalidAddress;
131 bus_options.connection_type = dbus::Bus::PRIVATE;
132 bus_options.dbus_thread_message_loop_proxy =
133 dbus_thread_->message_loop_proxy();
134 bus_ = new dbus::Bus(bus_options);
135 ASSERT_TRUE(bus_->HasDBusThread());
137 // Create new object proxy.
138 object_proxy_ = bus_->GetObjectProxy(
139 "org.chromium.TestService",
140 dbus::ObjectPath("/org/chromium/TestObject"));
143 // Calls the method asynchronously. OnResponse() will be called once the
144 // response is received.
145 void CallMethod(dbus::MethodCall* method_call,
146 int timeout_ms) {
147 object_proxy_->CallMethod(method_call,
148 timeout_ms,
149 base::Bind(&EndToEndAsyncTest::OnResponse,
150 base::Unretained(this)));
153 // Calls the method asynchronously. OnResponse() will be called once the
154 // response is received without error, otherwise OnError() will be called.
155 void CallMethodWithErrorCallback(dbus::MethodCall* method_call,
156 int timeout_ms) {
157 object_proxy_->CallMethodWithErrorCallback(
158 method_call,
159 timeout_ms,
160 base::Bind(&EndToEndAsyncTest::OnResponse, base::Unretained(this)),
161 base::Bind(&EndToEndAsyncTest::OnError, base::Unretained(this)));
164 // Wait for the give number of responses.
165 void WaitForResponses(size_t num_responses) {
166 while (response_strings_.size() < num_responses) {
167 message_loop_.Run();
171 // Called when the response is received.
172 void OnResponse(dbus::Response* response) {
173 // |response| will be deleted on exit of the function. Copy the
174 // payload to |response_strings_|.
175 if (response) {
176 dbus::MessageReader reader(response);
177 std::string response_string;
178 ASSERT_TRUE(reader.PopString(&response_string));
179 response_strings_.push_back(response_string);
180 } else {
181 response_strings_.push_back("");
183 message_loop_.Quit();
186 // Wait for the given number of errors.
187 void WaitForErrors(size_t num_errors) {
188 while (error_names_.size() < num_errors) {
189 message_loop_.Run();
193 // Called when an error is received.
194 void OnError(dbus::ErrorResponse* error) {
195 // |error| will be deleted on exit of the function. Copy the payload to
196 // |error_names_|.
197 if (error) {
198 ASSERT_NE("", error->GetErrorName());
199 error_names_.push_back(error->GetErrorName());
200 } else {
201 error_names_.push_back("");
203 message_loop_.Quit();
206 // Called when the "Test" signal is received, in the main thread.
207 // Copy the string payload to |test_signal_string_|.
208 void OnTestSignal(dbus::Signal* signal) {
209 dbus::MessageReader reader(signal);
210 ASSERT_TRUE(reader.PopString(&test_signal_string_));
211 message_loop_.Quit();
214 // Called when the "Test" signal is received, in the main thread, by
215 // the root object proxy. Copy the string payload to
216 // |root_test_signal_string_|.
217 void OnRootTestSignal(dbus::Signal* signal) {
218 dbus::MessageReader reader(signal);
219 ASSERT_TRUE(reader.PopString(&root_test_signal_string_));
220 message_loop_.Quit();
223 // Called when the "Test2" signal is received, in the main thread.
224 void OnTest2Signal(dbus::Signal* signal) {
225 dbus::MessageReader reader(signal);
226 message_loop_.Quit();
229 // Called when connected to the signal.
230 void OnConnected(const std::string& interface_name,
231 const std::string& signal_name,
232 bool success) {
233 ASSERT_TRUE(success);
234 message_loop_.Quit();
237 // Wait for the hey signal to be received.
238 void WaitForTestSignal() {
239 // OnTestSignal() will quit the message loop.
240 message_loop_.Run();
243 MessageLoop message_loop_;
244 std::vector<std::string> response_strings_;
245 std::vector<std::string> error_names_;
246 scoped_ptr<base::Thread> dbus_thread_;
247 scoped_refptr<dbus::Bus> bus_;
248 dbus::ObjectProxy* object_proxy_;
249 dbus::ObjectProxy* root_object_proxy_;
250 scoped_ptr<dbus::TestService> test_service_;
251 // Text message from "Test" signal.
252 std::string test_signal_string_;
253 // Text message from "Test" signal delivered to root.
254 std::string root_test_signal_string_;
257 TEST_F(EndToEndAsyncTest, Echo) {
258 const char* kHello = "hello";
260 // Create the method call.
261 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
262 dbus::MessageWriter writer(&method_call);
263 writer.AppendString(kHello);
265 // Call the method.
266 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
267 CallMethod(&method_call, timeout_ms);
269 // Check the response.
270 WaitForResponses(1);
271 EXPECT_EQ(kHello, response_strings_[0]);
274 TEST_F(EndToEndAsyncTest, EchoWithErrorCallback) {
275 const char* kHello = "hello";
277 // Create the method call.
278 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
279 dbus::MessageWriter writer(&method_call);
280 writer.AppendString(kHello);
282 // Call the method.
283 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
284 CallMethodWithErrorCallback(&method_call, timeout_ms);
286 // Check the response.
287 WaitForResponses(1);
288 EXPECT_EQ(kHello, response_strings_[0]);
289 EXPECT_TRUE(error_names_.empty());
292 // Call Echo method three times.
293 TEST_F(EndToEndAsyncTest, EchoThreeTimes) {
294 const char* kMessages[] = { "foo", "bar", "baz" };
296 for (size_t i = 0; i < arraysize(kMessages); ++i) {
297 // Create the method call.
298 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
299 dbus::MessageWriter writer(&method_call);
300 writer.AppendString(kMessages[i]);
302 // Call the method.
303 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
304 CallMethod(&method_call, timeout_ms);
307 // Check the responses.
308 WaitForResponses(3);
309 // Sort as the order of the returned messages is not deterministic.
310 std::sort(response_strings_.begin(), response_strings_.end());
311 EXPECT_EQ("bar", response_strings_[0]);
312 EXPECT_EQ("baz", response_strings_[1]);
313 EXPECT_EQ("foo", response_strings_[2]);
316 TEST_F(EndToEndAsyncTest, BrokenBus) {
317 const char* kHello = "hello";
319 // Set up a broken bus.
320 SetUpBrokenBus();
322 // Create the method call.
323 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
324 dbus::MessageWriter writer(&method_call);
325 writer.AppendString(kHello);
327 // Call the method.
328 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
329 CallMethod(&method_call, timeout_ms);
330 WaitForResponses(1);
332 // Should fail because of the broken bus.
333 ASSERT_EQ("", response_strings_[0]);
336 TEST_F(EndToEndAsyncTest, BrokenBusWithErrorCallback) {
337 const char* kHello = "hello";
339 // Set up a broken bus.
340 SetUpBrokenBus();
342 // Create the method call.
343 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
344 dbus::MessageWriter writer(&method_call);
345 writer.AppendString(kHello);
347 // Call the method.
348 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
349 CallMethodWithErrorCallback(&method_call, timeout_ms);
350 WaitForErrors(1);
352 // Should fail because of the broken bus.
353 ASSERT_TRUE(response_strings_.empty());
354 ASSERT_EQ("", error_names_[0]);
357 TEST_F(EndToEndAsyncTest, Timeout) {
358 const char* kHello = "hello";
360 // Create the method call.
361 dbus::MethodCall method_call("org.chromium.TestInterface", "SlowEcho");
362 dbus::MessageWriter writer(&method_call);
363 writer.AppendString(kHello);
365 // Call the method with timeout of 0ms.
366 const int timeout_ms = 0;
367 CallMethod(&method_call, timeout_ms);
368 WaitForResponses(1);
370 // Should fail because of timeout.
371 ASSERT_EQ("", response_strings_[0]);
374 TEST_F(EndToEndAsyncTest, TimeoutWithErrorCallback) {
375 const char* kHello = "hello";
377 // Create the method call.
378 dbus::MethodCall method_call("org.chromium.TestInterface", "SlowEcho");
379 dbus::MessageWriter writer(&method_call);
380 writer.AppendString(kHello);
382 // Call the method with timeout of 0ms.
383 const int timeout_ms = 0;
384 CallMethodWithErrorCallback(&method_call, timeout_ms);
385 WaitForErrors(1);
387 // Should fail because of timeout.
388 ASSERT_TRUE(response_strings_.empty());
389 ASSERT_EQ(DBUS_ERROR_NO_REPLY, error_names_[0]);
392 // Tests calling a method that sends its reply asynchronously.
393 TEST_F(EndToEndAsyncTest, AsyncEcho) {
394 const char* kHello = "hello";
396 // Create the method call.
397 dbus::MethodCall method_call("org.chromium.TestInterface", "AsyncEcho");
398 dbus::MessageWriter writer(&method_call);
399 writer.AppendString(kHello);
401 // Call the method.
402 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
403 CallMethod(&method_call, timeout_ms);
405 // Check the response.
406 WaitForResponses(1);
407 EXPECT_EQ(kHello, response_strings_[0]);
410 TEST_F(EndToEndAsyncTest, NonexistentMethod) {
411 dbus::MethodCall method_call("org.chromium.TestInterface", "Nonexistent");
413 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
414 CallMethod(&method_call, timeout_ms);
415 WaitForResponses(1);
417 // Should fail because the method is nonexistent.
418 ASSERT_EQ("", response_strings_[0]);
421 TEST_F(EndToEndAsyncTest, NonexistentMethodWithErrorCallback) {
422 dbus::MethodCall method_call("org.chromium.TestInterface", "Nonexistent");
424 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
425 CallMethodWithErrorCallback(&method_call, timeout_ms);
426 WaitForErrors(1);
428 // Should fail because the method is nonexistent.
429 ASSERT_TRUE(response_strings_.empty());
430 ASSERT_EQ(DBUS_ERROR_UNKNOWN_METHOD, error_names_[0]);
433 TEST_F(EndToEndAsyncTest, BrokenMethod) {
434 dbus::MethodCall method_call("org.chromium.TestInterface", "BrokenMethod");
436 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
437 CallMethod(&method_call, timeout_ms);
438 WaitForResponses(1);
440 // Should fail because the method is broken.
441 ASSERT_EQ("", response_strings_[0]);
444 TEST_F(EndToEndAsyncTest, BrokenMethodWithErrorCallback) {
445 dbus::MethodCall method_call("org.chromium.TestInterface", "BrokenMethod");
447 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
448 CallMethodWithErrorCallback(&method_call, timeout_ms);
449 WaitForErrors(1);
451 // Should fail because the method is broken.
452 ASSERT_TRUE(response_strings_.empty());
453 ASSERT_EQ(DBUS_ERROR_FAILED, error_names_[0]);
456 TEST_F(EndToEndAsyncTest, EmptyResponseCallback) {
457 const char* kHello = "hello";
459 // Create the method call.
460 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
461 dbus::MessageWriter writer(&method_call);
462 writer.AppendString(kHello);
464 // Call the method with an empty callback.
465 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
466 object_proxy_->CallMethod(&method_call,
467 timeout_ms,
468 dbus::ObjectProxy::EmptyResponseCallback());
469 // Post a delayed task to quit the message loop.
470 message_loop_.PostDelayedTask(FROM_HERE,
471 MessageLoop::QuitClosure(),
472 TestTimeouts::tiny_timeout());
473 message_loop_.Run();
474 // We cannot tell if the empty callback is called, but at least we can
475 // check if the test does not crash.
478 TEST_F(EndToEndAsyncTest, TestSignal) {
479 const char kMessage[] = "hello, world";
480 // Send the test signal from the exported object.
481 test_service_->SendTestSignal(kMessage);
482 // Receive the signal with the object proxy. The signal is handled in
483 // EndToEndAsyncTest::OnTestSignal() in the main thread.
484 WaitForTestSignal();
485 ASSERT_EQ(kMessage, test_signal_string_);
488 TEST_F(EndToEndAsyncTest, TestSignalFromRoot) {
489 const char kMessage[] = "hello, world";
490 // Object proxies are tied to a particular object path, if a signal
491 // arrives from a different object path like "/" the first object proxy
492 // |object_proxy_| should not handle it, and should leave it for the root
493 // object proxy |root_object_proxy_|.
494 test_service_->SendTestSignalFromRoot(kMessage);
495 WaitForTestSignal();
496 // Verify the signal was not received by the specific proxy.
497 ASSERT_TRUE(test_signal_string_.empty());
498 // Verify the string WAS received by the root proxy.
499 ASSERT_EQ(kMessage, root_test_signal_string_);
502 class SignalReplacementTest : public EndToEndAsyncTest {
503 public:
504 SignalReplacementTest() {
507 virtual void SetUp() {
508 // Set up base class.
509 EndToEndAsyncTest::SetUp();
511 // Reconnect the root object proxy's signal handler to a new handler
512 // so that we can verify that a second call to ConnectSignal() delivers
513 // to our new handler and not the old.
514 object_proxy_->ConnectToSignal(
515 "org.chromium.TestInterface",
516 "Test",
517 base::Bind(&SignalReplacementTest::OnReplacementTestSignal,
518 base::Unretained(this)),
519 base::Bind(&SignalReplacementTest::OnReplacementConnected,
520 base::Unretained(this)));
521 // Wait until the object proxy is connected to the signal.
522 message_loop_.Run();
525 protected:
526 // Called when the "Test" signal is received, in the main thread.
527 // Copy the string payload to |replacement_test_signal_string_|.
528 void OnReplacementTestSignal(dbus::Signal* signal) {
529 dbus::MessageReader reader(signal);
530 ASSERT_TRUE(reader.PopString(&replacement_test_signal_string_));
531 message_loop_.Quit();
534 // Called when connected to the signal.
535 void OnReplacementConnected(const std::string& interface_name,
536 const std::string& signal_name,
537 bool success) {
538 ASSERT_TRUE(success);
539 message_loop_.Quit();
542 // Text message from "Test" signal delivered to replacement handler.
543 std::string replacement_test_signal_string_;
546 TEST_F(SignalReplacementTest, TestSignalReplacement) {
547 const char kMessage[] = "hello, world";
548 // Send the test signal from the exported object.
549 test_service_->SendTestSignal(kMessage);
550 // Receive the signal with the object proxy.
551 WaitForTestSignal();
552 // Verify the string WAS NOT received by the original handler.
553 ASSERT_TRUE(test_signal_string_.empty());
554 // Verify the signal WAS received by the replacement handler.
555 ASSERT_EQ(kMessage, replacement_test_signal_string_);