[email protected] | a67fc9e1 | 2012-02-14 00:29:14 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame^] | 5 | #include "base/timer/timer.h" |
| 6 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame^] | 9 | #include <memory> |
| 10 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 11 | #include "base/macros.h" |
[email protected] | 495cad9 | 2013-07-18 08:12:40 | [diff] [blame] | 12 | #include "base/message_loop/message_loop.h" |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 13 | #include "base/test/test_simple_task_runner.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 14 | #include "build/build_config.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 17 | using base::TimeDelta; |
jsbell | 763cd40 | 2015-12-17 00:38:27 | [diff] [blame] | 18 | using base::SingleThreadTaskRunner; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 19 | |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 20 | namespace { |
| 21 | |
[email protected] | 835332b | 2012-07-17 11:22:59 | [diff] [blame] | 22 | // The message loops on which each timer should be tested. |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 23 | const base::MessageLoop::Type testing_message_loops[] = { |
| 24 | base::MessageLoop::TYPE_DEFAULT, |
| 25 | base::MessageLoop::TYPE_IO, |
[email protected] | 835332b | 2012-07-17 11:22:59 | [diff] [blame] | 26 | #if !defined(OS_IOS) // iOS does not allow direct running of the UI loop. |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 27 | base::MessageLoop::TYPE_UI, |
[email protected] | 835332b | 2012-07-17 11:22:59 | [diff] [blame] | 28 | #endif |
| 29 | }; |
| 30 | |
| 31 | const int kNumTestingMessageLoops = arraysize(testing_message_loops); |
| 32 | |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 33 | class OneShotTimerTester { |
| 34 | public: |
[email protected] | f3c697c5 | 2013-01-15 10:52:11 | [diff] [blame] | 35 | explicit OneShotTimerTester(bool* did_run, unsigned milliseconds = 10) |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 36 | : did_run_(did_run), |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 37 | delay_ms_(milliseconds), |
| 38 | quit_message_loop_(true) { |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 39 | } |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 40 | |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 41 | void Start() { |
[email protected] | d323a17 | 2011-09-02 18:23:02 | [diff] [blame] | 42 | timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(delay_ms_), this, |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 43 | &OneShotTimerTester::Run); |
| 44 | } |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 45 | |
jsbell | 763cd40 | 2015-12-17 00:38:27 | [diff] [blame] | 46 | void SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner) { |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 47 | quit_message_loop_ = false; |
| 48 | timer_.SetTaskRunner(task_runner); |
| 49 | } |
| 50 | |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 51 | private: |
| 52 | void Run() { |
| 53 | *did_run_ = true; |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 54 | if (quit_message_loop_) { |
| 55 | base::MessageLoop::current()->QuitWhenIdle(); |
| 56 | } |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 57 | } |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 58 | |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 59 | bool* did_run_; |
danakj | 8c3eb80 | 2015-09-24 07:53:00 | [diff] [blame] | 60 | base::OneShotTimer timer_; |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 61 | const unsigned delay_ms_; |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 62 | bool quit_message_loop_; |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 63 | }; |
| 64 | |
[email protected] | 9528432 | 2009-02-07 00:37:01 | [diff] [blame] | 65 | class OneShotSelfDeletingTimerTester { |
| 66 | public: |
danakj | 8c3eb80 | 2015-09-24 07:53:00 | [diff] [blame] | 67 | explicit OneShotSelfDeletingTimerTester(bool* did_run) |
| 68 | : did_run_(did_run), timer_(new base::OneShotTimer()) {} |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 69 | |
[email protected] | 9528432 | 2009-02-07 00:37:01 | [diff] [blame] | 70 | void Start() { |
[email protected] | d323a17 | 2011-09-02 18:23:02 | [diff] [blame] | 71 | timer_->Start(FROM_HERE, TimeDelta::FromMilliseconds(10), this, |
[email protected] | 9528432 | 2009-02-07 00:37:01 | [diff] [blame] | 72 | &OneShotSelfDeletingTimerTester::Run); |
| 73 | } |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 74 | |
[email protected] | 9528432 | 2009-02-07 00:37:01 | [diff] [blame] | 75 | private: |
| 76 | void Run() { |
| 77 | *did_run_ = true; |
| 78 | timer_.reset(); |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 79 | base::MessageLoop::current()->QuitWhenIdle(); |
[email protected] | 9528432 | 2009-02-07 00:37:01 | [diff] [blame] | 80 | } |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 81 | |
[email protected] | 9528432 | 2009-02-07 00:37:01 | [diff] [blame] | 82 | bool* did_run_; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame^] | 83 | std::unique_ptr<base::OneShotTimer> timer_; |
[email protected] | 9528432 | 2009-02-07 00:37:01 | [diff] [blame] | 84 | }; |
| 85 | |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 86 | class RepeatingTimerTester { |
| 87 | public: |
[email protected] | e495c53 | 2013-12-06 15:22:35 | [diff] [blame] | 88 | explicit RepeatingTimerTester(bool* did_run, const TimeDelta& delay) |
| 89 | : did_run_(did_run), counter_(10), delay_(delay) { |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 90 | } |
[email protected] | 2fdc86a | 2010-01-26 23:08:02 | [diff] [blame] | 91 | |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 92 | void Start() { |
[email protected] | e495c53 | 2013-12-06 15:22:35 | [diff] [blame] | 93 | timer_.Start(FROM_HERE, delay_, this, &RepeatingTimerTester::Run); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 94 | } |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 95 | |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 96 | private: |
| 97 | void Run() { |
| 98 | if (--counter_ == 0) { |
| 99 | *did_run_ = true; |
[email protected] | e495c53 | 2013-12-06 15:22:35 | [diff] [blame] | 100 | timer_.Stop(); |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 101 | base::MessageLoop::current()->QuitWhenIdle(); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 102 | } |
| 103 | } |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 104 | |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 105 | bool* did_run_; |
| 106 | int counter_; |
[email protected] | e495c53 | 2013-12-06 15:22:35 | [diff] [blame] | 107 | TimeDelta delay_; |
danakj | 8c3eb80 | 2015-09-24 07:53:00 | [diff] [blame] | 108 | base::RepeatingTimer timer_; |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 109 | }; |
| 110 | |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 111 | void RunTest_OneShotTimer(base::MessageLoop::Type message_loop_type) { |
| 112 | base::MessageLoop loop(message_loop_type); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 113 | |
| 114 | bool did_run = false; |
| 115 | OneShotTimerTester f(&did_run); |
| 116 | f.Start(); |
| 117 | |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 118 | base::MessageLoop::current()->Run(); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 119 | |
| 120 | EXPECT_TRUE(did_run); |
| 121 | } |
| 122 | |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 123 | void RunTest_OneShotTimer_Cancel(base::MessageLoop::Type message_loop_type) { |
| 124 | base::MessageLoop loop(message_loop_type); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 125 | |
| 126 | bool did_run_a = false; |
| 127 | OneShotTimerTester* a = new OneShotTimerTester(&did_run_a); |
| 128 | |
| 129 | // This should run before the timer expires. |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 130 | base::MessageLoop::current()->DeleteSoon(FROM_HERE, a); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 131 | |
| 132 | // Now start the timer. |
| 133 | a->Start(); |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 134 | |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 135 | bool did_run_b = false; |
| 136 | OneShotTimerTester b(&did_run_b); |
| 137 | b.Start(); |
| 138 | |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 139 | base::MessageLoop::current()->Run(); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 140 | |
| 141 | EXPECT_FALSE(did_run_a); |
| 142 | EXPECT_TRUE(did_run_b); |
| 143 | } |
| 144 | |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 145 | void RunTest_OneShotSelfDeletingTimer( |
| 146 | base::MessageLoop::Type message_loop_type) { |
| 147 | base::MessageLoop loop(message_loop_type); |
[email protected] | 9528432 | 2009-02-07 00:37:01 | [diff] [blame] | 148 | |
| 149 | bool did_run = false; |
| 150 | OneShotSelfDeletingTimerTester f(&did_run); |
| 151 | f.Start(); |
| 152 | |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 153 | base::MessageLoop::current()->Run(); |
[email protected] | 9528432 | 2009-02-07 00:37:01 | [diff] [blame] | 154 | |
| 155 | EXPECT_TRUE(did_run); |
| 156 | } |
| 157 | |
[email protected] | e495c53 | 2013-12-06 15:22:35 | [diff] [blame] | 158 | void RunTest_RepeatingTimer(base::MessageLoop::Type message_loop_type, |
| 159 | const TimeDelta& delay) { |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 160 | base::MessageLoop loop(message_loop_type); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 161 | |
| 162 | bool did_run = false; |
[email protected] | e495c53 | 2013-12-06 15:22:35 | [diff] [blame] | 163 | RepeatingTimerTester f(&did_run, delay); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 164 | f.Start(); |
| 165 | |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 166 | base::MessageLoop::current()->Run(); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 167 | |
| 168 | EXPECT_TRUE(did_run); |
| 169 | } |
| 170 | |
[email protected] | e495c53 | 2013-12-06 15:22:35 | [diff] [blame] | 171 | void RunTest_RepeatingTimer_Cancel(base::MessageLoop::Type message_loop_type, |
| 172 | const TimeDelta& delay) { |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 173 | base::MessageLoop loop(message_loop_type); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 174 | |
| 175 | bool did_run_a = false; |
[email protected] | e495c53 | 2013-12-06 15:22:35 | [diff] [blame] | 176 | RepeatingTimerTester* a = new RepeatingTimerTester(&did_run_a, delay); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 177 | |
| 178 | // This should run before the timer expires. |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 179 | base::MessageLoop::current()->DeleteSoon(FROM_HERE, a); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 180 | |
| 181 | // Now start the timer. |
| 182 | a->Start(); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 183 | |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 184 | bool did_run_b = false; |
[email protected] | e495c53 | 2013-12-06 15:22:35 | [diff] [blame] | 185 | RepeatingTimerTester b(&did_run_b, delay); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 186 | b.Start(); |
| 187 | |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 188 | base::MessageLoop::current()->Run(); |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 189 | |
| 190 | EXPECT_FALSE(did_run_a); |
| 191 | EXPECT_TRUE(did_run_b); |
| 192 | } |
| 193 | |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 194 | class DelayTimerTarget { |
| 195 | public: |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 196 | bool signaled() const { return signaled_; } |
| 197 | |
| 198 | void Signal() { |
| 199 | ASSERT_FALSE(signaled_); |
| 200 | signaled_ = true; |
| 201 | } |
| 202 | |
| 203 | private: |
hashimoto | 42a14e86 | 2015-01-22 15:21:41 | [diff] [blame] | 204 | bool signaled_ = false; |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 205 | }; |
| 206 | |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 207 | void RunTest_DelayTimer_NoCall(base::MessageLoop::Type message_loop_type) { |
| 208 | base::MessageLoop loop(message_loop_type); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 209 | |
| 210 | // If Delay is never called, the timer shouldn't go off. |
| 211 | DelayTimerTarget target; |
danakj | 8c3eb80 | 2015-09-24 07:53:00 | [diff] [blame] | 212 | base::DelayTimer timer(FROM_HERE, TimeDelta::FromMilliseconds(1), &target, |
| 213 | &DelayTimerTarget::Signal); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 214 | |
| 215 | bool did_run = false; |
| 216 | OneShotTimerTester tester(&did_run); |
| 217 | tester.Start(); |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 218 | base::MessageLoop::current()->Run(); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 219 | |
| 220 | ASSERT_FALSE(target.signaled()); |
| 221 | } |
| 222 | |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 223 | void RunTest_DelayTimer_OneCall(base::MessageLoop::Type message_loop_type) { |
| 224 | base::MessageLoop loop(message_loop_type); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 225 | |
| 226 | DelayTimerTarget target; |
danakj | 8c3eb80 | 2015-09-24 07:53:00 | [diff] [blame] | 227 | base::DelayTimer timer(FROM_HERE, TimeDelta::FromMilliseconds(1), &target, |
| 228 | &DelayTimerTarget::Signal); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 229 | timer.Reset(); |
| 230 | |
| 231 | bool did_run = false; |
| 232 | OneShotTimerTester tester(&did_run, 100 /* milliseconds */); |
| 233 | tester.Start(); |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 234 | base::MessageLoop::current()->Run(); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 235 | |
| 236 | ASSERT_TRUE(target.signaled()); |
| 237 | } |
| 238 | |
| 239 | struct ResetHelper { |
danakj | 8c3eb80 | 2015-09-24 07:53:00 | [diff] [blame] | 240 | ResetHelper(base::DelayTimer* timer, DelayTimerTarget* target) |
| 241 | : timer_(timer), target_(target) {} |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 242 | |
| 243 | void Reset() { |
| 244 | ASSERT_FALSE(target_->signaled()); |
| 245 | timer_->Reset(); |
| 246 | } |
| 247 | |
| 248 | private: |
danakj | 8c3eb80 | 2015-09-24 07:53:00 | [diff] [blame] | 249 | base::DelayTimer* const timer_; |
| 250 | DelayTimerTarget* const target_; |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 251 | }; |
| 252 | |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 253 | void RunTest_DelayTimer_Reset(base::MessageLoop::Type message_loop_type) { |
| 254 | base::MessageLoop loop(message_loop_type); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 255 | |
| 256 | // If Delay is never called, the timer shouldn't go off. |
| 257 | DelayTimerTarget target; |
danakj | 8c3eb80 | 2015-09-24 07:53:00 | [diff] [blame] | 258 | base::DelayTimer timer(FROM_HERE, TimeDelta::FromMilliseconds(50), &target, |
| 259 | &DelayTimerTarget::Signal); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 260 | timer.Reset(); |
| 261 | |
| 262 | ResetHelper reset_helper(&timer, &target); |
| 263 | |
danakj | 8c3eb80 | 2015-09-24 07:53:00 | [diff] [blame] | 264 | base::OneShotTimer timers[20]; |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 265 | for (size_t i = 0; i < arraysize(timers); ++i) { |
[email protected] | d323a17 | 2011-09-02 18:23:02 | [diff] [blame] | 266 | timers[i].Start(FROM_HERE, TimeDelta::FromMilliseconds(i * 10), |
| 267 | &reset_helper, &ResetHelper::Reset); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | bool did_run = false; |
| 271 | OneShotTimerTester tester(&did_run, 300); |
| 272 | tester.Start(); |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 273 | base::MessageLoop::current()->Run(); |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 274 | |
| 275 | ASSERT_TRUE(target.signaled()); |
| 276 | } |
| 277 | |
[email protected] | 02bf7f27 | 2009-02-26 23:17:56 | [diff] [blame] | 278 | class DelayTimerFatalTarget { |
| 279 | public: |
| 280 | void Signal() { |
| 281 | ASSERT_TRUE(false); |
| 282 | } |
| 283 | }; |
| 284 | |
| 285 | |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 286 | void RunTest_DelayTimer_Deleted(base::MessageLoop::Type message_loop_type) { |
| 287 | base::MessageLoop loop(message_loop_type); |
[email protected] | 02bf7f27 | 2009-02-26 23:17:56 | [diff] [blame] | 288 | |
| 289 | DelayTimerFatalTarget target; |
| 290 | |
| 291 | { |
danakj | 8c3eb80 | 2015-09-24 07:53:00 | [diff] [blame] | 292 | base::DelayTimer timer(FROM_HERE, TimeDelta::FromMilliseconds(50), &target, |
| 293 | &DelayTimerFatalTarget::Signal); |
[email protected] | 02bf7f27 | 2009-02-26 23:17:56 | [diff] [blame] | 294 | timer.Reset(); |
| 295 | } |
| 296 | |
| 297 | // When the timer is deleted, the DelayTimerFatalTarget should never be |
| 298 | // called. |
[email protected] | a1b75b94 | 2011-12-31 22:53:51 | [diff] [blame] | 299 | base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100)); |
[email protected] | 02bf7f27 | 2009-02-26 23:17:56 | [diff] [blame] | 300 | } |
| 301 | |
[email protected] | 4d9bdfaf | 2008-08-26 05:53:57 | [diff] [blame] | 302 | } // namespace |
| 303 | |
| 304 | //----------------------------------------------------------------------------- |
| 305 | // Each test is run against each type of MessageLoop. That way we are sure |
| 306 | // that timers work properly in all configurations. |
| 307 | |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 308 | TEST(TimerTest, OneShotTimer) { |
[email protected] | 835332b | 2012-07-17 11:22:59 | [diff] [blame] | 309 | for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 310 | RunTest_OneShotTimer(testing_message_loops[i]); |
| 311 | } |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | TEST(TimerTest, OneShotTimer_Cancel) { |
[email protected] | 835332b | 2012-07-17 11:22:59 | [diff] [blame] | 315 | for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 316 | RunTest_OneShotTimer_Cancel(testing_message_loops[i]); |
| 317 | } |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 318 | } |
| 319 | |
[email protected] | 9528432 | 2009-02-07 00:37:01 | [diff] [blame] | 320 | // If underline timer does not handle properly, we will crash or fail |
[email protected] | 64e95e1 | 2011-08-17 17:41:02 | [diff] [blame] | 321 | // in full page heap environment. |
[email protected] | 9528432 | 2009-02-07 00:37:01 | [diff] [blame] | 322 | TEST(TimerTest, OneShotSelfDeletingTimer) { |
[email protected] | 835332b | 2012-07-17 11:22:59 | [diff] [blame] | 323 | for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 324 | RunTest_OneShotSelfDeletingTimer(testing_message_loops[i]); |
| 325 | } |
[email protected] | 9528432 | 2009-02-07 00:37:01 | [diff] [blame] | 326 | } |
| 327 | |
petrcermak | 7652da6d | 2014-11-06 02:17:57 | [diff] [blame] | 328 | TEST(TimerTest, OneShotTimer_CustomTaskRunner) { |
| 329 | scoped_refptr<base::TestSimpleTaskRunner> task_runner = |
| 330 | new base::TestSimpleTaskRunner(); |
| 331 | |
| 332 | bool did_run = false; |
| 333 | OneShotTimerTester f(&did_run); |
| 334 | f.SetTaskRunner(task_runner); |
| 335 | f.Start(); |
| 336 | |
| 337 | EXPECT_FALSE(did_run); |
| 338 | task_runner->RunUntilIdle(); |
| 339 | EXPECT_TRUE(did_run); |
| 340 | } |
| 341 | |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 342 | TEST(TimerTest, RepeatingTimer) { |
[email protected] | 835332b | 2012-07-17 11:22:59 | [diff] [blame] | 343 | for (int i = 0; i < kNumTestingMessageLoops; i++) { |
[email protected] | e495c53 | 2013-12-06 15:22:35 | [diff] [blame] | 344 | RunTest_RepeatingTimer(testing_message_loops[i], |
| 345 | TimeDelta::FromMilliseconds(10)); |
[email protected] | 835332b | 2012-07-17 11:22:59 | [diff] [blame] | 346 | } |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | TEST(TimerTest, RepeatingTimer_Cancel) { |
[email protected] | 835332b | 2012-07-17 11:22:59 | [diff] [blame] | 350 | for (int i = 0; i < kNumTestingMessageLoops; i++) { |
[email protected] | e495c53 | 2013-12-06 15:22:35 | [diff] [blame] | 351 | RunTest_RepeatingTimer_Cancel(testing_message_loops[i], |
| 352 | TimeDelta::FromMilliseconds(10)); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | TEST(TimerTest, RepeatingTimerZeroDelay) { |
| 357 | for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 358 | RunTest_RepeatingTimer(testing_message_loops[i], |
| 359 | TimeDelta::FromMilliseconds(0)); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | TEST(TimerTest, RepeatingTimerZeroDelay_Cancel) { |
| 364 | for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 365 | RunTest_RepeatingTimer_Cancel(testing_message_loops[i], |
| 366 | TimeDelta::FromMilliseconds(0)); |
[email protected] | 835332b | 2012-07-17 11:22:59 | [diff] [blame] | 367 | } |
[email protected] | aeab57ea | 2008-08-28 20:50:12 | [diff] [blame] | 368 | } |
[email protected] | a0287cbd | 2008-12-02 23:16:55 | [diff] [blame] | 369 | |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 370 | TEST(TimerTest, DelayTimer_NoCall) { |
[email protected] | 835332b | 2012-07-17 11:22:59 | [diff] [blame] | 371 | for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 372 | RunTest_DelayTimer_NoCall(testing_message_loops[i]); |
| 373 | } |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | TEST(TimerTest, DelayTimer_OneCall) { |
[email protected] | 835332b | 2012-07-17 11:22:59 | [diff] [blame] | 377 | for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 378 | RunTest_DelayTimer_OneCall(testing_message_loops[i]); |
| 379 | } |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 380 | } |
| 381 | |
[email protected] | 471d7d6 | 2009-10-16 15:26:16 | [diff] [blame] | 382 | // It's flaky on the buildbot, http://crbug.com/25038. |
[email protected] | a67fc9e1 | 2012-02-14 00:29:14 | [diff] [blame] | 383 | TEST(TimerTest, DISABLED_DelayTimer_Reset) { |
[email protected] | 835332b | 2012-07-17 11:22:59 | [diff] [blame] | 384 | for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 385 | RunTest_DelayTimer_Reset(testing_message_loops[i]); |
| 386 | } |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 387 | } |
| 388 | |
[email protected] | 02bf7f27 | 2009-02-26 23:17:56 | [diff] [blame] | 389 | TEST(TimerTest, DelayTimer_Deleted) { |
[email protected] | 835332b | 2012-07-17 11:22:59 | [diff] [blame] | 390 | for (int i = 0; i < kNumTestingMessageLoops; i++) { |
| 391 | RunTest_DelayTimer_Deleted(testing_message_loops[i]); |
| 392 | } |
[email protected] | 02bf7f27 | 2009-02-26 23:17:56 | [diff] [blame] | 393 | } |
| 394 | |
[email protected] | a0287cbd | 2008-12-02 23:16:55 | [diff] [blame] | 395 | TEST(TimerTest, MessageLoopShutdown) { |
| 396 | // This test is designed to verify that shutdown of the |
| 397 | // message loop does not cause crashes if there were pending |
| 398 | // timers not yet fired. It may only trigger exceptions |
[email protected] | 64e95e1 | 2011-08-17 17:41:02 | [diff] [blame] | 399 | // if debug heap checking is enabled. |
[email protected] | a0287cbd | 2008-12-02 23:16:55 | [diff] [blame] | 400 | bool did_run = false; |
| 401 | { |
| 402 | OneShotTimerTester a(&did_run); |
| 403 | OneShotTimerTester b(&did_run); |
| 404 | OneShotTimerTester c(&did_run); |
| 405 | OneShotTimerTester d(&did_run); |
| 406 | { |
[email protected] | 1ef9001 | 2014-01-15 22:24:33 | [diff] [blame] | 407 | base::MessageLoop loop; |
[email protected] | a0287cbd | 2008-12-02 23:16:55 | [diff] [blame] | 408 | a.Start(); |
| 409 | b.Start(); |
| 410 | } // MessageLoop destructs by falling out of scope. |
| 411 | } // OneShotTimers destruct. SHOULD NOT CRASH, of course. |
| 412 | |
[email protected] | 74e3af7 | 2010-10-03 21:44:39 | [diff] [blame] | 413 | EXPECT_FALSE(did_run); |
[email protected] | a0287cbd | 2008-12-02 23:16:55 | [diff] [blame] | 414 | } |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 415 | |
| 416 | void TimerTestCallback() { |
| 417 | } |
| 418 | |
| 419 | TEST(TimerTest, NonRepeatIsRunning) { |
| 420 | { |
[email protected] | 1ef9001 | 2014-01-15 22:24:33 | [diff] [blame] | 421 | base::MessageLoop loop; |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 422 | base::Timer timer(false, false); |
| 423 | EXPECT_FALSE(timer.IsRunning()); |
| 424 | timer.Start(FROM_HERE, TimeDelta::FromDays(1), |
| 425 | base::Bind(&TimerTestCallback)); |
| 426 | EXPECT_TRUE(timer.IsRunning()); |
| 427 | timer.Stop(); |
| 428 | EXPECT_FALSE(timer.IsRunning()); |
| 429 | EXPECT_TRUE(timer.user_task().is_null()); |
| 430 | } |
| 431 | |
| 432 | { |
| 433 | base::Timer timer(true, false); |
[email protected] | 1ef9001 | 2014-01-15 22:24:33 | [diff] [blame] | 434 | base::MessageLoop loop; |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 435 | EXPECT_FALSE(timer.IsRunning()); |
| 436 | timer.Start(FROM_HERE, TimeDelta::FromDays(1), |
| 437 | base::Bind(&TimerTestCallback)); |
| 438 | EXPECT_TRUE(timer.IsRunning()); |
| 439 | timer.Stop(); |
| 440 | EXPECT_FALSE(timer.IsRunning()); |
| 441 | ASSERT_FALSE(timer.user_task().is_null()); |
| 442 | timer.Reset(); |
| 443 | EXPECT_TRUE(timer.IsRunning()); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | TEST(TimerTest, NonRepeatMessageLoopDeath) { |
| 448 | base::Timer timer(false, false); |
| 449 | { |
[email protected] | 1ef9001 | 2014-01-15 22:24:33 | [diff] [blame] | 450 | base::MessageLoop loop; |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 451 | EXPECT_FALSE(timer.IsRunning()); |
| 452 | timer.Start(FROM_HERE, TimeDelta::FromDays(1), |
| 453 | base::Bind(&TimerTestCallback)); |
| 454 | EXPECT_TRUE(timer.IsRunning()); |
| 455 | } |
| 456 | EXPECT_FALSE(timer.IsRunning()); |
| 457 | EXPECT_TRUE(timer.user_task().is_null()); |
| 458 | } |
| 459 | |
| 460 | TEST(TimerTest, RetainRepeatIsRunning) { |
[email protected] | 1ef9001 | 2014-01-15 22:24:33 | [diff] [blame] | 461 | base::MessageLoop loop; |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 462 | base::Timer timer(FROM_HERE, TimeDelta::FromDays(1), |
| 463 | base::Bind(&TimerTestCallback), true); |
| 464 | EXPECT_FALSE(timer.IsRunning()); |
| 465 | timer.Reset(); |
| 466 | EXPECT_TRUE(timer.IsRunning()); |
| 467 | timer.Stop(); |
| 468 | EXPECT_FALSE(timer.IsRunning()); |
| 469 | timer.Reset(); |
| 470 | EXPECT_TRUE(timer.IsRunning()); |
| 471 | } |
| 472 | |
| 473 | TEST(TimerTest, RetainNonRepeatIsRunning) { |
[email protected] | 1ef9001 | 2014-01-15 22:24:33 | [diff] [blame] | 474 | base::MessageLoop loop; |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 475 | base::Timer timer(FROM_HERE, TimeDelta::FromDays(1), |
| 476 | base::Bind(&TimerTestCallback), false); |
| 477 | EXPECT_FALSE(timer.IsRunning()); |
| 478 | timer.Reset(); |
| 479 | EXPECT_TRUE(timer.IsRunning()); |
| 480 | timer.Stop(); |
| 481 | EXPECT_FALSE(timer.IsRunning()); |
| 482 | timer.Reset(); |
| 483 | EXPECT_TRUE(timer.IsRunning()); |
| 484 | } |
| 485 | |
| 486 | namespace { |
| 487 | |
| 488 | bool g_callback_happened1 = false; |
| 489 | bool g_callback_happened2 = false; |
| 490 | |
| 491 | void ClearAllCallbackHappened() { |
| 492 | g_callback_happened1 = false; |
| 493 | g_callback_happened2 = false; |
| 494 | } |
| 495 | |
| 496 | void SetCallbackHappened1() { |
| 497 | g_callback_happened1 = true; |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 498 | base::MessageLoop::current()->QuitWhenIdle(); |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | void SetCallbackHappened2() { |
| 502 | g_callback_happened2 = true; |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 503 | base::MessageLoop::current()->QuitWhenIdle(); |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | TEST(TimerTest, ContinuationStopStart) { |
| 507 | { |
| 508 | ClearAllCallbackHappened(); |
[email protected] | 1ef9001 | 2014-01-15 22:24:33 | [diff] [blame] | 509 | base::MessageLoop loop; |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 510 | base::Timer timer(false, false); |
| 511 | timer.Start(FROM_HERE, TimeDelta::FromMilliseconds(10), |
| 512 | base::Bind(&SetCallbackHappened1)); |
| 513 | timer.Stop(); |
| 514 | timer.Start(FROM_HERE, TimeDelta::FromMilliseconds(40), |
| 515 | base::Bind(&SetCallbackHappened2)); |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 516 | base::MessageLoop::current()->Run(); |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 517 | EXPECT_FALSE(g_callback_happened1); |
| 518 | EXPECT_TRUE(g_callback_happened2); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | TEST(TimerTest, ContinuationReset) { |
| 523 | { |
| 524 | ClearAllCallbackHappened(); |
[email protected] | 1ef9001 | 2014-01-15 22:24:33 | [diff] [blame] | 525 | base::MessageLoop loop; |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 526 | base::Timer timer(false, false); |
| 527 | timer.Start(FROM_HERE, TimeDelta::FromMilliseconds(10), |
| 528 | base::Bind(&SetCallbackHappened1)); |
| 529 | timer.Reset(); |
| 530 | // Since Reset happened before task ran, the user_task must not be cleared: |
| 531 | ASSERT_FALSE(timer.user_task().is_null()); |
[email protected] | 9e715412 | 2013-05-30 23:11:04 | [diff] [blame] | 532 | base::MessageLoop::current()->Run(); |
[email protected] | df9076a | 2012-03-27 00:18:57 | [diff] [blame] | 533 | EXPECT_TRUE(g_callback_happened1); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | } // namespace |