Unverified Commit 1cfd792a authored by cg021's avatar cg021 Committed by GitHub

remove pending timers list code out of assert message (#57287)

* move pending timers list code out of assert

* style change
Co-authored-by: 's avatarEmmanuel Garcia <egarciad@google.com>

* add unit tests

* formatting

* pending timer test

* pending timer assertion test

* add setup and teardown
Co-authored-by: 's avatarEmmanuel Garcia <egarciad@google.com>
parent aa498216
......@@ -1116,23 +1116,20 @@ class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
void _verifyInvariants() {
super._verifyInvariants();
assert(() {
if ( _currentFakeAsync.periodicTimerCount == 0
&& _currentFakeAsync.nonPeriodicTimerCount == 0) {
return true;
}
debugPrint('Pending timers:');
for (final FakeTimer timer in _currentFakeAsync.pendingTimers) {
debugPrint(
'Timer (duration: ${timer.duration}, '
'periodic: ${timer.isPeriodic}), created:');
debugPrintStack(stackTrace: timer.creationStackTrace);
debugPrint('');
}
return false;
}(), 'A Timer is still pending even after the widget tree was disposed.');
bool timersPending = false;
if (_currentFakeAsync.periodicTimerCount != 0 ||
_currentFakeAsync.nonPeriodicTimerCount != 0) {
debugPrint('Pending timers:');
for (final FakeTimer timer in _currentFakeAsync.pendingTimers) {
debugPrint(
'Timer (duration: ${timer.duration}, '
'periodic: ${timer.isPeriodic}), created:');
debugPrintStack(stackTrace: timer.creationStackTrace);
debugPrint('');
}
timersPending = true;
}
assert(!timersPending, 'A Timer is still pending even after the widget tree was disposed.');
assert(_currentFakeAsync.microtaskCount == 0); // Shouldn't be possible.
}
......
......@@ -775,6 +775,33 @@ void main() {
expect(desktop.values.union(mobile.values), equals(all.values));
});
});
group('Pending timer', () {
TestExceptionReporter currentExceptionReporter;
setUp(() {
currentExceptionReporter = reportTestException;
});
tearDown(() {
reportTestException = currentExceptionReporter;
});
test('Throws assertion message without code', () async {
FlutterErrorDetails flutterErrorDetails;
reportTestException = (FlutterErrorDetails details, String testDescription) {
flutterErrorDetails = details;
};
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized() as TestWidgetsFlutterBinding;
await binding.runTest(() async {
final Timer timer = Timer(const Duration(seconds: 1), () {});
expect(timer.isActive, true);
}, () {});
expect(flutterErrorDetails?.exception, isA<AssertionError>());
expect(flutterErrorDetails?.exception?.message, 'A Timer is still pending even after the widget tree was disposed.');
});
});
}
class FakeMatcher extends AsyncMatcher {
......
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