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") { ...@@ -941,8 +941,6 @@ jumbo_component("base") {
"timer/elapsed_timer.h", "timer/elapsed_timer.h",
"timer/hi_res_timer_manager.h", "timer/hi_res_timer_manager.h",
"timer/hi_res_timer_manager_win.cc", "timer/hi_res_timer_manager_win.cc",
"timer/mock_timer.cc",
"timer/mock_timer.h",
"timer/timer.cc", "timer/timer.cc",
"timer/timer.h", "timer/timer.h",
"trace_event/auto_open_close_event.cc", "trace_event/auto_open_close_event.cc",
......
...@@ -26,6 +26,8 @@ static_library("test_config") { ...@@ -26,6 +26,8 @@ static_library("test_config") {
static_library("test_support") { static_library("test_support") {
testonly = true testonly = true
sources = [ sources = [
"../timer/mock_timer.cc",
"../timer/mock_timer.h",
"../trace_event/trace_config_memory_test_util.h", "../trace_event/trace_config_memory_test_util.h",
"android/java_handler_thread_helpers.cc", "android/java_handler_thread_helpers.cc",
"android/java_handler_thread_helpers.h", "android/java_handler_thread_helpers.h",
......
...@@ -4,56 +4,32 @@ ...@@ -4,56 +4,32 @@
#include "base/timer/mock_timer.h" #include "base/timer/mock_timer.h"
#include "base/test/test_simple_task_runner.h"
namespace base { namespace base {
MockTimer::MockTimer(bool retain_user_task, bool is_repeating) MockTimer::MockTimer(bool retain_user_task, bool is_repeating)
: Timer(retain_user_task, is_repeating), : Timer(retain_user_task, is_repeating, &clock_),
is_running_(false) { 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; MockTimer::~MockTimer() = default;
bool MockTimer::IsRunning() const { void MockTimer::SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner) {
return is_running_; NOTREACHED() << "MockTimer doesn't support SetTaskRunner().";
}
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::Fire() { void MockTimer::Fire() {
DCHECK(is_running_); DCHECK(IsRunning());
base::Closure old_task = user_task_; clock_.Advance(std::max(TimeDelta(), desired_run_time() - clock_.NowTicks()));
if (is_repeating())
Reset(); // Do not use TestSimpleTaskRunner::RunPendingTasks() here. As RunPendingTasks
else // overrides ThreadTaskRunnerHandle when it runs tasks, tasks posted by timer
Stop(); // tasks to TTRH go to |test_task_runner_|, though they should be posted to
old_task.Run(); // the original task runner.
for (TestPendingTask& task : test_task_runner_->TakePendingTasks())
std::move(task.task).Run();
} }
} // namespace base } // namespace base
...@@ -5,35 +5,32 @@ ...@@ -5,35 +5,32 @@
#ifndef BASE_TIMER_MOCK_TIMER_H_ #ifndef BASE_TIMER_MOCK_TIMER_H_
#define BASE_TIMER_MOCK_TIMER_H_ #define BASE_TIMER_MOCK_TIMER_H_
#include "base/test/simple_test_tick_clock.h"
#include "base/timer/timer.h" #include "base/timer/timer.h"
namespace base { 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: public:
MockTimer(bool retain_user_task, bool is_repeating); 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; ~MockTimer() override;
// base::Timer implementation. // Testing method.
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.
void Fire(); void Fire();
private: private:
base::Closure user_task_; // Timer implementation.
TimeDelta delay_; // MockTimer doesn't support SetTaskRunner. Do not use this.
bool is_running_; void SetTaskRunner(scoped_refptr<SequencedTaskRunner> task_runner) override;
SimpleTestTickClock clock_;
scoped_refptr<TestSimpleTaskRunner> test_task_runner_;
}; };
} // namespace base } // namespace base
......
...@@ -106,17 +106,17 @@ class BASE_EXPORT Timer { ...@@ -106,17 +106,17 @@ class BASE_EXPORT Timer {
virtual ~Timer(); virtual ~Timer();
// Returns true if the timer is running (i.e., not stopped). // 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. // 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 // 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 // only be called before any tasks have been scheduled. If |task_runner| runs
// tasks on a different sequence than the sequence owning this Timer, // 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 // |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). // 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 // 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|. // already running, it will be replaced to call the given |user_task|.
...@@ -164,9 +164,6 @@ class BASE_EXPORT Timer { ...@@ -164,9 +164,6 @@ class BASE_EXPORT Timer {
void set_is_running(bool running) { is_running_ = running; } void set_is_running(bool running) { is_running_ = running; }
const Location& posted_from() const { return posted_from_; } 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: private:
friend class BaseTimerTaskInternal; friend class BaseTimerTaskInternal;
......
...@@ -179,6 +179,7 @@ static_library("test_support") { ...@@ -179,6 +179,7 @@ static_library("test_support") {
deps = [ deps = [
":secure_channel", ":secure_channel",
"//base", "//base",
"//base/test:test_support",
"//chromeos/services/secure_channel/public/cpp/shared", "//chromeos/services/secure_channel/public/cpp/shared",
"//chromeos/services/secure_channel/public/cpp/shared:connection_priority", "//chromeos/services/secure_channel/public/cpp/shared:connection_priority",
"//chromeos/services/secure_channel/public/mojom", "//chromeos/services/secure_channel/public/mojom",
......
...@@ -33,7 +33,7 @@ SimpleAlarmTimer::~SimpleAlarmTimer() { ...@@ -33,7 +33,7 @@ SimpleAlarmTimer::~SimpleAlarmTimer() {
void SimpleAlarmTimer::Stop() { void SimpleAlarmTimer::Stop() {
DCHECK(origin_task_runner_->RunsTasksInCurrentSequence()); DCHECK(origin_task_runner_->RunsTasksInCurrentSequence());
if (!base::Timer::is_running()) if (!IsRunning())
return; return;
if (!CanWakeFromSuspend()) { if (!CanWakeFromSuspend()) {
......
...@@ -12,6 +12,7 @@ source_set("unit_tests") { ...@@ -12,6 +12,7 @@ source_set("unit_tests") {
deps = [ deps = [
"//base", "//base",
"//base/test:test_support",
"//components/visitedlink/browser", "//components/visitedlink/browser",
"//components/visitedlink/common", "//components/visitedlink/common",
"//components/visitedlink/renderer", "//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