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,14 @@ |
| return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
| } |
| +// A test helper for BusTest.ListenForServiceOwnerChange. |
| +void OnServiceOwnerChanged(std::string* service_owner, |
| + int* num_of_owner_changes, |
| + const std::string& new_service_owner) { |
| + *service_owner = new_service_owner; |
| + ++(*num_of_owner_changes); |
| +} |
| + |
| } // namespace |
| TEST(BusTest, GetObjectProxy) { |
| @@ -296,3 +306,76 @@ |
| 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); |
| + |
| + // 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, |
| + &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(); |
|
satorux1
2013/06/10 01:16:23
this call looks unnecessary.
|
| + |
| + // 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")); |
| + base::RunLoop().RunUntilIdle(); |
|
satorux1
2013/06/10 01:16:23
Not sure if this race-free (i.e. OnServiceOwnerCha
|
| + |
| + { |
| + // 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, |
| + &service_owner2, |
| + &num_of_owner_changes2); |
| + bus->ListenForServiceOwnerChange("org.chromium.TestService", callback2); |
| + |
| + // 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")); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + 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()); |
| +} |