mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
Export elapseBlocking
to test binding, so slow sync work can be simulated such as a slow widget build (#112619)
This commit is contained in:
parent
d554fcb36a
commit
c7b40a5270
@ -1277,6 +1277,12 @@ class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
|
|||||||
return Future<void>.value();
|
return Future<void>.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Simulates the synchronous passage of time, resulting from blocking or
|
||||||
|
/// expensive calls.
|
||||||
|
void elapseBlocking(Duration duration) {
|
||||||
|
_currentFakeAsync!.elapseBlocking(duration);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> runTest(
|
Future<void> runTest(
|
||||||
Future<void> Function() testBody,
|
Future<void> Function() testBody,
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
// https://github.com/flutter/flutter/issues/85160
|
// https://github.com/flutter/flutter/issues/85160
|
||||||
// Fails with "flutter test --test-randomize-ordering-seed=20210721"
|
// Fails with "flutter test --test-randomize-ordering-seed=20210721"
|
||||||
@Tags(<String>['no-shuffle'])
|
@Tags(<String>['no-shuffle'])
|
||||||
|
import 'dart:async';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:flutter/widgets.dart';
|
import 'package:flutter/widgets.dart';
|
||||||
@ -56,4 +56,38 @@ void main() {
|
|||||||
expect(binding.testTextInput.isRegistered, isFalse);
|
expect(binding.testTextInput.isRegistered, isFalse);
|
||||||
order += 1;
|
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();
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user