Skip to content

Commit c617498

Browse files
committed
Fix
1 parent 5270cee commit c617498

2 files changed

Lines changed: 95 additions & 6 deletions

File tree

‎packages/flutter_test/lib/src/binding.dart‎

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,6 +1783,7 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
17831783
);
17841784
}
17851785

1786+
_currentTestDescription = description;
17861787
_oldExceptionHandler = FlutterError.onError;
17871788
_oldStackTraceDemangler = FlutterError.demangleStackTrace;
17881789
var exceptionCount = 0; // number of un-taken exceptions
@@ -1989,6 +1990,9 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
19891990
asyncBarrier(); // When using AutomatedTestWidgetsFlutterBinding, this flushes the microtasks.
19901991
}
19911992

1993+
// The description of the currently running test, saved in [_runTest] so that
1994+
// [postTest] can include it when reporting invariant failures.
1995+
String _currentTestDescription = '';
19921996
bool _beforeTestAutoUpdateGoldens = false;
19931997
late TestExceptionReporter _beforeTestReportTestException;
19941998
late ErrorWidgetBuilder _beforeTestErrorWidgetBuilder;
@@ -2087,7 +2091,13 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
20872091
void _verifyAutoUpdateGoldensUnset(bool valueBeforeTest) {
20882092
assert(() {
20892093
if (autoUpdateGoldenFiles != valueBeforeTest) {
2090-
throw FlutterError('The value of autoUpdateGoldenFiles was changed by the test.');
2094+
FlutterError.reportError(
2095+
FlutterErrorDetails(
2096+
exception: FlutterError('The value of autoUpdateGoldenFiles was changed by the test.'),
2097+
stack: StackTrace.current,
2098+
library: 'Flutter test framework',
2099+
),
2100+
);
20912101
}
20922102
return true;
20932103
}());
@@ -2101,7 +2111,13 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
21012111
// So we reset the error reporter to its initial value and then report
21022112
// this error.
21032113
reportTestException = valueBeforeTest;
2104-
throw FlutterError('The value of reportTestException was changed by the test.');
2114+
FlutterError.reportError(
2115+
FlutterErrorDetails(
2116+
exception: FlutterError('The value of reportTestException was changed by the test.'),
2117+
stack: StackTrace.current,
2118+
library: 'Flutter test framework',
2119+
),
2120+
);
21052121
}
21062122
return true;
21072123
}());
@@ -2110,7 +2126,13 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
21102126
void _verifyErrorWidgetBuilderUnset(ErrorWidgetBuilder valueBeforeTest) {
21112127
assert(() {
21122128
if (ErrorWidget.builder != valueBeforeTest) {
2113-
throw FlutterError('The value of ErrorWidget.builder was changed by the test.');
2129+
FlutterError.reportError(
2130+
FlutterErrorDetails(
2131+
exception: FlutterError('The value of ErrorWidget.builder was changed by the test.'),
2132+
stack: StackTrace.current,
2133+
library: 'Flutter test framework',
2134+
),
2135+
);
21142136
}
21152137
return true;
21162138
}());
@@ -2119,8 +2141,14 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
21192141
void _verifyShouldPropagateDevicePointerEventsUnset(bool valueBeforeTest) {
21202142
assert(() {
21212143
if (shouldPropagateDevicePointerEvents != valueBeforeTest) {
2122-
throw FlutterError(
2123-
'The value of shouldPropagateDevicePointerEvents was changed by the test.',
2144+
FlutterError.reportError(
2145+
FlutterErrorDetails(
2146+
exception: FlutterError(
2147+
'The value of shouldPropagateDevicePointerEvents was changed by the test.',
2148+
),
2149+
stack: StackTrace.current,
2150+
library: 'Flutter test framework',
2151+
),
21242152
);
21252153
}
21262154
return true;
@@ -2185,7 +2213,25 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
21852213
library: 'Flutter test framework',
21862214
);
21872215
}
2216+
// Some invariant checks (such as debugAssertNoTransientCallbacks and the
2217+
// _verify*Unset helpers above) report failures through
2218+
// FlutterError.reportError instead of throwing. During the test,
2219+
// FlutterError.onError stores them in _pendingExceptionDetails, which is
2220+
// normally reported by the test completion handler. That handler has
2221+
// already run by the time postTest is called, so these errors must be
2222+
// picked up here or they would be silently dropped.
2223+
final FlutterErrorDetails? reportedError = _pendingExceptionDetails;
2224+
if (reportedError != null) {
2225+
if (invariantError == null) {
2226+
invariantError = reportedError;
2227+
} else {
2228+
debugPrint = debugPrintOverride; // just in case the test overrides it -- otherwise we won't see the error!
2229+
FlutterError.dumpErrorToConsole(reportedError, forceReport: true);
2230+
}
2231+
}
21882232
}
2233+
final String testDescription = _currentTestDescription;
2234+
_currentTestDescription = '';
21892235
FlutterError.onError = _oldExceptionHandler;
21902236
FlutterError.demangleStackTrace = _oldStackTraceDemangler;
21912237
_pendingExceptionDetails = null;
@@ -2223,7 +2269,8 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
22232269
ServicesBinding.instance.resetInternalState();
22242270

22252271
if (invariantError != null) {
2226-
reportTestException(invariantError, '');
2272+
debugPrint = debugPrintOverride; // just in case the test overrides it -- otherwise we won't see the error!
2273+
reportTestException(invariantError, testDescription);
22272274
}
22282275
}
22292276
}

‎packages/flutter_test/test/bindings_invariants_test.dart‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
import 'package:flutter/foundation.dart';
6+
import 'package:flutter/scheduler.dart';
67
import 'package:flutter/widgets.dart';
78
import 'package:flutter_test/flutter_test.dart';
89

@@ -171,4 +172,45 @@ void main() {
171172
});
172173
},
173174
);
175+
176+
// Verifies that invariant checks that report failures via
177+
// [FlutterError.reportError] instead of throwing (such as
178+
// [SchedulerBinding.debugAssertNoTransientCallbacks]) are captured and
179+
// reported with the test description in [TestWidgetsFlutterBinding.postTest].
180+
test('postTest reports invariant failures logged via FlutterError.reportError', () async {
181+
FlutterErrorDetails? reportedError;
182+
String? reportedDescription;
183+
final TestExceptionReporter oldReporter = reportTestException;
184+
reportTestException = (FlutterErrorDetails details, String testDescription) {
185+
reportedError = details;
186+
reportedDescription = testDescription;
187+
};
188+
addTearDown(() {
189+
reportTestException = oldReporter;
190+
});
191+
192+
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized();
193+
// A Ticker reschedules its frame callback on every tick, so it survives the
194+
// frames pumped while unmounting the tree. A one-shot frame callback would
195+
// be consumed by those frames instead.
196+
late final Ticker ticker;
197+
await binding.runTest(
198+
() async {
199+
ticker = Ticker((Duration _) {})..start();
200+
},
201+
() {},
202+
description: 'leaking ticker test',
203+
);
204+
205+
expect(() => binding.postTest(), returnsNormally);
206+
expect(reportedError, isNotNull);
207+
expect(
208+
reportedError!.exception,
209+
'An animation is still running even after the widget tree was disposed.',
210+
);
211+
expect(reportedDescription, 'leaking ticker test');
212+
expect(binding.inTest, isFalse);
213+
214+
ticker.dispose();
215+
});
174216
}

0 commit comments

Comments
 (0)