1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/timer/mock_timer.h"
6
7 namespace base {
8
MockTimer(bool retain_user_task,bool is_repeating)9 MockTimer::MockTimer(bool retain_user_task, bool is_repeating)
10 : Timer(retain_user_task, is_repeating),
11 is_running_(false) {
12 }
13
MockTimer(const tracked_objects::Location & posted_from,TimeDelta delay,const base::Closure & user_task,bool is_repeating)14 MockTimer::MockTimer(const tracked_objects::Location& posted_from,
15 TimeDelta delay,
16 const base::Closure& user_task,
17 bool is_repeating)
18 : Timer(true, is_repeating),
19 delay_(delay),
20 is_running_(false) {
21 }
22
~MockTimer()23 MockTimer::~MockTimer() {
24 }
25
IsRunning() const26 bool MockTimer::IsRunning() const {
27 return is_running_;
28 }
29
GetCurrentDelay() const30 base::TimeDelta MockTimer::GetCurrentDelay() const {
31 return delay_;
32 }
33
Start(const tracked_objects::Location & posted_from,TimeDelta delay,const base::Closure & user_task)34 void MockTimer::Start(const tracked_objects::Location& posted_from,
35 TimeDelta delay,
36 const base::Closure& user_task) {
37 delay_ = delay;
38 user_task_ = user_task;
39 Reset();
40 }
41
Stop()42 void MockTimer::Stop() {
43 is_running_ = false;
44 if (!retain_user_task())
45 user_task_.Reset();
46 }
47
Reset()48 void MockTimer::Reset() {
49 DCHECK(!user_task_.is_null());
50 is_running_ = true;
51 }
52
Fire()53 void MockTimer::Fire() {
54 DCHECK(is_running_);
55 base::Closure old_task = user_task_;
56 if (is_repeating())
57 Reset();
58 else
59 Stop();
60 old_task.Run();
61 }
62
63 } // namespace base
64