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 // 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.
| |
30 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.
| |
31 public: | |
32 RunLoopState() : expected_quit_calls_(0), actual_quit_calls_(0) {} | |
33 ~RunLoopState() {} | |
34 | |
35 void Run(int expected_quit_calls) { | |
36 DCHECK_EQ(0, expected_quit_calls_); | |
37 DCHECK_EQ(0, actual_quit_calls_); | |
38 expected_quit_calls_ = expected_quit_calls; | |
39 run_loop_.reset(new base::RunLoop()); | |
40 run_loop_->Run(); | |
41 } | |
42 | |
43 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.
| |
44 if (++actual_quit_calls_ != expected_quit_calls_) | |
45 return; | |
46 run_loop_->Quit(); | |
47 expected_quit_calls_ = 0; | |
48 actual_quit_calls_ = 0; | |
49 } | |
50 | |
51 private: | |
52 scoped_ptr<base::RunLoop> run_loop_; | |
53 int expected_quit_calls_; | |
54 int actual_quit_calls_; | |
55 | |
56 DISALLOW_COPY_AND_ASSIGN(RunLoopState); | |
57 }; | |
58 | |
59 void OnServiceOwnerChanged(RunLoopState* run_loop, | |
satorux1
2013/06/10 04:58:46
run_loop_state ?
Lei Zhang
2013/06/10 05:20:32
Done.
| |
60 std::string* service_owner, | |
61 int* num_of_owner_changes, | |
62 const std::string& new_service_owner) { | |
63 *service_owner = new_service_owner; | |
64 ++(*num_of_owner_changes); | |
65 run_loop->Quit(); | |
66 } | |
67 | |
27 } // namespace | 68 } // namespace |
28 | 69 |
29 TEST(BusTest, GetObjectProxy) { | 70 TEST(BusTest, GetObjectProxy) { |
30 dbus::Bus::Options options; | 71 dbus::Bus::Options options; |
31 scoped_refptr<dbus::Bus> bus = new dbus::Bus(options); | 72 scoped_refptr<dbus::Bus> bus = new dbus::Bus(options); |
32 | 73 |
33 dbus::ObjectProxy* object_proxy1 = | 74 dbus::ObjectProxy* object_proxy1 = |
34 bus->GetObjectProxy("org.chromium.TestService", | 75 bus->GetObjectProxy("org.chromium.TestService", |
35 dbus::ObjectPath("/org/chromium/TestObject")); | 76 dbus::ObjectPath("/org/chromium/TestObject")); |
36 ASSERT_TRUE(object_proxy1); | 77 ASSERT_TRUE(object_proxy1); |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
289 error.get())); | 330 error.get())); |
290 ASSERT_FALSE(error.is_set()); | 331 ASSERT_FALSE(error.is_set()); |
291 | 332 |
292 // A third attemp to remove the same rule should fail. | 333 // A third attemp to remove the same rule should fail. |
293 ASSERT_FALSE(bus->RemoveMatch( | 334 ASSERT_FALSE(bus->RemoveMatch( |
294 "type='signal',interface='org.chromium.TestService',path='/'", | 335 "type='signal',interface='org.chromium.TestService',path='/'", |
295 error.get())); | 336 error.get())); |
296 | 337 |
297 bus->ShutdownAndBlock(); | 338 bus->ShutdownAndBlock(); |
298 } | 339 } |
340 | |
341 TEST(BusTest, ListenForServiceOwnerChange) { | |
342 // Setup the current thread's MessageLoop. Must be of TYPE_IO for the | |
343 // listeners to work. | |
344 base::MessageLoop message_loop(base::MessageLoop::TYPE_IO); | |
345 RunLoopState run_loop_state; | |
346 | |
347 // Create the bus. | |
348 dbus::Bus::Options bus_options; | |
349 scoped_refptr<dbus::Bus> bus = new dbus::Bus(bus_options); | |
350 | |
351 // Add a listener. | |
352 std::string service_owner1; | |
353 int num_of_owner_changes1 = 0; | |
354 dbus::Bus::GetServiceOwnerCallback callback1 = | |
355 base::Bind(&OnServiceOwnerChanged, | |
356 &run_loop_state, | |
357 &service_owner1, | |
358 &num_of_owner_changes1); | |
359 bus->ListenForServiceOwnerChange("org.chromium.TestService", callback1); | |
360 // This should be a no-op. | |
361 bus->ListenForServiceOwnerChange("org.chromium.TestService", callback1); | |
362 base::RunLoop().RunUntilIdle(); | |
363 | |
364 // Nothing has happened yet. Check initial state. | |
365 EXPECT_TRUE(service_owner1.empty()); | |
366 EXPECT_EQ(0, num_of_owner_changes1); | |
367 | |
368 // Make an ownership change. | |
369 ASSERT_TRUE(bus->RequestOwnershipAndBlock("org.chromium.TestService")); | |
370 run_loop_state.Run(1); | |
371 | |
372 { | |
373 // Get the current service owner and check to make sure the listener got | |
374 // the right value. | |
375 std::string current_service_owner = | |
376 bus->GetServiceOwnerAndBlock("org.chromium.TestService", | |
377 dbus::Bus::REPORT_ERRORS); | |
378 ASSERT_FALSE(current_service_owner.empty()); | |
379 | |
380 // Make sure the listener heard about the new owner. | |
381 EXPECT_EQ(current_service_owner, service_owner1); | |
382 | |
383 // Test the second ListenForServiceOwnerChange() above is indeed a no-op. | |
384 EXPECT_EQ(1, num_of_owner_changes1); | |
385 } | |
386 | |
387 // Add a second listener. | |
388 std::string service_owner2; | |
389 int num_of_owner_changes2 = 0; | |
390 dbus::Bus::GetServiceOwnerCallback callback2 = | |
391 base::Bind(&OnServiceOwnerChanged, | |
392 &run_loop_state, | |
393 &service_owner2, | |
394 &num_of_owner_changes2); | |
395 bus->ListenForServiceOwnerChange("org.chromium.TestService", callback2); | |
396 base::RunLoop().RunUntilIdle(); | |
397 | |
398 // Release the ownership and make sure the service owner listeners fire with | |
399 // the right values and the right number of times. | |
400 ASSERT_TRUE(bus->ReleaseOwnership("org.chromium.TestService")); | |
401 run_loop_state.Run(2); | |
402 | |
403 EXPECT_TRUE(service_owner1.empty()); | |
404 EXPECT_TRUE(service_owner2.empty()); | |
405 EXPECT_EQ(2, num_of_owner_changes1); | |
406 EXPECT_EQ(1, num_of_owner_changes2); | |
407 | |
408 // Unlisten so shutdown can proceed correctly. | |
409 bus->UnlistenForServiceOwnerChange("org.chromium.TestService", callback1); | |
410 bus->UnlistenForServiceOwnerChange("org.chromium.TestService", callback2); | |
411 base::RunLoop().RunUntilIdle(); | |
412 | |
413 // Shut down synchronously. | |
414 bus->ShutdownAndBlock(); | |
415 EXPECT_TRUE(bus->shutdown_completed()); | |
416 } | |
OLD | NEW |