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 'package:flutter/foundation.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
Future<void> setAppLifeCycleState(AppLifecycleState state) async {
final ByteData? message =
const StringCodec().encodeMessage(state.toString());
await ServicesBinding.instance!.defaultBinaryMessenger
.handlePlatformMessage('flutter/lifecycle', message, (_) {});
}
testWidgets('Ticker mute control test', (WidgetTester tester) async {
int tickCount = 0;
void handleTick(Duration duration) {
tickCount += 1;
}
final Ticker ticker = Ticker(handleTick);
expect(ticker.isTicking, isFalse);
expect(ticker.isActive, isFalse);
ticker.start();
expect(ticker.isTicking, isTrue);
expect(ticker.isActive, isTrue);
expect(tickCount, equals(0));
FlutterError? error;
try {
ticker.start();
} on FlutterError catch (e) {
error = e;
}
expect(error, isNotNull);
expect(error!.diagnostics.length, 3);
expect(error.diagnostics.last, isA<DiagnosticsProperty<Ticker>>());
expect(
error.toStringDeep(),
startsWith(
'FlutterError\n'
' A ticker was started twice.\n'
' A ticker that is already active cannot be started again without\n'
' first stopping it.\n'
' The affected ticker was:\n'
' Ticker()\n',
),
);
await tester.pump(const Duration(milliseconds: 10));
expect(tickCount, equals(1));
ticker.muted = true;
await tester.pump(const Duration(milliseconds: 10));
expect(tickCount, equals(1));
expect(ticker.isTicking, isFalse);
expect(ticker.isActive, isTrue);
ticker.muted = false;
await tester.pump(const Duration(milliseconds: 10));
expect(tickCount, equals(2));
expect(ticker.isTicking, isTrue);
expect(ticker.isActive, isTrue);
ticker.muted = true;
await tester.pump(const Duration(milliseconds: 10));
expect(tickCount, equals(2));
expect(ticker.isTicking, isFalse);
expect(ticker.isActive, isTrue);
ticker.stop();
expect(tickCount, equals(2));
expect(ticker.isTicking, isFalse);
expect(ticker.isActive, isFalse);
ticker.muted = false;
expect(tickCount, equals(2));
expect(ticker.isTicking, isFalse);
expect(ticker.isActive, isFalse);
await tester.pump(const Duration(milliseconds: 10));
expect(tickCount, equals(2));
expect(ticker.isTicking, isFalse);
expect(ticker.isActive, isFalse);
});
testWidgets('Ticker control test', (WidgetTester tester) async {
late Ticker ticker;
void testFunction() {
ticker = Ticker((Duration _) { });
}
testFunction();
expect(ticker, hasOneLineDescription);
expect(ticker.toString(debugIncludeStack: true), contains('testFunction'));
});
testWidgets('Ticker can be sped up with time dilation', (WidgetTester tester) async {
timeDilation = 0.5; // Move twice as fast.
late Duration lastDuration;
void handleTick(Duration duration) {
lastDuration = duration;
}
final Ticker ticker = Ticker(handleTick);
ticker.start();
await tester.pump(const Duration(milliseconds: 10));
await tester.pump(const Duration(milliseconds: 10));
expect(lastDuration, const Duration(milliseconds: 20));
ticker.dispose();
});
testWidgets('Ticker can be slowed down with time dilation', (WidgetTester tester) async {
timeDilation = 2.0; // Move half as fast.
late Duration lastDuration;
void handleTick(Duration duration) {
lastDuration = duration;
}
final Ticker ticker = Ticker(handleTick);
ticker.start();
await tester.pump(const Duration(milliseconds: 10));
await tester.pump(const Duration(milliseconds: 10));
expect(lastDuration, const Duration(milliseconds: 5));
ticker.dispose();
});
testWidgets('Ticker stops ticking when application is paused', (WidgetTester tester) async {
int tickCount = 0;
void handleTick(Duration duration) {
tickCount += 1;
}
final Ticker ticker = Ticker(handleTick);
ticker.start();
expect(ticker.isTicking, isTrue);
expect(ticker.isActive, isTrue);
expect(tickCount, equals(0));
setAppLifeCycleState(AppLifecycleState.paused);
expect(ticker.isTicking, isFalse);
expect(ticker.isActive, isTrue);
ticker.stop();
setAppLifeCycleState(AppLifecycleState.resumed);
});
testWidgets('Ticker can be created before application unpauses', (WidgetTester tester) async {
setAppLifeCycleState(AppLifecycleState.paused);
int tickCount = 0;
void handleTick(Duration duration) {
tickCount += 1;
}
final Ticker ticker = Ticker(handleTick);
ticker.start();
expect(tickCount, equals(0));
expect(ticker.isTicking, isFalse);
await tester.pump(const Duration(milliseconds: 10));
expect(tickCount, equals(0));
expect(ticker.isTicking, isFalse);
setAppLifeCycleState(AppLifecycleState.resumed);
await tester.pump(const Duration(milliseconds: 10));
expect(tickCount, equals(1));
expect(ticker.isTicking, isTrue);
ticker.stop();
});
}