Skip to content

Commit 61deaef

Browse files
authored
Fix bug thattimeDilation is not reset, causing subsequent test errors, and add verifications to ensure such problem does not exist in the future (#113830)
1 parent c23b5ca commit 61deaef

File tree

6 files changed

+40
-0
lines changed

6 files changed

+40
-0
lines changed

dev/tools/examples_smoke_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ Future<File> generateTest(Directory apiDir) async {
119119
// Collect the examples, and import them all as separate symbols.
120120
final List<String> imports = <String>[];
121121
imports.add('''import 'package:flutter/widgets.dart';''');
122+
imports.add('''import 'package:flutter/scheduler.dart';''');
122123
imports.add('''import 'package:flutter_test/flutter_test.dart';''');
123124
imports.add('''import 'package:integration_test/integration_test.dart';''');
124125
final List<ExampleInfo> infoList = <ExampleInfo>[];
@@ -165,6 +166,7 @@ void main() {
165166
expect(find.byType(WidgetsApp), findsOneWidget);
166167
} finally {
167168
ErrorWidget.builder = originalBuilder;
169+
timeDilation = 1.0;
168170
}
169171
},
170172
);

packages/flutter/lib/src/scheduler/binding.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,6 +652,20 @@ mixin SchedulerBinding on BindingBase {
652652
return true;
653653
}
654654

655+
/// Asserts that there is no artificial time dilation in debug mode.
656+
///
657+
/// Throws a [FlutterError] if there are such dilation, as this will make
658+
/// subsequent tests see dilation and thus flaky.
659+
bool debugAssertNoTimeDilation(String reason) {
660+
assert(() {
661+
if (timeDilation != 1.0) {
662+
throw FlutterError(reason);
663+
}
664+
return true;
665+
}());
666+
return true;
667+
}
668+
655669
/// Prints the stack for where the current transient callback was registered.
656670
///
657671
/// A transient frame callback is one that was registered with

packages/flutter/test/scheduler/binding_test.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,19 @@ void main() {
1313
SchedulerBinding.instance.scheduleForcedFrame();
1414
expect(SchedulerBinding.instance.platformDispatcher.onBeginFrame, isNotNull);
1515
});
16+
17+
test('debugAssertNoTimeDilation does not throw if time dilate already reset', () async {
18+
timeDilation = 2.0;
19+
timeDilation = 1.0;
20+
SchedulerBinding.instance.debugAssertNoTimeDilation('reason'); // no error
21+
});
22+
23+
test('debugAssertNoTimeDilation throw if time dilate not reset', () async {
24+
timeDilation = 3.0;
25+
expect(
26+
() => SchedulerBinding.instance.debugAssertNoTimeDilation('reason'),
27+
throwsA(isA<FlutterError>().having((FlutterError e) => e.message, 'message', 'reason')),
28+
);
29+
timeDilation = 1.0;
30+
});
1631
}

packages/flutter/test/scheduler/scheduler_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ void main() {
215215
tick(const Duration(seconds: 8));
216216
expect(lastTimeStamp, const Duration(seconds: 3)); // 2s + (8 - 6)s / 2
217217
expect(lastSystemTimeStamp, const Duration(seconds: 8));
218+
219+
timeDilation = 1.0; // restore time dilation, or it will affect other tests
218220
});
219221

220222
test('Animation frame scheduled in the middle of the warm-up frame', () {

packages/flutter/test/scheduler/ticker_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ void main() {
124124
expect(lastDuration, const Duration(milliseconds: 20));
125125

126126
ticker.dispose();
127+
128+
timeDilation = 1.0; // restore time dilation, or it will affect other tests
127129
});
128130

129131
testWidgets('Ticker can be slowed down with time dilation', (WidgetTester tester) async {
@@ -140,6 +142,8 @@ void main() {
140142
expect(lastDuration, const Duration(milliseconds: 5));
141143

142144
ticker.dispose();
145+
146+
timeDilation = 1.0; // restore time dilation, or it will affect other tests
143147
});
144148

145149
testWidgets('Ticker stops ticking when application is paused', (WidgetTester tester) async {

packages/flutter_test/lib/src/binding.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,9 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
965965
assert(debugAssertNoPendingPerformanceModeRequests(
966966
'A performance mode was requested and not disposed by a test.'
967967
));
968+
assert(debugAssertNoTimeDilation(
969+
'The timeDilation was changed and not reset by the test.'
970+
));
968971
assert(debugAssertAllFoundationVarsUnset(
969972
'The value of a foundation debug variable was changed by the test.',
970973
debugPrintOverride: debugPrintOverride,

0 commit comments

Comments
 (0)