OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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" |
| 8 #include "base/message_loop.h" |
7 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/threading/thread.h" |
8 #include "dbus/exported_object.h" | 11 #include "dbus/exported_object.h" |
9 #include "dbus/object_proxy.h" | 12 #include "dbus/object_proxy.h" |
10 | 13 |
11 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
12 | 15 |
13 TEST(BusTest, GetObjectProxy) { | 16 class BusTest : public testing::Test { |
| 17 public: |
| 18 BusTest() |
| 19 : on_shutdown_called_(false) { |
| 20 } |
| 21 |
| 22 // Called when a Bus object is shut down by Bus::Shutdown(). |
| 23 void OnShutdown() { |
| 24 on_shutdown_called_ = true; |
| 25 message_loop_.Quit(); |
| 26 } |
| 27 |
| 28 protected: |
| 29 MessageLoop message_loop_; |
| 30 bool on_shutdown_called_; |
| 31 }; |
| 32 |
| 33 TEST_F(BusTest, GetObjectProxy) { |
14 dbus::Bus::Options options; | 34 dbus::Bus::Options options; |
15 scoped_refptr<dbus::Bus> bus = new dbus::Bus(options); | 35 scoped_refptr<dbus::Bus> bus = new dbus::Bus(options); |
16 | 36 |
17 dbus::ObjectProxy* object_proxy1 = | 37 dbus::ObjectProxy* object_proxy1 = |
18 bus->GetObjectProxy("org.chromium.TestService", | 38 bus->GetObjectProxy("org.chromium.TestService", |
19 "/org/chromium/TestObject"); | 39 "/org/chromium/TestObject"); |
20 ASSERT_TRUE(object_proxy1); | 40 ASSERT_TRUE(object_proxy1); |
21 | 41 |
22 // This should return the same object. | 42 // This should return the same object. |
23 dbus::ObjectProxy* object_proxy2 = | 43 dbus::ObjectProxy* object_proxy2 = |
24 bus->GetObjectProxy("org.chromium.TestService", | 44 bus->GetObjectProxy("org.chromium.TestService", |
25 "/org/chromium/TestObject"); | 45 "/org/chromium/TestObject"); |
26 ASSERT_TRUE(object_proxy2); | 46 ASSERT_TRUE(object_proxy2); |
27 EXPECT_EQ(object_proxy1, object_proxy2); | 47 EXPECT_EQ(object_proxy1, object_proxy2); |
28 | 48 |
29 // This should not. | 49 // This should not. |
30 dbus::ObjectProxy* object_proxy3 = | 50 dbus::ObjectProxy* object_proxy3 = |
31 bus->GetObjectProxy("org.chromium.TestService", | 51 bus->GetObjectProxy("org.chromium.TestService", |
32 "/org/chromium/DifferentTestObject"); | 52 "/org/chromium/DifferentTestObject"); |
33 ASSERT_TRUE(object_proxy3); | 53 ASSERT_TRUE(object_proxy3); |
34 EXPECT_NE(object_proxy1, object_proxy3); | 54 EXPECT_NE(object_proxy1, object_proxy3); |
35 } | 55 } |
36 | 56 |
37 TEST(BusTest, GetExportedObject) { | 57 TEST_F(BusTest, GetExportedObject) { |
38 dbus::Bus::Options options; | 58 dbus::Bus::Options options; |
39 scoped_refptr<dbus::Bus> bus = new dbus::Bus(options); | 59 scoped_refptr<dbus::Bus> bus = new dbus::Bus(options); |
40 | 60 |
41 dbus::ExportedObject* object_proxy1 = | 61 dbus::ExportedObject* object_proxy1 = |
42 bus->GetExportedObject("org.chromium.TestService", | 62 bus->GetExportedObject("org.chromium.TestService", |
43 "/org/chromium/TestObject"); | 63 "/org/chromium/TestObject"); |
44 ASSERT_TRUE(object_proxy1); | 64 ASSERT_TRUE(object_proxy1); |
45 | 65 |
46 // This should return the same object. | 66 // This should return the same object. |
47 dbus::ExportedObject* object_proxy2 = | 67 dbus::ExportedObject* object_proxy2 = |
48 bus->GetExportedObject("org.chromium.TestService", | 68 bus->GetExportedObject("org.chromium.TestService", |
49 "/org/chromium/TestObject"); | 69 "/org/chromium/TestObject"); |
50 ASSERT_TRUE(object_proxy2); | 70 ASSERT_TRUE(object_proxy2); |
51 EXPECT_EQ(object_proxy1, object_proxy2); | 71 EXPECT_EQ(object_proxy1, object_proxy2); |
52 | 72 |
53 // This should not. | 73 // This should not. |
54 dbus::ExportedObject* object_proxy3 = | 74 dbus::ExportedObject* object_proxy3 = |
55 bus->GetExportedObject("org.chromium.TestService", | 75 bus->GetExportedObject("org.chromium.TestService", |
56 "/org/chromium/DifferentTestObject"); | 76 "/org/chromium/DifferentTestObject"); |
57 ASSERT_TRUE(object_proxy3); | 77 ASSERT_TRUE(object_proxy3); |
58 EXPECT_NE(object_proxy1, object_proxy3); | 78 EXPECT_NE(object_proxy1, object_proxy3); |
59 } | 79 } |
| 80 |
| 81 TEST_F(BusTest, ShutdownAndBlock) { |
| 82 dbus::Bus::Options options; |
| 83 scoped_refptr<dbus::Bus> bus = new dbus::Bus(options); |
| 84 ASSERT_FALSE(bus->shutdown_completed()); |
| 85 |
| 86 // Shut down synchronously. |
| 87 bus->ShutdownAndBlock(); |
| 88 EXPECT_TRUE(bus->shutdown_completed()); |
| 89 } |
| 90 |
| 91 TEST_F(BusTest, Shutdown) { |
| 92 dbus::Bus::Options options; |
| 93 scoped_refptr<dbus::Bus> bus = new dbus::Bus(options); |
| 94 ASSERT_FALSE(bus->shutdown_completed()); |
| 95 |
| 96 // Shut down asynchronously using the message loop. |
| 97 bus->Shutdown(base::Bind(&BusTest::OnShutdown, |
| 98 base::Unretained(this))); |
| 99 message_loop_.Run(); |
| 100 EXPECT_TRUE(on_shutdown_called_); |
| 101 EXPECT_TRUE(bus->shutdown_completed()); |
| 102 } |
| 103 |
| 104 TEST_F(BusTest, ShutdownAndBlockWithDBusThread) { |
| 105 // Start the D-Bus thread. |
| 106 base::Thread::Options thread_options; |
| 107 thread_options.message_loop_type = MessageLoop::TYPE_IO; |
| 108 base::Thread dbus_thread("D-Bus thread"); |
| 109 dbus_thread.StartWithOptions(thread_options); |
| 110 |
| 111 // Create the bus. |
| 112 dbus::Bus::Options options; |
| 113 options.dbus_thread = &dbus_thread; |
| 114 scoped_refptr<dbus::Bus> bus = new dbus::Bus(options); |
| 115 ASSERT_FALSE(bus->shutdown_completed()); |
| 116 |
| 117 // Shut down synchronously. |
| 118 bus->ShutdownAndBlockWithDBusThread(); |
| 119 EXPECT_TRUE(bus->shutdown_completed()); |
| 120 dbus_thread.Stop(); |
| 121 } |
OLD | NEW |