Commit 84db8912 authored by tzik's avatar tzik Committed by Commit Bot

Simplify base::MockTimer

This simplifies base::MockTimer implementation using overridden task
runner. Also, this moves base::MockTimer to //base/test:test_support.

Tbr: khorimoto@chromium.org, chirantan@chromium.org, sky@chromium.org
Bug: 850247
Change-Id: Id233bfdff24fc258d45c5fb3e6ebe292fc973775
Reviewed-on: https://chromium-review.googlesource.com/1094852
Commit-Queue: Taiju Tsuiki <tzik@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarGabriel Charette <gab@chromium.org>
Cr-Commit-Position: refs/heads/master@{#568356}
parent 0032e6cc
......@@ -941,8 +941,6 @@ jumbo_component("base") {
"timer/elapsed_timer.h",
"timer/hi_res_timer_manager.h",
"timer/hi_res_timer_manager_win.cc",
"timer/mock_timer.cc",
"timer/mock_timer.h",
"timer/timer.cc",
"timer/timer.h",
"trace_event/auto_open_close_event.cc",
......
......@@ -26,6 +26,8 @@ static_library("test_config") {
static_library("test_support") {
testonly = true
sources = [
"../timer/mock_timer.cc",
"../timer/mock_timer.h",
"../trace_event/trace_config_memory_test_util.h",
"android/java_handler_thread_helpers.cc",
"android/java_handler_thread_helpers.h",
......
......@@ -4,56 +4,32 @@
#include "base/timer/mock_timer.h"
#include "base/test/test_simple_task_runner.h"
namespace base {
MockTimer::MockTimer(bool retain_user_task, bool is_repeating)
: Timer(retain_user_task, is_repeating),
is_running_(false) {
: Timer(retain_user_task, is_repeating, &clock_),
test_task_runner_(MakeRefCounted<TestSimpleTaskRunner>()) {
Timer::SetTaskRunner(test_task_runner_);
}
MockTimer::MockTimer(const Location& posted_from,
TimeDelta delay,
const base::Closure& user_task,
bool is_repeating)
: Timer(true, is_repeating), delay_(delay), is_running_(false) {}
MockTimer::~MockTimer() = default;
bool MockTimer::IsRunning() const {
return is_running_;
}
base::TimeDelta MockTimer::GetCurrentDelay() const {
return delay_;
}
void MockTimer::Start(const Location& posted_from,
TimeDelta delay,
const base::Closure& user_task) {
delay_ = delay;
user_task_ = user_task;
Reset();
}
void MockTimer::Stop() {
is_running_ = false;
if (!retain_user_task())
user_task_.Reset();
}
void MockTimer::Reset() {
DCHECK(!user_task_.is_null());
is_running_ = true;
void MockTimer::SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner) {
NOTREACHED() << "MockTimer doesn't support SetTaskRunner().";
}
void MockTimer::Fire() {
DCHECK(is_running_);
base::Closure old_task = user_task_;
if (is_repeating())
Reset();
else
Stop();
old_task.Run();
DCHECK(IsRunning());
clock_.Advance(std::max(TimeDelta(), desired_run_time() - clock_.NowTicks()));
// Do not use TestSimpleTaskRunner::RunPendingTasks() here. As RunPendingTasks
// overrides ThreadTaskRunnerHandle when it runs tasks, tasks posted by timer
// tasks to TTRH go to |test_task_runner_|, though they should be posted to
// the original task runner.
for (TestPendingTask& task : test_task_runner_->TakePendingTasks())
std::move(task.task).Run();
}
} // namespace base
......@@ -5,35 +5,32 @@
#ifndef BASE_TIMER_MOCK_TIMER_H_
#define BASE_TIMER_MOCK_TIMER_H_
#include "base/test/simple_test_tick_clock.h"
#include "base/timer/timer.h"
namespace base {
class BASE_EXPORT MockTimer : public Timer {
class TestSimpleTaskRunner;
// A mock implementation of base::Timer which requires being explicitly
// Fire()'d.
// Prefer using ScopedTaskEnvironment::MOCK_TIME + FastForward*() to this when
// possible
class MockTimer : public Timer {
public:
MockTimer(bool retain_user_task, bool is_repeating);
MockTimer(const Location& posted_from,
TimeDelta delay,
const base::Closure& user_task,
bool is_repeating);
~MockTimer() override;
// base::Timer implementation.
bool IsRunning() const override;
base::TimeDelta GetCurrentDelay() const override;
void Start(const Location& posted_from,
base::TimeDelta delay,
const base::Closure& user_task) override;
void Stop() override;
void Reset() override;
// Testing methods.
// Testing method.
void Fire();
private:
base::Closure user_task_;
TimeDelta delay_;
bool is_running_;
// Timer implementation.
// MockTimer doesn't support SetTaskRunner. Do not use this.
void SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner) override;
SimpleTestTickClock clock_;
scoped_refptr<TestSimpleTaskRunner> test_task_runner_;
};
} // namespace base
......
......@@ -106,17 +106,17 @@ class BASE_EXPORT Timer {
virtual ~Timer();
// Returns true if the timer is running (i.e., not stopped).
virtual bool IsRunning() const;
bool IsRunning() const;
// Returns the current delay for this timer.
virtual TimeDelta GetCurrentDelay() const;
TimeDelta GetCurrentDelay() const;
// Set the task runner on which the task should be scheduled. This method can
// only be called before any tasks have been scheduled. If |task_runner| runs
// tasks on a different sequence than the sequence owning this Timer,
// |user_task_| will be posted to it when the Timer fires (note that this
// means |user_task_| can run after ~Timer() and should support that).
void SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner);
virtual void SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner);
// Start the timer to run at the given |delay| from now. If the timer is
// already running, it will be replaced to call the given |user_task|.
......@@ -164,9 +164,6 @@ class BASE_EXPORT Timer {
void set_is_running(bool running) { is_running_ = running; }
const Location& posted_from() const { return posted_from_; }
bool retain_user_task() const { return retain_user_task_; }
bool is_repeating() const { return is_repeating_; }
bool is_running() const { return is_running_; }
private:
friend class BaseTimerTaskInternal;
......
......@@ -179,6 +179,7 @@ static_library("test_support") {
deps = [
":secure_channel",
"//base",
"//base/test:test_support",
"//chromeos/services/secure_channel/public/cpp/shared",
"//chromeos/services/secure_channel/public/cpp/shared:connection_priority",
"//chromeos/services/secure_channel/public/mojom",
......
......@@ -33,7 +33,7 @@ SimpleAlarmTimer::~SimpleAlarmTimer() {
void SimpleAlarmTimer::Stop() {
DCHECK(origin_task_runner_->RunsTasksInCurrentSequence());
if (!base::Timer::is_running())
if (!IsRunning())
return;
if (!CanWakeFromSuspend()) {
......
......@@ -12,6 +12,7 @@ source_set("unit_tests") {
deps = [
"//base",
"//base/test:test_support",
"//components/visitedlink/browser",
"//components/visitedlink/common",
"//components/visitedlink/renderer",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment