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
3 // found in the LICENSE file.
8 #include "base/message_loop.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/threading/thread.h"
11 #include "dbus/exported_object.h"
12 #include "dbus/object_path.h"
13 #include "dbus/object_proxy.h"
15 #include "testing/gtest/include/gtest/gtest.h"
19 // Used to test AddFilterFunction().
20 DBusHandlerResult
DummyHandler(DBusConnection
* connection
,
21 DBusMessage
* raw_message
,
23 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
28 TEST(BusTest
, GetObjectProxy
) {
29 dbus::Bus::Options options
;
30 scoped_refptr
<dbus::Bus
> bus
= new dbus::Bus(options
);
32 dbus::ObjectProxy
* object_proxy1
=
33 bus
->GetObjectProxy("org.chromium.TestService",
34 dbus::ObjectPath("/org/chromium/TestObject"));
35 ASSERT_TRUE(object_proxy1
);
37 // This should return the same object.
38 dbus::ObjectProxy
* object_proxy2
=
39 bus
->GetObjectProxy("org.chromium.TestService",
40 dbus::ObjectPath("/org/chromium/TestObject"));
41 ASSERT_TRUE(object_proxy2
);
42 EXPECT_EQ(object_proxy1
, object_proxy2
);
45 dbus::ObjectProxy
* object_proxy3
=
47 "org.chromium.TestService",
48 dbus::ObjectPath("/org/chromium/DifferentTestObject"));
49 ASSERT_TRUE(object_proxy3
);
50 EXPECT_NE(object_proxy1
, object_proxy3
);
52 bus
->ShutdownAndBlock();
55 TEST(BusTest
, GetObjectProxyIgnoreUnknownService
) {
56 dbus::Bus::Options options
;
57 scoped_refptr
<dbus::Bus
> bus
= new dbus::Bus(options
);
59 dbus::ObjectProxy
* object_proxy1
=
60 bus
->GetObjectProxyWithOptions(
61 "org.chromium.TestService",
62 dbus::ObjectPath("/org/chromium/TestObject"),
63 dbus::ObjectProxy::IGNORE_SERVICE_UNKNOWN_ERRORS
);
64 ASSERT_TRUE(object_proxy1
);
66 // This should return the same object.
67 dbus::ObjectProxy
* object_proxy2
=
68 bus
->GetObjectProxyWithOptions(
69 "org.chromium.TestService",
70 dbus::ObjectPath("/org/chromium/TestObject"),
71 dbus::ObjectProxy::IGNORE_SERVICE_UNKNOWN_ERRORS
);
72 ASSERT_TRUE(object_proxy2
);
73 EXPECT_EQ(object_proxy1
, object_proxy2
);
76 dbus::ObjectProxy
* object_proxy3
=
77 bus
->GetObjectProxyWithOptions(
78 "org.chromium.TestService",
79 dbus::ObjectPath("/org/chromium/DifferentTestObject"),
80 dbus::ObjectProxy::IGNORE_SERVICE_UNKNOWN_ERRORS
);
81 ASSERT_TRUE(object_proxy3
);
82 EXPECT_NE(object_proxy1
, object_proxy3
);
84 bus
->ShutdownAndBlock();
87 TEST(BusTest
, GetExportedObject
) {
88 dbus::Bus::Options options
;
89 scoped_refptr
<dbus::Bus
> bus
= new dbus::Bus(options
);
91 dbus::ExportedObject
* object_proxy1
=
92 bus
->GetExportedObject(dbus::ObjectPath("/org/chromium/TestObject"));
93 ASSERT_TRUE(object_proxy1
);
95 // This should return the same object.
96 dbus::ExportedObject
* object_proxy2
=
97 bus
->GetExportedObject(dbus::ObjectPath("/org/chromium/TestObject"));
98 ASSERT_TRUE(object_proxy2
);
99 EXPECT_EQ(object_proxy1
, object_proxy2
);
102 dbus::ExportedObject
* object_proxy3
=
103 bus
->GetExportedObject(
104 dbus::ObjectPath("/org/chromium/DifferentTestObject"));
105 ASSERT_TRUE(object_proxy3
);
106 EXPECT_NE(object_proxy1
, object_proxy3
);
108 bus
->ShutdownAndBlock();
111 // http://crbug.com/137846
112 TEST(BusTest
, FLAKY_UnregisterExportedObject
) {
113 // Start the D-Bus thread.
114 base::Thread::Options thread_options
;
115 thread_options
.message_loop_type
= MessageLoop::TYPE_IO
;
116 base::Thread
dbus_thread("D-Bus thread");
117 dbus_thread
.StartWithOptions(thread_options
);
120 dbus::Bus::Options options
;
121 options
.dbus_thread_message_loop_proxy
= dbus_thread
.message_loop_proxy();
122 scoped_refptr
<dbus::Bus
> bus
= new dbus::Bus(options
);
123 ASSERT_FALSE(bus
->shutdown_completed());
125 dbus::ExportedObject
* object_proxy1
=
126 bus
->GetExportedObject(dbus::ObjectPath("/org/chromium/TestObject"));
127 ASSERT_TRUE(object_proxy1
);
129 bus
->UnregisterExportedObject(dbus::ObjectPath("/org/chromium/TestObject"));
131 // This should return a new object.
132 dbus::ExportedObject
* object_proxy2
=
133 bus
->GetExportedObject(dbus::ObjectPath("/org/chromium/TestObject"));
134 ASSERT_TRUE(object_proxy2
);
135 EXPECT_NE(object_proxy1
, object_proxy2
);
137 // Shut down synchronously.
138 bus
->ShutdownOnDBusThreadAndBlock();
139 EXPECT_TRUE(bus
->shutdown_completed());
143 TEST(BusTest
, ShutdownAndBlock
) {
144 dbus::Bus::Options options
;
145 scoped_refptr
<dbus::Bus
> bus
= new dbus::Bus(options
);
146 ASSERT_FALSE(bus
->shutdown_completed());
148 // Shut down synchronously.
149 bus
->ShutdownAndBlock();
150 EXPECT_TRUE(bus
->shutdown_completed());
153 TEST(BusTest
, ShutdownAndBlockWithDBusThread
) {
154 // Start the D-Bus thread.
155 base::Thread::Options thread_options
;
156 thread_options
.message_loop_type
= MessageLoop::TYPE_IO
;
157 base::Thread
dbus_thread("D-Bus thread");
158 dbus_thread
.StartWithOptions(thread_options
);
161 dbus::Bus::Options options
;
162 options
.dbus_thread_message_loop_proxy
= dbus_thread
.message_loop_proxy();
163 scoped_refptr
<dbus::Bus
> bus
= new dbus::Bus(options
);
164 ASSERT_FALSE(bus
->shutdown_completed());
166 // Shut down synchronously.
167 bus
->ShutdownOnDBusThreadAndBlock();
168 EXPECT_TRUE(bus
->shutdown_completed());
172 TEST(BusTest
, AddFilterFunction
) {
173 dbus::Bus::Options options
;
174 scoped_refptr
<dbus::Bus
> bus
= new dbus::Bus(options
);
175 // Should connect before calling AddFilterFunction().
180 ASSERT_TRUE(bus
->AddFilterFunction(&DummyHandler
, &data1
));
181 // Cannot add the same function with the same data.
182 ASSERT_FALSE(bus
->AddFilterFunction(&DummyHandler
, &data1
));
183 // Can add the same function with different data.
184 ASSERT_TRUE(bus
->AddFilterFunction(&DummyHandler
, &data2
));
186 ASSERT_TRUE(bus
->RemoveFilterFunction(&DummyHandler
, &data1
));
187 ASSERT_FALSE(bus
->RemoveFilterFunction(&DummyHandler
, &data1
));
188 ASSERT_TRUE(bus
->RemoveFilterFunction(&DummyHandler
, &data2
));
190 bus
->ShutdownAndBlock();