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" |
| 11 #include "base/run_loop.h" |
11 #include "base/test/test_timeouts.h" | 12 #include "base/test/test_timeouts.h" |
12 #include "base/threading/platform_thread.h" | 13 #include "base/threading/platform_thread.h" |
13 #include "base/threading/thread_restrictions.h" | 14 #include "base/threading/thread_restrictions.h" |
14 #include "dbus/bus.h" | 15 #include "dbus/bus.h" |
15 #include "dbus/message.h" | 16 #include "dbus/message.h" |
16 #include "dbus/object_proxy.h" | 17 #include "dbus/object_proxy.h" |
17 #include "dbus/test_service.h" | 18 #include "dbus/test_service.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
20 namespace dbus { | 21 namespace dbus { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 // Connect to the "Test" signal of "org.chromium.TestInterface" from | 59 // Connect to the "Test" signal of "org.chromium.TestInterface" from |
59 // the remote object. | 60 // the remote object. |
60 object_proxy_->ConnectToSignal( | 61 object_proxy_->ConnectToSignal( |
61 "org.chromium.TestInterface", | 62 "org.chromium.TestInterface", |
62 "Test", | 63 "Test", |
63 base::Bind(&SignalSenderVerificationTest::OnTestSignal, | 64 base::Bind(&SignalSenderVerificationTest::OnTestSignal, |
64 base::Unretained(this)), | 65 base::Unretained(this)), |
65 base::Bind(&SignalSenderVerificationTest::OnConnected, | 66 base::Bind(&SignalSenderVerificationTest::OnConnected, |
66 base::Unretained(this))); | 67 base::Unretained(this))); |
67 // Wait until the object proxy is connected to the signal. | 68 // Wait until the object proxy is connected to the signal. |
68 message_loop_.Run(); | 69 base::RunLoop().Run(); |
69 | 70 |
70 // Start the test service, using the D-Bus thread. | 71 // Start the test service, using the D-Bus thread. |
71 TestService::Options options; | 72 TestService::Options options; |
72 options.dbus_task_runner = dbus_thread_->message_loop_proxy(); | 73 options.dbus_task_runner = dbus_thread_->message_loop_proxy(); |
73 test_service_.reset(new TestService(options)); | 74 test_service_.reset(new TestService(options)); |
74 ASSERT_TRUE(test_service_->StartService()); | 75 ASSERT_TRUE(test_service_->StartService()); |
75 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted()); | 76 ASSERT_TRUE(test_service_->WaitUntilServiceIsStarted()); |
76 ASSERT_TRUE(test_service_->HasDBusThread()); | 77 ASSERT_TRUE(test_service_->HasDBusThread()); |
77 ASSERT_TRUE(test_service_->has_ownership()); | 78 ASSERT_TRUE(test_service_->has_ownership()); |
78 | 79 |
79 // Same setup for the second TestService. This service should not have the | 80 // Same setup for the second TestService. This service should not have the |
80 // ownership of the name at this point. | 81 // ownership of the name at this point. |
81 test_service2_.reset(new TestService(options)); | 82 test_service2_.reset(new TestService(options)); |
82 ASSERT_TRUE(test_service2_->StartService()); | 83 ASSERT_TRUE(test_service2_->StartService()); |
83 ASSERT_TRUE(test_service2_->WaitUntilServiceIsStarted()); | 84 ASSERT_TRUE(test_service2_->WaitUntilServiceIsStarted()); |
84 ASSERT_TRUE(test_service2_->HasDBusThread()); | 85 ASSERT_TRUE(test_service2_->HasDBusThread()); |
85 ASSERT_FALSE(test_service2_->has_ownership()); | 86 ASSERT_FALSE(test_service2_->has_ownership()); |
86 | 87 |
87 // The name should be owned and known at this point. | 88 // The name should be owned and known at this point. |
88 if (!on_name_owner_changed_called_) | 89 if (!on_name_owner_changed_called_) |
89 message_loop_.Run(); | 90 base::RunLoop().Run(); |
90 ASSERT_FALSE(latest_name_owner_.empty()); | 91 ASSERT_FALSE(latest_name_owner_.empty()); |
91 } | 92 } |
92 | 93 |
93 virtual void TearDown() { | 94 virtual void TearDown() { |
94 bus_->ShutdownOnDBusThreadAndBlock(); | 95 bus_->ShutdownOnDBusThreadAndBlock(); |
95 | 96 |
96 // Shut down the service. | 97 // Shut down the service. |
97 test_service_->ShutdownAndBlock(); | 98 test_service_->ShutdownAndBlock(); |
98 test_service2_->ShutdownAndBlock(); | 99 test_service2_->ShutdownAndBlock(); |
99 | 100 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 const std::string& signal_name, | 142 const std::string& signal_name, |
142 bool success) { | 143 bool success) { |
143 ASSERT_TRUE(success); | 144 ASSERT_TRUE(success); |
144 message_loop_.Quit(); | 145 message_loop_.Quit(); |
145 } | 146 } |
146 | 147 |
147 protected: | 148 protected: |
148 // Wait for the hey signal to be received. | 149 // Wait for the hey signal to be received. |
149 void WaitForTestSignal() { | 150 void WaitForTestSignal() { |
150 // OnTestSignal() will quit the message loop. | 151 // OnTestSignal() will quit the message loop. |
151 message_loop_.Run(); | 152 base::RunLoop().Run(); |
152 } | 153 } |
153 | 154 |
154 // Stopping a thread is considered an IO operation, so we need to fiddle with | 155 // Stopping a thread is considered an IO operation, so we need to fiddle with |
155 // thread restrictions before and after calling Stop() on a TestService. | 156 // thread restrictions before and after calling Stop() on a TestService. |
156 void SafeServiceStop(TestService* test_service) { | 157 void SafeServiceStop(TestService* test_service) { |
157 base::ThreadRestrictions::SetIOAllowed(true); | 158 base::ThreadRestrictions::SetIOAllowed(true); |
158 test_service->Stop(); | 159 test_service->Stop(); |
159 base::ThreadRestrictions::SetIOAllowed(false); | 160 base::ThreadRestrictions::SetIOAllowed(false); |
160 } | 161 } |
161 | 162 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 // Receive the signal with the object proxy. The signal is handled in | 218 // Receive the signal with the object proxy. The signal is handled in |
218 // SignalSenderVerificationTest::OnTestSignal() in the main thread. | 219 // SignalSenderVerificationTest::OnTestSignal() in the main thread. |
219 WaitForTestSignal(); | 220 WaitForTestSignal(); |
220 ASSERT_EQ(kMessage, test_signal_string_); | 221 ASSERT_EQ(kMessage, test_signal_string_); |
221 | 222 |
222 // Release and acquire the name ownership. | 223 // Release and acquire the name ownership. |
223 // latest_name_owner_ should be non empty as |test_service_| owns the name. | 224 // latest_name_owner_ should be non empty as |test_service_| owns the name. |
224 ASSERT_FALSE(latest_name_owner_.empty()); | 225 ASSERT_FALSE(latest_name_owner_.empty()); |
225 test_service_->ShutdownAndBlock(); | 226 test_service_->ShutdownAndBlock(); |
226 // OnNameOwnerChanged will PostTask to quit the message loop. | 227 // OnNameOwnerChanged will PostTask to quit the message loop. |
227 message_loop_.Run(); | 228 base::RunLoop().Run(); |
228 // latest_name_owner_ should be empty as the owner is gone. | 229 // latest_name_owner_ should be empty as the owner is gone. |
229 ASSERT_TRUE(latest_name_owner_.empty()); | 230 ASSERT_TRUE(latest_name_owner_.empty()); |
230 | 231 |
231 // Reset the flag as NameOwnerChanged is already received in setup. | 232 // Reset the flag as NameOwnerChanged is already received in setup. |
232 on_name_owner_changed_called_ = false; | 233 on_name_owner_changed_called_ = false; |
233 on_ownership_called_ = false; | 234 on_ownership_called_ = false; |
234 test_service2_->RequestOwnership( | 235 test_service2_->RequestOwnership( |
235 base::Bind(&SignalSenderVerificationTest::OnOwnership, | 236 base::Bind(&SignalSenderVerificationTest::OnOwnership, |
236 base::Unretained(this), true)); | 237 base::Unretained(this), true)); |
237 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop, | 238 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop, |
238 // but there's no expected order of those 2 event. | 239 // but there's no expected order of those 2 event. |
239 message_loop_.Run(); | 240 base::RunLoop().Run(); |
240 if (!on_name_owner_changed_called_ || !on_ownership_called_) | 241 if (!on_name_owner_changed_called_ || !on_ownership_called_) |
241 message_loop_.Run(); | 242 base::RunLoop().Run(); |
242 ASSERT_TRUE(on_name_owner_changed_called_); | 243 ASSERT_TRUE(on_name_owner_changed_called_); |
243 ASSERT_TRUE(on_ownership_called_); | 244 ASSERT_TRUE(on_ownership_called_); |
244 | 245 |
245 // latest_name_owner_ becomes non empty as the new owner appears. | 246 // latest_name_owner_ becomes non empty as the new owner appears. |
246 ASSERT_FALSE(latest_name_owner_.empty()); | 247 ASSERT_FALSE(latest_name_owner_.empty()); |
247 | 248 |
248 // Now the second service owns the name. | 249 // Now the second service owns the name. |
249 const char kNewMessage[] = "hello, new world"; | 250 const char kNewMessage[] = "hello, new world"; |
250 | 251 |
251 test_service2_->SendTestSignal(kNewMessage); | 252 test_service2_->SendTestSignal(kNewMessage); |
252 WaitForTestSignal(); | 253 WaitForTestSignal(); |
253 ASSERT_EQ(kNewMessage, test_signal_string_); | 254 ASSERT_EQ(kNewMessage, test_signal_string_); |
254 } | 255 } |
255 | 256 |
256 TEST_F(SignalSenderVerificationTest, TestOwnerStealing) { | 257 TEST_F(SignalSenderVerificationTest, TestOwnerStealing) { |
257 // Release and acquire the name ownership. | 258 // Release and acquire the name ownership. |
258 // latest_name_owner_ should be non empty as |test_service_| owns the name. | 259 // latest_name_owner_ should be non empty as |test_service_| owns the name. |
259 ASSERT_FALSE(latest_name_owner_.empty()); | 260 ASSERT_FALSE(latest_name_owner_.empty()); |
260 test_service_->ShutdownAndBlock(); | 261 test_service_->ShutdownAndBlock(); |
261 // OnNameOwnerChanged will PostTask to quit the message loop. | 262 // OnNameOwnerChanged will PostTask to quit the message loop. |
262 message_loop_.Run(); | 263 base::RunLoop().Run(); |
263 // latest_name_owner_ should be empty as the owner is gone. | 264 // latest_name_owner_ should be empty as the owner is gone. |
264 ASSERT_TRUE(latest_name_owner_.empty()); | 265 ASSERT_TRUE(latest_name_owner_.empty()); |
265 // Reset the flag as NameOwnerChanged is already received in setup. | 266 // Reset the flag as NameOwnerChanged is already received in setup. |
266 on_name_owner_changed_called_ = false; | 267 on_name_owner_changed_called_ = false; |
267 | 268 |
268 // Start a test service that allows theft, using the D-Bus thread. | 269 // Start a test service that allows theft, using the D-Bus thread. |
269 TestService::Options options; | 270 TestService::Options options; |
270 options.dbus_task_runner = dbus_thread_->message_loop_proxy(); | 271 options.dbus_task_runner = dbus_thread_->message_loop_proxy(); |
271 options.request_ownership_options = Bus::REQUIRE_PRIMARY_ALLOW_REPLACEMENT; | 272 options.request_ownership_options = Bus::REQUIRE_PRIMARY_ALLOW_REPLACEMENT; |
272 TestService stealable_test_service(options); | 273 TestService stealable_test_service(options); |
273 ASSERT_TRUE(stealable_test_service.StartService()); | 274 ASSERT_TRUE(stealable_test_service.StartService()); |
274 ASSERT_TRUE(stealable_test_service.WaitUntilServiceIsStarted()); | 275 ASSERT_TRUE(stealable_test_service.WaitUntilServiceIsStarted()); |
275 ASSERT_TRUE(stealable_test_service.HasDBusThread()); | 276 ASSERT_TRUE(stealable_test_service.HasDBusThread()); |
276 ASSERT_TRUE(stealable_test_service.has_ownership()); | 277 ASSERT_TRUE(stealable_test_service.has_ownership()); |
277 | 278 |
278 // OnNameOwnerChanged will PostTask to quit the message loop. | 279 // OnNameOwnerChanged will PostTask to quit the message loop. |
279 message_loop_.Run(); | 280 base::RunLoop().Run(); |
280 | 281 |
281 // Send a signal to check that the service is correctly owned. | 282 // Send a signal to check that the service is correctly owned. |
282 const char kMessage[] = "hello, world"; | 283 const char kMessage[] = "hello, world"; |
283 | 284 |
284 // Send the test signal from the exported object. | 285 // Send the test signal from the exported object. |
285 stealable_test_service.SendTestSignal(kMessage); | 286 stealable_test_service.SendTestSignal(kMessage); |
286 // Receive the signal with the object proxy. The signal is handled in | 287 // Receive the signal with the object proxy. The signal is handled in |
287 // SignalSenderVerificationTest::OnTestSignal() in the main thread. | 288 // SignalSenderVerificationTest::OnTestSignal() in the main thread. |
288 WaitForTestSignal(); | 289 WaitForTestSignal(); |
289 ASSERT_EQ(kMessage, test_signal_string_); | 290 ASSERT_EQ(kMessage, test_signal_string_); |
290 | 291 |
291 // Reset the flag as NameOwnerChanged was called above. | 292 // Reset the flag as NameOwnerChanged was called above. |
292 on_name_owner_changed_called_ = false; | 293 on_name_owner_changed_called_ = false; |
293 test_service2_->RequestOwnership( | 294 test_service2_->RequestOwnership( |
294 base::Bind(&SignalSenderVerificationTest::OnOwnership, | 295 base::Bind(&SignalSenderVerificationTest::OnOwnership, |
295 base::Unretained(this), true)); | 296 base::Unretained(this), true)); |
296 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop, | 297 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop, |
297 // but there's no expected order of those 2 event. | 298 // but there's no expected order of those 2 event. |
298 message_loop_.Run(); | 299 base::RunLoop().Run(); |
299 if (!on_name_owner_changed_called_ || !on_ownership_called_) | 300 if (!on_name_owner_changed_called_ || !on_ownership_called_) |
300 message_loop_.Run(); | 301 base::RunLoop().Run(); |
301 ASSERT_TRUE(on_name_owner_changed_called_); | 302 ASSERT_TRUE(on_name_owner_changed_called_); |
302 ASSERT_TRUE(on_ownership_called_); | 303 ASSERT_TRUE(on_ownership_called_); |
303 | 304 |
304 // Now the second service owns the name. | 305 // Now the second service owns the name. |
305 const char kNewMessage[] = "hello, new world"; | 306 const char kNewMessage[] = "hello, new world"; |
306 | 307 |
307 test_service2_->SendTestSignal(kNewMessage); | 308 test_service2_->SendTestSignal(kNewMessage); |
308 WaitForTestSignal(); | 309 WaitForTestSignal(); |
309 ASSERT_EQ(kNewMessage, test_signal_string_); | 310 ASSERT_EQ(kNewMessage, test_signal_string_); |
310 | 311 |
(...skipping 17 matching lines...) Expand all Loading... |
328 // Connect to a signal on the additional remote object to trigger the | 329 // Connect to a signal on the additional remote object to trigger the |
329 // name owner matching. | 330 // name owner matching. |
330 object_proxy2->ConnectToSignal( | 331 object_proxy2->ConnectToSignal( |
331 "org.chromium.DifferentTestInterface", | 332 "org.chromium.DifferentTestInterface", |
332 "Test", | 333 "Test", |
333 base::Bind(&SignalSenderVerificationTest::OnTestSignal, | 334 base::Bind(&SignalSenderVerificationTest::OnTestSignal, |
334 base::Unretained(this)), | 335 base::Unretained(this)), |
335 base::Bind(&SignalSenderVerificationTest::OnConnected, | 336 base::Bind(&SignalSenderVerificationTest::OnConnected, |
336 base::Unretained(this))); | 337 base::Unretained(this))); |
337 // Wait until the object proxy is connected to the signal. | 338 // Wait until the object proxy is connected to the signal. |
338 message_loop_.Run(); | 339 base::RunLoop().Run(); |
339 | 340 |
340 // Send the test signal from the exported object. | 341 // Send the test signal from the exported object. |
341 test_service_->SendTestSignal(kMessage); | 342 test_service_->SendTestSignal(kMessage); |
342 // Receive the signal with the object proxy. The signal is handled in | 343 // Receive the signal with the object proxy. The signal is handled in |
343 // SignalSenderVerificationTest::OnTestSignal() in the main thread. | 344 // SignalSenderVerificationTest::OnTestSignal() in the main thread. |
344 WaitForTestSignal(); | 345 WaitForTestSignal(); |
345 ASSERT_EQ(kMessage, test_signal_string_); | 346 ASSERT_EQ(kMessage, test_signal_string_); |
346 | 347 |
347 // Release and acquire the name ownership. | 348 // Release and acquire the name ownership. |
348 // latest_name_owner_ should be non empty as |test_service_| owns the name. | 349 // latest_name_owner_ should be non empty as |test_service_| owns the name. |
349 ASSERT_FALSE(latest_name_owner_.empty()); | 350 ASSERT_FALSE(latest_name_owner_.empty()); |
350 test_service_->ShutdownAndBlock(); | 351 test_service_->ShutdownAndBlock(); |
351 // OnNameOwnerChanged will PostTask to quit the message loop. | 352 // OnNameOwnerChanged will PostTask to quit the message loop. |
352 message_loop_.Run(); | 353 base::RunLoop().Run(); |
353 // latest_name_owner_ should be empty as the owner is gone. | 354 // latest_name_owner_ should be empty as the owner is gone. |
354 ASSERT_TRUE(latest_name_owner_.empty()); | 355 ASSERT_TRUE(latest_name_owner_.empty()); |
355 | 356 |
356 // Reset the flag as NameOwnerChanged is already received in setup. | 357 // Reset the flag as NameOwnerChanged is already received in setup. |
357 on_name_owner_changed_called_ = false; | 358 on_name_owner_changed_called_ = false; |
358 second_name_owner_changed_called = false; | 359 second_name_owner_changed_called = false; |
359 test_service2_->RequestOwnership( | 360 test_service2_->RequestOwnership( |
360 base::Bind(&SignalSenderVerificationTest::OnOwnership, | 361 base::Bind(&SignalSenderVerificationTest::OnOwnership, |
361 base::Unretained(this), true)); | 362 base::Unretained(this), true)); |
362 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop, | 363 // Both of OnNameOwnerChanged() and OnOwnership() should quit the MessageLoop, |
363 // but there's no expected order of those 2 event. | 364 // but there's no expected order of those 2 event. |
364 while (!on_name_owner_changed_called_ || !second_name_owner_changed_called || | 365 while (!on_name_owner_changed_called_ || !second_name_owner_changed_called || |
365 !on_ownership_called_) | 366 !on_ownership_called_) |
366 message_loop_.Run(); | 367 base::RunLoop().Run(); |
367 ASSERT_TRUE(on_name_owner_changed_called_); | 368 ASSERT_TRUE(on_name_owner_changed_called_); |
368 ASSERT_TRUE(second_name_owner_changed_called); | 369 ASSERT_TRUE(second_name_owner_changed_called); |
369 ASSERT_TRUE(on_ownership_called_); | 370 ASSERT_TRUE(on_ownership_called_); |
370 | 371 |
371 // latest_name_owner_ becomes non empty as the new owner appears. | 372 // latest_name_owner_ becomes non empty as the new owner appears. |
372 ASSERT_FALSE(latest_name_owner_.empty()); | 373 ASSERT_FALSE(latest_name_owner_.empty()); |
373 | 374 |
374 // Now the second service owns the name. | 375 // Now the second service owns the name. |
375 const char kNewMessage[] = "hello, new world"; | 376 const char kNewMessage[] = "hello, new world"; |
376 | 377 |
377 test_service2_->SendTestSignal(kNewMessage); | 378 test_service2_->SendTestSignal(kNewMessage); |
378 WaitForTestSignal(); | 379 WaitForTestSignal(); |
379 ASSERT_EQ(kNewMessage, test_signal_string_); | 380 ASSERT_EQ(kNewMessage, test_signal_string_); |
380 } | 381 } |
381 | 382 |
382 } // namespace dbus | 383 } // namespace dbus |
OLD | NEW |