Chromium Code Reviews| 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/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
| 6 #include "base/message_loop.h" | 6 #include "base/message_loop.h" |
| 7 #include "base/timer.h" | 7 #include "base/timer.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 9 |
| 10 using base::TimeDelta; | 10 using base::TimeDelta; |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 // The message loops on which each timer should be tested. | |
| 15 MessageLoop::Type testing_message_loops[] = { | |
| 16 MessageLoop::TYPE_DEFAULT, | |
| 17 MessageLoop::TYPE_IO, | |
| 18 #if !defined(OS_IOS) // iOS does not allow direct running of the UI loop. | |
| 19 MessageLoop::TYPE_UI, | |
| 20 #endif // !OS_IOS | |
|
stuartmorgan
2012/07/16 15:56:15
No need for the end comment for such a short ifdef
leng
2012/07/16 16:01:28
Done.
| |
| 21 }; | |
| 22 | |
| 23 const int kNumTestingMessageLoops = arraysize(testing_message_loops); | |
| 24 | |
| 14 class OneShotTimerTester { | 25 class OneShotTimerTester { |
| 15 public: | 26 public: |
| 16 OneShotTimerTester(bool* did_run, unsigned milliseconds = 10) | 27 OneShotTimerTester(bool* did_run, unsigned milliseconds = 10) |
| 17 : did_run_(did_run), | 28 : did_run_(did_run), |
| 18 delay_ms_(milliseconds) { | 29 delay_ms_(milliseconds) { |
| 19 } | 30 } |
| 20 void Start() { | 31 void Start() { |
| 21 timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(delay_ms_), this, | 32 timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(delay_ms_), this, |
| 22 &OneShotTimerTester::Run); | 33 &OneShotTimerTester::Run); |
| 23 } | 34 } |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 269 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100)); | 280 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100)); |
| 270 } | 281 } |
| 271 | 282 |
| 272 } // namespace | 283 } // namespace |
| 273 | 284 |
| 274 //----------------------------------------------------------------------------- | 285 //----------------------------------------------------------------------------- |
| 275 // Each test is run against each type of MessageLoop. That way we are sure | 286 // Each test is run against each type of MessageLoop. That way we are sure |
| 276 // that timers work properly in all configurations. | 287 // that timers work properly in all configurations. |
| 277 | 288 |
| 278 TEST(TimerTest, OneShotTimer) { | 289 TEST(TimerTest, OneShotTimer) { |
| 279 RunTest_OneShotTimer(MessageLoop::TYPE_DEFAULT); | 290 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 280 RunTest_OneShotTimer(MessageLoop::TYPE_UI); | 291 RunTest_OneShotTimer(testing_message_loops[i]); |
| 281 RunTest_OneShotTimer(MessageLoop::TYPE_IO); | 292 } |
| 282 } | 293 } |
| 283 | 294 |
| 284 TEST(TimerTest, OneShotTimer_Cancel) { | 295 TEST(TimerTest, OneShotTimer_Cancel) { |
| 285 RunTest_OneShotTimer_Cancel(MessageLoop::TYPE_DEFAULT); | 296 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 286 RunTest_OneShotTimer_Cancel(MessageLoop::TYPE_UI); | 297 RunTest_OneShotTimer_Cancel(testing_message_loops[i]); |
| 287 RunTest_OneShotTimer_Cancel(MessageLoop::TYPE_IO); | 298 } |
| 288 } | 299 } |
| 289 | 300 |
| 290 // If underline timer does not handle properly, we will crash or fail | 301 // If underline timer does not handle properly, we will crash or fail |
| 291 // in full page heap environment. | 302 // in full page heap environment. |
| 292 TEST(TimerTest, OneShotSelfDeletingTimer) { | 303 TEST(TimerTest, OneShotSelfDeletingTimer) { |
| 293 RunTest_OneShotSelfDeletingTimer(MessageLoop::TYPE_DEFAULT); | 304 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 294 RunTest_OneShotSelfDeletingTimer(MessageLoop::TYPE_UI); | 305 RunTest_OneShotSelfDeletingTimer(testing_message_loops[i]); |
| 295 RunTest_OneShotSelfDeletingTimer(MessageLoop::TYPE_IO); | 306 } |
| 296 } | 307 } |
| 297 | 308 |
| 298 TEST(TimerTest, RepeatingTimer) { | 309 TEST(TimerTest, RepeatingTimer) { |
| 299 RunTest_RepeatingTimer(MessageLoop::TYPE_DEFAULT); | 310 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 300 RunTest_RepeatingTimer(MessageLoop::TYPE_UI); | 311 RunTest_RepeatingTimer(testing_message_loops[i]); |
| 301 RunTest_RepeatingTimer(MessageLoop::TYPE_IO); | 312 } |
| 302 } | 313 } |
| 303 | 314 |
| 304 TEST(TimerTest, RepeatingTimer_Cancel) { | 315 TEST(TimerTest, RepeatingTimer_Cancel) { |
| 305 RunTest_RepeatingTimer_Cancel(MessageLoop::TYPE_DEFAULT); | 316 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 306 RunTest_RepeatingTimer_Cancel(MessageLoop::TYPE_UI); | 317 RunTest_RepeatingTimer_Cancel(testing_message_loops[i]); |
| 307 RunTest_RepeatingTimer_Cancel(MessageLoop::TYPE_IO); | 318 } |
| 308 } | 319 } |
| 309 | 320 |
| 310 TEST(TimerTest, DelayTimer_NoCall) { | 321 TEST(TimerTest, DelayTimer_NoCall) { |
| 311 RunTest_DelayTimer_NoCall(MessageLoop::TYPE_DEFAULT); | 322 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 312 RunTest_DelayTimer_NoCall(MessageLoop::TYPE_UI); | 323 RunTest_DelayTimer_NoCall(testing_message_loops[i]); |
| 313 RunTest_DelayTimer_NoCall(MessageLoop::TYPE_IO); | 324 } |
| 314 } | 325 } |
| 315 | 326 |
| 316 TEST(TimerTest, DelayTimer_OneCall) { | 327 TEST(TimerTest, DelayTimer_OneCall) { |
| 317 RunTest_DelayTimer_OneCall(MessageLoop::TYPE_DEFAULT); | 328 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 318 RunTest_DelayTimer_OneCall(MessageLoop::TYPE_UI); | 329 RunTest_DelayTimer_OneCall(testing_message_loops[i]); |
| 319 RunTest_DelayTimer_OneCall(MessageLoop::TYPE_IO); | 330 } |
| 320 } | 331 } |
| 321 | 332 |
| 322 // It's flaky on the buildbot, http://crbug.com/25038. | 333 // It's flaky on the buildbot, http://crbug.com/25038. |
| 323 TEST(TimerTest, DISABLED_DelayTimer_Reset) { | 334 TEST(TimerTest, DISABLED_DelayTimer_Reset) { |
| 324 RunTest_DelayTimer_Reset(MessageLoop::TYPE_DEFAULT); | 335 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 325 RunTest_DelayTimer_Reset(MessageLoop::TYPE_UI); | 336 RunTest_DelayTimer_Reset(testing_message_loops[i]); |
| 326 RunTest_DelayTimer_Reset(MessageLoop::TYPE_IO); | 337 } |
| 327 } | 338 } |
| 328 | 339 |
| 329 TEST(TimerTest, DelayTimer_Deleted) { | 340 TEST(TimerTest, DelayTimer_Deleted) { |
| 330 RunTest_DelayTimer_Deleted(MessageLoop::TYPE_DEFAULT); | 341 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 331 RunTest_DelayTimer_Deleted(MessageLoop::TYPE_UI); | 342 RunTest_DelayTimer_Deleted(testing_message_loops[i]); |
| 332 RunTest_DelayTimer_Deleted(MessageLoop::TYPE_IO); | 343 } |
| 333 } | 344 } |
| 334 | 345 |
| 335 TEST(TimerTest, MessageLoopShutdown) { | 346 TEST(TimerTest, MessageLoopShutdown) { |
| 336 // This test is designed to verify that shutdown of the | 347 // This test is designed to verify that shutdown of the |
| 337 // message loop does not cause crashes if there were pending | 348 // message loop does not cause crashes if there were pending |
| 338 // timers not yet fired. It may only trigger exceptions | 349 // timers not yet fired. It may only trigger exceptions |
| 339 // if debug heap checking is enabled. | 350 // if debug heap checking is enabled. |
| 340 bool did_run = false; | 351 bool did_run = false; |
| 341 { | 352 { |
| 342 OneShotTimerTester a(&did_run); | 353 OneShotTimerTester a(&did_run); |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 468 base::Bind(&SetCallbackHappened1)); | 479 base::Bind(&SetCallbackHappened1)); |
| 469 timer.Reset(); | 480 timer.Reset(); |
| 470 // Since Reset happened before task ran, the user_task must not be cleared: | 481 // Since Reset happened before task ran, the user_task must not be cleared: |
| 471 ASSERT_FALSE(timer.user_task().is_null()); | 482 ASSERT_FALSE(timer.user_task().is_null()); |
| 472 MessageLoop::current()->Run(); | 483 MessageLoop::current()->Run(); |
| 473 EXPECT_TRUE(g_callback_happened1); | 484 EXPECT_TRUE(g_callback_happened1); |
| 474 } | 485 } |
| 475 } | 486 } |
| 476 | 487 |
| 477 } // namespace | 488 } // namespace |
| OLD | NEW |