diff --git a/shell/platform/windows/task_runner.cc b/shell/platform/windows/task_runner.cc index fa8b1348c9edd..464afbc43827e 100644 --- a/shell/platform/windows/task_runner.cc +++ b/shell/platform/windows/task_runner.cc @@ -15,7 +15,7 @@ TaskRunner::TaskRunner(CurrentTimeProc get_current_time, on_task_expired_(std::move(on_task_expired)) {} std::chrono::nanoseconds TaskRunner::ProcessTasks() { - const TaskTimePoint now = TaskTimePoint::clock::now(); + const TaskTimePoint now = GetCurrentTimeForTask(); std::vector expired_tasks; @@ -64,7 +64,7 @@ std::chrono::nanoseconds TaskRunner::ProcessTasks() { TaskRunner::TaskTimePoint TaskRunner::TimePointFromFlutterTime( uint64_t flutter_target_time_nanos) const { - const auto now = TaskTimePoint::clock::now(); + const auto now = GetCurrentTimeForTask(); const auto flutter_duration = flutter_target_time_nanos - get_current_time_(); return now + std::chrono::nanoseconds(flutter_duration); } @@ -79,7 +79,7 @@ void TaskRunner::PostFlutterTask(FlutterTask flutter_task, void TaskRunner::PostTask(TaskClosure closure) { Task task; - task.fire_time = TaskTimePoint::clock::now(); + task.fire_time = GetCurrentTimeForTask(); task.variant = std::move(closure); EnqueueTask(std::move(task)); } diff --git a/shell/platform/windows/task_runner.h b/shell/platform/windows/task_runner.h index 7f181d0758975..685ed77e93f08 100644 --- a/shell/platform/windows/task_runner.h +++ b/shell/platform/windows/task_runner.h @@ -66,6 +66,14 @@ class TaskRunner { // Each platform implementations must call this to schedule the tasks. std::chrono::nanoseconds ProcessTasks(); + // Returns the current TaskTimePoint that can be used to determine whether a + // task is expired. + // + // Tests can override this to mock up the time. + virtual TaskTimePoint GetCurrentTimeForTask() const { + return TaskTimePoint::clock::now(); + } + private: typedef std::variant TaskVariant; diff --git a/shell/platform/windows/task_runner_unittests.cc b/shell/platform/windows/task_runner_unittests.cc index 8bf0b8abb1b09..d7d5cc7a4123a 100644 --- a/shell/platform/windows/task_runner_unittests.cc +++ b/shell/platform/windows/task_runner_unittests.cc @@ -25,6 +25,12 @@ class MockTaskRunner : public TaskRunner { // Do nothing to avoid processing tasks immediately after the tasks is // posted. } + + virtual TaskTimePoint GetCurrentTimeForTask() const override { + return TaskTimePoint( + std::chrono::duration_cast( + std::chrono::nanoseconds(10000))); + } }; uint64_t MockGetCurrentTime() {