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