Unverified Commit c7b40a52 authored by fzyzcjy's avatar fzyzcjy Committed by GitHub

Export `elapseBlocking` to test binding, so slow sync work can be simulated...

Export `elapseBlocking` to test binding, so slow sync work can be simulated such as a slow widget build (#112619)
parent d554fcb3
......@@ -1277,6 +1277,12 @@ class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
return Future<void>.value();
}
/// Simulates the synchronous passage of time, resulting from blocking or
/// expensive calls.
void elapseBlocking(Duration duration) {
_currentFakeAsync!.elapseBlocking(duration);
}
@override
Future<void> runTest(
Future<void> Function() testBody,
......
......@@ -7,7 +7,7 @@
// https://github.com/flutter/flutter/issues/85160
// Fails with "flutter test --test-randomize-ordering-seed=20210721"
@Tags(<String>['no-shuffle'])
import 'dart:async';
import 'dart:io';
import 'package:flutter/widgets.dart';
......@@ -56,4 +56,38 @@ void main() {
expect(binding.testTextInput.isRegistered, isFalse);
order += 1;
});
group('elapseBlocking', () {
testWidgets('timer is not called', (WidgetTester tester) async {
bool timerCalled = false;
Timer.run(() => timerCalled = true);
binding.elapseBlocking(const Duration(seconds: 1));
expect(timerCalled, false);
binding.idle();
});
testWidgets('can use to simulate slow build', (WidgetTester tester) async {
final DateTime beforeTime = binding.clock.now();
await tester.pumpWidget(Builder(builder: (_) {
bool timerCalled = false;
Timer.run(() => timerCalled = true);
binding.elapseBlocking(const Duration(seconds: 1));
// if we use `delayed` instead of `elapseBlocking`, such as
// binding.delayed(const Duration(seconds: 1));
// the timer will be called here. Surely, that violates how
// a flutter widget build works
expect(timerCalled, false);
return Container();
}));
expect(binding.clock.now(), beforeTime.add(const Duration(seconds: 1)));
binding.idle();
});
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment