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/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
| 7 #include "base/test/test_simple_task_runner.h" | |
| 7 #include "base/timer/timer.h" | 8 #include "base/timer/timer.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 10 |
| 10 using base::TimeDelta; | 11 using base::TimeDelta; |
| 12 using base::SingleThreadTaskRunner; | |
| 11 | 13 |
| 12 namespace { | 14 namespace { |
| 13 | 15 |
| 14 // The message loops on which each timer should be tested. | 16 // The message loops on which each timer should be tested. |
| 15 const base::MessageLoop::Type testing_message_loops[] = { | 17 const base::MessageLoop::Type testing_message_loops[] = { |
| 16 base::MessageLoop::TYPE_DEFAULT, | 18 base::MessageLoop::TYPE_DEFAULT, |
| 17 base::MessageLoop::TYPE_IO, | 19 base::MessageLoop::TYPE_IO, |
| 18 #if !defined(OS_IOS) // iOS does not allow direct running of the UI loop. | 20 #if !defined(OS_IOS) // iOS does not allow direct running of the UI loop. |
| 19 base::MessageLoop::TYPE_UI, | 21 base::MessageLoop::TYPE_UI, |
| 20 #endif | 22 #endif |
| 21 }; | 23 }; |
| 22 | 24 |
| 23 const int kNumTestingMessageLoops = arraysize(testing_message_loops); | 25 const int kNumTestingMessageLoops = arraysize(testing_message_loops); |
| 24 | 26 |
| 25 class OneShotTimerTester { | 27 class OneShotTimerTester { |
| 26 public: | 28 public: |
| 27 explicit OneShotTimerTester(bool* did_run, unsigned milliseconds = 10) | 29 explicit OneShotTimerTester(bool* did_run, unsigned milliseconds = 10) |
| 28 : did_run_(did_run), | 30 : did_run_(did_run), |
| 29 delay_ms_(milliseconds) { | 31 delay_ms_(milliseconds), |
| 32 quit_message_loop_(true) { | |
| 30 } | 33 } |
| 31 void Start() { | 34 void Start() { |
| 32 timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(delay_ms_), this, | 35 timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(delay_ms_), this, |
| 33 &OneShotTimerTester::Run); | 36 &OneShotTimerTester::Run); |
| 34 } | 37 } |
| 38 void SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner) { | |
|
danakj
2014/10/22 15:07:44
nit: should be 1 whitespace above/below the method
petrcermak
2014/10/23 10:59:37
Done (for all methods).
| |
| 39 quit_message_loop_ = false; | |
| 40 timer_.SetTaskRunner(task_runner); | |
| 41 } | |
| 35 private: | 42 private: |
| 36 void Run() { | 43 void Run() { |
| 37 *did_run_ = true; | 44 *did_run_ = true; |
| 38 base::MessageLoop::current()->QuitWhenIdle(); | 45 if (quit_message_loop_) { |
| 46 base::MessageLoop::current()->QuitWhenIdle(); | |
| 47 } | |
| 39 } | 48 } |
| 40 bool* did_run_; | 49 bool* did_run_; |
| 41 base::OneShotTimer<OneShotTimerTester> timer_; | 50 base::OneShotTimer<OneShotTimerTester> timer_; |
| 42 const unsigned delay_ms_; | 51 const unsigned delay_ms_; |
| 52 bool quit_message_loop_; | |
| 43 }; | 53 }; |
| 44 | 54 |
| 45 class OneShotSelfDeletingTimerTester { | 55 class OneShotSelfDeletingTimerTester { |
| 46 public: | 56 public: |
| 47 explicit OneShotSelfDeletingTimerTester(bool* did_run) : | 57 explicit OneShotSelfDeletingTimerTester(bool* did_run) : |
| 48 did_run_(did_run), | 58 did_run_(did_run), |
| 49 timer_(new base::OneShotTimer<OneShotSelfDeletingTimerTester>()) { | 59 timer_(new base::OneShotTimer<OneShotSelfDeletingTimerTester>()) { |
| 50 } | 60 } |
| 51 void Start() { | 61 void Start() { |
| 52 timer_->Start(FROM_HERE, TimeDelta::FromMilliseconds(10), this, | 62 timer_->Start(FROM_HERE, TimeDelta::FromMilliseconds(10), this, |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 303 } | 313 } |
| 304 | 314 |
| 305 // If underline timer does not handle properly, we will crash or fail | 315 // If underline timer does not handle properly, we will crash or fail |
| 306 // in full page heap environment. | 316 // in full page heap environment. |
| 307 TEST(TimerTest, OneShotSelfDeletingTimer) { | 317 TEST(TimerTest, OneShotSelfDeletingTimer) { |
| 308 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 318 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 309 RunTest_OneShotSelfDeletingTimer(testing_message_loops[i]); | 319 RunTest_OneShotSelfDeletingTimer(testing_message_loops[i]); |
| 310 } | 320 } |
| 311 } | 321 } |
| 312 | 322 |
| 323 TEST(TimerTest, OneShotTimer_CustomTaskRunner) { | |
| 324 scoped_refptr<base::TestSimpleTaskRunner> task_runner = | |
| 325 new base::TestSimpleTaskRunner(); | |
| 326 | |
| 327 bool did_run = false; | |
| 328 OneShotTimerTester f(&did_run); | |
| 329 f.SetTaskRunner(task_runner); | |
| 330 f.Start(); | |
| 331 | |
| 332 EXPECT_FALSE(did_run); | |
| 333 | |
|
danakj
2014/10/22 15:07:44
nit: drop whitespace please
petrcermak
2014/10/23 10:59:37
Done.
| |
| 334 task_runner->RunUntilIdle(); | |
| 335 | |
|
danakj
2014/10/22 15:07:44
ditto
petrcermak
2014/10/23 10:59:37
Done.
| |
| 336 EXPECT_TRUE(did_run); | |
| 337 } | |
| 338 | |
| 313 TEST(TimerTest, RepeatingTimer) { | 339 TEST(TimerTest, RepeatingTimer) { |
| 314 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 340 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 315 RunTest_RepeatingTimer(testing_message_loops[i], | 341 RunTest_RepeatingTimer(testing_message_loops[i], |
| 316 TimeDelta::FromMilliseconds(10)); | 342 TimeDelta::FromMilliseconds(10)); |
| 317 } | 343 } |
| 318 } | 344 } |
| 319 | 345 |
| 320 TEST(TimerTest, RepeatingTimer_Cancel) { | 346 TEST(TimerTest, RepeatingTimer_Cancel) { |
| 321 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 347 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 322 RunTest_RepeatingTimer_Cancel(testing_message_loops[i], | 348 RunTest_RepeatingTimer_Cancel(testing_message_loops[i], |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 499 base::Bind(&SetCallbackHappened1)); | 525 base::Bind(&SetCallbackHappened1)); |
| 500 timer.Reset(); | 526 timer.Reset(); |
| 501 // Since Reset happened before task ran, the user_task must not be cleared: | 527 // Since Reset happened before task ran, the user_task must not be cleared: |
| 502 ASSERT_FALSE(timer.user_task().is_null()); | 528 ASSERT_FALSE(timer.user_task().is_null()); |
| 503 base::MessageLoop::current()->Run(); | 529 base::MessageLoop::current()->Run(); |
| 504 EXPECT_TRUE(g_callback_happened1); | 530 EXPECT_TRUE(g_callback_happened1); |
| 505 } | 531 } |
| 506 } | 532 } |
| 507 | 533 |
| 508 } // namespace | 534 } // namespace |
| OLD | NEW |