| 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 } |
| 34 |
| 31 void Start() { | 35 void Start() { |
| 32 timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(delay_ms_), this, | 36 timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(delay_ms_), this, |
| 33 &OneShotTimerTester::Run); | 37 &OneShotTimerTester::Run); |
| 34 } | 38 } |
| 39 |
| 40 void SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner) { |
| 41 quit_message_loop_ = false; |
| 42 timer_.SetTaskRunner(task_runner); |
| 43 } |
| 44 |
| 35 private: | 45 private: |
| 36 void Run() { | 46 void Run() { |
| 37 *did_run_ = true; | 47 *did_run_ = true; |
| 38 base::MessageLoop::current()->QuitWhenIdle(); | 48 if (quit_message_loop_) { |
| 49 base::MessageLoop::current()->QuitWhenIdle(); |
| 50 } |
| 39 } | 51 } |
| 52 |
| 40 bool* did_run_; | 53 bool* did_run_; |
| 41 base::OneShotTimer<OneShotTimerTester> timer_; | 54 base::OneShotTimer<OneShotTimerTester> timer_; |
| 42 const unsigned delay_ms_; | 55 const unsigned delay_ms_; |
| 56 bool quit_message_loop_; |
| 43 }; | 57 }; |
| 44 | 58 |
| 45 class OneShotSelfDeletingTimerTester { | 59 class OneShotSelfDeletingTimerTester { |
| 46 public: | 60 public: |
| 47 explicit OneShotSelfDeletingTimerTester(bool* did_run) : | 61 explicit OneShotSelfDeletingTimerTester(bool* did_run) : |
| 48 did_run_(did_run), | 62 did_run_(did_run), |
| 49 timer_(new base::OneShotTimer<OneShotSelfDeletingTimerTester>()) { | 63 timer_(new base::OneShotTimer<OneShotSelfDeletingTimerTester>()) { |
| 50 } | 64 } |
| 65 |
| 51 void Start() { | 66 void Start() { |
| 52 timer_->Start(FROM_HERE, TimeDelta::FromMilliseconds(10), this, | 67 timer_->Start(FROM_HERE, TimeDelta::FromMilliseconds(10), this, |
| 53 &OneShotSelfDeletingTimerTester::Run); | 68 &OneShotSelfDeletingTimerTester::Run); |
| 54 } | 69 } |
| 70 |
| 55 private: | 71 private: |
| 56 void Run() { | 72 void Run() { |
| 57 *did_run_ = true; | 73 *did_run_ = true; |
| 58 timer_.reset(); | 74 timer_.reset(); |
| 59 base::MessageLoop::current()->QuitWhenIdle(); | 75 base::MessageLoop::current()->QuitWhenIdle(); |
| 60 } | 76 } |
| 77 |
| 61 bool* did_run_; | 78 bool* did_run_; |
| 62 scoped_ptr<base::OneShotTimer<OneShotSelfDeletingTimerTester> > timer_; | 79 scoped_ptr<base::OneShotTimer<OneShotSelfDeletingTimerTester> > timer_; |
| 63 }; | 80 }; |
| 64 | 81 |
| 65 class RepeatingTimerTester { | 82 class RepeatingTimerTester { |
| 66 public: | 83 public: |
| 67 explicit RepeatingTimerTester(bool* did_run, const TimeDelta& delay) | 84 explicit RepeatingTimerTester(bool* did_run, const TimeDelta& delay) |
| 68 : did_run_(did_run), counter_(10), delay_(delay) { | 85 : did_run_(did_run), counter_(10), delay_(delay) { |
| 69 } | 86 } |
| 70 | 87 |
| 71 void Start() { | 88 void Start() { |
| 72 timer_.Start(FROM_HERE, delay_, this, &RepeatingTimerTester::Run); | 89 timer_.Start(FROM_HERE, delay_, this, &RepeatingTimerTester::Run); |
| 73 } | 90 } |
| 91 |
| 74 private: | 92 private: |
| 75 void Run() { | 93 void Run() { |
| 76 if (--counter_ == 0) { | 94 if (--counter_ == 0) { |
| 77 *did_run_ = true; | 95 *did_run_ = true; |
| 78 timer_.Stop(); | 96 timer_.Stop(); |
| 79 base::MessageLoop::current()->QuitWhenIdle(); | 97 base::MessageLoop::current()->QuitWhenIdle(); |
| 80 } | 98 } |
| 81 } | 99 } |
| 100 |
| 82 bool* did_run_; | 101 bool* did_run_; |
| 83 int counter_; | 102 int counter_; |
| 84 TimeDelta delay_; | 103 TimeDelta delay_; |
| 85 base::RepeatingTimer<RepeatingTimerTester> timer_; | 104 base::RepeatingTimer<RepeatingTimerTester> timer_; |
| 86 }; | 105 }; |
| 87 | 106 |
| 88 void RunTest_OneShotTimer(base::MessageLoop::Type message_loop_type) { | 107 void RunTest_OneShotTimer(base::MessageLoop::Type message_loop_type) { |
| 89 base::MessageLoop loop(message_loop_type); | 108 base::MessageLoop loop(message_loop_type); |
| 90 | 109 |
| 91 bool did_run = false; | 110 bool did_run = false; |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 } | 322 } |
| 304 | 323 |
| 305 // If underline timer does not handle properly, we will crash or fail | 324 // If underline timer does not handle properly, we will crash or fail |
| 306 // in full page heap environment. | 325 // in full page heap environment. |
| 307 TEST(TimerTest, OneShotSelfDeletingTimer) { | 326 TEST(TimerTest, OneShotSelfDeletingTimer) { |
| 308 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 327 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 309 RunTest_OneShotSelfDeletingTimer(testing_message_loops[i]); | 328 RunTest_OneShotSelfDeletingTimer(testing_message_loops[i]); |
| 310 } | 329 } |
| 311 } | 330 } |
| 312 | 331 |
| 332 TEST(TimerTest, OneShotTimer_CustomTaskRunner) { |
| 333 scoped_refptr<base::TestSimpleTaskRunner> task_runner = |
| 334 new base::TestSimpleTaskRunner(); |
| 335 |
| 336 bool did_run = false; |
| 337 OneShotTimerTester f(&did_run); |
| 338 f.SetTaskRunner(task_runner); |
| 339 f.Start(); |
| 340 |
| 341 EXPECT_FALSE(did_run); |
| 342 task_runner->RunUntilIdle(); |
| 343 EXPECT_TRUE(did_run); |
| 344 } |
| 345 |
| 313 TEST(TimerTest, RepeatingTimer) { | 346 TEST(TimerTest, RepeatingTimer) { |
| 314 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 347 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 315 RunTest_RepeatingTimer(testing_message_loops[i], | 348 RunTest_RepeatingTimer(testing_message_loops[i], |
| 316 TimeDelta::FromMilliseconds(10)); | 349 TimeDelta::FromMilliseconds(10)); |
| 317 } | 350 } |
| 318 } | 351 } |
| 319 | 352 |
| 320 TEST(TimerTest, RepeatingTimer_Cancel) { | 353 TEST(TimerTest, RepeatingTimer_Cancel) { |
| 321 for (int i = 0; i < kNumTestingMessageLoops; i++) { | 354 for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 322 RunTest_RepeatingTimer_Cancel(testing_message_loops[i], | 355 RunTest_RepeatingTimer_Cancel(testing_message_loops[i], |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 base::Bind(&SetCallbackHappened1)); | 532 base::Bind(&SetCallbackHappened1)); |
| 500 timer.Reset(); | 533 timer.Reset(); |
| 501 // Since Reset happened before task ran, the user_task must not be cleared: | 534 // Since Reset happened before task ran, the user_task must not be cleared: |
| 502 ASSERT_FALSE(timer.user_task().is_null()); | 535 ASSERT_FALSE(timer.user_task().is_null()); |
| 503 base::MessageLoop::current()->Run(); | 536 base::MessageLoop::current()->Run(); |
| 504 EXPECT_TRUE(g_callback_happened1); | 537 EXPECT_TRUE(g_callback_happened1); |
| 505 } | 538 } |
| 506 } | 539 } |
| 507 | 540 |
| 508 } // namespace | 541 } // namespace |
| OLD | NEW |