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