diff options
author | darin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-28 20:50:12 +0000 |
---|---|---|
committer | darin@google.com <darin@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-28 20:50:12 +0000 |
commit | aeab57ea8560065d6c513fcd46bb43e1bfbfd7a6 (patch) | |
tree | a63f2d36e86361d5c27122a6d6ef4098b755d7d9 /base/timer_unittest.cc | |
parent | e115558691eb08608fad56bb32f40265fdfa4ac5 (diff) | |
download | chromium_src-aeab57ea8560065d6c513fcd46bb43e1bfbfd7a6.zip chromium_src-aeab57ea8560065d6c513fcd46bb43e1bfbfd7a6.tar.gz chromium_src-aeab57ea8560065d6c513fcd46bb43e1bfbfd7a6.tar.bz2 |
Simplify OneShotTimer and RepeatingTimer. Fix up all consumers.
Major changes:
OneShotTimer and RepeatingTimer become template classes that no longer require
a Task or a Timer object. They just use PostDelayedTask. Under the hood that
still uses a Timer object.
The API is much simpler for consumers as they now no longer need to worry about
allocating a Task or managing the lifetime of the object pointer held by the
Task.
I added some new unit tests to timer_unittest.cc to cover the API.
I preserved the old TimerManager / Timer API for now, but I plan to soon kill
it.
R=brettw
BUG=1346553
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1502 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/timer_unittest.cc')
-rw-r--r-- | base/timer_unittest.cc | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/base/timer_unittest.cc b/base/timer_unittest.cc index 1d1a964..9b251b2 100644 --- a/base/timer_unittest.cc +++ b/base/timer_unittest.cc @@ -7,6 +7,8 @@ #include "base/timer.h" #include "testing/gtest/include/gtest/gtest.h" +using base::TimerComparison; + namespace { class TimerTest : public testing::Test {}; @@ -322,6 +324,115 @@ void RunTest_FifoOrder(MessageLoop::Type message_loop_type) { EXPECT_GT(new_id, last_id); } +namespace { + +class OneShotTimerTester { + public: + OneShotTimerTester(bool* did_run) : did_run_(did_run) { + } + void Start() { + timer_.Start(TimeDelta::FromMilliseconds(10), this, + &OneShotTimerTester::Run); + } + private: + void Run() { + *did_run_ = true; + MessageLoop::current()->Quit(); + } + bool* did_run_; + base::OneShotTimer<OneShotTimerTester> timer_; +}; + +class RepeatingTimerTester { + public: + RepeatingTimerTester(bool* did_run) : did_run_(did_run), counter_(10) { + } + void Start() { + timer_.Start(TimeDelta::FromMilliseconds(10), this, + &RepeatingTimerTester::Run); + } + private: + void Run() { + if (--counter_ == 0) { + *did_run_ = true; + MessageLoop::current()->Quit(); + } + } + bool* did_run_; + int counter_; + base::RepeatingTimer<RepeatingTimerTester> timer_; +}; + +} // namespace + +void RunTest_OneShotTimer(MessageLoop::Type message_loop_type) { + MessageLoop loop(message_loop_type); + + bool did_run = false; + OneShotTimerTester f(&did_run); + f.Start(); + + MessageLoop::current()->Run(); + + EXPECT_TRUE(did_run); +} + +void RunTest_OneShotTimer_Cancel(MessageLoop::Type message_loop_type) { + MessageLoop loop(message_loop_type); + + bool did_run_a = false; + OneShotTimerTester* a = new OneShotTimerTester(&did_run_a); + + // This should run before the timer expires. + MessageLoop::current()->DeleteSoon(FROM_HERE, a); + + // Now start the timer. + a->Start(); + + bool did_run_b = false; + OneShotTimerTester b(&did_run_b); + b.Start(); + + MessageLoop::current()->Run(); + + EXPECT_FALSE(did_run_a); + EXPECT_TRUE(did_run_b); +} + +void RunTest_RepeatingTimer(MessageLoop::Type message_loop_type) { + MessageLoop loop(message_loop_type); + + bool did_run = false; + RepeatingTimerTester f(&did_run); + f.Start(); + + MessageLoop::current()->Run(); + + EXPECT_TRUE(did_run); +} + +void RunTest_RepeatingTimer_Cancel(MessageLoop::Type message_loop_type) { + MessageLoop loop(message_loop_type); + + bool did_run_a = false; + RepeatingTimerTester* a = new RepeatingTimerTester(&did_run_a); + + // This should run before the timer expires. + MessageLoop::current()->DeleteSoon(FROM_HERE, a); + + // Now start the timer. + a->Start(); + + bool did_run_b = false; + RepeatingTimerTester b(&did_run_b); + b.Start(); + + MessageLoop::current()->Run(); + + EXPECT_FALSE(did_run_a); + EXPECT_TRUE(did_run_b); +} + } // namespace //----------------------------------------------------------------------------- @@ -363,3 +474,27 @@ TEST(TimerTest, FifoOrder) { RunTest_FifoOrder(MessageLoop::TYPE_UI); RunTest_FifoOrder(MessageLoop::TYPE_IO); } + +TEST(TimerTest, OneShotTimer) { + RunTest_OneShotTimer(MessageLoop::TYPE_DEFAULT); + RunTest_OneShotTimer(MessageLoop::TYPE_UI); + RunTest_OneShotTimer(MessageLoop::TYPE_IO); +} + +TEST(TimerTest, OneShotTimer_Cancel) { + RunTest_OneShotTimer_Cancel(MessageLoop::TYPE_DEFAULT); + RunTest_OneShotTimer_Cancel(MessageLoop::TYPE_UI); + RunTest_OneShotTimer_Cancel(MessageLoop::TYPE_IO); +} + +TEST(TimerTest, RepeatingTimer) { + RunTest_RepeatingTimer(MessageLoop::TYPE_DEFAULT); + RunTest_RepeatingTimer(MessageLoop::TYPE_UI); + RunTest_RepeatingTimer(MessageLoop::TYPE_IO); +} + +TEST(TimerTest, RepeatingTimer_Cancel) { + RunTest_RepeatingTimer_Cancel(MessageLoop::TYPE_DEFAULT); + RunTest_RepeatingTimer_Cancel(MessageLoop::TYPE_UI); + RunTest_RepeatingTimer_Cancel(MessageLoop::TYPE_IO); +} |