blob: e56efac6e39e615a72353844a05a4026a9ede0ab [file] [log] [blame]
[email protected]a67fc9e12012-02-14 00:29:141// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
dcheng093de9b2016-04-04 21:25:515#include "base/timer/timer.h"
6
avi9b6f42932015-12-26 22:15:147#include <stddef.h>
8
dcheng093de9b2016-04-04 21:25:519#include <memory>
10
avi9b6f42932015-12-26 22:15:1411#include "base/macros.h"
[email protected]495cad92013-07-18 08:12:4012#include "base/message_loop/message_loop.h"
petrcermak7652da6d2014-11-06 02:17:5713#include "base/test/test_simple_task_runner.h"
avi9b6f42932015-12-26 22:15:1414#include "build/build_config.h"
initial.commitd7cae122008-07-26 21:49:3815#include "testing/gtest/include/gtest/gtest.h"
16
[email protected]e1acf6f2008-10-27 20:43:3317using base::TimeDelta;
jsbell763cd402015-12-17 00:38:2718using base::SingleThreadTaskRunner;
[email protected]e1acf6f2008-10-27 20:43:3319
[email protected]aeab57ea2008-08-28 20:50:1220namespace {
21
[email protected]835332b2012-07-17 11:22:5922// The message loops on which each timer should be tested.
[email protected]9e7154122013-05-30 23:11:0423const base::MessageLoop::Type testing_message_loops[] = {
24 base::MessageLoop::TYPE_DEFAULT,
25 base::MessageLoop::TYPE_IO,
[email protected]835332b2012-07-17 11:22:5926#if !defined(OS_IOS) // iOS does not allow direct running of the UI loop.
[email protected]9e7154122013-05-30 23:11:0427 base::MessageLoop::TYPE_UI,
[email protected]835332b2012-07-17 11:22:5928#endif
29};
30
31const int kNumTestingMessageLoops = arraysize(testing_message_loops);
32
[email protected]aeab57ea2008-08-28 20:50:1233class OneShotTimerTester {
34 public:
[email protected]f3c697c52013-01-15 10:52:1135 explicit OneShotTimerTester(bool* did_run, unsigned milliseconds = 10)
[email protected]e68e62fa2009-02-20 02:00:0436 : did_run_(did_run),
petrcermak7652da6d2014-11-06 02:17:5737 delay_ms_(milliseconds),
38 quit_message_loop_(true) {
[email protected]aeab57ea2008-08-28 20:50:1239 }
petrcermak7652da6d2014-11-06 02:17:5740
[email protected]aeab57ea2008-08-28 20:50:1241 void Start() {
[email protected]d323a172011-09-02 18:23:0242 timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(delay_ms_), this,
[email protected]aeab57ea2008-08-28 20:50:1243 &OneShotTimerTester::Run);
44 }
petrcermak7652da6d2014-11-06 02:17:5745
jsbell763cd402015-12-17 00:38:2746 void SetTaskRunner(scoped_refptr<SingleThreadTaskRunner> task_runner) {
petrcermak7652da6d2014-11-06 02:17:5747 quit_message_loop_ = false;
48 timer_.SetTaskRunner(task_runner);
49 }
50
[email protected]aeab57ea2008-08-28 20:50:1251 private:
52 void Run() {
53 *did_run_ = true;
petrcermak7652da6d2014-11-06 02:17:5754 if (quit_message_loop_) {
55 base::MessageLoop::current()->QuitWhenIdle();
56 }
[email protected]aeab57ea2008-08-28 20:50:1257 }
petrcermak7652da6d2014-11-06 02:17:5758
[email protected]aeab57ea2008-08-28 20:50:1259 bool* did_run_;
danakj8c3eb802015-09-24 07:53:0060 base::OneShotTimer timer_;
[email protected]e68e62fa2009-02-20 02:00:0461 const unsigned delay_ms_;
petrcermak7652da6d2014-11-06 02:17:5762 bool quit_message_loop_;
[email protected]aeab57ea2008-08-28 20:50:1263};
64
[email protected]95284322009-02-07 00:37:0165class OneShotSelfDeletingTimerTester {
66 public:
danakj8c3eb802015-09-24 07:53:0067 explicit OneShotSelfDeletingTimerTester(bool* did_run)
68 : did_run_(did_run), timer_(new base::OneShotTimer()) {}
petrcermak7652da6d2014-11-06 02:17:5769
[email protected]95284322009-02-07 00:37:0170 void Start() {
[email protected]d323a172011-09-02 18:23:0271 timer_->Start(FROM_HERE, TimeDelta::FromMilliseconds(10), this,
[email protected]95284322009-02-07 00:37:0172 &OneShotSelfDeletingTimerTester::Run);
73 }
petrcermak7652da6d2014-11-06 02:17:5774
[email protected]95284322009-02-07 00:37:0175 private:
76 void Run() {
77 *did_run_ = true;
78 timer_.reset();
[email protected]9e7154122013-05-30 23:11:0479 base::MessageLoop::current()->QuitWhenIdle();
[email protected]95284322009-02-07 00:37:0180 }
petrcermak7652da6d2014-11-06 02:17:5781
[email protected]95284322009-02-07 00:37:0182 bool* did_run_;
dcheng093de9b2016-04-04 21:25:5183 std::unique_ptr<base::OneShotTimer> timer_;
[email protected]95284322009-02-07 00:37:0184};
85
[email protected]aeab57ea2008-08-28 20:50:1286class RepeatingTimerTester {
87 public:
[email protected]e495c532013-12-06 15:22:3588 explicit RepeatingTimerTester(bool* did_run, const TimeDelta& delay)
89 : did_run_(did_run), counter_(10), delay_(delay) {
[email protected]aeab57ea2008-08-28 20:50:1290 }
[email protected]2fdc86a2010-01-26 23:08:0291
[email protected]aeab57ea2008-08-28 20:50:1292 void Start() {
[email protected]e495c532013-12-06 15:22:3593 timer_.Start(FROM_HERE, delay_, this, &RepeatingTimerTester::Run);
[email protected]aeab57ea2008-08-28 20:50:1294 }
petrcermak7652da6d2014-11-06 02:17:5795
[email protected]aeab57ea2008-08-28 20:50:1296 private:
97 void Run() {
98 if (--counter_ == 0) {
99 *did_run_ = true;
[email protected]e495c532013-12-06 15:22:35100 timer_.Stop();
[email protected]9e7154122013-05-30 23:11:04101 base::MessageLoop::current()->QuitWhenIdle();
[email protected]aeab57ea2008-08-28 20:50:12102 }
103 }
petrcermak7652da6d2014-11-06 02:17:57104
[email protected]aeab57ea2008-08-28 20:50:12105 bool* did_run_;
106 int counter_;
[email protected]e495c532013-12-06 15:22:35107 TimeDelta delay_;
danakj8c3eb802015-09-24 07:53:00108 base::RepeatingTimer timer_;
[email protected]aeab57ea2008-08-28 20:50:12109};
110
[email protected]9e7154122013-05-30 23:11:04111void RunTest_OneShotTimer(base::MessageLoop::Type message_loop_type) {
112 base::MessageLoop loop(message_loop_type);
[email protected]aeab57ea2008-08-28 20:50:12113
114 bool did_run = false;
115 OneShotTimerTester f(&did_run);
116 f.Start();
117
[email protected]9e7154122013-05-30 23:11:04118 base::MessageLoop::current()->Run();
[email protected]aeab57ea2008-08-28 20:50:12119
120 EXPECT_TRUE(did_run);
121}
122
[email protected]9e7154122013-05-30 23:11:04123void RunTest_OneShotTimer_Cancel(base::MessageLoop::Type message_loop_type) {
124 base::MessageLoop loop(message_loop_type);
[email protected]aeab57ea2008-08-28 20:50:12125
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]9e7154122013-05-30 23:11:04130 base::MessageLoop::current()->DeleteSoon(FROM_HERE, a);
[email protected]aeab57ea2008-08-28 20:50:12131
132 // Now start the timer.
133 a->Start();
[email protected]52a261f2009-03-03 15:01:12134
[email protected]aeab57ea2008-08-28 20:50:12135 bool did_run_b = false;
136 OneShotTimerTester b(&did_run_b);
137 b.Start();
138
[email protected]9e7154122013-05-30 23:11:04139 base::MessageLoop::current()->Run();
[email protected]aeab57ea2008-08-28 20:50:12140
141 EXPECT_FALSE(did_run_a);
142 EXPECT_TRUE(did_run_b);
143}
144
[email protected]9e7154122013-05-30 23:11:04145void RunTest_OneShotSelfDeletingTimer(
146 base::MessageLoop::Type message_loop_type) {
147 base::MessageLoop loop(message_loop_type);
[email protected]95284322009-02-07 00:37:01148
149 bool did_run = false;
150 OneShotSelfDeletingTimerTester f(&did_run);
151 f.Start();
152
[email protected]9e7154122013-05-30 23:11:04153 base::MessageLoop::current()->Run();
[email protected]95284322009-02-07 00:37:01154
155 EXPECT_TRUE(did_run);
156}
157
[email protected]e495c532013-12-06 15:22:35158void RunTest_RepeatingTimer(base::MessageLoop::Type message_loop_type,
159 const TimeDelta& delay) {
[email protected]9e7154122013-05-30 23:11:04160 base::MessageLoop loop(message_loop_type);
[email protected]aeab57ea2008-08-28 20:50:12161
162 bool did_run = false;
[email protected]e495c532013-12-06 15:22:35163 RepeatingTimerTester f(&did_run, delay);
[email protected]aeab57ea2008-08-28 20:50:12164 f.Start();
165
[email protected]9e7154122013-05-30 23:11:04166 base::MessageLoop::current()->Run();
[email protected]aeab57ea2008-08-28 20:50:12167
168 EXPECT_TRUE(did_run);
169}
170
[email protected]e495c532013-12-06 15:22:35171void RunTest_RepeatingTimer_Cancel(base::MessageLoop::Type message_loop_type,
172 const TimeDelta& delay) {
[email protected]9e7154122013-05-30 23:11:04173 base::MessageLoop loop(message_loop_type);
[email protected]aeab57ea2008-08-28 20:50:12174
175 bool did_run_a = false;
[email protected]e495c532013-12-06 15:22:35176 RepeatingTimerTester* a = new RepeatingTimerTester(&did_run_a, delay);
[email protected]aeab57ea2008-08-28 20:50:12177
178 // This should run before the timer expires.
[email protected]9e7154122013-05-30 23:11:04179 base::MessageLoop::current()->DeleteSoon(FROM_HERE, a);
[email protected]aeab57ea2008-08-28 20:50:12180
181 // Now start the timer.
182 a->Start();
[email protected]e68e62fa2009-02-20 02:00:04183
[email protected]aeab57ea2008-08-28 20:50:12184 bool did_run_b = false;
[email protected]e495c532013-12-06 15:22:35185 RepeatingTimerTester b(&did_run_b, delay);
[email protected]aeab57ea2008-08-28 20:50:12186 b.Start();
187
[email protected]9e7154122013-05-30 23:11:04188 base::MessageLoop::current()->Run();
[email protected]aeab57ea2008-08-28 20:50:12189
190 EXPECT_FALSE(did_run_a);
191 EXPECT_TRUE(did_run_b);
192}
193
[email protected]e68e62fa2009-02-20 02:00:04194class DelayTimerTarget {
195 public:
[email protected]e68e62fa2009-02-20 02:00:04196 bool signaled() const { return signaled_; }
197
198 void Signal() {
199 ASSERT_FALSE(signaled_);
200 signaled_ = true;
201 }
202
203 private:
hashimoto42a14e862015-01-22 15:21:41204 bool signaled_ = false;
[email protected]e68e62fa2009-02-20 02:00:04205};
206
[email protected]9e7154122013-05-30 23:11:04207void RunTest_DelayTimer_NoCall(base::MessageLoop::Type message_loop_type) {
208 base::MessageLoop loop(message_loop_type);
[email protected]e68e62fa2009-02-20 02:00:04209
210 // If Delay is never called, the timer shouldn't go off.
211 DelayTimerTarget target;
danakj8c3eb802015-09-24 07:53:00212 base::DelayTimer timer(FROM_HERE, TimeDelta::FromMilliseconds(1), &target,
213 &DelayTimerTarget::Signal);
[email protected]e68e62fa2009-02-20 02:00:04214
215 bool did_run = false;
216 OneShotTimerTester tester(&did_run);
217 tester.Start();
[email protected]9e7154122013-05-30 23:11:04218 base::MessageLoop::current()->Run();
[email protected]e68e62fa2009-02-20 02:00:04219
220 ASSERT_FALSE(target.signaled());
221}
222
[email protected]9e7154122013-05-30 23:11:04223void RunTest_DelayTimer_OneCall(base::MessageLoop::Type message_loop_type) {
224 base::MessageLoop loop(message_loop_type);
[email protected]e68e62fa2009-02-20 02:00:04225
226 DelayTimerTarget target;
danakj8c3eb802015-09-24 07:53:00227 base::DelayTimer timer(FROM_HERE, TimeDelta::FromMilliseconds(1), &target,
228 &DelayTimerTarget::Signal);
[email protected]e68e62fa2009-02-20 02:00:04229 timer.Reset();
230
231 bool did_run = false;
232 OneShotTimerTester tester(&did_run, 100 /* milliseconds */);
233 tester.Start();
[email protected]9e7154122013-05-30 23:11:04234 base::MessageLoop::current()->Run();
[email protected]e68e62fa2009-02-20 02:00:04235
236 ASSERT_TRUE(target.signaled());
237}
238
239struct ResetHelper {
danakj8c3eb802015-09-24 07:53:00240 ResetHelper(base::DelayTimer* timer, DelayTimerTarget* target)
241 : timer_(timer), target_(target) {}
[email protected]e68e62fa2009-02-20 02:00:04242
243 void Reset() {
244 ASSERT_FALSE(target_->signaled());
245 timer_->Reset();
246 }
247
248 private:
danakj8c3eb802015-09-24 07:53:00249 base::DelayTimer* const timer_;
250 DelayTimerTarget* const target_;
[email protected]e68e62fa2009-02-20 02:00:04251};
252
[email protected]9e7154122013-05-30 23:11:04253void RunTest_DelayTimer_Reset(base::MessageLoop::Type message_loop_type) {
254 base::MessageLoop loop(message_loop_type);
[email protected]e68e62fa2009-02-20 02:00:04255
256 // If Delay is never called, the timer shouldn't go off.
257 DelayTimerTarget target;
danakj8c3eb802015-09-24 07:53:00258 base::DelayTimer timer(FROM_HERE, TimeDelta::FromMilliseconds(50), &target,
259 &DelayTimerTarget::Signal);
[email protected]e68e62fa2009-02-20 02:00:04260 timer.Reset();
261
262 ResetHelper reset_helper(&timer, &target);
263
danakj8c3eb802015-09-24 07:53:00264 base::OneShotTimer timers[20];
[email protected]e68e62fa2009-02-20 02:00:04265 for (size_t i = 0; i < arraysize(timers); ++i) {
[email protected]d323a172011-09-02 18:23:02266 timers[i].Start(FROM_HERE, TimeDelta::FromMilliseconds(i * 10),
267 &reset_helper, &ResetHelper::Reset);
[email protected]e68e62fa2009-02-20 02:00:04268 }
269
270 bool did_run = false;
271 OneShotTimerTester tester(&did_run, 300);
272 tester.Start();
[email protected]9e7154122013-05-30 23:11:04273 base::MessageLoop::current()->Run();
[email protected]e68e62fa2009-02-20 02:00:04274
275 ASSERT_TRUE(target.signaled());
276}
277
[email protected]02bf7f272009-02-26 23:17:56278class DelayTimerFatalTarget {
279 public:
280 void Signal() {
281 ASSERT_TRUE(false);
282 }
283};
284
285
[email protected]9e7154122013-05-30 23:11:04286void RunTest_DelayTimer_Deleted(base::MessageLoop::Type message_loop_type) {
287 base::MessageLoop loop(message_loop_type);
[email protected]02bf7f272009-02-26 23:17:56288
289 DelayTimerFatalTarget target;
290
291 {
danakj8c3eb802015-09-24 07:53:00292 base::DelayTimer timer(FROM_HERE, TimeDelta::FromMilliseconds(50), &target,
293 &DelayTimerFatalTarget::Signal);
[email protected]02bf7f272009-02-26 23:17:56294 timer.Reset();
295 }
296
297 // When the timer is deleted, the DelayTimerFatalTarget should never be
298 // called.
[email protected]a1b75b942011-12-31 22:53:51299 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
[email protected]02bf7f272009-02-26 23:17:56300}
301
[email protected]4d9bdfaf2008-08-26 05:53:57302} // 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]aeab57ea2008-08-28 20:50:12308TEST(TimerTest, OneShotTimer) {
[email protected]835332b2012-07-17 11:22:59309 for (int i = 0; i < kNumTestingMessageLoops; i++) {
310 RunTest_OneShotTimer(testing_message_loops[i]);
311 }
[email protected]aeab57ea2008-08-28 20:50:12312}
313
314TEST(TimerTest, OneShotTimer_Cancel) {
[email protected]835332b2012-07-17 11:22:59315 for (int i = 0; i < kNumTestingMessageLoops; i++) {
316 RunTest_OneShotTimer_Cancel(testing_message_loops[i]);
317 }
[email protected]aeab57ea2008-08-28 20:50:12318}
319
[email protected]95284322009-02-07 00:37:01320// If underline timer does not handle properly, we will crash or fail
[email protected]64e95e12011-08-17 17:41:02321// in full page heap environment.
[email protected]95284322009-02-07 00:37:01322TEST(TimerTest, OneShotSelfDeletingTimer) {
[email protected]835332b2012-07-17 11:22:59323 for (int i = 0; i < kNumTestingMessageLoops; i++) {
324 RunTest_OneShotSelfDeletingTimer(testing_message_loops[i]);
325 }
[email protected]95284322009-02-07 00:37:01326}
327
petrcermak7652da6d2014-11-06 02:17:57328TEST(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]aeab57ea2008-08-28 20:50:12342TEST(TimerTest, RepeatingTimer) {
[email protected]835332b2012-07-17 11:22:59343 for (int i = 0; i < kNumTestingMessageLoops; i++) {
[email protected]e495c532013-12-06 15:22:35344 RunTest_RepeatingTimer(testing_message_loops[i],
345 TimeDelta::FromMilliseconds(10));
[email protected]835332b2012-07-17 11:22:59346 }
[email protected]aeab57ea2008-08-28 20:50:12347}
348
349TEST(TimerTest, RepeatingTimer_Cancel) {
[email protected]835332b2012-07-17 11:22:59350 for (int i = 0; i < kNumTestingMessageLoops; i++) {
[email protected]e495c532013-12-06 15:22:35351 RunTest_RepeatingTimer_Cancel(testing_message_loops[i],
352 TimeDelta::FromMilliseconds(10));
353 }
354}
355
356TEST(TimerTest, RepeatingTimerZeroDelay) {
357 for (int i = 0; i < kNumTestingMessageLoops; i++) {
358 RunTest_RepeatingTimer(testing_message_loops[i],
359 TimeDelta::FromMilliseconds(0));
360 }
361}
362
363TEST(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]835332b2012-07-17 11:22:59367 }
[email protected]aeab57ea2008-08-28 20:50:12368}
[email protected]a0287cbd2008-12-02 23:16:55369
[email protected]e68e62fa2009-02-20 02:00:04370TEST(TimerTest, DelayTimer_NoCall) {
[email protected]835332b2012-07-17 11:22:59371 for (int i = 0; i < kNumTestingMessageLoops; i++) {
372 RunTest_DelayTimer_NoCall(testing_message_loops[i]);
373 }
[email protected]e68e62fa2009-02-20 02:00:04374}
375
376TEST(TimerTest, DelayTimer_OneCall) {
[email protected]835332b2012-07-17 11:22:59377 for (int i = 0; i < kNumTestingMessageLoops; i++) {
378 RunTest_DelayTimer_OneCall(testing_message_loops[i]);
379 }
[email protected]e68e62fa2009-02-20 02:00:04380}
381
[email protected]471d7d62009-10-16 15:26:16382// It's flaky on the buildbot, http://crbug.com/25038.
[email protected]a67fc9e12012-02-14 00:29:14383TEST(TimerTest, DISABLED_DelayTimer_Reset) {
[email protected]835332b2012-07-17 11:22:59384 for (int i = 0; i < kNumTestingMessageLoops; i++) {
385 RunTest_DelayTimer_Reset(testing_message_loops[i]);
386 }
[email protected]e68e62fa2009-02-20 02:00:04387}
388
[email protected]02bf7f272009-02-26 23:17:56389TEST(TimerTest, DelayTimer_Deleted) {
[email protected]835332b2012-07-17 11:22:59390 for (int i = 0; i < kNumTestingMessageLoops; i++) {
391 RunTest_DelayTimer_Deleted(testing_message_loops[i]);
392 }
[email protected]02bf7f272009-02-26 23:17:56393}
394
[email protected]a0287cbd2008-12-02 23:16:55395TEST(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]64e95e12011-08-17 17:41:02399 // if debug heap checking is enabled.
[email protected]a0287cbd2008-12-02 23:16:55400 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]1ef90012014-01-15 22:24:33407 base::MessageLoop loop;
[email protected]a0287cbd2008-12-02 23:16:55408 a.Start();
409 b.Start();
410 } // MessageLoop destructs by falling out of scope.
411 } // OneShotTimers destruct. SHOULD NOT CRASH, of course.
412
[email protected]74e3af72010-10-03 21:44:39413 EXPECT_FALSE(did_run);
[email protected]a0287cbd2008-12-02 23:16:55414}
[email protected]df9076a2012-03-27 00:18:57415
416void TimerTestCallback() {
417}
418
419TEST(TimerTest, NonRepeatIsRunning) {
420 {
[email protected]1ef90012014-01-15 22:24:33421 base::MessageLoop loop;
[email protected]df9076a2012-03-27 00:18:57422 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]1ef90012014-01-15 22:24:33434 base::MessageLoop loop;
[email protected]df9076a2012-03-27 00:18:57435 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
447TEST(TimerTest, NonRepeatMessageLoopDeath) {
448 base::Timer timer(false, false);
449 {
[email protected]1ef90012014-01-15 22:24:33450 base::MessageLoop loop;
[email protected]df9076a2012-03-27 00:18:57451 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
460TEST(TimerTest, RetainRepeatIsRunning) {
[email protected]1ef90012014-01-15 22:24:33461 base::MessageLoop loop;
[email protected]df9076a2012-03-27 00:18:57462 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
473TEST(TimerTest, RetainNonRepeatIsRunning) {
[email protected]1ef90012014-01-15 22:24:33474 base::MessageLoop loop;
[email protected]df9076a2012-03-27 00:18:57475 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
486namespace {
487
488bool g_callback_happened1 = false;
489bool g_callback_happened2 = false;
490
491void ClearAllCallbackHappened() {
492 g_callback_happened1 = false;
493 g_callback_happened2 = false;
494}
495
496void SetCallbackHappened1() {
497 g_callback_happened1 = true;
[email protected]9e7154122013-05-30 23:11:04498 base::MessageLoop::current()->QuitWhenIdle();
[email protected]df9076a2012-03-27 00:18:57499}
500
501void SetCallbackHappened2() {
502 g_callback_happened2 = true;
[email protected]9e7154122013-05-30 23:11:04503 base::MessageLoop::current()->QuitWhenIdle();
[email protected]df9076a2012-03-27 00:18:57504}
505
506TEST(TimerTest, ContinuationStopStart) {
507 {
508 ClearAllCallbackHappened();
[email protected]1ef90012014-01-15 22:24:33509 base::MessageLoop loop;
[email protected]df9076a2012-03-27 00:18:57510 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]9e7154122013-05-30 23:11:04516 base::MessageLoop::current()->Run();
[email protected]df9076a2012-03-27 00:18:57517 EXPECT_FALSE(g_callback_happened1);
518 EXPECT_TRUE(g_callback_happened2);
519 }
520}
521
522TEST(TimerTest, ContinuationReset) {
523 {
524 ClearAllCallbackHappened();
[email protected]1ef90012014-01-15 22:24:33525 base::MessageLoop loop;
[email protected]df9076a2012-03-27 00:18:57526 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]9e7154122013-05-30 23:11:04532 base::MessageLoop::current()->Run();
[email protected]df9076a2012-03-27 00:18:57533 EXPECT_TRUE(g_callback_happened1);
534 }
535}
536
537} // namespace