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/threading/thread.h" | 10 #include "base/threading/thread.h" |
11 #include "dbus/exported_object.h" | 11 #include "dbus/exported_object.h" |
12 #include "dbus/object_path.h" | 12 #include "dbus/object_path.h" |
13 #include "dbus/object_proxy.h" | 13 #include "dbus/object_proxy.h" |
14 | 14 |
15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 // Used to test AddFilterFunction(). | 19 // Used to test AddFilterFunction(). |
20 DBusHandlerResult DummyHandler(DBusConnection* connection, | 20 DBusHandlerResult DummyHandler(DBusConnection* connection, |
21 DBusMessage* raw_message, | 21 DBusMessage* raw_message, |
22 void* user_data) { | 22 void* user_data) { |
23 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | 23 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; |
24 } | 24 } |
25 | 25 |
26 // Used to test RemoveObjectProxy | |
27 void OnRemoveObjectProxy( | |
28 const std::string& service_name, | |
29 const dbus::ObjectPath& object_path, | |
30 int options) { | |
31 } | |
32 | |
26 } // namespace | 33 } // namespace |
27 | 34 |
28 TEST(BusTest, GetObjectProxy) { | 35 TEST(BusTest, GetObjectProxy) { |
29 dbus::Bus::Options options; | 36 dbus::Bus::Options options; |
30 scoped_refptr<dbus::Bus> bus = new dbus::Bus(options); | 37 scoped_refptr<dbus::Bus> bus = new dbus::Bus(options); |
31 | 38 |
32 dbus::ObjectProxy* object_proxy1 = | 39 dbus::ObjectProxy* object_proxy1 = |
33 bus->GetObjectProxy("org.chromium.TestService", | 40 bus->GetObjectProxy("org.chromium.TestService", |
34 dbus::ObjectPath("/org/chromium/TestObject")); | 41 dbus::ObjectPath("/org/chromium/TestObject")); |
35 ASSERT_TRUE(object_proxy1); | 42 ASSERT_TRUE(object_proxy1); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
77 bus->GetObjectProxyWithOptions( | 84 bus->GetObjectProxyWithOptions( |
78 "org.chromium.TestService", | 85 "org.chromium.TestService", |
79 dbus::ObjectPath("/org/chromium/DifferentTestObject"), | 86 dbus::ObjectPath("/org/chromium/DifferentTestObject"), |
80 dbus::ObjectProxy::IGNORE_SERVICE_UNKNOWN_ERRORS); | 87 dbus::ObjectProxy::IGNORE_SERVICE_UNKNOWN_ERRORS); |
81 ASSERT_TRUE(object_proxy3); | 88 ASSERT_TRUE(object_proxy3); |
82 EXPECT_NE(object_proxy1, object_proxy3); | 89 EXPECT_NE(object_proxy1, object_proxy3); |
83 | 90 |
84 bus->ShutdownAndBlock(); | 91 bus->ShutdownAndBlock(); |
85 } | 92 } |
86 | 93 |
94 TEST(BusTest, RemoveObjectProxy) { | |
95 // Setup the current thread's MessageLoop. | |
96 MessageLoop message_loop; | |
97 | |
98 // Start the D-Bus thread. | |
99 base::Thread::Options thread_options; | |
100 thread_options.message_loop_type = MessageLoop::TYPE_IO; | |
101 base::Thread dbus_thread("D-Bus thread"); | |
102 dbus_thread.StartWithOptions(thread_options); | |
103 | |
104 // Create the bus. | |
105 dbus::Bus::Options options; | |
106 options.dbus_thread_message_loop_proxy = dbus_thread.message_loop_proxy(); | |
107 scoped_refptr<dbus::Bus> bus = new dbus::Bus(options); | |
108 ASSERT_FALSE(bus->shutdown_completed()); | |
109 | |
110 // Try to remove a non existant object proxy should return false. | |
111 ASSERT_FALSE( | |
112 bus->RemoveObjectProxy("org.chromium.TestService", | |
113 dbus::ObjectPath("/org/chromium/TestObject"), | |
114 base::Bind(&OnRemoveObjectProxy))); | |
satorux1
2013/01/25 01:46:06
If you switch to base::Closure, you can use base::
deymo
2013/01/25 20:50:55
Done.
| |
115 | |
116 dbus::ObjectProxy* object_proxy1 = | |
117 bus->GetObjectProxy("org.chromium.TestService", | |
118 dbus::ObjectPath("/org/chromium/TestObject")); | |
119 ASSERT_TRUE(object_proxy1); | |
120 | |
121 // Increment the reference count to the object proxy to avoid destroying it | |
122 // while removing the object. | |
123 object_proxy1->AddRef(); | |
124 | |
125 // Remove the object from the bus. This will invalidate any other usage of | |
126 // object_proxy1 other than destroy it. | |
127 ASSERT_TRUE( | |
128 bus->RemoveObjectProxy("org.chromium.TestService", | |
129 dbus::ObjectPath("/org/chromium/TestObject"), | |
130 base::Bind(&OnRemoveObjectProxy))); | |
131 | |
132 // This should return a different object because the first object still | |
133 // exists thanks to the increased reference. | |
134 dbus::ObjectProxy* object_proxy2 = | |
135 bus->GetObjectProxy("org.chromium.TestService", | |
136 dbus::ObjectPath("/org/chromium/TestObject")); | |
137 ASSERT_TRUE(object_proxy2); | |
138 EXPECT_NE(object_proxy1, object_proxy2); | |
139 | |
140 // Release object_proxy1. | |
141 object_proxy1->Release(); | |
142 | |
143 // Shut down synchronously. | |
144 bus->ShutdownOnDBusThreadAndBlock(); | |
145 EXPECT_TRUE(bus->shutdown_completed()); | |
146 dbus_thread.Stop(); | |
147 } | |
148 | |
87 TEST(BusTest, GetExportedObject) { | 149 TEST(BusTest, GetExportedObject) { |
88 dbus::Bus::Options options; | 150 dbus::Bus::Options options; |
89 scoped_refptr<dbus::Bus> bus = new dbus::Bus(options); | 151 scoped_refptr<dbus::Bus> bus = new dbus::Bus(options); |
90 | 152 |
91 dbus::ExportedObject* object_proxy1 = | 153 dbus::ExportedObject* object_proxy1 = |
92 bus->GetExportedObject(dbus::ObjectPath("/org/chromium/TestObject")); | 154 bus->GetExportedObject(dbus::ObjectPath("/org/chromium/TestObject")); |
93 ASSERT_TRUE(object_proxy1); | 155 ASSERT_TRUE(object_proxy1); |
94 | 156 |
95 // This should return the same object. | 157 // This should return the same object. |
96 dbus::ExportedObject* object_proxy2 = | 158 dbus::ExportedObject* object_proxy2 = |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
182 ASSERT_FALSE(bus->AddFilterFunction(&DummyHandler, &data1)); | 244 ASSERT_FALSE(bus->AddFilterFunction(&DummyHandler, &data1)); |
183 // Can add the same function with different data. | 245 // Can add the same function with different data. |
184 ASSERT_TRUE(bus->AddFilterFunction(&DummyHandler, &data2)); | 246 ASSERT_TRUE(bus->AddFilterFunction(&DummyHandler, &data2)); |
185 | 247 |
186 ASSERT_TRUE(bus->RemoveFilterFunction(&DummyHandler, &data1)); | 248 ASSERT_TRUE(bus->RemoveFilterFunction(&DummyHandler, &data1)); |
187 ASSERT_FALSE(bus->RemoveFilterFunction(&DummyHandler, &data1)); | 249 ASSERT_FALSE(bus->RemoveFilterFunction(&DummyHandler, &data1)); |
188 ASSERT_TRUE(bus->RemoveFilterFunction(&DummyHandler, &data2)); | 250 ASSERT_TRUE(bus->RemoveFilterFunction(&DummyHandler, &data2)); |
189 | 251 |
190 bus->ShutdownAndBlock(); | 252 bus->ShutdownAndBlock(); |
191 } | 253 } |
OLD | NEW |