blob: 210431f205805eb6a8327ad8f90a9072143c72e7 [file] [log] [blame]
[email protected]216ed0b2012-02-14 21:29:061// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]a51076112011-08-17 20:58:122// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <algorithm>
6#include <string>
7#include <vector>
8
9#include "base/bind.h"
10#include "base/memory/scoped_ptr.h"
11#include "base/message_loop.h"
12#include "base/stl_util.h"
[email protected]65e8e252011-11-11 08:36:2213#include "base/test/test_timeouts.h"
[email protected]a51076112011-08-17 20:58:1214#include "base/threading/thread.h"
15#include "base/threading/thread_restrictions.h"
16#include "dbus/bus.h"
17#include "dbus/message.h"
[email protected]216ed0b2012-02-14 21:29:0618#include "dbus/object_path.h"
[email protected]a51076112011-08-17 20:58:1219#include "dbus/object_proxy.h"
20#include "dbus/test_service.h"
21#include "testing/gtest/include/gtest/gtest.h"
22
[email protected]3635d322012-06-02 02:53:2023namespace {
24
25// See comments in ObjectProxy::RunResponseCallback() for why the number was
26// chosen.
27const int kHugePayloadSize = 64 << 20; // 64 MB
28
29} // namespace
30
[email protected]829f0e4c2011-08-31 18:02:4331// The end-to-end test exercises the asynchronous APIs in ObjectProxy and
[email protected]a51076112011-08-17 20:58:1232// ExportedObject.
33class EndToEndAsyncTest : public testing::Test {
34 public:
[email protected]ea545192013-02-15 16:44:4035 EndToEndAsyncTest() : on_disconnected_call_count_(0) {}
[email protected]a51076112011-08-17 20:58:1236
[email protected]ea78b1e2011-08-27 07:26:3437 virtual void SetUp() {
[email protected]a51076112011-08-17 20:58:1238 // Make the main thread not to allow IO.
39 base::ThreadRestrictions::SetIOAllowed(false);
40
[email protected]a51076112011-08-17 20:58:1241 // Start the D-Bus thread.
42 dbus_thread_.reset(new base::Thread("D-Bus Thread"));
43 base::Thread::Options thread_options;
[email protected]ff33b18e2013-05-01 16:10:3044 thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
[email protected]12f97662011-08-20 01:07:1745 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options));
[email protected]a51076112011-08-17 20:58:1246
[email protected]12f97662011-08-20 01:07:1747 // Start the test service, using the D-Bus thread.
48 dbus::TestService::Options options;
[email protected]200328a2013-02-20 01:36:5349 options.dbus_task_runner = dbus_thread_->message_loop_proxy();
[email protected]12f97662011-08-20 01:07:1750 test_service_.reset(new dbus::TestService(options));
51 ASSERT_TRUE(test_service_->StartService());
52 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted());
53 ASSERT_TRUE(test_service_->HasDBusThread());
54
55 // Create the client, using the D-Bus thread.
[email protected]a51076112011-08-17 20:58:1256 dbus::Bus::Options bus_options;
57 bus_options.bus_type = dbus::Bus::SESSION;
58 bus_options.connection_type = dbus::Bus::PRIVATE;
[email protected]200328a2013-02-20 01:36:5359 bus_options.dbus_task_runner = dbus_thread_->message_loop_proxy();
[email protected]ea545192013-02-15 16:44:4060 bus_options.disconnected_callback =
61 base::Bind(&EndToEndAsyncTest::OnDisconnected, base::Unretained(this));
[email protected]a51076112011-08-17 20:58:1262 bus_ = new dbus::Bus(bus_options);
[email protected]216ed0b2012-02-14 21:29:0663 object_proxy_ = bus_->GetObjectProxy(
64 "org.chromium.TestService",
65 dbus::ObjectPath("/org/chromium/TestObject"));
[email protected]12f97662011-08-20 01:07:1766 ASSERT_TRUE(bus_->HasDBusThread());
[email protected]3beaaa4e2011-08-23 07:29:2167
[email protected]0ad9ef82011-11-23 22:08:3868 // Connect to the "Test" signal of "org.chromium.TestInterface" from
69 // the remote object.
[email protected]3beaaa4e2011-08-23 07:29:2170 object_proxy_->ConnectToSignal(
71 "org.chromium.TestInterface",
72 "Test",
73 base::Bind(&EndToEndAsyncTest::OnTestSignal,
74 base::Unretained(this)),
75 base::Bind(&EndToEndAsyncTest::OnConnected,
76 base::Unretained(this)));
[email protected]4c985c62011-12-13 17:08:4277 // Wait until the object proxy is connected to the signal.
78 message_loop_.Run();
79
[email protected]0ad9ef82011-11-23 22:08:3880 // Connect to the "Test2" signal of "org.chromium.TestInterface" from
81 // the remote object. There was a bug where we were emitting error
82 // messages like "Requested to remove an unknown match rule: ..." at
83 // the shutdown of Bus when an object proxy is connected to more than
84 // one signal of the same interface. See crosbug.com/23382 for details.
85 object_proxy_->ConnectToSignal(
86 "org.chromium.TestInterface",
87 "Test2",
88 base::Bind(&EndToEndAsyncTest::OnTest2Signal,
89 base::Unretained(this)),
90 base::Bind(&EndToEndAsyncTest::OnConnected,
91 base::Unretained(this)));
[email protected]3beaaa4e2011-08-23 07:29:2192 // Wait until the object proxy is connected to the signal.
93 message_loop_.Run();
[email protected]4d97f23372012-03-01 04:01:0594
95 // Create a second object proxy for the root object.
96 root_object_proxy_ = bus_->GetObjectProxy(
97 "org.chromium.TestService",
98 dbus::ObjectPath("/"));
99 ASSERT_TRUE(bus_->HasDBusThread());
100
101 // Connect to the "Test" signal of "org.chromium.TestInterface" from
102 // the root remote object too.
103 root_object_proxy_->ConnectToSignal(
104 "org.chromium.TestInterface",
105 "Test",
106 base::Bind(&EndToEndAsyncTest::OnRootTestSignal,
107 base::Unretained(this)),
108 base::Bind(&EndToEndAsyncTest::OnConnected,
109 base::Unretained(this)));
110 // Wait until the root object proxy is connected to the signal.
111 message_loop_.Run();
[email protected]a51076112011-08-17 20:58:12112 }
113
[email protected]ea78b1e2011-08-27 07:26:34114 virtual void TearDown() {
[email protected]e20d39fe2011-09-02 06:56:23115 bus_->ShutdownOnDBusThreadAndBlock();
[email protected]a51076112011-08-17 20:58:12116
[email protected]12f97662011-08-20 01:07:17117 // Shut down the service.
[email protected]e20d39fe2011-09-02 06:56:23118 test_service_->ShutdownAndBlock();
[email protected]12f97662011-08-20 01:07:17119
[email protected]a51076112011-08-17 20:58:12120 // Reset to the default.
121 base::ThreadRestrictions::SetIOAllowed(true);
122
[email protected]829f0e4c2011-08-31 18:02:43123 // Stopping a thread is considered an IO operation, so do this after
[email protected]a51076112011-08-17 20:58:12124 // allowing IO.
125 test_service_->Stop();
126 }
127
128 protected:
[email protected]91fe7ea2012-04-20 03:18:27129 // Replaces the bus with a broken one.
130 void SetUpBrokenBus() {
131 // Shut down the existing bus.
132 bus_->ShutdownOnDBusThreadAndBlock();
133
134 // Create new bus with invalid address.
135 const char kInvalidAddress[] = "";
136 dbus::Bus::Options bus_options;
137 bus_options.bus_type = dbus::Bus::CUSTOM_ADDRESS;
138 bus_options.address = kInvalidAddress;
139 bus_options.connection_type = dbus::Bus::PRIVATE;
[email protected]200328a2013-02-20 01:36:53140 bus_options.dbus_task_runner = dbus_thread_->message_loop_proxy();
[email protected]91fe7ea2012-04-20 03:18:27141 bus_ = new dbus::Bus(bus_options);
142 ASSERT_TRUE(bus_->HasDBusThread());
143
144 // Create new object proxy.
145 object_proxy_ = bus_->GetObjectProxy(
146 "org.chromium.TestService",
147 dbus::ObjectPath("/org/chromium/TestObject"));
148 }
149
[email protected]829f0e4c2011-08-31 18:02:43150 // Calls the method asynchronously. OnResponse() will be called once the
[email protected]a51076112011-08-17 20:58:12151 // response is received.
152 void CallMethod(dbus::MethodCall* method_call,
153 int timeout_ms) {
154 object_proxy_->CallMethod(method_call,
155 timeout_ms,
156 base::Bind(&EndToEndAsyncTest::OnResponse,
157 base::Unretained(this)));
158 }
159
[email protected]91fe7ea2012-04-20 03:18:27160 // Calls the method asynchronously. OnResponse() will be called once the
161 // response is received without error, otherwise OnError() will be called.
162 void CallMethodWithErrorCallback(dbus::MethodCall* method_call,
163 int timeout_ms) {
164 object_proxy_->CallMethodWithErrorCallback(
165 method_call,
166 timeout_ms,
167 base::Bind(&EndToEndAsyncTest::OnResponse, base::Unretained(this)),
168 base::Bind(&EndToEndAsyncTest::OnError, base::Unretained(this)));
169 }
170
[email protected]a51076112011-08-17 20:58:12171 // Wait for the give number of responses.
172 void WaitForResponses(size_t num_responses) {
173 while (response_strings_.size() < num_responses) {
174 message_loop_.Run();
175 }
176 }
177
178 // Called when the response is received.
179 void OnResponse(dbus::Response* response) {
180 // |response| will be deleted on exit of the function. Copy the
181 // payload to |response_strings_|.
182 if (response) {
183 dbus::MessageReader reader(response);
184 std::string response_string;
185 ASSERT_TRUE(reader.PopString(&response_string));
186 response_strings_.push_back(response_string);
187 } else {
[email protected]007b3f82013-04-09 08:46:45188 response_strings_.push_back(std::string());
[email protected]a51076112011-08-17 20:58:12189 }
190 message_loop_.Quit();
191 };
192
[email protected]91fe7ea2012-04-20 03:18:27193 // Wait for the given number of errors.
194 void WaitForErrors(size_t num_errors) {
195 while (error_names_.size() < num_errors) {
196 message_loop_.Run();
197 }
198 }
199
200 // Called when an error is received.
201 void OnError(dbus::ErrorResponse* error) {
202 // |error| will be deleted on exit of the function. Copy the payload to
203 // |error_names_|.
204 if (error) {
205 ASSERT_NE("", error->GetErrorName());
206 error_names_.push_back(error->GetErrorName());
207 } else {
[email protected]007b3f82013-04-09 08:46:45208 error_names_.push_back(std::string());
[email protected]91fe7ea2012-04-20 03:18:27209 }
210 message_loop_.Quit();
211 }
212
[email protected]3beaaa4e2011-08-23 07:29:21213 // Called when the "Test" signal is received, in the main thread.
214 // Copy the string payload to |test_signal_string_|.
215 void OnTestSignal(dbus::Signal* signal) {
216 dbus::MessageReader reader(signal);
217 ASSERT_TRUE(reader.PopString(&test_signal_string_));
218 message_loop_.Quit();
219 }
220
[email protected]4d97f23372012-03-01 04:01:05221 // Called when the "Test" signal is received, in the main thread, by
222 // the root object proxy. Copy the string payload to
223 // |root_test_signal_string_|.
224 void OnRootTestSignal(dbus::Signal* signal) {
225 dbus::MessageReader reader(signal);
226 ASSERT_TRUE(reader.PopString(&root_test_signal_string_));
227 message_loop_.Quit();
228 }
229
[email protected]0ad9ef82011-11-23 22:08:38230 // Called when the "Test2" signal is received, in the main thread.
231 void OnTest2Signal(dbus::Signal* signal) {
232 dbus::MessageReader reader(signal);
233 message_loop_.Quit();
234 }
235
[email protected]3beaaa4e2011-08-23 07:29:21236 // Called when connected to the signal.
237 void OnConnected(const std::string& interface_name,
238 const std::string& signal_name,
239 bool success) {
240 ASSERT_TRUE(success);
241 message_loop_.Quit();
242 }
243
[email protected]ea545192013-02-15 16:44:40244 // Called when the connection with dbus-daemon is disconnected.
245 void OnDisconnected() {
246 message_loop_.Quit();
247 ++on_disconnected_call_count_;
248 }
249
[email protected]3beaaa4e2011-08-23 07:29:21250 // Wait for the hey signal to be received.
251 void WaitForTestSignal() {
252 // OnTestSignal() will quit the message loop.
253 message_loop_.Run();
254 }
255
[email protected]ff33b18e2013-05-01 16:10:30256 base::MessageLoop message_loop_;
[email protected]a51076112011-08-17 20:58:12257 std::vector<std::string> response_strings_;
[email protected]91fe7ea2012-04-20 03:18:27258 std::vector<std::string> error_names_;
[email protected]a51076112011-08-17 20:58:12259 scoped_ptr<base::Thread> dbus_thread_;
260 scoped_refptr<dbus::Bus> bus_;
261 dbus::ObjectProxy* object_proxy_;
[email protected]4d97f23372012-03-01 04:01:05262 dbus::ObjectProxy* root_object_proxy_;
[email protected]a51076112011-08-17 20:58:12263 scoped_ptr<dbus::TestService> test_service_;
[email protected]3beaaa4e2011-08-23 07:29:21264 // Text message from "Test" signal.
265 std::string test_signal_string_;
[email protected]4d97f23372012-03-01 04:01:05266 // Text message from "Test" signal delivered to root.
267 std::string root_test_signal_string_;
[email protected]ea545192013-02-15 16:44:40268 int on_disconnected_call_count_;
[email protected]a51076112011-08-17 20:58:12269};
270
271TEST_F(EndToEndAsyncTest, Echo) {
272 const char* kHello = "hello";
273
274 // Create the method call.
275 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
276 dbus::MessageWriter writer(&method_call);
277 writer.AppendString(kHello);
278
279 // Call the method.
280 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
281 CallMethod(&method_call, timeout_ms);
282
283 // Check the response.
284 WaitForResponses(1);
285 EXPECT_EQ(kHello, response_strings_[0]);
286}
287
[email protected]91fe7ea2012-04-20 03:18:27288TEST_F(EndToEndAsyncTest, EchoWithErrorCallback) {
289 const char* kHello = "hello";
290
291 // Create the method call.
292 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
293 dbus::MessageWriter writer(&method_call);
294 writer.AppendString(kHello);
295
296 // Call the method.
297 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
298 CallMethodWithErrorCallback(&method_call, timeout_ms);
299
300 // Check the response.
301 WaitForResponses(1);
302 EXPECT_EQ(kHello, response_strings_[0]);
303 EXPECT_TRUE(error_names_.empty());
304}
305
[email protected]a51076112011-08-17 20:58:12306// Call Echo method three times.
307TEST_F(EndToEndAsyncTest, EchoThreeTimes) {
308 const char* kMessages[] = { "foo", "bar", "baz" };
309
310 for (size_t i = 0; i < arraysize(kMessages); ++i) {
311 // Create the method call.
312 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
313 dbus::MessageWriter writer(&method_call);
314 writer.AppendString(kMessages[i]);
315
316 // Call the method.
317 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
318 CallMethod(&method_call, timeout_ms);
319 }
320
321 // Check the responses.
322 WaitForResponses(3);
323 // Sort as the order of the returned messages is not deterministic.
324 std::sort(response_strings_.begin(), response_strings_.end());
325 EXPECT_EQ("bar", response_strings_[0]);
326 EXPECT_EQ("baz", response_strings_[1]);
327 EXPECT_EQ("foo", response_strings_[2]);
328}
329
[email protected]3635d322012-06-02 02:53:20330TEST_F(EndToEndAsyncTest, Echo_HugePayload) {
331 const std::string kHugePayload(kHugePayloadSize, 'o');
332
333 // Create the method call with a huge payload.
334 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
335 dbus::MessageWriter writer(&method_call);
336 writer.AppendString(kHugePayload);
337
338 // Call the method.
339 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
340 CallMethod(&method_call, timeout_ms);
341
342 // This caused a DCHECK failure before. Ensure that the issue is fixed.
343 WaitForResponses(1);
344 EXPECT_EQ(kHugePayload, response_strings_[0]);
345}
346
[email protected]91fe7ea2012-04-20 03:18:27347TEST_F(EndToEndAsyncTest, BrokenBus) {
348 const char* kHello = "hello";
349
350 // Set up a broken bus.
351 SetUpBrokenBus();
352
353 // Create the method call.
354 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
355 dbus::MessageWriter writer(&method_call);
356 writer.AppendString(kHello);
357
358 // Call the method.
359 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
360 CallMethod(&method_call, timeout_ms);
361 WaitForResponses(1);
362
363 // Should fail because of the broken bus.
364 ASSERT_EQ("", response_strings_[0]);
365}
366
367TEST_F(EndToEndAsyncTest, BrokenBusWithErrorCallback) {
368 const char* kHello = "hello";
369
370 // Set up a broken bus.
371 SetUpBrokenBus();
372
373 // Create the method call.
374 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
375 dbus::MessageWriter writer(&method_call);
376 writer.AppendString(kHello);
377
378 // Call the method.
379 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
380 CallMethodWithErrorCallback(&method_call, timeout_ms);
381 WaitForErrors(1);
382
383 // Should fail because of the broken bus.
384 ASSERT_TRUE(response_strings_.empty());
385 ASSERT_EQ("", error_names_[0]);
386}
387
[email protected]a51076112011-08-17 20:58:12388TEST_F(EndToEndAsyncTest, Timeout) {
389 const char* kHello = "hello";
390
391 // Create the method call.
392 dbus::MethodCall method_call("org.chromium.TestInterface", "SlowEcho");
393 dbus::MessageWriter writer(&method_call);
394 writer.AppendString(kHello);
395
[email protected]12f97662011-08-20 01:07:17396 // Call the method with timeout of 0ms.
397 const int timeout_ms = 0;
[email protected]a51076112011-08-17 20:58:12398 CallMethod(&method_call, timeout_ms);
399 WaitForResponses(1);
400
401 // Should fail because of timeout.
402 ASSERT_EQ("", response_strings_[0]);
403}
404
[email protected]91fe7ea2012-04-20 03:18:27405TEST_F(EndToEndAsyncTest, TimeoutWithErrorCallback) {
406 const char* kHello = "hello";
407
408 // Create the method call.
409 dbus::MethodCall method_call("org.chromium.TestInterface", "SlowEcho");
410 dbus::MessageWriter writer(&method_call);
411 writer.AppendString(kHello);
412
413 // Call the method with timeout of 0ms.
414 const int timeout_ms = 0;
415 CallMethodWithErrorCallback(&method_call, timeout_ms);
416 WaitForErrors(1);
417
418 // Should fail because of timeout.
419 ASSERT_TRUE(response_strings_.empty());
420 ASSERT_EQ(DBUS_ERROR_NO_REPLY, error_names_[0]);
421}
422
[email protected]9aa74cc2011-11-30 04:57:42423// Tests calling a method that sends its reply asynchronously.
424TEST_F(EndToEndAsyncTest, AsyncEcho) {
425 const char* kHello = "hello";
426
427 // Create the method call.
428 dbus::MethodCall method_call("org.chromium.TestInterface", "AsyncEcho");
429 dbus::MessageWriter writer(&method_call);
430 writer.AppendString(kHello);
431
432 // Call the method.
433 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
434 CallMethod(&method_call, timeout_ms);
435
436 // Check the response.
437 WaitForResponses(1);
438 EXPECT_EQ(kHello, response_strings_[0]);
439}
440
[email protected]a51076112011-08-17 20:58:12441TEST_F(EndToEndAsyncTest, NonexistentMethod) {
442 dbus::MethodCall method_call("org.chromium.TestInterface", "Nonexistent");
443
444 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
445 CallMethod(&method_call, timeout_ms);
446 WaitForResponses(1);
447
448 // Should fail because the method is nonexistent.
449 ASSERT_EQ("", response_strings_[0]);
450}
451
[email protected]91fe7ea2012-04-20 03:18:27452TEST_F(EndToEndAsyncTest, NonexistentMethodWithErrorCallback) {
453 dbus::MethodCall method_call("org.chromium.TestInterface", "Nonexistent");
454
455 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
456 CallMethodWithErrorCallback(&method_call, timeout_ms);
457 WaitForErrors(1);
458
459 // Should fail because the method is nonexistent.
460 ASSERT_TRUE(response_strings_.empty());
461 ASSERT_EQ(DBUS_ERROR_UNKNOWN_METHOD, error_names_[0]);
462}
463
[email protected]a51076112011-08-17 20:58:12464TEST_F(EndToEndAsyncTest, BrokenMethod) {
465 dbus::MethodCall method_call("org.chromium.TestInterface", "BrokenMethod");
466
467 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
468 CallMethod(&method_call, timeout_ms);
469 WaitForResponses(1);
470
471 // Should fail because the method is broken.
472 ASSERT_EQ("", response_strings_[0]);
473}
[email protected]3beaaa4e2011-08-23 07:29:21474
[email protected]91fe7ea2012-04-20 03:18:27475TEST_F(EndToEndAsyncTest, BrokenMethodWithErrorCallback) {
476 dbus::MethodCall method_call("org.chromium.TestInterface", "BrokenMethod");
477
478 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
479 CallMethodWithErrorCallback(&method_call, timeout_ms);
480 WaitForErrors(1);
481
482 // Should fail because the method is broken.
483 ASSERT_TRUE(response_strings_.empty());
484 ASSERT_EQ(DBUS_ERROR_FAILED, error_names_[0]);
485}
486
[email protected]ca72ff22012-05-23 06:55:22487TEST_F(EndToEndAsyncTest, InvalidObjectPath) {
488 // Trailing '/' is only allowed for the root path.
489 const dbus::ObjectPath invalid_object_path("/org/chromium/TestObject/");
490
491 // Replace object proxy with new one.
492 object_proxy_ = bus_->GetObjectProxy("org.chromium.TestService",
493 invalid_object_path);
494
495 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
496
497 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
498 CallMethodWithErrorCallback(&method_call, timeout_ms);
499 WaitForErrors(1);
500
501 // Should fail because of the invalid path.
502 ASSERT_TRUE(response_strings_.empty());
503 ASSERT_EQ("", error_names_[0]);
504}
505
506TEST_F(EndToEndAsyncTest, InvalidServiceName) {
507 // Bus name cannot contain '/'.
508 const std::string invalid_service_name = ":1/2";
509
510 // Replace object proxy with new one.
511 object_proxy_ = bus_->GetObjectProxy(
512 invalid_service_name, dbus::ObjectPath("org.chromium.TestObject"));
513
514 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
515
516 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
517 CallMethodWithErrorCallback(&method_call, timeout_ms);
518 WaitForErrors(1);
519
520 // Should fail because of the invalid bus name.
521 ASSERT_TRUE(response_strings_.empty());
522 ASSERT_EQ("", error_names_[0]);
523}
524
[email protected]65e8e252011-11-11 08:36:22525TEST_F(EndToEndAsyncTest, EmptyResponseCallback) {
526 const char* kHello = "hello";
527
528 // Create the method call.
529 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo");
530 dbus::MessageWriter writer(&method_call);
531 writer.AppendString(kHello);
532
533 // Call the method with an empty callback.
534 const int timeout_ms = dbus::ObjectProxy::TIMEOUT_USE_DEFAULT;
535 object_proxy_->CallMethod(&method_call,
536 timeout_ms,
537 dbus::ObjectProxy::EmptyResponseCallback());
538 // Post a delayed task to quit the message loop.
539 message_loop_.PostDelayedTask(FROM_HERE,
[email protected]ff33b18e2013-05-01 16:10:30540 base::MessageLoop::QuitClosure(),
[email protected]4b4542682012-03-11 01:12:20541 TestTimeouts::tiny_timeout());
[email protected]65e8e252011-11-11 08:36:22542 message_loop_.Run();
543 // We cannot tell if the empty callback is called, but at least we can
544 // check if the test does not crash.
545}
546
[email protected]4c985c62011-12-13 17:08:42547TEST_F(EndToEndAsyncTest, TestSignal) {
[email protected]3beaaa4e2011-08-23 07:29:21548 const char kMessage[] = "hello, world";
549 // Send the test signal from the exported object.
550 test_service_->SendTestSignal(kMessage);
[email protected]829f0e4c2011-08-31 18:02:43551 // Receive the signal with the object proxy. The signal is handled in
[email protected]3beaaa4e2011-08-23 07:29:21552 // EndToEndAsyncTest::OnTestSignal() in the main thread.
553 WaitForTestSignal();
554 ASSERT_EQ(kMessage, test_signal_string_);
555}
[email protected]df159b22011-09-14 22:10:24556
[email protected]4c985c62011-12-13 17:08:42557TEST_F(EndToEndAsyncTest, TestSignalFromRoot) {
[email protected]df159b22011-09-14 22:10:24558 const char kMessage[] = "hello, world";
[email protected]4d97f23372012-03-01 04:01:05559 // Object proxies are tied to a particular object path, if a signal
560 // arrives from a different object path like "/" the first object proxy
561 // |object_proxy_| should not handle it, and should leave it for the root
562 // object proxy |root_object_proxy_|.
[email protected]df159b22011-09-14 22:10:24563 test_service_->SendTestSignalFromRoot(kMessage);
[email protected]df159b22011-09-14 22:10:24564 WaitForTestSignal();
[email protected]4d97f23372012-03-01 04:01:05565 // Verify the signal was not received by the specific proxy.
566 ASSERT_TRUE(test_signal_string_.empty());
567 // Verify the string WAS received by the root proxy.
568 ASSERT_EQ(kMessage, root_test_signal_string_);
[email protected]df159b22011-09-14 22:10:24569}
[email protected]3b6205c2012-03-21 23:39:17570
[email protected]3635d322012-06-02 02:53:20571TEST_F(EndToEndAsyncTest, TestHugeSignal) {
572 const std::string kHugeMessage(kHugePayloadSize, 'o');
573
574 // Send the huge signal from the exported object.
575 test_service_->SendTestSignal(kHugeMessage);
576 // This caused a DCHECK failure before. Ensure that the issue is fixed.
577 WaitForTestSignal();
578 ASSERT_EQ(kHugeMessage, test_signal_string_);
579}
580
[email protected]ea545192013-02-15 16:44:40581TEST_F(EndToEndAsyncTest, DisconnectedSignal) {
582 bus_->PostTaskToDBusThread(FROM_HERE,
583 base::Bind(&dbus::Bus::ClosePrivateConnection,
584 base::Unretained(bus_.get())));
585 // OnDisconnected callback quits message loop.
586 message_loop_.Run();
587 EXPECT_EQ(1, on_disconnected_call_count_);
588}
589
[email protected]012e781dc2013-04-24 00:12:35590class SignalMultipleHandlerTest : public EndToEndAsyncTest {
[email protected]3b6205c2012-03-21 23:39:17591 public:
[email protected]012e781dc2013-04-24 00:12:35592 SignalMultipleHandlerTest() {
[email protected]3b6205c2012-03-21 23:39:17593 }
594
595 virtual void SetUp() {
596 // Set up base class.
597 EndToEndAsyncTest::SetUp();
598
[email protected]012e781dc2013-04-24 00:12:35599 // Connect the root object proxy's signal handler to a new handler
[email protected]3b6205c2012-03-21 23:39:17600 // so that we can verify that a second call to ConnectSignal() delivers
[email protected]012e781dc2013-04-24 00:12:35601 // to both our new handler and the old.
[email protected]3b6205c2012-03-21 23:39:17602 object_proxy_->ConnectToSignal(
603 "org.chromium.TestInterface",
604 "Test",
[email protected]012e781dc2013-04-24 00:12:35605 base::Bind(&SignalMultipleHandlerTest::OnAdditionalTestSignal,
[email protected]3b6205c2012-03-21 23:39:17606 base::Unretained(this)),
[email protected]012e781dc2013-04-24 00:12:35607 base::Bind(&SignalMultipleHandlerTest::OnAdditionalConnected,
[email protected]3b6205c2012-03-21 23:39:17608 base::Unretained(this)));
609 // Wait until the object proxy is connected to the signal.
610 message_loop_.Run();
611 }
612
613 protected:
614 // Called when the "Test" signal is received, in the main thread.
[email protected]012e781dc2013-04-24 00:12:35615 // Copy the string payload to |additional_test_signal_string_|.
616 void OnAdditionalTestSignal(dbus::Signal* signal) {
[email protected]3b6205c2012-03-21 23:39:17617 dbus::MessageReader reader(signal);
[email protected]012e781dc2013-04-24 00:12:35618 ASSERT_TRUE(reader.PopString(&additional_test_signal_string_));
[email protected]3b6205c2012-03-21 23:39:17619 message_loop_.Quit();
620 }
621
622 // Called when connected to the signal.
[email protected]012e781dc2013-04-24 00:12:35623 void OnAdditionalConnected(const std::string& interface_name,
624 const std::string& signal_name,
625 bool success) {
[email protected]3b6205c2012-03-21 23:39:17626 ASSERT_TRUE(success);
627 message_loop_.Quit();
628 }
629
[email protected]012e781dc2013-04-24 00:12:35630 // Text message from "Test" signal delivered to additional handler.
631 std::string additional_test_signal_string_;
[email protected]3b6205c2012-03-21 23:39:17632};
633
[email protected]012e781dc2013-04-24 00:12:35634TEST_F(SignalMultipleHandlerTest, TestMultipleHandlers) {
[email protected]3b6205c2012-03-21 23:39:17635 const char kMessage[] = "hello, world";
636 // Send the test signal from the exported object.
637 test_service_->SendTestSignal(kMessage);
638 // Receive the signal with the object proxy.
639 WaitForTestSignal();
[email protected]012e781dc2013-04-24 00:12:35640 // Verify the string WAS received by the original handler.
641 ASSERT_EQ(kMessage, test_signal_string_);
642 // Verify the signal WAS ALSO received by the additional handler.
643 ASSERT_EQ(kMessage, additional_test_signal_string_);
[email protected]3b6205c2012-03-21 23:39:17644}