OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/metrics/histogram_samples.h" | 9 #include "base/metrics/histogram_samples.h" |
10 #include "base/metrics/statistics_recorder.h" | 10 #include "base/metrics/statistics_recorder.h" |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 message_loop_.Quit(); | 147 message_loop_.Quit(); |
148 } | 148 } |
149 | 149 |
150 protected: | 150 protected: |
151 // Wait for the hey signal to be received. | 151 // Wait for the hey signal to be received. |
152 void WaitForTestSignal() { | 152 void WaitForTestSignal() { |
153 // OnTestSignal() will quit the message loop. | 153 // OnTestSignal() will quit the message loop. |
154 message_loop_.Run(); | 154 message_loop_.Run(); |
155 } | 155 } |
156 | 156 |
| 157 // Stopping a thread is considered an IO operation, so we need to fiddle with |
| 158 // thread restrictions before and after calling Stop() on a TestService. |
| 159 void SafeServiceStop(TestService* test_service) { |
| 160 base::ThreadRestrictions::SetIOAllowed(true); |
| 161 test_service->Stop(); |
| 162 base::ThreadRestrictions::SetIOAllowed(false); |
| 163 } |
| 164 |
157 base::MessageLoop message_loop_; | 165 base::MessageLoop message_loop_; |
158 scoped_ptr<base::Thread> dbus_thread_; | 166 scoped_ptr<base::Thread> dbus_thread_; |
159 scoped_refptr<Bus> bus_; | 167 scoped_refptr<Bus> bus_; |
160 ObjectProxy* object_proxy_; | 168 ObjectProxy* object_proxy_; |
161 scoped_ptr<TestService> test_service_; | 169 scoped_ptr<TestService> test_service_; |
162 scoped_ptr<TestService> test_service2_; | 170 scoped_ptr<TestService> test_service2_; |
163 // Text message from "Test" signal. | 171 // Text message from "Test" signal. |
164 std::string test_signal_string_; | 172 std::string test_signal_string_; |
165 | 173 |
166 // The known latest name owner of TestService. Updated in OnNameOwnerChanged. | 174 // The known latest name owner of TestService. Updated in OnNameOwnerChanged. |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 // latest_name_owner_ should be non empty as |test_service_| owns the name. | 225 // latest_name_owner_ should be non empty as |test_service_| owns the name. |
218 ASSERT_FALSE(latest_name_owner_.empty()); | 226 ASSERT_FALSE(latest_name_owner_.empty()); |
219 test_service_->ShutdownAndBlock(); | 227 test_service_->ShutdownAndBlock(); |
220 // OnNameOwnerChanged will PostTask to quit the message loop. | 228 // OnNameOwnerChanged will PostTask to quit the message loop. |
221 message_loop_.Run(); | 229 message_loop_.Run(); |
222 // latest_name_owner_ should be empty as the owner is gone. | 230 // latest_name_owner_ should be empty as the owner is gone. |
223 ASSERT_TRUE(latest_name_owner_.empty()); | 231 ASSERT_TRUE(latest_name_owner_.empty()); |
224 | 232 |
225 // Reset the flag as NameOwnerChanged is already received in setup. | 233 // Reset the flag as NameOwnerChanged is already received in setup. |
226 on_name_owner_changed_called_ = false; | 234 on_name_owner_changed_called_ = false; |
| 235 on_ownership_called_ = false; |
227 test_service2_->RequestOwnership( | 236 test_service2_->RequestOwnership( |
228 base::Bind(&SignalSenderVerificationTest::OnOwnership, | 237 base::Bind(&SignalSenderVerificationTest::OnOwnership, |
229 base::Unretained(this), true)); | 238 base::Unretained(this), true)); |
230 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop, | 239 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop, |
231 // but there's no expected order of those 2 event. | 240 // but there's no expected order of those 2 event. |
232 message_loop_.Run(); | 241 message_loop_.Run(); |
233 if (!on_name_owner_changed_called_ || !on_ownership_called_) | 242 if (!on_name_owner_changed_called_ || !on_ownership_called_) |
234 message_loop_.Run(); | 243 message_loop_.Run(); |
235 ASSERT_TRUE(on_name_owner_changed_called_); | 244 ASSERT_TRUE(on_name_owner_changed_called_); |
236 ASSERT_TRUE(on_ownership_called_); | 245 ASSERT_TRUE(on_ownership_called_); |
237 | 246 |
238 // latest_name_owner_ becomes non empty as the new owner appears. | 247 // latest_name_owner_ becomes non empty as the new owner appears. |
239 ASSERT_FALSE(latest_name_owner_.empty()); | 248 ASSERT_FALSE(latest_name_owner_.empty()); |
240 | 249 |
241 // Now the second service owns the name. | 250 // Now the second service owns the name. |
242 const char kNewMessage[] = "hello, new world"; | 251 const char kNewMessage[] = "hello, new world"; |
243 | 252 |
244 test_service2_->SendTestSignal(kNewMessage); | 253 test_service2_->SendTestSignal(kNewMessage); |
245 WaitForTestSignal(); | 254 WaitForTestSignal(); |
246 ASSERT_EQ(kNewMessage, test_signal_string_); | 255 ASSERT_EQ(kNewMessage, test_signal_string_); |
247 } | 256 } |
248 | 257 |
| 258 TEST_F(SignalSenderVerificationTest, TestOwnerStealing) { |
| 259 // Release and acquire the name ownership. |
| 260 // latest_name_owner_ should be non empty as |test_service_| owns the name. |
| 261 ASSERT_FALSE(latest_name_owner_.empty()); |
| 262 test_service_->ShutdownAndBlock(); |
| 263 // OnNameOwnerChanged will PostTask to quit the message loop. |
| 264 message_loop_.Run(); |
| 265 // latest_name_owner_ should be empty as the owner is gone. |
| 266 ASSERT_TRUE(latest_name_owner_.empty()); |
| 267 // Reset the flag as NameOwnerChanged is already received in setup. |
| 268 on_name_owner_changed_called_ = false; |
| 269 |
| 270 // Start a test service that allows theft, using the D-Bus thread. |
| 271 TestService::Options options; |
| 272 options.dbus_task_runner = dbus_thread_->message_loop_proxy(); |
| 273 options.request_ownership_options = Bus::REQUIRE_PRIMARY_ALLOW_REPLACEMENT; |
| 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. |
| 281 message_loop_.Run(); |
| 282 |
| 283 // Send a signal to check that the service is correctly owned. |
| 284 const char kMessage[] = "hello, world"; |
| 285 |
| 286 // Send the test signal from the exported object. |
| 287 stealable_test_service.SendTestSignal(kMessage); |
| 288 // Receive the signal with the object proxy. The signal is handled in |
| 289 // SignalSenderVerificationTest::OnTestSignal() in the main thread. |
| 290 WaitForTestSignal(); |
| 291 ASSERT_EQ(kMessage, test_signal_string_); |
| 292 |
| 293 // Reset the flag as NameOwnerChanged was called above. |
| 294 on_name_owner_changed_called_ = false; |
| 295 test_service2_->RequestOwnership( |
| 296 base::Bind(&SignalSenderVerificationTest::OnOwnership, |
| 297 base::Unretained(this), true)); |
| 298 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop, |
| 299 // but there's no expected order of those 2 event. |
| 300 message_loop_.Run(); |
| 301 if (!on_name_owner_changed_called_ || !on_ownership_called_) |
| 302 message_loop_.Run(); |
| 303 ASSERT_TRUE(on_name_owner_changed_called_); |
| 304 ASSERT_TRUE(on_ownership_called_); |
| 305 |
| 306 // Now the second service owns the name. |
| 307 const char kNewMessage[] = "hello, new world"; |
| 308 |
| 309 test_service2_->SendTestSignal(kNewMessage); |
| 310 WaitForTestSignal(); |
| 311 ASSERT_EQ(kNewMessage, test_signal_string_); |
| 312 |
| 313 SafeServiceStop(&stealable_test_service); |
| 314 } |
| 315 |
249 // Fails on Linux ChromiumOS Tests | 316 // Fails on Linux ChromiumOS Tests |
250 TEST_F(SignalSenderVerificationTest, DISABLED_TestMultipleObjects) { | 317 TEST_F(SignalSenderVerificationTest, DISABLED_TestMultipleObjects) { |
251 const char kMessage[] = "hello, world"; | 318 const char kMessage[] = "hello, world"; |
252 | 319 |
253 ObjectProxy* object_proxy2 = bus_->GetObjectProxy( | 320 ObjectProxy* object_proxy2 = bus_->GetObjectProxy( |
254 "org.chromium.TestService", | 321 "org.chromium.TestService", |
255 ObjectPath("/org/chromium/DifferentObject")); | 322 ObjectPath("/org/chromium/DifferentObject")); |
256 | 323 |
257 bool second_name_owner_changed_called = false; | 324 bool second_name_owner_changed_called = false; |
258 object_proxy2->SetNameOwnerChangedCallback( | 325 object_proxy2->SetNameOwnerChangedCallback( |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 | 375 |
309 // Now the second service owns the name. | 376 // Now the second service owns the name. |
310 const char kNewMessage[] = "hello, new world"; | 377 const char kNewMessage[] = "hello, new world"; |
311 | 378 |
312 test_service2_->SendTestSignal(kNewMessage); | 379 test_service2_->SendTestSignal(kNewMessage); |
313 WaitForTestSignal(); | 380 WaitForTestSignal(); |
314 ASSERT_EQ(kNewMessage, test_signal_string_); | 381 ASSERT_EQ(kNewMessage, test_signal_string_); |
315 } | 382 } |
316 | 383 |
317 } // namespace dbus | 384 } // namespace dbus |
OLD | NEW |