1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'dart:ui' show window;
import 'package:flutter/foundation.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import '../flutter_test_alternative.dart';
import 'scheduler_tester.dart';
class TestSchedulerBinding extends BindingBase with SchedulerBinding, ServicesBinding {
final Map<String, List<Map<String, dynamic>>> eventsDispatched = <String, List<Map<String, dynamic>>>{};
@override
void postEvent(String eventKind, Map<String, dynamic> eventData) {
getEventsDispatched(eventKind).add(eventData);
}
List<Map<String, dynamic>> getEventsDispatched(String eventKind) {
return eventsDispatched.putIfAbsent(eventKind, () => <Map<String, dynamic>>[]);
}
}
class TestStrategy {
int allowedPriority = 10000;
bool shouldRunTaskWithPriority({ int priority, SchedulerBinding scheduler }) {
return priority >= allowedPriority;
}
}
void main() {
TestSchedulerBinding scheduler;
setUpAll(() {
scheduler = TestSchedulerBinding();
});
test('Tasks are executed in the right order', () {
final TestStrategy strategy = TestStrategy();
scheduler.schedulingStrategy = strategy.shouldRunTaskWithPriority;
final List<int> input = <int>[2, 23, 23, 11, 0, 80, 3];
final List<int> executedTasks = <int>[];
void scheduleAddingTask(int x) {
scheduler.scheduleTask(() { executedTasks.add(x); }, Priority.idle + x);
}
input.forEach(scheduleAddingTask);
strategy.allowedPriority = 100;
for (int i = 0; i < 3; i += 1)
expect(scheduler.handleEventLoopCallback(), isFalse);
expect(executedTasks.isEmpty, isTrue);
strategy.allowedPriority = 50;
for (int i = 0; i < 3; i += 1)
expect(scheduler.handleEventLoopCallback(), i == 0 ? isTrue : isFalse);
expect(executedTasks, hasLength(1));
expect(executedTasks.single, equals(80));
executedTasks.clear();
strategy.allowedPriority = 20;
for (int i = 0; i < 3; i += 1)
expect(scheduler.handleEventLoopCallback(), i < 2 ? isTrue : isFalse);
expect(executedTasks, hasLength(2));
expect(executedTasks[0], equals(23));
expect(executedTasks[1], equals(23));
executedTasks.clear();
scheduleAddingTask(99);
scheduleAddingTask(19);
scheduleAddingTask(5);
scheduleAddingTask(97);
for (int i = 0; i < 3; i += 1)
expect(scheduler.handleEventLoopCallback(), i < 2 ? isTrue : isFalse);
expect(executedTasks, hasLength(2));
expect(executedTasks[0], equals(99));
expect(executedTasks[1], equals(97));
executedTasks.clear();
strategy.allowedPriority = 10;
for (int i = 0; i < 3; i += 1)
expect(scheduler.handleEventLoopCallback(), i < 2 ? isTrue : isFalse);
expect(executedTasks, hasLength(2));
expect(executedTasks[0], equals(19));
expect(executedTasks[1], equals(11));
executedTasks.clear();
strategy.allowedPriority = 1;
for (int i = 0; i < 4; i += 1)
expect(scheduler.handleEventLoopCallback(), i < 3 ? isTrue : isFalse);
expect(executedTasks, hasLength(3));
expect(executedTasks[0], equals(5));
expect(executedTasks[1], equals(3));
expect(executedTasks[2], equals(2));
executedTasks.clear();
strategy.allowedPriority = 0;
expect(scheduler.handleEventLoopCallback(), isFalse);
expect(executedTasks, hasLength(1));
expect(executedTasks[0], equals(0));
});
test('2 calls to scheduleWarmUpFrame just schedules it once', () {
final List<VoidCallback> timerQueueTasks = <VoidCallback>[];
bool taskExecuted = false;
runZoned<void>(
() {
// Run it twice without processing the queued tasks.
scheduler.scheduleWarmUpFrame();
scheduler.scheduleWarmUpFrame();
scheduler.scheduleTask(() { taskExecuted = true; }, Priority.touch);
},
zoneSpecification: ZoneSpecification(
createTimer: (Zone self, ZoneDelegate parent, Zone zone, Duration duration, void f()) {
// Don't actually run the tasks, just record that it was scheduled.
timerQueueTasks.add(f);
return null;
},
),
);
// scheduleWarmUpFrame scheduled 2 Timers, scheduleTask scheduled 0 because
// events are locked.
expect(timerQueueTasks.length, 2);
expect(taskExecuted, false);
});
test('Flutter.Frame event fired', () async {
window.onReportTimings(<FrameTiming>[FrameTiming(<int>[
// build start, build finish
10000, 15000,
// raster start, raster finish
16000, 20000,
])]);
final List<Map<String, dynamic>> events = scheduler.getEventsDispatched('Flutter.Frame');
expect(events, hasLength(1));
final Map<String, dynamic> event = events.first;
expect(event['number'], isNonNegative);
expect(event['startTime'], 10000);
expect(event['elapsed'], 10000);
expect(event['build'], 5000);
expect(event['raster'], 4000);
});
test('TimingsCallback exceptions are caught', () {
FlutterErrorDetails errorCaught;
FlutterError.onError = (FlutterErrorDetails details) {
errorCaught = details;
};
SchedulerBinding.instance.addTimingsCallback((List<FrameTiming> timings) {
throw Exception('Test');
});
window.onReportTimings(<FrameTiming>[]);
expect(errorCaught.exceptionAsString(), equals('Exception: Test'));
});
test('currentSystemFrameTimeStamp is the raw timestamp', () {
Duration lastTimeStamp;
Duration lastSystemTimeStamp;
void frameCallback(Duration timeStamp) {
expect(timeStamp, scheduler.currentFrameTimeStamp);
lastTimeStamp = scheduler.currentFrameTimeStamp;
lastSystemTimeStamp = scheduler.currentSystemFrameTimeStamp;
}
scheduler.scheduleFrameCallback(frameCallback);
tick(const Duration(seconds: 2));
expect(lastTimeStamp, Duration.zero);
expect(lastSystemTimeStamp, const Duration(seconds: 2));
scheduler.scheduleFrameCallback(frameCallback);
tick(const Duration(seconds: 4));
expect(lastTimeStamp, const Duration(seconds: 2));
expect(lastSystemTimeStamp, const Duration(seconds: 4));
timeDilation = 2;
scheduler.scheduleFrameCallback(frameCallback);
tick(const Duration(seconds: 6));
expect(lastTimeStamp, const Duration(seconds: 2)); // timeDilation calls SchedulerBinding.resetEpoch
expect(lastSystemTimeStamp, const Duration(seconds: 6));
scheduler.scheduleFrameCallback(frameCallback);
tick(const Duration(seconds: 8));
expect(lastTimeStamp, const Duration(seconds: 3)); // 2s + (8 - 6)s / 2
expect(lastSystemTimeStamp, const Duration(seconds: 8));
});
}