ellyjones@chromium.org | ca61fd9 | 2014-03-14 07:04:39 +0900 | [diff] [blame] | 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 | |
| 9 | MockTimer::MockTimer(bool retain_user_task, bool is_repeating) |
| 10 | : Timer(retain_user_task, is_repeating), |
| 11 | is_running_(false) { |
| 12 | } |
| 13 | |
Brett Wilson | 89388db | 2017-09-12 14:22:16 +0900 | [diff] [blame] | 14 | MockTimer::MockTimer(const Location& posted_from, |
ellyjones@chromium.org | ca61fd9 | 2014-03-14 07:04:39 +0900 | [diff] [blame] | 15 | TimeDelta delay, |
| 16 | const base::Closure& user_task, |
| 17 | bool is_repeating) |
Brett Wilson | 89388db | 2017-09-12 14:22:16 +0900 | [diff] [blame] | 18 | : Timer(true, is_repeating), delay_(delay), is_running_(false) {} |
ellyjones@chromium.org | ca61fd9 | 2014-03-14 07:04:39 +0900 | [diff] [blame] | 19 | |
Chris Watkins | d155d9f | 2017-11-29 16:16:38 +0900 | [diff] [blame^] | 20 | MockTimer::~MockTimer() = default; |
ellyjones@chromium.org | ca61fd9 | 2014-03-14 07:04:39 +0900 | [diff] [blame] | 21 | |
| 22 | bool MockTimer::IsRunning() const { |
| 23 | return is_running_; |
| 24 | } |
| 25 | |
| 26 | base::TimeDelta MockTimer::GetCurrentDelay() const { |
| 27 | return delay_; |
| 28 | } |
| 29 | |
Brett Wilson | 89388db | 2017-09-12 14:22:16 +0900 | [diff] [blame] | 30 | void MockTimer::Start(const Location& posted_from, |
ellyjones@chromium.org | ca61fd9 | 2014-03-14 07:04:39 +0900 | [diff] [blame] | 31 | TimeDelta delay, |
| 32 | const base::Closure& user_task) { |
| 33 | delay_ = delay; |
| 34 | user_task_ = user_task; |
| 35 | Reset(); |
| 36 | } |
| 37 | |
| 38 | void MockTimer::Stop() { |
| 39 | is_running_ = false; |
| 40 | if (!retain_user_task()) |
| 41 | user_task_.Reset(); |
| 42 | } |
| 43 | |
| 44 | void MockTimer::Reset() { |
| 45 | DCHECK(!user_task_.is_null()); |
| 46 | is_running_ = true; |
| 47 | } |
| 48 | |
| 49 | void 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 |