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 "dbus/bus.h" | 5 #include "dbus/bus.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/run_loop.h" | |
10 #include "base/threading/thread.h" | 11 #include "base/threading/thread.h" |
11 #include "dbus/exported_object.h" | 12 #include "dbus/exported_object.h" |
12 #include "dbus/object_path.h" | 13 #include "dbus/object_path.h" |
13 #include "dbus/object_proxy.h" | 14 #include "dbus/object_proxy.h" |
14 #include "dbus/scoped_dbus_error.h" | 15 #include "dbus/scoped_dbus_error.h" |
16 #include "dbus/test_service.h" | |
15 | 17 |
16 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
17 | 19 |
18 namespace { | 20 namespace { |
19 | 21 |
20 // Used to test AddFilterFunction(). | 22 // Used to test AddFilterFunction(). |
21 DBusHandlerResult DummyHandler(DBusConnection* connection, | 23 DBusHandlerResult DummyHandler(DBusConnection* connection, |
22 DBusMessage* raw_message, | 24 DBusMessage* raw_message, |
23 void* user_data) { | 25 void* user_data) { |
24 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | 26 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
25 } | 27 } |
26 | 28 |
29 // A test helper for BusTest.ListenForServiceOwnerChange. | |
30 void OnServiceOwnerChanged(std::string* service_owner, | |
31 int* num_of_owner_changes, | |
32 const std::string& new_service_owner) { | |
33 *service_owner = new_service_owner; | |
34 ++(*num_of_owner_changes); | |
35 } | |
36 | |
27 } // namespace | 37 } // namespace |
28 | 38 |
29 TEST(BusTest, GetObjectProxy) { | 39 TEST(BusTest, GetObjectProxy) { |
30 dbus::Bus::Options options; | 40 dbus::Bus::Options options; |
31 scoped_refptr<dbus::Bus> bus = new dbus::Bus(options); | 41 scoped_refptr<dbus::Bus> bus = new dbus::Bus(options); |
32 | 42 |
33 dbus::ObjectProxy* object_proxy1 = | 43 dbus::ObjectProxy* object_proxy1 = |
34 bus->GetObjectProxy("org.chromium.TestService", | 44 bus->GetObjectProxy("org.chromium.TestService", |
35 dbus::ObjectPath("/org/chromium/TestObject")); | 45 dbus::ObjectPath("/org/chromium/TestObject")); |
36 ASSERT_TRUE(object_proxy1); | 46 ASSERT_TRUE(object_proxy1); |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
289 error.get())); | 299 error.get())); |
290 ASSERT_FALSE(error.is_set()); | 300 ASSERT_FALSE(error.is_set()); |
291 | 301 |
292 // A third attemp to remove the same rule should fail. | 302 // A third attemp to remove the same rule should fail. |
293 ASSERT_FALSE(bus->RemoveMatch( | 303 ASSERT_FALSE(bus->RemoveMatch( |
294 "type='signal',interface='org.chromium.TestService',path='/'", | 304 "type='signal',interface='org.chromium.TestService',path='/'", |
295 error.get())); | 305 error.get())); |
296 | 306 |
297 bus->ShutdownAndBlock(); | 307 bus->ShutdownAndBlock(); |
298 } | 308 } |
309 | |
310 TEST(BusTest, ListenForServiceOwnerChange) { | |
311 // Setup the current thread's MessageLoop. Must be of TYPE_IO for the | |
312 // listeners to work. | |
313 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); | |
314 | |
315 // Create the bus. | |
316 dbus::Bus::Options bus_options; | |
317 scoped_refptr<dbus::Bus> bus = new dbus::Bus(bus_options); | |
318 | |
319 // Add a listener. | |
320 std::string service_owner1; | |
321 int num_of_owner_changes1 = 0; | |
322 dbus::Bus::GetServiceOwnerCallback callback1 = | |
323 base::Bind(&OnServiceOwnerChanged, | |
324 &service_owner1, | |
325 &num_of_owner_changes1); | |
326 bus->ListenForServiceOwnerChange("org.chromium.TestService", callback1); | |
327 // This should be a no-op. | |
328 bus->ListenForServiceOwnerChange("org.chromium.TestService", callback1); | |
329 base::RunLoop().RunUntilIdle(); | |
satorux1
2013/06/10 01:16:23
this call looks unnecessary.
| |
330 | |
331 // Nothing has happened yet. Check initial state. | |
332 EXPECT_TRUE(service_owner1.empty()); | |
333 EXPECT_EQ(0, num_of_owner_changes1); | |
334 | |
335 // Make an ownership change. | |
336 ASSERT_TRUE(bus->RequestOwnershipAndBlock("org.chromium.TestService")); | |
337 base::RunLoop().RunUntilIdle(); | |
satorux1
2013/06/10 01:16:23
Not sure if this race-free (i.e. OnServiceOwnerCha
| |
338 | |
339 { | |
340 // Get the current service owner and check to make sure the listener got | |
341 // the right value. | |
342 std::string current_service_owner = | |
343 bus->GetServiceOwnerAndBlock("org.chromium.TestService", | |
344 dbus::Bus::REPORT_ERRORS); | |
345 ASSERT_FALSE(current_service_owner.empty()); | |
346 | |
347 // Make sure the listener heard about the new owner. | |
348 EXPECT_EQ(current_service_owner, service_owner1); | |
349 | |
350 // Test the second ListenForServiceOwnerChange() above is indeed a no-op. | |
351 EXPECT_EQ(1, num_of_owner_changes1); | |
352 } | |
353 | |
354 // Add a second listener. | |
355 std::string service_owner2; | |
356 int num_of_owner_changes2 = 0; | |
357 dbus::Bus::GetServiceOwnerCallback callback2 = | |
358 base::Bind(&OnServiceOwnerChanged, | |
359 &service_owner2, | |
360 &num_of_owner_changes2); | |
361 bus->ListenForServiceOwnerChange("org.chromium.TestService", callback2); | |
362 | |
363 // Release the ownership and make sure the service owner listeners fire with | |
364 // the right values and the right number of times. | |
365 ASSERT_TRUE(bus->ReleaseOwnership("org.chromium.TestService")); | |
366 base::RunLoop().RunUntilIdle(); | |
367 | |
368 EXPECT_TRUE(service_owner1.empty()); | |
369 EXPECT_TRUE(service_owner2.empty()); | |
370 EXPECT_EQ(2, num_of_owner_changes1); | |
371 EXPECT_EQ(1, num_of_owner_changes2); | |
372 | |
373 // Unlisten so shutdown can proceed correctly. | |
374 bus->UnlistenForServiceOwnerChange("org.chromium.TestService", callback1); | |
375 bus->UnlistenForServiceOwnerChange("org.chromium.TestService", callback2); | |
376 base::RunLoop().RunUntilIdle(); | |
377 | |
378 // Shut down synchronously. | |
379 bus->ShutdownAndBlock(); | |
380 EXPECT_TRUE(bus->shutdown_completed()); | |
381 } | |
OLD | NEW |