| 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 <algorithm> | 5 #include <algorithm> |
| 6 #include <string> | 6 #include <string> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 // See comments in ObjectProxy::RunResponseCallback() for why the number was | 25 // See comments in ObjectProxy::RunResponseCallback() for why the number was |
| 26 // chosen. | 26 // chosen. |
| 27 const int kHugePayloadSize = 64 << 20; // 64 MB | 27 const int kHugePayloadSize = 64 << 20; // 64 MB |
| 28 | 28 |
| 29 } // namespace | 29 } // namespace |
| 30 | 30 |
| 31 // The end-to-end test exercises the asynchronous APIs in ObjectProxy and | 31 // The end-to-end test exercises the asynchronous APIs in ObjectProxy and |
| 32 // ExportedObject. | 32 // ExportedObject. |
| 33 class EndToEndAsyncTest : public testing::Test { | 33 class EndToEndAsyncTest : public testing::Test { |
| 34 public: | 34 public: |
| 35 EndToEndAsyncTest() { | 35 EndToEndAsyncTest() : on_disconnected_call_count_(0) {} |
| 36 } | |
| 37 | 36 |
| 38 virtual void SetUp() { | 37 virtual void SetUp() { |
| 39 // Make the main thread not to allow IO. | 38 // Make the main thread not to allow IO. |
| 40 base::ThreadRestrictions::SetIOAllowed(false); | 39 base::ThreadRestrictions::SetIOAllowed(false); |
| 41 | 40 |
| 42 // Start the D-Bus thread. | 41 // Start the D-Bus thread. |
| 43 dbus_thread_.reset(new base::Thread("D-Bus Thread")); | 42 dbus_thread_.reset(new base::Thread("D-Bus Thread")); |
| 44 base::Thread::Options thread_options; | 43 base::Thread::Options thread_options; |
| 45 thread_options.message_loop_type = MessageLoop::TYPE_IO; | 44 thread_options.message_loop_type = MessageLoop::TYPE_IO; |
| 46 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options)); | 45 ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options)); |
| 47 | 46 |
| 48 // Start the test service, using the D-Bus thread. | 47 // Start the test service, using the D-Bus thread. |
| 49 dbus::TestService::Options options; | 48 dbus::TestService::Options options; |
| 50 options.dbus_thread_message_loop_proxy = dbus_thread_->message_loop_proxy(); | 49 options.dbus_thread_message_loop_proxy = dbus_thread_->message_loop_proxy(); |
| 51 test_service_.reset(new dbus::TestService(options)); | 50 test_service_.reset(new dbus::TestService(options)); |
| 52 ASSERT_TRUE(test_service_->StartService()); | 51 ASSERT_TRUE(test_service_->StartService()); |
| 53 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted()); | 52 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted()); |
| 54 ASSERT_TRUE(test_service_->HasDBusThread()); | 53 ASSERT_TRUE(test_service_->HasDBusThread()); |
| 55 | 54 |
| 56 // Create the client, using the D-Bus thread. | 55 // Create the client, using the D-Bus thread. |
| 57 dbus::Bus::Options bus_options; | 56 dbus::Bus::Options bus_options; |
| 58 bus_options.bus_type = dbus::Bus::SESSION; | 57 bus_options.bus_type = dbus::Bus::SESSION; |
| 59 bus_options.connection_type = dbus::Bus::PRIVATE; | 58 bus_options.connection_type = dbus::Bus::PRIVATE; |
| 60 bus_options.dbus_thread_message_loop_proxy = | 59 bus_options.dbus_thread_message_loop_proxy = |
| 61 dbus_thread_->message_loop_proxy(); | 60 dbus_thread_->message_loop_proxy(); |
| 61 bus_options.disconnected_callback = |
| 62 base::Bind(&EndToEndAsyncTest::OnDisconnected, base::Unretained(this)); |
| 62 bus_ = new dbus::Bus(bus_options); | 63 bus_ = new dbus::Bus(bus_options); |
| 63 object_proxy_ = bus_->GetObjectProxy( | 64 object_proxy_ = bus_->GetObjectProxy( |
| 64 "org.chromium.TestService", | 65 "org.chromium.TestService", |
| 65 dbus::ObjectPath("/org/chromium/TestObject")); | 66 dbus::ObjectPath("/org/chromium/TestObject")); |
| 66 ASSERT_TRUE(bus_->HasDBusThread()); | 67 ASSERT_TRUE(bus_->HasDBusThread()); |
| 67 | 68 |
| 68 // Connect to the "Test" signal of "org.chromium.TestInterface" from | 69 // Connect to the "Test" signal of "org.chromium.TestInterface" from |
| 69 // the remote object. | 70 // the remote object. |
| 70 object_proxy_->ConnectToSignal( | 71 object_proxy_->ConnectToSignal( |
| 71 "org.chromium.TestInterface", | 72 "org.chromium.TestInterface", |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 } | 236 } |
| 236 | 237 |
| 237 // Called when connected to the signal. | 238 // Called when connected to the signal. |
| 238 void OnConnected(const std::string& interface_name, | 239 void OnConnected(const std::string& interface_name, |
| 239 const std::string& signal_name, | 240 const std::string& signal_name, |
| 240 bool success) { | 241 bool success) { |
| 241 ASSERT_TRUE(success); | 242 ASSERT_TRUE(success); |
| 242 message_loop_.Quit(); | 243 message_loop_.Quit(); |
| 243 } | 244 } |
| 244 | 245 |
| 246 // Called when the connection with dbus-daemon is disconnected. |
| 247 void OnDisconnected() { |
| 248 message_loop_.Quit(); |
| 249 ++on_disconnected_call_count_; |
| 250 } |
| 251 |
| 245 // Wait for the hey signal to be received. | 252 // Wait for the hey signal to be received. |
| 246 void WaitForTestSignal() { | 253 void WaitForTestSignal() { |
| 247 // OnTestSignal() will quit the message loop. | 254 // OnTestSignal() will quit the message loop. |
| 248 message_loop_.Run(); | 255 message_loop_.Run(); |
| 249 } | 256 } |
| 250 | 257 |
| 251 MessageLoop message_loop_; | 258 MessageLoop message_loop_; |
| 252 std::vector<std::string> response_strings_; | 259 std::vector<std::string> response_strings_; |
| 253 std::vector<std::string> error_names_; | 260 std::vector<std::string> error_names_; |
| 254 scoped_ptr<base::Thread> dbus_thread_; | 261 scoped_ptr<base::Thread> dbus_thread_; |
| 255 scoped_refptr<dbus::Bus> bus_; | 262 scoped_refptr<dbus::Bus> bus_; |
| 256 dbus::ObjectProxy* object_proxy_; | 263 dbus::ObjectProxy* object_proxy_; |
| 257 dbus::ObjectProxy* root_object_proxy_; | 264 dbus::ObjectProxy* root_object_proxy_; |
| 258 scoped_ptr<dbus::TestService> test_service_; | 265 scoped_ptr<dbus::TestService> test_service_; |
| 259 // Text message from "Test" signal. | 266 // Text message from "Test" signal. |
| 260 std::string test_signal_string_; | 267 std::string test_signal_string_; |
| 261 // Text message from "Test" signal delivered to root. | 268 // Text message from "Test" signal delivered to root. |
| 262 std::string root_test_signal_string_; | 269 std::string root_test_signal_string_; |
| 270 int on_disconnected_call_count_; |
| 263 }; | 271 }; |
| 264 | 272 |
| 265 TEST_F(EndToEndAsyncTest, Echo) { | 273 TEST_F(EndToEndAsyncTest, Echo) { |
| 266 const char* kHello = "hello"; | 274 const char* kHello = "hello"; |
| 267 | 275 |
| 268 // Create the method call. | 276 // Create the method call. |
| 269 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo"); | 277 dbus::MethodCall method_call("org.chromium.TestInterface", "Echo"); |
| 270 dbus::MessageWriter writer(&method_call); | 278 dbus::MessageWriter writer(&method_call); |
| 271 writer.AppendString(kHello); | 279 writer.AppendString(kHello); |
| 272 | 280 |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 565 TEST_F(EndToEndAsyncTest, TestHugeSignal) { | 573 TEST_F(EndToEndAsyncTest, TestHugeSignal) { |
| 566 const std::string kHugeMessage(kHugePayloadSize, 'o'); | 574 const std::string kHugeMessage(kHugePayloadSize, 'o'); |
| 567 | 575 |
| 568 // Send the huge signal from the exported object. | 576 // Send the huge signal from the exported object. |
| 569 test_service_->SendTestSignal(kHugeMessage); | 577 test_service_->SendTestSignal(kHugeMessage); |
| 570 // This caused a DCHECK failure before. Ensure that the issue is fixed. | 578 // This caused a DCHECK failure before. Ensure that the issue is fixed. |
| 571 WaitForTestSignal(); | 579 WaitForTestSignal(); |
| 572 ASSERT_EQ(kHugeMessage, test_signal_string_); | 580 ASSERT_EQ(kHugeMessage, test_signal_string_); |
| 573 } | 581 } |
| 574 | 582 |
| 583 TEST_F(EndToEndAsyncTest, DisconnectedSignal) { |
| 584 bus_->PostTaskToDBusThread(FROM_HERE, |
| 585 base::Bind(&dbus::Bus::ClosePrivateConnection, |
| 586 base::Unretained(bus_.get()))); |
| 587 // OnDisconnected callback quits message loop. |
| 588 message_loop_.Run(); |
| 589 EXPECT_EQ(1, on_disconnected_call_count_); |
| 590 } |
| 591 |
| 575 class SignalReplacementTest : public EndToEndAsyncTest { | 592 class SignalReplacementTest : public EndToEndAsyncTest { |
| 576 public: | 593 public: |
| 577 SignalReplacementTest() { | 594 SignalReplacementTest() { |
| 578 } | 595 } |
| 579 | 596 |
| 580 virtual void SetUp() { | 597 virtual void SetUp() { |
| 581 // Set up base class. | 598 // Set up base class. |
| 582 EndToEndAsyncTest::SetUp(); | 599 EndToEndAsyncTest::SetUp(); |
| 583 | 600 |
| 584 // Reconnect the root object proxy's signal handler to a new handler | 601 // Reconnect the root object proxy's signal handler to a new handler |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 620 const char kMessage[] = "hello, world"; | 637 const char kMessage[] = "hello, world"; |
| 621 // Send the test signal from the exported object. | 638 // Send the test signal from the exported object. |
| 622 test_service_->SendTestSignal(kMessage); | 639 test_service_->SendTestSignal(kMessage); |
| 623 // Receive the signal with the object proxy. | 640 // Receive the signal with the object proxy. |
| 624 WaitForTestSignal(); | 641 WaitForTestSignal(); |
| 625 // Verify the string WAS NOT received by the original handler. | 642 // Verify the string WAS NOT received by the original handler. |
| 626 ASSERT_TRUE(test_signal_string_.empty()); | 643 ASSERT_TRUE(test_signal_string_.empty()); |
| 627 // Verify the signal WAS received by the replacement handler. | 644 // Verify the signal WAS received by the replacement handler. |
| 628 ASSERT_EQ(kMessage, replacement_test_signal_string_); | 645 ASSERT_EQ(kMessage, replacement_test_signal_string_); |
| 629 } | 646 } |
| OLD | NEW |