Chromium Code Reviews| Index: dbus/bus_unittest.cc |
| =================================================================== |
| --- dbus/bus_unittest.cc (revision 204929) |
| +++ dbus/bus_unittest.cc (working copy) |
| @@ -7,11 +7,13 @@ |
| #include "base/bind.h" |
| #include "base/message_loop.h" |
| #include "base/memory/ref_counted.h" |
| +#include "base/run_loop.h" |
| #include "base/threading/thread.h" |
| #include "dbus/exported_object.h" |
| #include "dbus/object_path.h" |
| #include "dbus/object_proxy.h" |
| #include "dbus/scoped_dbus_error.h" |
| +#include "dbus/test_service.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| @@ -24,6 +26,45 @@ |
| return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| } |
| +// test helpers for BusTest.ListenForServiceOwnerChange. |
|
satorux1
2013/06/10 04:58:46
This class does a non-trivial thing. Could you wri
Lei Zhang
2013/06/10 05:20:32
Done.
|
| +class RunLoopState { |
|
satorux1
2013/06/10 04:58:46
RunLoopWithExpectedCount may be a bit more descrip
Lei Zhang
2013/06/10 05:20:32
Done.
|
| + public: |
| + RunLoopState() : expected_quit_calls_(0), actual_quit_calls_(0) {} |
| + ~RunLoopState() {} |
| + |
| + void Run(int expected_quit_calls) { |
| + DCHECK_EQ(0, expected_quit_calls_); |
| + DCHECK_EQ(0, actual_quit_calls_); |
| + expected_quit_calls_ = expected_quit_calls; |
| + run_loop_.reset(new base::RunLoop()); |
| + run_loop_->Run(); |
| + } |
| + |
| + void Quit() { |
|
satorux1
2013/06/10 04:58:46
Quit() may be a misnomer as this may not quit. May
Lei Zhang
2013/06/10 05:20:32
Done.
|
| + if (++actual_quit_calls_ != expected_quit_calls_) |
| + return; |
| + run_loop_->Quit(); |
| + expected_quit_calls_ = 0; |
| + actual_quit_calls_ = 0; |
| + } |
| + |
| + private: |
| + scoped_ptr<base::RunLoop> run_loop_; |
| + int expected_quit_calls_; |
| + int actual_quit_calls_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(RunLoopState); |
| +}; |
| + |
| +void OnServiceOwnerChanged(RunLoopState* run_loop, |
|
satorux1
2013/06/10 04:58:46
run_loop_state ?
Lei Zhang
2013/06/10 05:20:32
Done.
|
| + std::string* service_owner, |
| + int* num_of_owner_changes, |
| + const std::string& new_service_owner) { |
| + *service_owner = new_service_owner; |
| + ++(*num_of_owner_changes); |
| + run_loop->Quit(); |
| +} |
| + |
| } // namespace |
| TEST(BusTest, GetObjectProxy) { |
| @@ -296,3 +337,80 @@ |
| bus->ShutdownAndBlock(); |
| } |
| + |
| +TEST(BusTest, ListenForServiceOwnerChange) { |
| + // Setup the current thread's MessageLoop. Must be of TYPE_IO for the |
| + // listeners to work. |
| + base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); |
| + RunLoopState run_loop_state; |
| + |
| + // Create the bus. |
| + dbus::Bus::Options bus_options; |
| + scoped_refptr<dbus::Bus> bus = new dbus::Bus(bus_options); |
| + |
| + // Add a listener. |
| + std::string service_owner1; |
| + int num_of_owner_changes1 = 0; |
| + dbus::Bus::GetServiceOwnerCallback callback1 = |
| + base::Bind(&OnServiceOwnerChanged, |
| + &run_loop_state, |
| + &service_owner1, |
| + &num_of_owner_changes1); |
| + bus->ListenForServiceOwnerChange("org.chromium.TestService", callback1); |
| + // This should be a no-op. |
| + bus->ListenForServiceOwnerChange("org.chromium.TestService", callback1); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + // Nothing has happened yet. Check initial state. |
| + EXPECT_TRUE(service_owner1.empty()); |
| + EXPECT_EQ(0, num_of_owner_changes1); |
| + |
| + // Make an ownership change. |
| + ASSERT_TRUE(bus->RequestOwnershipAndBlock("org.chromium.TestService")); |
| + run_loop_state.Run(1); |
| + |
| + { |
| + // Get the current service owner and check to make sure the listener got |
| + // the right value. |
| + std::string current_service_owner = |
| + bus->GetServiceOwnerAndBlock("org.chromium.TestService", |
| + dbus::Bus::REPORT_ERRORS); |
| + ASSERT_FALSE(current_service_owner.empty()); |
| + |
| + // Make sure the listener heard about the new owner. |
| + EXPECT_EQ(current_service_owner, service_owner1); |
| + |
| + // Test the second ListenForServiceOwnerChange() above is indeed a no-op. |
| + EXPECT_EQ(1, num_of_owner_changes1); |
| + } |
| + |
| + // Add a second listener. |
| + std::string service_owner2; |
| + int num_of_owner_changes2 = 0; |
| + dbus::Bus::GetServiceOwnerCallback callback2 = |
| + base::Bind(&OnServiceOwnerChanged, |
| + &run_loop_state, |
| + &service_owner2, |
| + &num_of_owner_changes2); |
| + bus->ListenForServiceOwnerChange("org.chromium.TestService", callback2); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + // Release the ownership and make sure the service owner listeners fire with |
| + // the right values and the right number of times. |
| + ASSERT_TRUE(bus->ReleaseOwnership("org.chromium.TestService")); |
| + run_loop_state.Run(2); |
| + |
| + EXPECT_TRUE(service_owner1.empty()); |
| + EXPECT_TRUE(service_owner2.empty()); |
| + EXPECT_EQ(2, num_of_owner_changes1); |
| + EXPECT_EQ(1, num_of_owner_changes2); |
| + |
| + // Unlisten so shutdown can proceed correctly. |
| + bus->UnlistenForServiceOwnerChange("org.chromium.TestService", callback1); |
| + bus->UnlistenForServiceOwnerChange("org.chromium.TestService", callback2); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + // Shut down synchronously. |
| + bus->ShutdownAndBlock(); |
| + EXPECT_TRUE(bus->shutdown_completed()); |
| +} |