xref: /aosp_15_r20/external/libchrome/dbus/signal_sender_verification_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 <memory>
6*635a8641SAndroid Build Coastguard Worker 
7*635a8641SAndroid Build Coastguard Worker #include "base/bind.h"
8*635a8641SAndroid Build Coastguard Worker #include "base/message_loop/message_loop.h"
9*635a8641SAndroid Build Coastguard Worker #include "base/metrics/histogram_macros.h"
10*635a8641SAndroid Build Coastguard Worker #include "base/metrics/histogram_samples.h"
11*635a8641SAndroid Build Coastguard Worker #include "base/run_loop.h"
12*635a8641SAndroid Build Coastguard Worker #include "base/single_thread_task_runner.h"
13*635a8641SAndroid Build Coastguard Worker #include "base/test/test_timeouts.h"
14*635a8641SAndroid Build Coastguard Worker #include "base/threading/platform_thread.h"
15*635a8641SAndroid Build Coastguard Worker #include "base/threading/thread_restrictions.h"
16*635a8641SAndroid Build Coastguard Worker #include "dbus/bus.h"
17*635a8641SAndroid Build Coastguard Worker #include "dbus/message.h"
18*635a8641SAndroid Build Coastguard Worker #include "dbus/object_proxy.h"
19*635a8641SAndroid Build Coastguard Worker #include "dbus/test_service.h"
20*635a8641SAndroid Build Coastguard Worker #include "testing/gtest/include/gtest/gtest.h"
21*635a8641SAndroid Build Coastguard Worker 
22*635a8641SAndroid Build Coastguard Worker namespace dbus {
23*635a8641SAndroid Build Coastguard Worker 
24*635a8641SAndroid Build Coastguard Worker // The test for sender verification in ObjectProxy.
25*635a8641SAndroid Build Coastguard Worker class SignalSenderVerificationTest : public testing::Test {
26*635a8641SAndroid Build Coastguard Worker  public:
SignalSenderVerificationTest()27*635a8641SAndroid Build Coastguard Worker   SignalSenderVerificationTest()
28*635a8641SAndroid Build Coastguard Worker       : on_name_owner_changed_called_(false),
29*635a8641SAndroid Build Coastguard Worker         on_ownership_called_(false) {
30*635a8641SAndroid Build Coastguard Worker   }
31*635a8641SAndroid Build Coastguard Worker 
SetUp()32*635a8641SAndroid Build Coastguard Worker   void SetUp() override {
33*635a8641SAndroid Build Coastguard Worker     // Make the main thread not to allow IO.
34*635a8641SAndroid Build Coastguard Worker     base::ThreadRestrictions::SetIOAllowed(false);
35*635a8641SAndroid Build Coastguard Worker 
36*635a8641SAndroid Build Coastguard Worker     // Start the D-Bus thread.
37*635a8641SAndroid Build Coastguard Worker     dbus_thread_.reset(new base::Thread("D-Bus Thread"));
38*635a8641SAndroid Build Coastguard Worker     base::Thread::Options thread_options;
39*635a8641SAndroid Build Coastguard Worker     thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
40*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(dbus_thread_->StartWithOptions(thread_options));
41*635a8641SAndroid Build Coastguard Worker 
42*635a8641SAndroid Build Coastguard Worker     // Create the test service, using the D-Bus thread.
43*635a8641SAndroid Build Coastguard Worker     TestService::Options options;
44*635a8641SAndroid Build Coastguard Worker     options.dbus_task_runner = dbus_thread_->task_runner();
45*635a8641SAndroid Build Coastguard Worker     test_service_.reset(new TestService(options));
46*635a8641SAndroid Build Coastguard Worker 
47*635a8641SAndroid Build Coastguard Worker     // Create the client, using the D-Bus thread.
48*635a8641SAndroid Build Coastguard Worker     Bus::Options bus_options;
49*635a8641SAndroid Build Coastguard Worker     bus_options.bus_type = Bus::SESSION;
50*635a8641SAndroid Build Coastguard Worker     bus_options.connection_type = Bus::PRIVATE;
51*635a8641SAndroid Build Coastguard Worker     bus_options.dbus_task_runner = dbus_thread_->task_runner();
52*635a8641SAndroid Build Coastguard Worker     bus_ = new Bus(bus_options);
53*635a8641SAndroid Build Coastguard Worker     object_proxy_ = bus_->GetObjectProxy(
54*635a8641SAndroid Build Coastguard Worker         test_service_->service_name(),
55*635a8641SAndroid Build Coastguard Worker         ObjectPath("/org/chromium/TestObject"));
56*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(bus_->HasDBusThread());
57*635a8641SAndroid Build Coastguard Worker 
58*635a8641SAndroid Build Coastguard Worker     object_proxy_->SetNameOwnerChangedCallback(
59*635a8641SAndroid Build Coastguard Worker         base::Bind(&SignalSenderVerificationTest::OnNameOwnerChanged,
60*635a8641SAndroid Build Coastguard Worker                    base::Unretained(this),
61*635a8641SAndroid Build Coastguard Worker                    &on_name_owner_changed_called_));
62*635a8641SAndroid Build Coastguard Worker 
63*635a8641SAndroid Build Coastguard Worker     // Connect to the "Test" signal of "org.chromium.TestInterface" from
64*635a8641SAndroid Build Coastguard Worker     // the remote object.
65*635a8641SAndroid Build Coastguard Worker     object_proxy_->ConnectToSignal(
66*635a8641SAndroid Build Coastguard Worker         "org.chromium.TestInterface",
67*635a8641SAndroid Build Coastguard Worker         "Test",
68*635a8641SAndroid Build Coastguard Worker         base::Bind(&SignalSenderVerificationTest::OnTestSignal,
69*635a8641SAndroid Build Coastguard Worker                    base::Unretained(this)),
70*635a8641SAndroid Build Coastguard Worker         base::Bind(&SignalSenderVerificationTest::OnConnected,
71*635a8641SAndroid Build Coastguard Worker                    base::Unretained(this)));
72*635a8641SAndroid Build Coastguard Worker     // Wait until the object proxy is connected to the signal.
73*635a8641SAndroid Build Coastguard Worker     run_loop_.reset(new base::RunLoop);
74*635a8641SAndroid Build Coastguard Worker     run_loop_->Run();
75*635a8641SAndroid Build Coastguard Worker 
76*635a8641SAndroid Build Coastguard Worker     // Start the test service.
77*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(test_service_->StartService());
78*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted());
79*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(test_service_->HasDBusThread());
80*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(test_service_->has_ownership());
81*635a8641SAndroid Build Coastguard Worker 
82*635a8641SAndroid Build Coastguard Worker     // Same setup for the second TestService. This service should not have the
83*635a8641SAndroid Build Coastguard Worker     // ownership of the name at this point.
84*635a8641SAndroid Build Coastguard Worker     options.service_name = test_service_->service_name();
85*635a8641SAndroid Build Coastguard Worker     test_service2_.reset(new TestService(options));
86*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(test_service2_->StartService());
87*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(test_service2_->WaitUntilServiceIsStarted());
88*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(test_service2_->HasDBusThread());
89*635a8641SAndroid Build Coastguard Worker     ASSERT_FALSE(test_service2_->has_ownership());
90*635a8641SAndroid Build Coastguard Worker 
91*635a8641SAndroid Build Coastguard Worker     // The name should be owned and known at this point.
92*635a8641SAndroid Build Coastguard Worker     if (!on_name_owner_changed_called_) {
93*635a8641SAndroid Build Coastguard Worker       run_loop_.reset(new base::RunLoop);
94*635a8641SAndroid Build Coastguard Worker       run_loop_->Run();
95*635a8641SAndroid Build Coastguard Worker     }
96*635a8641SAndroid Build Coastguard Worker     ASSERT_FALSE(latest_name_owner_.empty());
97*635a8641SAndroid Build Coastguard Worker   }
98*635a8641SAndroid Build Coastguard Worker 
TearDown()99*635a8641SAndroid Build Coastguard Worker   void TearDown() override {
100*635a8641SAndroid Build Coastguard Worker     bus_->ShutdownOnDBusThreadAndBlock();
101*635a8641SAndroid Build Coastguard Worker 
102*635a8641SAndroid Build Coastguard Worker     // Shut down the service.
103*635a8641SAndroid Build Coastguard Worker     test_service_->ShutdownAndBlock();
104*635a8641SAndroid Build Coastguard Worker     test_service2_->ShutdownAndBlock();
105*635a8641SAndroid Build Coastguard Worker 
106*635a8641SAndroid Build Coastguard Worker     // Reset to the default.
107*635a8641SAndroid Build Coastguard Worker     base::ThreadRestrictions::SetIOAllowed(true);
108*635a8641SAndroid Build Coastguard Worker 
109*635a8641SAndroid Build Coastguard Worker     // Stopping a thread is considered an IO operation, so do this after
110*635a8641SAndroid Build Coastguard Worker     // allowing IO.
111*635a8641SAndroid Build Coastguard Worker     test_service_->Stop();
112*635a8641SAndroid Build Coastguard Worker     test_service2_->Stop();
113*635a8641SAndroid Build Coastguard Worker   }
114*635a8641SAndroid Build Coastguard Worker 
OnOwnership(bool expected,bool success)115*635a8641SAndroid Build Coastguard Worker   void OnOwnership(bool expected, bool success) {
116*635a8641SAndroid Build Coastguard Worker     ASSERT_EQ(expected, success);
117*635a8641SAndroid Build Coastguard Worker     // PostTask to quit the MessageLoop as this is called from D-Bus thread.
118*635a8641SAndroid Build Coastguard Worker     message_loop_.task_runner()->PostTask(
119*635a8641SAndroid Build Coastguard Worker         FROM_HERE,
120*635a8641SAndroid Build Coastguard Worker         base::Bind(&SignalSenderVerificationTest::OnOwnershipInternal,
121*635a8641SAndroid Build Coastguard Worker                    base::Unretained(this)));
122*635a8641SAndroid Build Coastguard Worker   }
123*635a8641SAndroid Build Coastguard Worker 
OnOwnershipInternal()124*635a8641SAndroid Build Coastguard Worker   void OnOwnershipInternal() {
125*635a8641SAndroid Build Coastguard Worker     on_ownership_called_ = true;
126*635a8641SAndroid Build Coastguard Worker     run_loop_->Quit();
127*635a8641SAndroid Build Coastguard Worker   }
128*635a8641SAndroid Build Coastguard Worker 
OnNameOwnerChanged(bool * called_flag,const std::string & old_owner,const std::string & new_owner)129*635a8641SAndroid Build Coastguard Worker   void OnNameOwnerChanged(bool* called_flag,
130*635a8641SAndroid Build Coastguard Worker                           const std::string& old_owner,
131*635a8641SAndroid Build Coastguard Worker                           const std::string& new_owner) {
132*635a8641SAndroid Build Coastguard Worker     latest_name_owner_ = new_owner;
133*635a8641SAndroid Build Coastguard Worker     *called_flag = true;
134*635a8641SAndroid Build Coastguard Worker     run_loop_->Quit();
135*635a8641SAndroid Build Coastguard Worker   }
136*635a8641SAndroid Build Coastguard Worker 
137*635a8641SAndroid Build Coastguard Worker   // Called when the "Test" signal is received, in the main thread.
138*635a8641SAndroid Build Coastguard Worker   // Copy the string payload to |test_signal_string_|.
OnTestSignal(Signal * signal)139*635a8641SAndroid Build Coastguard Worker   void OnTestSignal(Signal* signal) {
140*635a8641SAndroid Build Coastguard Worker     MessageReader reader(signal);
141*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(reader.PopString(&test_signal_string_));
142*635a8641SAndroid Build Coastguard Worker     run_loop_->Quit();
143*635a8641SAndroid Build Coastguard Worker   }
144*635a8641SAndroid Build Coastguard Worker 
145*635a8641SAndroid Build Coastguard Worker   // Called when connected to the signal.
OnConnected(const std::string & interface_name,const std::string & signal_name,bool success)146*635a8641SAndroid Build Coastguard Worker   void OnConnected(const std::string& interface_name,
147*635a8641SAndroid Build Coastguard Worker                    const std::string& signal_name,
148*635a8641SAndroid Build Coastguard Worker                    bool success) {
149*635a8641SAndroid Build Coastguard Worker     ASSERT_TRUE(success);
150*635a8641SAndroid Build Coastguard Worker     run_loop_->Quit();
151*635a8641SAndroid Build Coastguard Worker   }
152*635a8641SAndroid Build Coastguard Worker 
153*635a8641SAndroid Build Coastguard Worker  protected:
154*635a8641SAndroid Build Coastguard Worker   // Wait for the hey signal to be received.
WaitForTestSignal()155*635a8641SAndroid Build Coastguard Worker   void WaitForTestSignal() {
156*635a8641SAndroid Build Coastguard Worker     // OnTestSignal() will quit the message loop.
157*635a8641SAndroid Build Coastguard Worker     run_loop_.reset(new base::RunLoop);
158*635a8641SAndroid Build Coastguard Worker     run_loop_->Run();
159*635a8641SAndroid Build Coastguard Worker   }
160*635a8641SAndroid Build Coastguard Worker 
161*635a8641SAndroid Build Coastguard Worker   // Stopping a thread is considered an IO operation, so we need to fiddle with
162*635a8641SAndroid Build Coastguard Worker   // thread restrictions before and after calling Stop() on a TestService.
SafeServiceStop(TestService * test_service)163*635a8641SAndroid Build Coastguard Worker   void SafeServiceStop(TestService* test_service) {
164*635a8641SAndroid Build Coastguard Worker     base::ThreadRestrictions::SetIOAllowed(true);
165*635a8641SAndroid Build Coastguard Worker     test_service->Stop();
166*635a8641SAndroid Build Coastguard Worker     base::ThreadRestrictions::SetIOAllowed(false);
167*635a8641SAndroid Build Coastguard Worker   }
168*635a8641SAndroid Build Coastguard Worker 
169*635a8641SAndroid Build Coastguard Worker   base::MessageLoop message_loop_;
170*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<base::RunLoop> run_loop_;
171*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<base::Thread> dbus_thread_;
172*635a8641SAndroid Build Coastguard Worker   scoped_refptr<Bus> bus_;
173*635a8641SAndroid Build Coastguard Worker   ObjectProxy* object_proxy_;
174*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<TestService> test_service_;
175*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<TestService> test_service2_;
176*635a8641SAndroid Build Coastguard Worker   // Text message from "Test" signal.
177*635a8641SAndroid Build Coastguard Worker   std::string test_signal_string_;
178*635a8641SAndroid Build Coastguard Worker 
179*635a8641SAndroid Build Coastguard Worker   // The known latest name owner of TestService. Updated in OnNameOwnerChanged.
180*635a8641SAndroid Build Coastguard Worker   std::string latest_name_owner_;
181*635a8641SAndroid Build Coastguard Worker 
182*635a8641SAndroid Build Coastguard Worker   // Boolean flags to record callback calls.
183*635a8641SAndroid Build Coastguard Worker   bool on_name_owner_changed_called_;
184*635a8641SAndroid Build Coastguard Worker   bool on_ownership_called_;
185*635a8641SAndroid Build Coastguard Worker };
186*635a8641SAndroid Build Coastguard Worker 
TEST_F(SignalSenderVerificationTest,TestSignalAccepted)187*635a8641SAndroid Build Coastguard Worker TEST_F(SignalSenderVerificationTest, TestSignalAccepted) {
188*635a8641SAndroid Build Coastguard Worker   const char kMessage[] = "hello, world";
189*635a8641SAndroid Build Coastguard Worker   // Send the test signal from the exported object.
190*635a8641SAndroid Build Coastguard Worker   test_service_->SendTestSignal(kMessage);
191*635a8641SAndroid Build Coastguard Worker   // Receive the signal with the object proxy. The signal is handled in
192*635a8641SAndroid Build Coastguard Worker   // SignalSenderVerificationTest::OnTestSignal() in the main thread.
193*635a8641SAndroid Build Coastguard Worker   WaitForTestSignal();
194*635a8641SAndroid Build Coastguard Worker   ASSERT_EQ(kMessage, test_signal_string_);
195*635a8641SAndroid Build Coastguard Worker }
196*635a8641SAndroid Build Coastguard Worker 
TEST_F(SignalSenderVerificationTest,TestSignalRejected)197*635a8641SAndroid Build Coastguard Worker TEST_F(SignalSenderVerificationTest, TestSignalRejected) {
198*635a8641SAndroid Build Coastguard Worker   const char kNewMessage[] = "hello, new world";
199*635a8641SAndroid Build Coastguard Worker   test_service2_->SendTestSignal(kNewMessage);
200*635a8641SAndroid Build Coastguard Worker 
201*635a8641SAndroid Build Coastguard Worker   // This test tests that our callback is NOT called by the ObjectProxy.
202*635a8641SAndroid Build Coastguard Worker   // Sleep to have message delivered to the client via the D-Bus service.
203*635a8641SAndroid Build Coastguard Worker   base::PlatformThread::Sleep(TestTimeouts::tiny_timeout());
204*635a8641SAndroid Build Coastguard Worker 
205*635a8641SAndroid Build Coastguard Worker   ASSERT_EQ("", test_signal_string_);
206*635a8641SAndroid Build Coastguard Worker }
207*635a8641SAndroid Build Coastguard Worker 
208*635a8641SAndroid Build Coastguard Worker // Flaky. https://crbug.com/785555
TEST_F(SignalSenderVerificationTest,DISABLED_TestOwnerChanged)209*635a8641SAndroid Build Coastguard Worker TEST_F(SignalSenderVerificationTest, DISABLED_TestOwnerChanged) {
210*635a8641SAndroid Build Coastguard Worker   const char kMessage[] = "hello, world";
211*635a8641SAndroid Build Coastguard Worker 
212*635a8641SAndroid Build Coastguard Worker   // Send the test signal from the exported object.
213*635a8641SAndroid Build Coastguard Worker   test_service_->SendTestSignal(kMessage);
214*635a8641SAndroid Build Coastguard Worker   // Receive the signal with the object proxy. The signal is handled in
215*635a8641SAndroid Build Coastguard Worker   // SignalSenderVerificationTest::OnTestSignal() in the main thread.
216*635a8641SAndroid Build Coastguard Worker   WaitForTestSignal();
217*635a8641SAndroid Build Coastguard Worker   ASSERT_EQ(kMessage, test_signal_string_);
218*635a8641SAndroid Build Coastguard Worker 
219*635a8641SAndroid Build Coastguard Worker   // Release and acquire the name ownership.
220*635a8641SAndroid Build Coastguard Worker   // latest_name_owner_ should be non empty as |test_service_| owns the name.
221*635a8641SAndroid Build Coastguard Worker   ASSERT_FALSE(latest_name_owner_.empty());
222*635a8641SAndroid Build Coastguard Worker   test_service_->ShutdownAndBlock();
223*635a8641SAndroid Build Coastguard Worker   // OnNameOwnerChanged will PostTask to quit the message loop.
224*635a8641SAndroid Build Coastguard Worker   run_loop_.reset(new base::RunLoop);
225*635a8641SAndroid Build Coastguard Worker   run_loop_->Run();
226*635a8641SAndroid Build Coastguard Worker   // latest_name_owner_ should be empty as the owner is gone.
227*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(latest_name_owner_.empty());
228*635a8641SAndroid Build Coastguard Worker 
229*635a8641SAndroid Build Coastguard Worker   // Reset the flag as NameOwnerChanged is already received in setup.
230*635a8641SAndroid Build Coastguard Worker   on_name_owner_changed_called_ = false;
231*635a8641SAndroid Build Coastguard Worker   on_ownership_called_ = false;
232*635a8641SAndroid Build Coastguard Worker   test_service2_->RequestOwnership(
233*635a8641SAndroid Build Coastguard Worker       base::Bind(&SignalSenderVerificationTest::OnOwnership,
234*635a8641SAndroid Build Coastguard Worker                  base::Unretained(this), true));
235*635a8641SAndroid Build Coastguard Worker   // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop,
236*635a8641SAndroid Build Coastguard Worker   // but there's no expected order of those 2 event.
237*635a8641SAndroid Build Coastguard Worker   run_loop_.reset(new base::RunLoop);
238*635a8641SAndroid Build Coastguard Worker   run_loop_->Run();
239*635a8641SAndroid Build Coastguard Worker   if (!on_name_owner_changed_called_ || !on_ownership_called_) {
240*635a8641SAndroid Build Coastguard Worker     run_loop_.reset(new base::RunLoop);
241*635a8641SAndroid Build Coastguard Worker     run_loop_->Run();
242*635a8641SAndroid Build Coastguard Worker   }
243*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(on_name_owner_changed_called_);
244*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(on_ownership_called_);
245*635a8641SAndroid Build Coastguard Worker 
246*635a8641SAndroid Build Coastguard Worker   // latest_name_owner_ becomes non empty as the new owner appears.
247*635a8641SAndroid Build Coastguard Worker   ASSERT_FALSE(latest_name_owner_.empty());
248*635a8641SAndroid Build Coastguard Worker 
249*635a8641SAndroid Build Coastguard Worker   // Now the second service owns the name.
250*635a8641SAndroid Build Coastguard Worker   const char kNewMessage[] = "hello, new world";
251*635a8641SAndroid Build Coastguard Worker 
252*635a8641SAndroid Build Coastguard Worker   test_service2_->SendTestSignal(kNewMessage);
253*635a8641SAndroid Build Coastguard Worker   WaitForTestSignal();
254*635a8641SAndroid Build Coastguard Worker   ASSERT_EQ(kNewMessage, test_signal_string_);
255*635a8641SAndroid Build Coastguard Worker }
256*635a8641SAndroid Build Coastguard Worker 
257*635a8641SAndroid Build Coastguard Worker // Flaky. https://crbug.com/785555
TEST_F(SignalSenderVerificationTest,DISABLED_TestOwnerStealing)258*635a8641SAndroid Build Coastguard Worker TEST_F(SignalSenderVerificationTest, DISABLED_TestOwnerStealing) {
259*635a8641SAndroid Build Coastguard Worker   // Release and acquire the name ownership.
260*635a8641SAndroid Build Coastguard Worker   // latest_name_owner_ should be non empty as |test_service_| owns the name.
261*635a8641SAndroid Build Coastguard Worker   ASSERT_FALSE(latest_name_owner_.empty());
262*635a8641SAndroid Build Coastguard Worker   test_service_->ShutdownAndBlock();
263*635a8641SAndroid Build Coastguard Worker   // OnNameOwnerChanged will PostTask to quit the message loop.
264*635a8641SAndroid Build Coastguard Worker   run_loop_.reset(new base::RunLoop);
265*635a8641SAndroid Build Coastguard Worker   run_loop_->Run();
266*635a8641SAndroid Build Coastguard Worker   // latest_name_owner_ should be empty as the owner is gone.
267*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(latest_name_owner_.empty());
268*635a8641SAndroid Build Coastguard Worker   // Reset the flag as NameOwnerChanged is already received in setup.
269*635a8641SAndroid Build Coastguard Worker   on_name_owner_changed_called_ = false;
270*635a8641SAndroid Build Coastguard Worker 
271*635a8641SAndroid Build Coastguard Worker   // Start a test service that allows theft, using the D-Bus thread.
272*635a8641SAndroid Build Coastguard Worker   TestService::Options options;
273*635a8641SAndroid Build Coastguard Worker   options.dbus_task_runner = dbus_thread_->task_runner();
274*635a8641SAndroid Build Coastguard Worker   options.request_ownership_options = Bus::REQUIRE_PRIMARY_ALLOW_REPLACEMENT;
275*635a8641SAndroid Build Coastguard Worker   options.service_name = test_service_->service_name();
276*635a8641SAndroid Build Coastguard Worker   TestService stealable_test_service(options);
277*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(stealable_test_service.StartService());
278*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(stealable_test_service.WaitUntilServiceIsStarted());
279*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(stealable_test_service.HasDBusThread());
280*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(stealable_test_service.has_ownership());
281*635a8641SAndroid Build Coastguard Worker 
282*635a8641SAndroid Build Coastguard Worker   // OnNameOwnerChanged will PostTask to quit the message loop.
283*635a8641SAndroid Build Coastguard Worker   run_loop_.reset(new base::RunLoop);
284*635a8641SAndroid Build Coastguard Worker   run_loop_->Run();
285*635a8641SAndroid Build Coastguard Worker 
286*635a8641SAndroid Build Coastguard Worker   // Send a signal to check that the service is correctly owned.
287*635a8641SAndroid Build Coastguard Worker   const char kMessage[] = "hello, world";
288*635a8641SAndroid Build Coastguard Worker 
289*635a8641SAndroid Build Coastguard Worker   // Send the test signal from the exported object.
290*635a8641SAndroid Build Coastguard Worker   stealable_test_service.SendTestSignal(kMessage);
291*635a8641SAndroid Build Coastguard Worker   // Receive the signal with the object proxy. The signal is handled in
292*635a8641SAndroid Build Coastguard Worker   // SignalSenderVerificationTest::OnTestSignal() in the main thread.
293*635a8641SAndroid Build Coastguard Worker   WaitForTestSignal();
294*635a8641SAndroid Build Coastguard Worker   ASSERT_EQ(kMessage, test_signal_string_);
295*635a8641SAndroid Build Coastguard Worker 
296*635a8641SAndroid Build Coastguard Worker   // Reset the flag as NameOwnerChanged was called above.
297*635a8641SAndroid Build Coastguard Worker   on_name_owner_changed_called_ = false;
298*635a8641SAndroid Build Coastguard Worker   test_service2_->RequestOwnership(
299*635a8641SAndroid Build Coastguard Worker       base::Bind(&SignalSenderVerificationTest::OnOwnership,
300*635a8641SAndroid Build Coastguard Worker                  base::Unretained(this), true));
301*635a8641SAndroid Build Coastguard Worker   // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop,
302*635a8641SAndroid Build Coastguard Worker   // but there's no expected order of those 2 event.
303*635a8641SAndroid Build Coastguard Worker   run_loop_.reset(new base::RunLoop);
304*635a8641SAndroid Build Coastguard Worker   run_loop_->Run();
305*635a8641SAndroid Build Coastguard Worker   if (!on_name_owner_changed_called_ || !on_ownership_called_) {
306*635a8641SAndroid Build Coastguard Worker     run_loop_.reset(new base::RunLoop);
307*635a8641SAndroid Build Coastguard Worker     run_loop_->Run();
308*635a8641SAndroid Build Coastguard Worker   }
309*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(on_name_owner_changed_called_);
310*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(on_ownership_called_);
311*635a8641SAndroid Build Coastguard Worker 
312*635a8641SAndroid Build Coastguard Worker   // Now the second service owns the name.
313*635a8641SAndroid Build Coastguard Worker   const char kNewMessage[] = "hello, new world";
314*635a8641SAndroid Build Coastguard Worker 
315*635a8641SAndroid Build Coastguard Worker   test_service2_->SendTestSignal(kNewMessage);
316*635a8641SAndroid Build Coastguard Worker   WaitForTestSignal();
317*635a8641SAndroid Build Coastguard Worker   ASSERT_EQ(kNewMessage, test_signal_string_);
318*635a8641SAndroid Build Coastguard Worker 
319*635a8641SAndroid Build Coastguard Worker   SafeServiceStop(&stealable_test_service);
320*635a8641SAndroid Build Coastguard Worker }
321*635a8641SAndroid Build Coastguard Worker 
322*635a8641SAndroid Build Coastguard Worker // Fails on Linux ChromiumOS Tests
TEST_F(SignalSenderVerificationTest,DISABLED_TestMultipleObjects)323*635a8641SAndroid Build Coastguard Worker TEST_F(SignalSenderVerificationTest, DISABLED_TestMultipleObjects) {
324*635a8641SAndroid Build Coastguard Worker   const char kMessage[] = "hello, world";
325*635a8641SAndroid Build Coastguard Worker 
326*635a8641SAndroid Build Coastguard Worker   ObjectProxy* object_proxy2 = bus_->GetObjectProxy(
327*635a8641SAndroid Build Coastguard Worker       test_service_->service_name(),
328*635a8641SAndroid Build Coastguard Worker       ObjectPath("/org/chromium/DifferentObject"));
329*635a8641SAndroid Build Coastguard Worker 
330*635a8641SAndroid Build Coastguard Worker   bool second_name_owner_changed_called = false;
331*635a8641SAndroid Build Coastguard Worker   object_proxy2->SetNameOwnerChangedCallback(
332*635a8641SAndroid Build Coastguard Worker       base::Bind(&SignalSenderVerificationTest::OnNameOwnerChanged,
333*635a8641SAndroid Build Coastguard Worker                  base::Unretained(this),
334*635a8641SAndroid Build Coastguard Worker                  &second_name_owner_changed_called));
335*635a8641SAndroid Build Coastguard Worker 
336*635a8641SAndroid Build Coastguard Worker   // Connect to a signal on the additional remote object to trigger the
337*635a8641SAndroid Build Coastguard Worker   // name owner matching.
338*635a8641SAndroid Build Coastguard Worker   object_proxy2->ConnectToSignal(
339*635a8641SAndroid Build Coastguard Worker       "org.chromium.DifferentTestInterface",
340*635a8641SAndroid Build Coastguard Worker       "Test",
341*635a8641SAndroid Build Coastguard Worker       base::Bind(&SignalSenderVerificationTest::OnTestSignal,
342*635a8641SAndroid Build Coastguard Worker                  base::Unretained(this)),
343*635a8641SAndroid Build Coastguard Worker       base::Bind(&SignalSenderVerificationTest::OnConnected,
344*635a8641SAndroid Build Coastguard Worker                  base::Unretained(this)));
345*635a8641SAndroid Build Coastguard Worker   // Wait until the object proxy is connected to the signal.
346*635a8641SAndroid Build Coastguard Worker   run_loop_.reset(new base::RunLoop);
347*635a8641SAndroid Build Coastguard Worker   run_loop_->Run();
348*635a8641SAndroid Build Coastguard Worker 
349*635a8641SAndroid Build Coastguard Worker   // Send the test signal from the exported object.
350*635a8641SAndroid Build Coastguard Worker   test_service_->SendTestSignal(kMessage);
351*635a8641SAndroid Build Coastguard Worker   // Receive the signal with the object proxy. The signal is handled in
352*635a8641SAndroid Build Coastguard Worker   // SignalSenderVerificationTest::OnTestSignal() in the main thread.
353*635a8641SAndroid Build Coastguard Worker   WaitForTestSignal();
354*635a8641SAndroid Build Coastguard Worker   ASSERT_EQ(kMessage, test_signal_string_);
355*635a8641SAndroid Build Coastguard Worker 
356*635a8641SAndroid Build Coastguard Worker   // Release and acquire the name ownership.
357*635a8641SAndroid Build Coastguard Worker   // latest_name_owner_ should be non empty as |test_service_| owns the name.
358*635a8641SAndroid Build Coastguard Worker   ASSERT_FALSE(latest_name_owner_.empty());
359*635a8641SAndroid Build Coastguard Worker   test_service_->ShutdownAndBlock();
360*635a8641SAndroid Build Coastguard Worker   // OnNameOwnerChanged will PostTask to quit the message loop.
361*635a8641SAndroid Build Coastguard Worker   run_loop_.reset(new base::RunLoop);
362*635a8641SAndroid Build Coastguard Worker   run_loop_->Run();
363*635a8641SAndroid Build Coastguard Worker   // latest_name_owner_ should be empty as the owner is gone.
364*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(latest_name_owner_.empty());
365*635a8641SAndroid Build Coastguard Worker 
366*635a8641SAndroid Build Coastguard Worker   // Reset the flag as NameOwnerChanged is already received in setup.
367*635a8641SAndroid Build Coastguard Worker   on_name_owner_changed_called_ = false;
368*635a8641SAndroid Build Coastguard Worker   second_name_owner_changed_called = false;
369*635a8641SAndroid Build Coastguard Worker   test_service2_->RequestOwnership(
370*635a8641SAndroid Build Coastguard Worker       base::Bind(&SignalSenderVerificationTest::OnOwnership,
371*635a8641SAndroid Build Coastguard Worker                  base::Unretained(this), true));
372*635a8641SAndroid Build Coastguard Worker   // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop,
373*635a8641SAndroid Build Coastguard Worker   // but there's no expected order of those 2 event.
374*635a8641SAndroid Build Coastguard Worker   while (!on_name_owner_changed_called_ || !second_name_owner_changed_called ||
375*635a8641SAndroid Build Coastguard Worker          !on_ownership_called_) {
376*635a8641SAndroid Build Coastguard Worker     run_loop_.reset(new base::RunLoop);
377*635a8641SAndroid Build Coastguard Worker     run_loop_->Run();
378*635a8641SAndroid Build Coastguard Worker   }
379*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(on_name_owner_changed_called_);
380*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(second_name_owner_changed_called);
381*635a8641SAndroid Build Coastguard Worker   ASSERT_TRUE(on_ownership_called_);
382*635a8641SAndroid Build Coastguard Worker 
383*635a8641SAndroid Build Coastguard Worker   // latest_name_owner_ becomes non empty as the new owner appears.
384*635a8641SAndroid Build Coastguard Worker   ASSERT_FALSE(latest_name_owner_.empty());
385*635a8641SAndroid Build Coastguard Worker 
386*635a8641SAndroid Build Coastguard Worker   // Now the second service owns the name.
387*635a8641SAndroid Build Coastguard Worker   const char kNewMessage[] = "hello, new world";
388*635a8641SAndroid Build Coastguard Worker 
389*635a8641SAndroid Build Coastguard Worker   test_service2_->SendTestSignal(kNewMessage);
390*635a8641SAndroid Build Coastguard Worker   WaitForTestSignal();
391*635a8641SAndroid Build Coastguard Worker   ASSERT_EQ(kNewMessage, test_signal_string_);
392*635a8641SAndroid Build Coastguard Worker }
393*635a8641SAndroid Build Coastguard Worker 
394*635a8641SAndroid Build Coastguard Worker }  // namespace dbus
395