7 #include "flutter/fml/macros.h"
8 #include "gtest/gtest.h"
14 class MockTaskRunner :
public TaskRunner {
17 const TaskExpiredCallback& on_task_expired)
18 : TaskRunner(get_current_time, on_task_expired) {}
20 virtual bool RunsTasksOnCurrentThread()
const override {
return true; }
22 void SimulateTimerAwake() { ProcessTasks(); }
25 virtual void WakeUp()
override {
30 virtual TaskTimePoint GetCurrentTimeForTask()
const override {
32 std::chrono::duration_cast<std::chrono::steady_clock::duration>(
33 std::chrono::nanoseconds(10000)));
37 FML_DISALLOW_COPY_AND_ASSIGN(MockTaskRunner);
40 uint64_t MockGetCurrentTime() {
45 TEST(TaskRunnerTest, MaybeExecuteTaskWithExactOrder) {
46 std::vector<uint64_t> executed_task_order;
48 MockTaskRunner(MockGetCurrentTime,
49 [&executed_task_order](
const FlutterTask* expired_task) {
50 executed_task_order.push_back(expired_task->task);
53 uint64_t time_now = MockGetCurrentTime();
55 runner.PostFlutterTask(FlutterTask{
nullptr, 1}, time_now);
56 runner.PostFlutterTask(FlutterTask{
nullptr, 2}, time_now);
58 [&executed_task_order]() { executed_task_order.push_back(3); });
60 [&executed_task_order]() { executed_task_order.push_back(4); });
62 runner.SimulateTimerAwake();
64 std::vector<uint64_t> posted_task_order{1, 2, 3, 4};
65 EXPECT_EQ(executed_task_order, posted_task_order);
68 TEST(TaskRunnerTest, MaybeExecuteTaskOnlyExpired) {
69 std::set<uint64_t> executed_task;
70 auto runner = MockTaskRunner(
71 MockGetCurrentTime, [&executed_task](
const FlutterTask* expired_task) {
72 executed_task.insert(expired_task->task);
75 uint64_t task_expired_before_now = 1;
76 uint64_t time_before_now = 0;
77 runner.PostFlutterTask(FlutterTask{
nullptr, task_expired_before_now},
80 uint64_t task_expired_after_now = 2;
81 uint64_t time_after_now = MockGetCurrentTime() * 2;
82 runner.PostFlutterTask(FlutterTask{
nullptr, task_expired_after_now},
85 runner.SimulateTimerAwake();
87 std::set<uint64_t> only_task_expired_before_now{task_expired_before_now};
88 EXPECT_EQ(executed_task, only_task_expired_before_now);