xref: /aosp_15_r20/external/libchrome/dbus/bus_unittest.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #include "dbus/bus.h"
6*635a8641SAndroid Build Coastguard Worker 
7*635a8641SAndroid Build Coastguard Worker #include "base/bind.h"
8*635a8641SAndroid Build Coastguard Worker #include "base/bind_helpers.h"
9*635a8641SAndroid Build Coastguard Worker #include "base/files/file_descriptor_watcher_posix.h"
10*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
11*635a8641SAndroid Build Coastguard Worker #include "base/memory/ref_counted.h"
12*635a8641SAndroid Build Coastguard Worker #include "base/message_loop/message_loop.h"
13*635a8641SAndroid Build Coastguard Worker #include "base/run_loop.h"
14*635a8641SAndroid Build Coastguard Worker #include "base/threading/thread.h"
15*635a8641SAndroid Build Coastguard Worker #include "dbus/exported_object.h"
16*635a8641SAndroid Build Coastguard Worker #include "dbus/object_path.h"
17*635a8641SAndroid Build Coastguard Worker #include "dbus/object_proxy.h"
18*635a8641SAndroid Build Coastguard Worker #include "dbus/scoped_dbus_error.h"
19*635a8641SAndroid Build Coastguard Worker #include "dbus/test_service.h"
20*635a8641SAndroid Build Coastguard Worker 
21*635a8641SAndroid Build Coastguard Worker #include "testing/gtest/include/gtest/gtest.h"
22*635a8641SAndroid Build Coastguard Worker 
23*635a8641SAndroid Build Coastguard Worker namespace dbus {
24*635a8641SAndroid Build Coastguard Worker 
25*635a8641SAndroid Build Coastguard Worker namespace {
26*635a8641SAndroid Build Coastguard Worker 
27*635a8641SAndroid Build Coastguard Worker // Test helper for BusTest.ListenForServiceOwnerChange that wraps a
28*635a8641SAndroid Build Coastguard Worker // base::RunLoop. At Run() time, the caller pass in the expected number of
29*635a8641SAndroid Build Coastguard Worker // quit calls, and at QuitIfConditionIsSatisified() time, only quit the RunLoop
30*635a8641SAndroid Build Coastguard Worker // if the expected number of quit calls have been reached.
31*635a8641SAndroid Build Coastguard Worker class RunLoopWithExpectedCount {
32*635a8641SAndroid Build Coastguard Worker  public:
RunLoopWithExpectedCount()33*635a8641SAndroid Build Coastguard Worker   RunLoopWithExpectedCount() : expected_quit_calls_(0), actual_quit_calls_(0) {}
34*635a8641SAndroid Build Coastguard Worker   ~RunLoopWithExpectedCount() = default;
35*635a8641SAndroid Build Coastguard Worker 
Run(int expected_quit_calls)36*635a8641SAndroid Build Coastguard Worker   void Run(int expected_quit_calls) {
37*635a8641SAndroid Build Coastguard Worker     DCHECK_EQ(0, expected_quit_calls_);
38*635a8641SAndroid Build Coastguard Worker     DCHECK_EQ(0, actual_quit_calls_);
39*635a8641SAndroid Build Coastguard Worker     expected_quit_calls_ = expected_quit_calls;
40*635a8641SAndroid Build Coastguard Worker     run_loop_.reset(new base::RunLoop());
41*635a8641SAndroid Build Coastguard Worker     run_loop_->Run();
42*635a8641SAndroid Build Coastguard Worker   }
43*635a8641SAndroid Build Coastguard Worker 
QuitIfConditionIsSatisified()44*635a8641SAndroid Build Coastguard Worker   void QuitIfConditionIsSatisified() {
45*635a8641SAndroid Build Coastguard Worker     if (++actual_quit_calls_ != expected_quit_calls_)
46*635a8641SAndroid Build Coastguard Worker       return;
47*635a8641SAndroid Build Coastguard Worker     run_loop_->Quit();
48*635a8641SAndroid Build Coastguard Worker     expected_quit_calls_ = 0;
49*635a8641SAndroid Build Coastguard Worker     actual_quit_calls_ = 0;
50*635a8641SAndroid Build Coastguard Worker   }
51*635a8641SAndroid Build Coastguard Worker 
52*635a8641SAndroid Build Coastguard Worker  private:
53*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<base::RunLoop> run_loop_;
54*635a8641SAndroid Build Coastguard Worker   int expected_quit_calls_;
55*635a8641SAndroid Build Coastguard Worker   int actual_quit_calls_;
56*635a8641SAndroid Build Coastguard Worker 
57*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(RunLoopWithExpectedCount);
58*635a8641SAndroid Build Coastguard Worker };
59*635a8641SAndroid Build Coastguard Worker 
60*635a8641SAndroid Build Coastguard Worker // Test helper for BusTest.ListenForServiceOwnerChange.
OnServiceOwnerChanged(RunLoopWithExpectedCount * run_loop_state,std::string * service_owner,int * num_of_owner_changes,const std::string & new_service_owner)61*635a8641SAndroid Build Coastguard Worker void OnServiceOwnerChanged(RunLoopWithExpectedCount* run_loop_state,
62*635a8641SAndroid Build Coastguard Worker                            std::string* service_owner,
63*635a8641SAndroid Build Coastguard Worker                            int* num_of_owner_changes,
64*635a8641SAndroid Build Coastguard Worker                            const std::string& new_service_owner) {
65*635a8641SAndroid Build Coastguard Worker   *service_owner = new_service_owner;
66*635a8641SAndroid Build Coastguard Worker   ++(*num_of_owner_changes);
67*635a8641SAndroid Build Coastguard Worker   run_loop_state->QuitIfConditionIsSatisified();
68*635a8641SAndroid Build Coastguard Worker }
69*635a8641SAndroid Build Coastguard Worker 
70*635a8641SAndroid Build Coastguard Worker }  // namespace
71*635a8641SAndroid Build Coastguard Worker 
TEST(BusTest,GetObjectProxy)72*635a8641SAndroid Build Coastguard Worker TEST(BusTest, GetObjectProxy) {
73*635a8641SAndroid Build Coastguard Worker   Bus::Options options;
74*635a8641SAndroid Build Coastguard Worker   scoped_refptr<Bus> bus = new Bus(options);
75*635a8641SAndroid Build Coastguard Worker 
76*635a8641SAndroid Build Coastguard Worker   ObjectProxy* object_proxy1 =
77*635a8641SAndroid Build Coastguard Worker       bus->GetObjectProxy("org.chromium.TestService",
78*635a8641SAndroid Build Coastguard Worker                           ObjectPath("/org/chromium/TestObject"));
79*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(object_proxy1);
80*635a8641SAndroid Build Coastguard Worker 
81*635a8641SAndroid Build Coastguard Worker   // This should return the same object.
82*635a8641SAndroid Build Coastguard Worker   ObjectProxy* object_proxy2 =
83*635a8641SAndroid Build Coastguard Worker       bus->GetObjectProxy("org.chromium.TestService",
84*635a8641SAndroid Build Coastguard Worker                           ObjectPath("/org/chromium/TestObject"));
85*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(object_proxy2);
86*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(object_proxy1, object_proxy2);
87*635a8641SAndroid Build Coastguard Worker 
88*635a8641SAndroid Build Coastguard Worker   // This should not.
89*635a8641SAndroid Build Coastguard Worker   ObjectProxy* object_proxy3 =
90*635a8641SAndroid Build Coastguard Worker       bus->GetObjectProxy(
91*635a8641SAndroid Build Coastguard Worker           "org.chromium.TestService",
92*635a8641SAndroid Build Coastguard Worker           ObjectPath("/org/chromium/DifferentTestObject"));
93*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(object_proxy3);
94*635a8641SAndroid Build Coastguard Worker   EXPECT_NE(object_proxy1, object_proxy3);
95*635a8641SAndroid Build Coastguard Worker 
96*635a8641SAndroid Build Coastguard Worker   bus->ShutdownAndBlock();
97*635a8641SAndroid Build Coastguard Worker }
98*635a8641SAndroid Build Coastguard Worker 
TEST(BusTest,GetObjectProxyIgnoreUnknownService)99*635a8641SAndroid Build Coastguard Worker TEST(BusTest, GetObjectProxyIgnoreUnknownService) {
100*635a8641SAndroid Build Coastguard Worker   Bus::Options options;
101*635a8641SAndroid Build Coastguard Worker   scoped_refptr<Bus> bus = new Bus(options);
102*635a8641SAndroid Build Coastguard Worker 
103*635a8641SAndroid Build Coastguard Worker   ObjectProxy* object_proxy1 =
104*635a8641SAndroid Build Coastguard Worker       bus->GetObjectProxyWithOptions(
105*635a8641SAndroid Build Coastguard Worker           "org.chromium.TestService",
106*635a8641SAndroid Build Coastguard Worker           ObjectPath("/org/chromium/TestObject"),
107*635a8641SAndroid Build Coastguard Worker           ObjectProxy::IGNORE_SERVICE_UNKNOWN_ERRORS);
108*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(object_proxy1);
109*635a8641SAndroid Build Coastguard Worker 
110*635a8641SAndroid Build Coastguard Worker   // This should return the same object.
111*635a8641SAndroid Build Coastguard Worker   ObjectProxy* object_proxy2 =
112*635a8641SAndroid Build Coastguard Worker       bus->GetObjectProxyWithOptions(
113*635a8641SAndroid Build Coastguard Worker           "org.chromium.TestService",
114*635a8641SAndroid Build Coastguard Worker           ObjectPath("/org/chromium/TestObject"),
115*635a8641SAndroid Build Coastguard Worker           ObjectProxy::IGNORE_SERVICE_UNKNOWN_ERRORS);
116*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(object_proxy2);
117*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(object_proxy1, object_proxy2);
118*635a8641SAndroid Build Coastguard Worker 
119*635a8641SAndroid Build Coastguard Worker   // This should not.
120*635a8641SAndroid Build Coastguard Worker   ObjectProxy* object_proxy3 =
121*635a8641SAndroid Build Coastguard Worker       bus->GetObjectProxyWithOptions(
122*635a8641SAndroid Build Coastguard Worker           "org.chromium.TestService",
123*635a8641SAndroid Build Coastguard Worker           ObjectPath("/org/chromium/DifferentTestObject"),
124*635a8641SAndroid Build Coastguard Worker           ObjectProxy::IGNORE_SERVICE_UNKNOWN_ERRORS);
125*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(object_proxy3);
126*635a8641SAndroid Build Coastguard Worker   EXPECT_NE(object_proxy1, object_proxy3);
127*635a8641SAndroid Build Coastguard Worker 
128*635a8641SAndroid Build Coastguard Worker   bus->ShutdownAndBlock();
129*635a8641SAndroid Build Coastguard Worker }
130*635a8641SAndroid Build Coastguard Worker 
TEST(BusTest,RemoveObjectProxy)131*635a8641SAndroid Build Coastguard Worker TEST(BusTest, RemoveObjectProxy) {
132*635a8641SAndroid Build Coastguard Worker   // Setup the current thread's MessageLoop.
133*635a8641SAndroid Build Coastguard Worker   base::MessageLoop message_loop;
134*635a8641SAndroid Build Coastguard Worker 
135*635a8641SAndroid Build Coastguard Worker   // Start the D-Bus thread.
136*635a8641SAndroid Build Coastguard Worker   base::Thread::Options thread_options;
137*635a8641SAndroid Build Coastguard Worker   thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
138*635a8641SAndroid Build Coastguard Worker   base::Thread dbus_thread("D-Bus thread");
139*635a8641SAndroid Build Coastguard Worker   dbus_thread.StartWithOptions(thread_options);
140*635a8641SAndroid Build Coastguard Worker 
141*635a8641SAndroid Build Coastguard Worker   // Create the bus.
142*635a8641SAndroid Build Coastguard Worker   Bus::Options options;
143*635a8641SAndroid Build Coastguard Worker   options.dbus_task_runner = dbus_thread.task_runner();
144*635a8641SAndroid Build Coastguard Worker   scoped_refptr<Bus> bus = new Bus(options);
145*635a8641SAndroid Build Coastguard Worker   ASSERT_FALSE(bus->shutdown_completed());
146*635a8641SAndroid Build Coastguard Worker 
147*635a8641SAndroid Build Coastguard Worker   // Try to remove a non existant object proxy should return false.
148*635a8641SAndroid Build Coastguard Worker   ASSERT_FALSE(bus->RemoveObjectProxy("org.chromium.TestService",
149*635a8641SAndroid Build Coastguard Worker                                       ObjectPath("/org/chromium/TestObject"),
150*635a8641SAndroid Build Coastguard Worker                                       base::DoNothing()));
151*635a8641SAndroid Build Coastguard Worker 
152*635a8641SAndroid Build Coastguard Worker   ObjectProxy* object_proxy1 =
153*635a8641SAndroid Build Coastguard Worker       bus->GetObjectProxy("org.chromium.TestService",
154*635a8641SAndroid Build Coastguard Worker                           ObjectPath("/org/chromium/TestObject"));
155*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(object_proxy1);
156*635a8641SAndroid Build Coastguard Worker 
157*635a8641SAndroid Build Coastguard Worker   // Increment the reference count to the object proxy to avoid destroying it
158*635a8641SAndroid Build Coastguard Worker   // while removing the object.
159*635a8641SAndroid Build Coastguard Worker   object_proxy1->AddRef();
160*635a8641SAndroid Build Coastguard Worker 
161*635a8641SAndroid Build Coastguard Worker   // Remove the object from the bus. This will invalidate any other usage of
162*635a8641SAndroid Build Coastguard Worker   // object_proxy1 other than destroy it. We keep this object for a comparison
163*635a8641SAndroid Build Coastguard Worker   // at a later time.
164*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(bus->RemoveObjectProxy("org.chromium.TestService",
165*635a8641SAndroid Build Coastguard Worker                                      ObjectPath("/org/chromium/TestObject"),
166*635a8641SAndroid Build Coastguard Worker                                      base::DoNothing()));
167*635a8641SAndroid Build Coastguard Worker 
168*635a8641SAndroid Build Coastguard Worker   // This should return a different object because the first object was removed
169*635a8641SAndroid Build Coastguard Worker   // from the bus, but not deleted from memory.
170*635a8641SAndroid Build Coastguard Worker   ObjectProxy* object_proxy2 =
171*635a8641SAndroid Build Coastguard Worker       bus->GetObjectProxy("org.chromium.TestService",
172*635a8641SAndroid Build Coastguard Worker                           ObjectPath("/org/chromium/TestObject"));
173*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(object_proxy2);
174*635a8641SAndroid Build Coastguard Worker 
175*635a8641SAndroid Build Coastguard Worker   // Compare the new object with the first object. The first object still exists
176*635a8641SAndroid Build Coastguard Worker   // thanks to the increased reference.
177*635a8641SAndroid Build Coastguard Worker   EXPECT_NE(object_proxy1, object_proxy2);
178*635a8641SAndroid Build Coastguard Worker 
179*635a8641SAndroid Build Coastguard Worker   // Release object_proxy1.
180*635a8641SAndroid Build Coastguard Worker   object_proxy1->Release();
181*635a8641SAndroid Build Coastguard Worker 
182*635a8641SAndroid Build Coastguard Worker   // Shut down synchronously.
183*635a8641SAndroid Build Coastguard Worker   bus->ShutdownOnDBusThreadAndBlock();
184*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(bus->shutdown_completed());
185*635a8641SAndroid Build Coastguard Worker   dbus_thread.Stop();
186*635a8641SAndroid Build Coastguard Worker }
187*635a8641SAndroid Build Coastguard Worker 
TEST(BusTest,GetExportedObject)188*635a8641SAndroid Build Coastguard Worker TEST(BusTest, GetExportedObject) {
189*635a8641SAndroid Build Coastguard Worker   Bus::Options options;
190*635a8641SAndroid Build Coastguard Worker   scoped_refptr<Bus> bus = new Bus(options);
191*635a8641SAndroid Build Coastguard Worker 
192*635a8641SAndroid Build Coastguard Worker   ExportedObject* object_proxy1 =
193*635a8641SAndroid Build Coastguard Worker       bus->GetExportedObject(ObjectPath("/org/chromium/TestObject"));
194*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(object_proxy1);
195*635a8641SAndroid Build Coastguard Worker 
196*635a8641SAndroid Build Coastguard Worker   // This should return the same object.
197*635a8641SAndroid Build Coastguard Worker   ExportedObject* object_proxy2 =
198*635a8641SAndroid Build Coastguard Worker       bus->GetExportedObject(ObjectPath("/org/chromium/TestObject"));
199*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(object_proxy2);
200*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(object_proxy1, object_proxy2);
201*635a8641SAndroid Build Coastguard Worker 
202*635a8641SAndroid Build Coastguard Worker   // This should not.
203*635a8641SAndroid Build Coastguard Worker   ExportedObject* object_proxy3 =
204*635a8641SAndroid Build Coastguard Worker       bus->GetExportedObject(
205*635a8641SAndroid Build Coastguard Worker           ObjectPath("/org/chromium/DifferentTestObject"));
206*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(object_proxy3);
207*635a8641SAndroid Build Coastguard Worker   EXPECT_NE(object_proxy1, object_proxy3);
208*635a8641SAndroid Build Coastguard Worker 
209*635a8641SAndroid Build Coastguard Worker   bus->ShutdownAndBlock();
210*635a8641SAndroid Build Coastguard Worker }
211*635a8641SAndroid Build Coastguard Worker 
TEST(BusTest,UnregisterExportedObject)212*635a8641SAndroid Build Coastguard Worker TEST(BusTest, UnregisterExportedObject) {
213*635a8641SAndroid Build Coastguard Worker   // Start the D-Bus thread.
214*635a8641SAndroid Build Coastguard Worker   base::Thread::Options thread_options;
215*635a8641SAndroid Build Coastguard Worker   thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
216*635a8641SAndroid Build Coastguard Worker   base::Thread dbus_thread("D-Bus thread");
217*635a8641SAndroid Build Coastguard Worker   dbus_thread.StartWithOptions(thread_options);
218*635a8641SAndroid Build Coastguard Worker 
219*635a8641SAndroid Build Coastguard Worker   // Create the bus.
220*635a8641SAndroid Build Coastguard Worker   Bus::Options options;
221*635a8641SAndroid Build Coastguard Worker   options.dbus_task_runner = dbus_thread.task_runner();
222*635a8641SAndroid Build Coastguard Worker   scoped_refptr<Bus> bus = new Bus(options);
223*635a8641SAndroid Build Coastguard Worker   ASSERT_FALSE(bus->shutdown_completed());
224*635a8641SAndroid Build Coastguard Worker 
225*635a8641SAndroid Build Coastguard Worker   ExportedObject* object_proxy1 =
226*635a8641SAndroid Build Coastguard Worker       bus->GetExportedObject(ObjectPath("/org/chromium/TestObject"));
227*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(object_proxy1);
228*635a8641SAndroid Build Coastguard Worker 
229*635a8641SAndroid Build Coastguard Worker   // Increment the reference count to the object proxy to avoid destroying it
230*635a8641SAndroid Build Coastguard Worker   // calling UnregisterExportedObject. This ensures the dbus::ExportedObject is
231*635a8641SAndroid Build Coastguard Worker   // not freed from memory. See http://crbug.com/137846 for details.
232*635a8641SAndroid Build Coastguard Worker   object_proxy1->AddRef();
233*635a8641SAndroid Build Coastguard Worker 
234*635a8641SAndroid Build Coastguard Worker   bus->UnregisterExportedObject(ObjectPath("/org/chromium/TestObject"));
235*635a8641SAndroid Build Coastguard Worker 
236*635a8641SAndroid Build Coastguard Worker   // This should return a new object because the object_proxy1 is still in
237*635a8641SAndroid Build Coastguard Worker   // alloc'ed memory.
238*635a8641SAndroid Build Coastguard Worker   ExportedObject* object_proxy2 =
239*635a8641SAndroid Build Coastguard Worker       bus->GetExportedObject(ObjectPath("/org/chromium/TestObject"));
240*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(object_proxy2);
241*635a8641SAndroid Build Coastguard Worker   EXPECT_NE(object_proxy1, object_proxy2);
242*635a8641SAndroid Build Coastguard Worker 
243*635a8641SAndroid Build Coastguard Worker   // Release the incremented reference.
244*635a8641SAndroid Build Coastguard Worker   object_proxy1->Release();
245*635a8641SAndroid Build Coastguard Worker 
246*635a8641SAndroid Build Coastguard Worker   // Shut down synchronously.
247*635a8641SAndroid Build Coastguard Worker   bus->ShutdownOnDBusThreadAndBlock();
248*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(bus->shutdown_completed());
249*635a8641SAndroid Build Coastguard Worker   dbus_thread.Stop();
250*635a8641SAndroid Build Coastguard Worker }
251*635a8641SAndroid Build Coastguard Worker 
TEST(BusTest,ShutdownAndBlock)252*635a8641SAndroid Build Coastguard Worker TEST(BusTest, ShutdownAndBlock) {
253*635a8641SAndroid Build Coastguard Worker   Bus::Options options;
254*635a8641SAndroid Build Coastguard Worker   scoped_refptr<Bus> bus = new Bus(options);
255*635a8641SAndroid Build Coastguard Worker   ASSERT_FALSE(bus->shutdown_completed());
256*635a8641SAndroid Build Coastguard Worker 
257*635a8641SAndroid Build Coastguard Worker   // Shut down synchronously.
258*635a8641SAndroid Build Coastguard Worker   bus->ShutdownAndBlock();
259*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(bus->shutdown_completed());
260*635a8641SAndroid Build Coastguard Worker }
261*635a8641SAndroid Build Coastguard Worker 
TEST(BusTest,ShutdownAndBlockWithDBusThread)262*635a8641SAndroid Build Coastguard Worker TEST(BusTest, ShutdownAndBlockWithDBusThread) {
263*635a8641SAndroid Build Coastguard Worker   // Start the D-Bus thread.
264*635a8641SAndroid Build Coastguard Worker   base::Thread::Options thread_options;
265*635a8641SAndroid Build Coastguard Worker   thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
266*635a8641SAndroid Build Coastguard Worker   base::Thread dbus_thread("D-Bus thread");
267*635a8641SAndroid Build Coastguard Worker   dbus_thread.StartWithOptions(thread_options);
268*635a8641SAndroid Build Coastguard Worker 
269*635a8641SAndroid Build Coastguard Worker   // Create the bus.
270*635a8641SAndroid Build Coastguard Worker   Bus::Options options;
271*635a8641SAndroid Build Coastguard Worker   options.dbus_task_runner = dbus_thread.task_runner();
272*635a8641SAndroid Build Coastguard Worker   scoped_refptr<Bus> bus = new Bus(options);
273*635a8641SAndroid Build Coastguard Worker   ASSERT_FALSE(bus->shutdown_completed());
274*635a8641SAndroid Build Coastguard Worker 
275*635a8641SAndroid Build Coastguard Worker   // Shut down synchronously.
276*635a8641SAndroid Build Coastguard Worker   bus->ShutdownOnDBusThreadAndBlock();
277*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(bus->shutdown_completed());
278*635a8641SAndroid Build Coastguard Worker   dbus_thread.Stop();
279*635a8641SAndroid Build Coastguard Worker }
280*635a8641SAndroid Build Coastguard Worker 
TEST(BusTest,DoubleAddAndRemoveMatch)281*635a8641SAndroid Build Coastguard Worker TEST(BusTest, DoubleAddAndRemoveMatch) {
282*635a8641SAndroid Build Coastguard Worker   Bus::Options options;
283*635a8641SAndroid Build Coastguard Worker   scoped_refptr<Bus> bus = new Bus(options);
284*635a8641SAndroid Build Coastguard Worker   ScopedDBusError error;
285*635a8641SAndroid Build Coastguard Worker 
286*635a8641SAndroid Build Coastguard Worker   bus->Connect();
287*635a8641SAndroid Build Coastguard Worker 
288*635a8641SAndroid Build Coastguard Worker   // Adds the same rule twice.
289*635a8641SAndroid Build Coastguard Worker   bus->AddMatch(
290*635a8641SAndroid Build Coastguard Worker       "type='signal',interface='org.chromium.TestService',path='/'",
291*635a8641SAndroid Build Coastguard Worker       error.get());
292*635a8641SAndroid Build Coastguard Worker   ASSERT_FALSE(error.is_set());
293*635a8641SAndroid Build Coastguard Worker 
294*635a8641SAndroid Build Coastguard Worker   bus->AddMatch(
295*635a8641SAndroid Build Coastguard Worker       "type='signal',interface='org.chromium.TestService',path='/'",
296*635a8641SAndroid Build Coastguard Worker       error.get());
297*635a8641SAndroid Build Coastguard Worker   ASSERT_FALSE(error.is_set());
298*635a8641SAndroid Build Coastguard Worker 
299*635a8641SAndroid Build Coastguard Worker   // Removes the same rule twice.
300*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(bus->RemoveMatch(
301*635a8641SAndroid Build Coastguard Worker       "type='signal',interface='org.chromium.TestService',path='/'",
302*635a8641SAndroid Build Coastguard Worker       error.get()));
303*635a8641SAndroid Build Coastguard Worker   ASSERT_FALSE(error.is_set());
304*635a8641SAndroid Build Coastguard Worker 
305*635a8641SAndroid Build Coastguard Worker   // The rule should be still in the bus since it was removed only once.
306*635a8641SAndroid Build Coastguard Worker   // A second removal shouldn't give an error.
307*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(bus->RemoveMatch(
308*635a8641SAndroid Build Coastguard Worker       "type='signal',interface='org.chromium.TestService',path='/'",
309*635a8641SAndroid Build Coastguard Worker       error.get()));
310*635a8641SAndroid Build Coastguard Worker   ASSERT_FALSE(error.is_set());
311*635a8641SAndroid Build Coastguard Worker 
312*635a8641SAndroid Build Coastguard Worker   // A third attemp to remove the same rule should fail.
313*635a8641SAndroid Build Coastguard Worker   ASSERT_FALSE(bus->RemoveMatch(
314*635a8641SAndroid Build Coastguard Worker       "type='signal',interface='org.chromium.TestService',path='/'",
315*635a8641SAndroid Build Coastguard Worker       error.get()));
316*635a8641SAndroid Build Coastguard Worker 
317*635a8641SAndroid Build Coastguard Worker   bus->ShutdownAndBlock();
318*635a8641SAndroid Build Coastguard Worker }
319*635a8641SAndroid Build Coastguard Worker 
TEST(BusTest,ListenForServiceOwnerChange)320*635a8641SAndroid Build Coastguard Worker TEST(BusTest, ListenForServiceOwnerChange) {
321*635a8641SAndroid Build Coastguard Worker   base::MessageLoopForIO message_loop;
322*635a8641SAndroid Build Coastguard Worker 
323*635a8641SAndroid Build Coastguard Worker   // This enables FileDescriptorWatcher, which is required by dbus::Watch.
324*635a8641SAndroid Build Coastguard Worker   base::FileDescriptorWatcher file_descriptor_watcher(&message_loop);
325*635a8641SAndroid Build Coastguard Worker 
326*635a8641SAndroid Build Coastguard Worker   RunLoopWithExpectedCount run_loop_state;
327*635a8641SAndroid Build Coastguard Worker 
328*635a8641SAndroid Build Coastguard Worker   // Create the bus.
329*635a8641SAndroid Build Coastguard Worker   Bus::Options bus_options;
330*635a8641SAndroid Build Coastguard Worker   scoped_refptr<Bus> bus = new Bus(bus_options);
331*635a8641SAndroid Build Coastguard Worker 
332*635a8641SAndroid Build Coastguard Worker   // Add a listener.
333*635a8641SAndroid Build Coastguard Worker   std::string service_owner1;
334*635a8641SAndroid Build Coastguard Worker   int num_of_owner_changes1 = 0;
335*635a8641SAndroid Build Coastguard Worker   Bus::GetServiceOwnerCallback callback1 =
336*635a8641SAndroid Build Coastguard Worker       base::Bind(&OnServiceOwnerChanged,
337*635a8641SAndroid Build Coastguard Worker                  &run_loop_state,
338*635a8641SAndroid Build Coastguard Worker                  &service_owner1,
339*635a8641SAndroid Build Coastguard Worker                  &num_of_owner_changes1);
340*635a8641SAndroid Build Coastguard Worker   bus->ListenForServiceOwnerChange("org.chromium.TestService", callback1);
341*635a8641SAndroid Build Coastguard Worker   // This should be a no-op.
342*635a8641SAndroid Build Coastguard Worker   bus->ListenForServiceOwnerChange("org.chromium.TestService", callback1);
343*635a8641SAndroid Build Coastguard Worker   base::RunLoop().RunUntilIdle();
344*635a8641SAndroid Build Coastguard Worker 
345*635a8641SAndroid Build Coastguard Worker   // Nothing has happened yet. Check initial state.
346*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(service_owner1.empty());
347*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(0, num_of_owner_changes1);
348*635a8641SAndroid Build Coastguard Worker 
349*635a8641SAndroid Build Coastguard Worker   // Make an ownership change.
350*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(bus->RequestOwnershipAndBlock("org.chromium.TestService",
351*635a8641SAndroid Build Coastguard Worker                                             Bus::REQUIRE_PRIMARY));
352*635a8641SAndroid Build Coastguard Worker   run_loop_state.Run(1);
353*635a8641SAndroid Build Coastguard Worker 
354*635a8641SAndroid Build Coastguard Worker   {
355*635a8641SAndroid Build Coastguard Worker     // Get the current service owner and check to make sure the listener got
356*635a8641SAndroid Build Coastguard Worker     // the right value.
357*635a8641SAndroid Build Coastguard Worker     std::string current_service_owner =
358*635a8641SAndroid Build Coastguard Worker         bus->GetServiceOwnerAndBlock("org.chromium.TestService",
359*635a8641SAndroid Build Coastguard Worker                                      Bus::REPORT_ERRORS);
360*635a8641SAndroid Build Coastguard Worker     ASSERT_FALSE(current_service_owner.empty());
361*635a8641SAndroid Build Coastguard Worker 
362*635a8641SAndroid Build Coastguard Worker     // Make sure the listener heard about the new owner.
363*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(current_service_owner, service_owner1);
364*635a8641SAndroid Build Coastguard Worker 
365*635a8641SAndroid Build Coastguard Worker     // Test the second ListenForServiceOwnerChange() above is indeed a no-op.
366*635a8641SAndroid Build Coastguard Worker     EXPECT_EQ(1, num_of_owner_changes1);
367*635a8641SAndroid Build Coastguard Worker   }
368*635a8641SAndroid Build Coastguard Worker 
369*635a8641SAndroid Build Coastguard Worker   // Add a second listener.
370*635a8641SAndroid Build Coastguard Worker   std::string service_owner2;
371*635a8641SAndroid Build Coastguard Worker   int num_of_owner_changes2 = 0;
372*635a8641SAndroid Build Coastguard Worker   Bus::GetServiceOwnerCallback callback2 =
373*635a8641SAndroid Build Coastguard Worker       base::Bind(&OnServiceOwnerChanged,
374*635a8641SAndroid Build Coastguard Worker                  &run_loop_state,
375*635a8641SAndroid Build Coastguard Worker                  &service_owner2,
376*635a8641SAndroid Build Coastguard Worker                  &num_of_owner_changes2);
377*635a8641SAndroid Build Coastguard Worker   bus->ListenForServiceOwnerChange("org.chromium.TestService", callback2);
378*635a8641SAndroid Build Coastguard Worker   base::RunLoop().RunUntilIdle();
379*635a8641SAndroid Build Coastguard Worker 
380*635a8641SAndroid Build Coastguard Worker   // Release the ownership and make sure the service owner listeners fire with
381*635a8641SAndroid Build Coastguard Worker   // the right values and the right number of times.
382*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(bus->ReleaseOwnership("org.chromium.TestService"));
383*635a8641SAndroid Build Coastguard Worker   run_loop_state.Run(2);
384*635a8641SAndroid Build Coastguard Worker 
385*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(service_owner1.empty());
386*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(service_owner2.empty());
387*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(2, num_of_owner_changes1);
388*635a8641SAndroid Build Coastguard Worker   EXPECT_EQ(1, num_of_owner_changes2);
389*635a8641SAndroid Build Coastguard Worker 
390*635a8641SAndroid Build Coastguard Worker   // Unlisten so shutdown can proceed correctly.
391*635a8641SAndroid Build Coastguard Worker   bus->UnlistenForServiceOwnerChange("org.chromium.TestService", callback1);
392*635a8641SAndroid Build Coastguard Worker   bus->UnlistenForServiceOwnerChange("org.chromium.TestService", callback2);
393*635a8641SAndroid Build Coastguard Worker   base::RunLoop().RunUntilIdle();
394*635a8641SAndroid Build Coastguard Worker 
395*635a8641SAndroid Build Coastguard Worker   // Shut down synchronously.
396*635a8641SAndroid Build Coastguard Worker   bus->ShutdownAndBlock();
397*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(bus->shutdown_completed());
398*635a8641SAndroid Build Coastguard Worker }
399*635a8641SAndroid Build Coastguard Worker 
TEST(BusTest,GetConnectionName)400*635a8641SAndroid Build Coastguard Worker TEST(BusTest, GetConnectionName) {
401*635a8641SAndroid Build Coastguard Worker   Bus::Options options;
402*635a8641SAndroid Build Coastguard Worker   scoped_refptr<Bus> bus = new Bus(options);
403*635a8641SAndroid Build Coastguard Worker 
404*635a8641SAndroid Build Coastguard Worker   // Connection name is empty since bus is not connected.
405*635a8641SAndroid Build Coastguard Worker   EXPECT_FALSE(bus->is_connected());
406*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(bus->GetConnectionName().empty());
407*635a8641SAndroid Build Coastguard Worker 
408*635a8641SAndroid Build Coastguard Worker   // Connect bus to D-Bus.
409*635a8641SAndroid Build Coastguard Worker   bus->Connect();
410*635a8641SAndroid Build Coastguard Worker 
411*635a8641SAndroid Build Coastguard Worker   // Connection name is not empty after connection is established.
412*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(bus->is_connected());
413*635a8641SAndroid Build Coastguard Worker   EXPECT_FALSE(bus->GetConnectionName().empty());
414*635a8641SAndroid Build Coastguard Worker 
415*635a8641SAndroid Build Coastguard Worker   // Shut down synchronously.
416*635a8641SAndroid Build Coastguard Worker   bus->ShutdownAndBlock();
417*635a8641SAndroid Build Coastguard Worker   EXPECT_TRUE(bus->shutdown_completed());
418*635a8641SAndroid Build Coastguard Worker }
419*635a8641SAndroid Build Coastguard Worker 
420*635a8641SAndroid Build Coastguard Worker }  // namespace dbus
421