remove pending timers list code out of assert message (#57287)
* move pending timers list code out of assert * style change Co-authored-by: Emmanuel Garcia <egarciad@google.com> * add unit tests * formatting * pending timer test * pending timer assertion test * add setup and teardown Co-authored-by: Emmanuel Garcia <egarciad@google.com>
This commit is contained in:
parent
aa49821613
commit
1cfd792a38
@ -1116,12 +1116,9 @@ class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
|
|||||||
void _verifyInvariants() {
|
void _verifyInvariants() {
|
||||||
super._verifyInvariants();
|
super._verifyInvariants();
|
||||||
|
|
||||||
assert(() {
|
bool timersPending = false;
|
||||||
if ( _currentFakeAsync.periodicTimerCount == 0
|
if (_currentFakeAsync.periodicTimerCount != 0 ||
|
||||||
&& _currentFakeAsync.nonPeriodicTimerCount == 0) {
|
_currentFakeAsync.nonPeriodicTimerCount != 0) {
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
debugPrint('Pending timers:');
|
debugPrint('Pending timers:');
|
||||||
for (final FakeTimer timer in _currentFakeAsync.pendingTimers) {
|
for (final FakeTimer timer in _currentFakeAsync.pendingTimers) {
|
||||||
debugPrint(
|
debugPrint(
|
||||||
@ -1130,9 +1127,9 @@ class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
|
|||||||
debugPrintStack(stackTrace: timer.creationStackTrace);
|
debugPrintStack(stackTrace: timer.creationStackTrace);
|
||||||
debugPrint('');
|
debugPrint('');
|
||||||
}
|
}
|
||||||
return false;
|
timersPending = true;
|
||||||
}(), 'A Timer is still pending even after the widget tree was disposed.');
|
}
|
||||||
|
assert(!timersPending, 'A Timer is still pending even after the widget tree was disposed.');
|
||||||
assert(_currentFakeAsync.microtaskCount == 0); // Shouldn't be possible.
|
assert(_currentFakeAsync.microtaskCount == 0); // Shouldn't be possible.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -775,6 +775,33 @@ void main() {
|
|||||||
expect(desktop.values.union(mobile.values), equals(all.values));
|
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 {
|
class FakeMatcher extends AsyncMatcher {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user