Flutter Engine
The Flutter Engine
task_runner_unittests.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter 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 "flutter/shell/platform/windows/task_runner.h"
6
7#include "flutter/fml/macros.h"
8#include "gtest/gtest.h"
9
10namespace flutter {
11namespace testing {
12
13namespace {
14class MockTaskRunner : public TaskRunner {
15 public:
16 MockTaskRunner(CurrentTimeProc get_current_time,
17 const TaskExpiredCallback& on_task_expired)
18 : TaskRunner(get_current_time, on_task_expired) {}
19
20 virtual bool RunsTasksOnCurrentThread() const override { return true; }
21
22 void SimulateTimerAwake() { ProcessTasks(); }
23
24 protected:
25 virtual void WakeUp() override {
26 // Do nothing to avoid processing tasks immediately after the tasks is
27 // posted.
28 }
29
30 virtual TaskTimePoint GetCurrentTimeForTask() const override {
31 return TaskTimePoint(
32 std::chrono::duration_cast<std::chrono::steady_clock::duration>(
33 std::chrono::nanoseconds(10000)));
34 }
35
36 private:
37 FML_DISALLOW_COPY_AND_ASSIGN(MockTaskRunner);
38};
39
40uint64_t MockGetCurrentTime() {
41 return 10000;
42}
43} // namespace
44
45TEST(TaskRunnerTest, MaybeExecuteTaskWithExactOrder) {
46 std::vector<uint64_t> executed_task_order;
47 auto runner =
48 MockTaskRunner(MockGetCurrentTime,
49 [&executed_task_order](const FlutterTask* expired_task) {
50 executed_task_order.push_back(expired_task->task);
51 });
52
53 uint64_t time_now = MockGetCurrentTime();
54
55 runner.PostFlutterTask(FlutterTask{nullptr, 1}, time_now);
56 runner.PostFlutterTask(FlutterTask{nullptr, 2}, time_now);
57 runner.PostTask(
58 [&executed_task_order]() { executed_task_order.push_back(3); });
59 runner.PostTask(
60 [&executed_task_order]() { executed_task_order.push_back(4); });
61
62 runner.SimulateTimerAwake();
63
64 std::vector<uint64_t> posted_task_order{1, 2, 3, 4};
65 EXPECT_EQ(executed_task_order, posted_task_order);
66}
67
68TEST(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);
73 });
74
75 uint64_t task_expired_before_now = 1;
76 uint64_t time_before_now = 0;
77 runner.PostFlutterTask(FlutterTask{nullptr, task_expired_before_now},
78 time_before_now);
79
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},
83 time_after_now);
84
85 runner.SimulateTimerAwake();
86
87 std::set<uint64_t> only_task_expired_before_now{task_expired_before_now};
88 EXPECT_EQ(executed_task, only_task_expired_before_now);
89}
90
91} // namespace testing
92} // namespace flutter
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:27
TEST(DisplayListComplexity, EmptyDisplayList)
uint64_t(* CurrentTimeProc)()
Definition: task_runner.h:21
uint64_t task
Definition: embedder.h:1552