Chromium Code Reviews| 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" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" | |
| 12 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 13 #include "base/test/test_timeouts.h" | 14 #include "base/test/test_timeouts.h" |
| 14 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
| 15 #include "base/threading/thread_restrictions.h" | 16 #include "base/threading/thread_restrictions.h" |
| 16 #include "dbus/bus.h" | 17 #include "dbus/bus.h" |
| 17 #include "dbus/message.h" | 18 #include "dbus/message.h" |
| 18 #include "dbus/object_path.h" | 19 #include "dbus/object_path.h" |
| 19 #include "dbus/object_proxy.h" | 20 #include "dbus/object_proxy.h" |
| 20 #include "dbus/test_service.h" | 21 #include "dbus/test_service.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 70 // Connect to the "Test" signal of "org.chromium.TestInterface" from | 71 // Connect to the "Test" signal of "org.chromium.TestInterface" from |
| 71 // the remote object. | 72 // the remote object. |
| 72 object_proxy_->ConnectToSignal( | 73 object_proxy_->ConnectToSignal( |
| 73 "org.chromium.TestInterface", | 74 "org.chromium.TestInterface", |
| 74 "Test", | 75 "Test", |
| 75 base::Bind(&EndToEndAsyncTest::OnTestSignal, | 76 base::Bind(&EndToEndAsyncTest::OnTestSignal, |
| 76 base::Unretained(this)), | 77 base::Unretained(this)), |
| 77 base::Bind(&EndToEndAsyncTest::OnConnected, | 78 base::Bind(&EndToEndAsyncTest::OnConnected, |
| 78 base::Unretained(this))); | 79 base::Unretained(this))); |
| 79 // Wait until the object proxy is connected to the signal. | 80 // Wait until the object proxy is connected to the signal. |
| 80 message_loop_.Run(); | 81 base::RunLoop().Run(); |
| 81 | 82 |
| 82 // Connect to the "Test2" signal of "org.chromium.TestInterface" from | 83 // Connect to the "Test2" signal of "org.chromium.TestInterface" from |
| 83 // the remote object. There was a bug where we were emitting error | 84 // the remote object. There was a bug where we were emitting error |
| 84 // messages like "Requested to remove an unknown match rule: ..." at | 85 // messages like "Requested to remove an unknown match rule: ..." at |
| 85 // the shutdown of Bus when an object proxy is connected to more than | 86 // the shutdown of Bus when an object proxy is connected to more than |
| 86 // one signal of the same interface. See crosbug.com/23382 for details. | 87 // one signal of the same interface. See crosbug.com/23382 for details. |
| 87 object_proxy_->ConnectToSignal( | 88 object_proxy_->ConnectToSignal( |
| 88 "org.chromium.TestInterface", | 89 "org.chromium.TestInterface", |
| 89 "Test2", | 90 "Test2", |
| 90 base::Bind(&EndToEndAsyncTest::OnTest2Signal, | 91 base::Bind(&EndToEndAsyncTest::OnTest2Signal, |
| 91 base::Unretained(this)), | 92 base::Unretained(this)), |
| 92 base::Bind(&EndToEndAsyncTest::OnConnected, | 93 base::Bind(&EndToEndAsyncTest::OnConnected, |
| 93 base::Unretained(this))); | 94 base::Unretained(this))); |
| 94 // Wait until the object proxy is connected to the signal. | 95 // Wait until the object proxy is connected to the signal. |
| 95 message_loop_.Run(); | 96 base::RunLoop().Run(); |
| 96 | 97 |
| 97 // Create a second object proxy for the root object. | 98 // Create a second object proxy for the root object. |
| 98 root_object_proxy_ = bus_->GetObjectProxy("org.chromium.TestService", | 99 root_object_proxy_ = bus_->GetObjectProxy("org.chromium.TestService", |
| 99 ObjectPath("/")); | 100 ObjectPath("/")); |
| 100 ASSERT_TRUE(bus_->HasDBusThread()); | 101 ASSERT_TRUE(bus_->HasDBusThread()); |
| 101 | 102 |
| 102 // Connect to the "Test" signal of "org.chromium.TestInterface" from | 103 // Connect to the "Test" signal of "org.chromium.TestInterface" from |
| 103 // the root remote object too. | 104 // the root remote object too. |
| 104 root_object_proxy_->ConnectToSignal( | 105 root_object_proxy_->ConnectToSignal( |
| 105 "org.chromium.TestInterface", | 106 "org.chromium.TestInterface", |
| 106 "Test", | 107 "Test", |
| 107 base::Bind(&EndToEndAsyncTest::OnRootTestSignal, | 108 base::Bind(&EndToEndAsyncTest::OnRootTestSignal, |
| 108 base::Unretained(this)), | 109 base::Unretained(this)), |
| 109 base::Bind(&EndToEndAsyncTest::OnConnected, | 110 base::Bind(&EndToEndAsyncTest::OnConnected, |
| 110 base::Unretained(this))); | 111 base::Unretained(this))); |
| 111 // Wait until the root object proxy is connected to the signal. | 112 // Wait until the root object proxy is connected to the signal. |
| 112 message_loop_.Run(); | 113 base::RunLoop().Run(); |
| 113 } | 114 } |
| 114 | 115 |
| 115 virtual void TearDown() { | 116 virtual void TearDown() { |
| 116 bus_->ShutdownOnDBusThreadAndBlock(); | 117 bus_->ShutdownOnDBusThreadAndBlock(); |
| 117 | 118 |
| 118 // Shut down the service. | 119 // Shut down the service. |
| 119 test_service_->ShutdownAndBlock(); | 120 test_service_->ShutdownAndBlock(); |
| 120 | 121 |
| 121 // Reset to the default. | 122 // Reset to the default. |
| 122 base::ThreadRestrictions::SetIOAllowed(true); | 123 base::ThreadRestrictions::SetIOAllowed(true); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 165 object_proxy_->CallMethodWithErrorCallback( | 166 object_proxy_->CallMethodWithErrorCallback( |
| 166 method_call, | 167 method_call, |
| 167 timeout_ms, | 168 timeout_ms, |
| 168 base::Bind(&EndToEndAsyncTest::OnResponse, base::Unretained(this)), | 169 base::Bind(&EndToEndAsyncTest::OnResponse, base::Unretained(this)), |
| 169 base::Bind(&EndToEndAsyncTest::OnError, base::Unretained(this))); | 170 base::Bind(&EndToEndAsyncTest::OnError, base::Unretained(this))); |
| 170 } | 171 } |
| 171 | 172 |
| 172 // Wait for the give number of responses. | 173 // Wait for the give number of responses. |
| 173 void WaitForResponses(size_t num_responses) { | 174 void WaitForResponses(size_t num_responses) { |
| 174 while (response_strings_.size() < num_responses) { | 175 while (response_strings_.size() < num_responses) { |
| 175 message_loop_.Run(); | 176 base::RunLoop().Run(); |
| 176 } | 177 } |
| 177 } | 178 } |
| 178 | 179 |
| 179 // Called when the response is received. | 180 // Called when the response is received. |
| 180 void OnResponse(Response* response) { | 181 void OnResponse(Response* response) { |
| 181 // |response| will be deleted on exit of the function. Copy the | 182 // |response| will be deleted on exit of the function. Copy the |
| 182 // payload to |response_strings_|. | 183 // payload to |response_strings_|. |
| 183 if (response) { | 184 if (response) { |
| 184 MessageReader reader(response); | 185 MessageReader reader(response); |
| 185 std::string response_string; | 186 std::string response_string; |
| 186 ASSERT_TRUE(reader.PopString(&response_string)); | 187 ASSERT_TRUE(reader.PopString(&response_string)); |
| 187 response_strings_.push_back(response_string); | 188 response_strings_.push_back(response_string); |
| 188 } else { | 189 } else { |
| 189 response_strings_.push_back(std::string()); | 190 response_strings_.push_back(std::string()); |
| 190 } | 191 } |
| 191 message_loop_.Quit(); | 192 message_loop_.Quit(); |
|
hashimoto
2014/09/02 03:34:02
Why don't you replace this MessageLoop::Quit call
| |
| 192 }; | 193 }; |
| 193 | 194 |
| 194 // Wait for the given number of errors. | 195 // Wait for the given number of errors. |
| 195 void WaitForErrors(size_t num_errors) { | 196 void WaitForErrors(size_t num_errors) { |
| 196 while (error_names_.size() < num_errors) { | 197 while (error_names_.size() < num_errors) { |
| 197 message_loop_.Run(); | 198 base::RunLoop().Run(); |
| 198 } | 199 } |
| 199 } | 200 } |
| 200 | 201 |
| 201 // Called when an error is received. | 202 // Called when an error is received. |
| 202 void OnError(ErrorResponse* error) { | 203 void OnError(ErrorResponse* error) { |
| 203 // |error| will be deleted on exit of the function. Copy the payload to | 204 // |error| will be deleted on exit of the function. Copy the payload to |
| 204 // |error_names_|. | 205 // |error_names_|. |
| 205 if (error) { | 206 if (error) { |
| 206 ASSERT_NE("", error->GetErrorName()); | 207 ASSERT_NE("", error->GetErrorName()); |
| 207 error_names_.push_back(error->GetErrorName()); | 208 error_names_.push_back(error->GetErrorName()); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 244 | 245 |
| 245 // Called when the connection with dbus-daemon is disconnected. | 246 // Called when the connection with dbus-daemon is disconnected. |
| 246 void OnDisconnected() { | 247 void OnDisconnected() { |
| 247 message_loop_.Quit(); | 248 message_loop_.Quit(); |
| 248 ++on_disconnected_call_count_; | 249 ++on_disconnected_call_count_; |
| 249 } | 250 } |
| 250 | 251 |
| 251 // Wait for the hey signal to be received. | 252 // Wait for the hey signal to be received. |
| 252 void WaitForTestSignal() { | 253 void WaitForTestSignal() { |
| 253 // OnTestSignal() will quit the message loop. | 254 // OnTestSignal() will quit the message loop. |
| 254 message_loop_.Run(); | 255 base::RunLoop().Run(); |
| 255 } | 256 } |
| 256 | 257 |
| 257 base::MessageLoop message_loop_; | 258 base::MessageLoop message_loop_; |
| 258 std::vector<std::string> response_strings_; | 259 std::vector<std::string> response_strings_; |
| 259 std::vector<std::string> error_names_; | 260 std::vector<std::string> error_names_; |
| 260 scoped_ptr<base::Thread> dbus_thread_; | 261 scoped_ptr<base::Thread> dbus_thread_; |
| 261 scoped_refptr<Bus> bus_; | 262 scoped_refptr<Bus> bus_; |
| 262 ObjectProxy* object_proxy_; | 263 ObjectProxy* object_proxy_; |
| 263 ObjectProxy* root_object_proxy_; | 264 ObjectProxy* root_object_proxy_; |
| 264 scoped_ptr<TestService> test_service_; | 265 scoped_ptr<TestService> test_service_; |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 533 | 534 |
| 534 // Call the method with an empty callback. | 535 // Call the method with an empty callback. |
| 535 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT; | 536 const int timeout_ms = ObjectProxy::TIMEOUT_USE_DEFAULT; |
| 536 object_proxy_->CallMethod(&method_call, | 537 object_proxy_->CallMethod(&method_call, |
| 537 timeout_ms, | 538 timeout_ms, |
| 538 ObjectProxy::EmptyResponseCallback()); | 539 ObjectProxy::EmptyResponseCallback()); |
| 539 // Post a delayed task to quit the message loop. | 540 // Post a delayed task to quit the message loop. |
| 540 message_loop_.PostDelayedTask(FROM_HERE, | 541 message_loop_.PostDelayedTask(FROM_HERE, |
| 541 base::MessageLoop::QuitClosure(), | 542 base::MessageLoop::QuitClosure(), |
| 542 TestTimeouts::tiny_timeout()); | 543 TestTimeouts::tiny_timeout()); |
| 543 message_loop_.Run(); | 544 base::RunLoop().Run(); |
| 544 // We cannot tell if the empty callback is called, but at least we can | 545 // We cannot tell if the empty callback is called, but at least we can |
| 545 // check if the test does not crash. | 546 // check if the test does not crash. |
| 546 } | 547 } |
| 547 | 548 |
| 548 TEST_F(EndToEndAsyncTest, TestSignal) { | 549 TEST_F(EndToEndAsyncTest, TestSignal) { |
| 549 const char kMessage[] = "hello, world"; | 550 const char kMessage[] = "hello, world"; |
| 550 // Send the test signal from the exported object. | 551 // Send the test signal from the exported object. |
| 551 test_service_->SendTestSignal(kMessage); | 552 test_service_->SendTestSignal(kMessage); |
| 552 // Receive the signal with the object proxy. The signal is handled in | 553 // Receive the signal with the object proxy. The signal is handled in |
| 553 // EndToEndAsyncTest::OnTestSignal() in the main thread. | 554 // EndToEndAsyncTest::OnTestSignal() in the main thread. |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 577 // 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. |
| 578 WaitForTestSignal(); | 579 WaitForTestSignal(); |
| 579 ASSERT_EQ(kHugeMessage, test_signal_string_); | 580 ASSERT_EQ(kHugeMessage, test_signal_string_); |
| 580 } | 581 } |
| 581 | 582 |
| 582 TEST_F(EndToEndAsyncTest, DisconnectedSignal) { | 583 TEST_F(EndToEndAsyncTest, DisconnectedSignal) { |
| 583 bus_->GetDBusTaskRunner()->PostTask(FROM_HERE, | 584 bus_->GetDBusTaskRunner()->PostTask(FROM_HERE, |
| 584 base::Bind(&Bus::ClosePrivateConnection, | 585 base::Bind(&Bus::ClosePrivateConnection, |
| 585 base::Unretained(bus_.get()))); | 586 base::Unretained(bus_.get()))); |
| 586 // OnDisconnected callback quits message loop. | 587 // OnDisconnected callback quits message loop. |
| 587 message_loop_.Run(); | 588 base::RunLoop().Run(); |
| 588 EXPECT_EQ(1, on_disconnected_call_count_); | 589 EXPECT_EQ(1, on_disconnected_call_count_); |
| 589 } | 590 } |
| 590 | 591 |
| 591 class SignalMultipleHandlerTest : public EndToEndAsyncTest { | 592 class SignalMultipleHandlerTest : public EndToEndAsyncTest { |
| 592 public: | 593 public: |
| 593 SignalMultipleHandlerTest() { | 594 SignalMultipleHandlerTest() { |
| 594 } | 595 } |
| 595 | 596 |
| 596 virtual void SetUp() { | 597 virtual void SetUp() { |
| 597 // Set up base class. | 598 // Set up base class. |
| 598 EndToEndAsyncTest::SetUp(); | 599 EndToEndAsyncTest::SetUp(); |
| 599 | 600 |
| 600 // Connect the root object proxy's signal handler to a new handler | 601 // Connect the root object proxy's signal handler to a new handler |
| 601 // so that we can verify that a second call to ConnectSignal() delivers | 602 // so that we can verify that a second call to ConnectSignal() delivers |
| 602 // to both our new handler and the old. | 603 // to both our new handler and the old. |
| 603 object_proxy_->ConnectToSignal( | 604 object_proxy_->ConnectToSignal( |
| 604 "org.chromium.TestInterface", | 605 "org.chromium.TestInterface", |
| 605 "Test", | 606 "Test", |
| 606 base::Bind(&SignalMultipleHandlerTest::OnAdditionalTestSignal, | 607 base::Bind(&SignalMultipleHandlerTest::OnAdditionalTestSignal, |
| 607 base::Unretained(this)), | 608 base::Unretained(this)), |
| 608 base::Bind(&SignalMultipleHandlerTest::OnAdditionalConnected, | 609 base::Bind(&SignalMultipleHandlerTest::OnAdditionalConnected, |
| 609 base::Unretained(this))); | 610 base::Unretained(this))); |
| 610 // Wait until the object proxy is connected to the signal. | 611 // Wait until the object proxy is connected to the signal. |
| 611 message_loop_.Run(); | 612 base::RunLoop().Run(); |
| 612 } | 613 } |
| 613 | 614 |
| 614 protected: | 615 protected: |
| 615 // Called when the "Test" signal is received, in the main thread. | 616 // Called when the "Test" signal is received, in the main thread. |
| 616 // Copy the string payload to |additional_test_signal_string_|. | 617 // Copy the string payload to |additional_test_signal_string_|. |
| 617 void OnAdditionalTestSignal(Signal* signal) { | 618 void OnAdditionalTestSignal(Signal* signal) { |
| 618 MessageReader reader(signal); | 619 MessageReader reader(signal); |
| 619 ASSERT_TRUE(reader.PopString(&additional_test_signal_string_)); | 620 ASSERT_TRUE(reader.PopString(&additional_test_signal_string_)); |
| 620 message_loop_.Quit(); | 621 message_loop_.Quit(); |
| 621 } | 622 } |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 638 test_service_->SendTestSignal(kMessage); | 639 test_service_->SendTestSignal(kMessage); |
| 639 // Receive the signal with the object proxy. | 640 // Receive the signal with the object proxy. |
| 640 WaitForTestSignal(); | 641 WaitForTestSignal(); |
| 641 // Verify the string WAS received by the original handler. | 642 // Verify the string WAS received by the original handler. |
| 642 ASSERT_EQ(kMessage, test_signal_string_); | 643 ASSERT_EQ(kMessage, test_signal_string_); |
| 643 // Verify the signal WAS ALSO received by the additional handler. | 644 // Verify the signal WAS ALSO received by the additional handler. |
| 644 ASSERT_EQ(kMessage, additional_test_signal_string_); | 645 ASSERT_EQ(kMessage, additional_test_signal_string_); |
| 645 } | 646 } |
| 646 | 647 |
| 647 } // namespace dbus | 648 } // namespace dbus |
| OLD | NEW |