Skip to content

Commit

Permalink
Fix bug thattimeDilation is not reset, causing subsequent test erro…
Browse files Browse the repository at this point in the history
…rs, and add verifications to ensure such problem does not exist in the future (flutter#113830)
  • Loading branch information
fzyzcjy committed Nov 1, 2022
1 parent c23b5ca commit 61deaef
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions dev/tools/examples_smoke_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ Future<File> generateTest(Directory apiDir) async {
// Collect the examples, and import them all as separate symbols.
final List<String> imports = <String>[];
imports.add('''import 'package:flutter/widgets.dart';''');
imports.add('''import 'package:flutter/scheduler.dart';''');
imports.add('''import 'package:flutter_test/flutter_test.dart';''');
imports.add('''import 'package:integration_test/integration_test.dart';''');
final List<ExampleInfo> infoList = <ExampleInfo>[];
Expand Down Expand Up @@ -165,6 +166,7 @@ void main() {
expect(find.byType(WidgetsApp), findsOneWidget);
} finally {
ErrorWidget.builder = originalBuilder;
timeDilation = 1.0;
}
},
);
Expand Down
14 changes: 14 additions & 0 deletions packages/flutter/lib/src/scheduler/binding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,20 @@ mixin SchedulerBinding on BindingBase {
return true;
}

/// Asserts that there is no artificial time dilation in debug mode.
///
/// Throws a [FlutterError] if there are such dilation, as this will make
/// subsequent tests see dilation and thus flaky.
bool debugAssertNoTimeDilation(String reason) {
assert(() {
if (timeDilation != 1.0) {
throw FlutterError(reason);
}
return true;
}());
return true;
}

/// Prints the stack for where the current transient callback was registered.
///
/// A transient frame callback is one that was registered with
Expand Down
15 changes: 15 additions & 0 deletions packages/flutter/test/scheduler/binding_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,19 @@ void main() {
SchedulerBinding.instance.scheduleForcedFrame();
expect(SchedulerBinding.instance.platformDispatcher.onBeginFrame, isNotNull);
});

test('debugAssertNoTimeDilation does not throw if time dilate already reset', () async {
timeDilation = 2.0;
timeDilation = 1.0;
SchedulerBinding.instance.debugAssertNoTimeDilation('reason'); // no error
});

test('debugAssertNoTimeDilation throw if time dilate not reset', () async {
timeDilation = 3.0;
expect(
() => SchedulerBinding.instance.debugAssertNoTimeDilation('reason'),
throwsA(isA<FlutterError>().having((FlutterError e) => e.message, 'message', 'reason')),
);
timeDilation = 1.0;
});
}
2 changes: 2 additions & 0 deletions packages/flutter/test/scheduler/scheduler_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ void main() {
tick(const Duration(seconds: 8));
expect(lastTimeStamp, const Duration(seconds: 3)); // 2s + (8 - 6)s / 2
expect(lastSystemTimeStamp, const Duration(seconds: 8));

timeDilation = 1.0; // restore time dilation, or it will affect other tests
});

test('Animation frame scheduled in the middle of the warm-up frame', () {
Expand Down
4 changes: 4 additions & 0 deletions packages/flutter/test/scheduler/ticker_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ void main() {
expect(lastDuration, const Duration(milliseconds: 20));

ticker.dispose();

timeDilation = 1.0; // restore time dilation, or it will affect other tests
});

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

ticker.dispose();

timeDilation = 1.0; // restore time dilation, or it will affect other tests
});

testWidgets('Ticker stops ticking when application is paused', (WidgetTester tester) async {
Expand Down
3 changes: 3 additions & 0 deletions packages/flutter_test/lib/src/binding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,9 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
assert(debugAssertNoPendingPerformanceModeRequests(
'A performance mode was requested and not disposed by a test.'
));
assert(debugAssertNoTimeDilation(
'The timeDilation was changed and not reset by the test.'
));
assert(debugAssertAllFoundationVarsUnset(
'The value of a foundation debug variable was changed by the test.',
debugPrintOverride: debugPrintOverride,
Expand Down

0 comments on commit 61deaef

Please sign in to comment.