blob: ca0893ba0a189a7785709970744637242c14b6c6 [file] [log] [blame]
ellyjones@chromium.orgca61fd92014-03-14 07:04:39 +09001// 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
7namespace base {
8
9MockTimer::MockTimer(bool retain_user_task, bool is_repeating)
10 : Timer(retain_user_task, is_repeating),
11 is_running_(false) {
12}
13
Brett Wilson89388db2017-09-12 14:22:16 +090014MockTimer::MockTimer(const Location& posted_from,
ellyjones@chromium.orgca61fd92014-03-14 07:04:39 +090015 TimeDelta delay,
16 const base::Closure& user_task,
17 bool is_repeating)
Brett Wilson89388db2017-09-12 14:22:16 +090018 : Timer(true, is_repeating), delay_(delay), is_running_(false) {}
ellyjones@chromium.orgca61fd92014-03-14 07:04:39 +090019
Chris Watkinsd155d9f2017-11-29 16:16:38 +090020MockTimer::~MockTimer() = default;
ellyjones@chromium.orgca61fd92014-03-14 07:04:39 +090021
22bool MockTimer::IsRunning() const {
23 return is_running_;
24}
25
26base::TimeDelta MockTimer::GetCurrentDelay() const {
27 return delay_;
28}
29
Brett Wilson89388db2017-09-12 14:22:16 +090030void MockTimer::Start(const Location& posted_from,
ellyjones@chromium.orgca61fd92014-03-14 07:04:39 +090031 TimeDelta delay,
32 const base::Closure& user_task) {
33 delay_ = delay;
34 user_task_ = user_task;
35 Reset();
36}
37
38void MockTimer::Stop() {
39 is_running_ = false;
40 if (!retain_user_task())
41 user_task_.Reset();
42}
43
44void MockTimer::Reset() {
45 DCHECK(!user_task_.is_null());
46 is_running_ = true;
47}
48
49void MockTimer::Fire() {
50 DCHECK(is_running_);
51 base::Closure old_task = user_task_;
52 if (is_repeating())
53 Reset();
54 else
55 Stop();
56 old_task.Run();
57}
58
59} // namespace base