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