Fix leaks in flutter_test tests (#180879)

Following https://github.com/flutter/flutter/pull/176992, this PR fixes
all leaks in `flutter_test` tests.

A new method is added to reliably dispose the root layer of render
views. Feel free to let me know if you have suggestions for the name!

## Pre-launch Checklist

- [ ] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [ ] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [ ] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [ ] I signed the [CLA].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [ ] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [ ] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
This commit is contained in:
Tong Mu 2026-01-22 16:37:19 -08:00 committed by GitHub
parent 65701bd31b
commit 0161fc7caf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 328 additions and 240 deletions

View File

@ -596,37 +596,44 @@ class CustomMinimumContrastGuideline extends AccessibilityGuideline {
@override
Future<Evaluation> evaluate(WidgetTester tester) async {
// Compute elements to be evaluated.
final List<Element> elements = finder.evaluate().toList();
final images = <FlutterView, ui.Image>{};
final byteDatas = <FlutterView, ByteData>{};
// Collate all evaluations into a final evaluation, then return.
var result = const Evaluation.pass();
for (final element in elements) {
final FlutterView view = tester.viewOf(find.byElementPredicate((Element e) => e == element));
final RenderView renderView = tester.binding.renderViews.firstWhere(
(RenderView r) => r.flutterView == view,
);
final layer = renderView.debugLayer! as OffsetLayer;
final images = <FlutterView, ui.Image>{};
try {
// Compute elements to be evaluated.
final List<Element> elements = finder.evaluate().toList();
final byteDatas = <FlutterView, ByteData>{};
late final ui.Image image;
late final ByteData byteData;
// Obtain a previously rendered image or render one for a new view.
await tester.binding.runAsync(() async {
image = images[view] ??= await layer.toImage(
renderView.paintBounds,
// Needs to be the same pixel ratio otherwise our dimensions
// won't match the last transform layer.
pixelRatio: 1 / view.devicePixelRatio,
for (final element in elements) {
final FlutterView view = tester.viewOf(
find.byElementPredicate((Element e) => e == element),
);
byteData = byteDatas[view] ??= (await image.toByteData())!;
});
final RenderView renderView = tester.binding.renderViews.firstWhere(
(RenderView r) => r.flutterView == view,
);
final layer = renderView.debugLayer! as OffsetLayer;
result = result + _evaluateElement(element, byteData, image);
late final ui.Image image;
late final ByteData byteData;
// Obtain a previously rendered image or render one for a new view.
await tester.binding.runAsync(() async {
image = images[view] ??= await layer.toImage(
renderView.paintBounds,
// Needs to be the same pixel ratio otherwise our dimensions
// won't match the last transform layer.
pixelRatio: 1 / view.devicePixelRatio,
);
byteData = byteDatas[view] ??= (await image.toByteData())!;
});
result = result + _evaluateElement(element, byteData, image);
}
} finally {
for (final ui.Image image in images.values) {
image.dispose();
}
}
return result;
}

View File

@ -1841,6 +1841,36 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
}());
}
/// Replaces the root layers of all managed [renderViews] with new, unused
/// layers.
///
/// This method is typically used in a test's [addTearDown] to ensure that
/// resources associated with the root transform layer are properly disposed
/// of between tests. It is useful for tests that modify the view
/// configuration (for example, by calling [setSurfaceSize] or changing
/// [ViewConfiguration]). Such tests might cause the root layer to not be
/// automatically disposed before the leak check, triggering false negatives.
///
/// This method also resets view configuration to its default value.
///
/// See also:
///
/// * [setSurfaceSize], which often necessitates calling this method during
/// tear down.
Future<void> resetLayers() async {
await setSurfaceSize(null);
for (final RenderView renderView in renderViews) {
// To reliably trigger [RenderView.replaceRootLayer], we assign a new
// device pixel ratio that differs from the current one.
renderView.configuration = ViewConfiguration(
devicePixelRatio: renderView.flutterView.devicePixelRatio + 1.0,
);
// Reset the device pixel ratio (and other view configurations) to their
// default values.
renderView.configuration = createViewConfigurationFor(renderView);
}
}
/// Called by the [testWidgets] function after a test is executed.
void postTest() {
assert(inTest);

View File

@ -189,6 +189,8 @@ void main() {
testWidgets('Material text field - amber on amber', (WidgetTester tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
final controller = TextEditingController(text: 'this is a test');
addTearDown(controller.dispose);
await tester.pumpWidget(
_boilerplate(
Container(
@ -197,7 +199,7 @@ void main() {
color: Colors.amberAccent,
child: TextField(
style: const TextStyle(color: Colors.amber),
controller: TextEditingController(text: 'this is a test'),
controller: controller,
),
),
),
@ -236,13 +238,10 @@ void main() {
testWidgets('Material text field - default style', (WidgetTester tester) async {
final SemanticsHandle handle = tester.ensureSemantics();
final controller = TextEditingController(text: 'this is a test');
addTearDown(controller.dispose);
await tester.pumpWidget(
_boilerplate(
SizedBox(
width: 100,
child: TextField(controller: TextEditingController(text: 'this is a test')),
),
),
_boilerplate(SizedBox(width: 100, child: TextField(controller: controller))),
);
await tester.idle();
await expectLater(tester, meetsGuideline(textContrastGuideline));
@ -820,6 +819,8 @@ void main() {
});
testWidgets('Does not fail on links', (WidgetTester tester) async {
final recognizer = TapGestureRecognizer()..onTap = () {};
addTearDown(recognizer.dispose);
Widget textWithLink() {
return Builder(
builder: (BuildContext context) {
@ -827,7 +828,7 @@ void main() {
text: TextSpan(
children: <InlineSpan>[
const TextSpan(text: 'See examples at '),
TextSpan(text: 'flutter repo', recognizer: TapGestureRecognizer()..onTap = () {}),
TextSpan(text: 'flutter repo', recognizer: recognizer),
],
),
);
@ -844,6 +845,7 @@ void main() {
testWidgets('Tap size test can handle partially off-screen items', (WidgetTester tester) async {
final controller = ScrollController();
addTearDown(controller.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(

View File

@ -1254,10 +1254,12 @@ void main() {
});
testWidgets('showOnScreen sends showOnScreen action', (WidgetTester tester) async {
final controller = ScrollController(initialScrollOffset: 50);
addTearDown(controller.dispose);
await tester.pumpWidget(
MaterialApp(
home: ListView(
controller: ScrollController(initialScrollOffset: 50),
controller: controller,
children: <Widget>[
const MergeSemantics(child: SizedBox(height: 40, child: Text('Test'))),
SizedBox(width: 40, height: tester.binding.window.physicalSize.height * 1.5),
@ -1280,6 +1282,7 @@ void main() {
const text = 'This is some text.';
int currentIndex = text.length;
final controller = TextEditingController(text: text);
addTearDown(controller.dispose);
await tester.pumpWidget(
MaterialApp(
home: Material(child: TextField(controller: controller)),
@ -1330,6 +1333,7 @@ void main() {
const text = 'This is some text.';
int currentIndex = text.length;
final controller = TextEditingController(text: text);
addTearDown(controller.dispose);
await tester.pumpWidget(
MaterialApp(
home: Material(child: TextField(controller: controller)),
@ -1377,6 +1381,7 @@ void main() {
testWidgets('setText causes semantics to set the text', (WidgetTester tester) async {
const expectedText = 'This is some text.';
final controller = TextEditingController();
addTearDown(controller.dispose);
await tester.pumpWidget(
MaterialApp(
home: Material(child: TextField(controller: controller)),
@ -1399,6 +1404,7 @@ void main() {
const int expectedStart = text.length - 8;
const int expectedEnd = text.length - 4;
final controller = TextEditingController(text: text);
addTearDown(controller.dispose);
await tester.pumpWidget(
MaterialApp(
home: Material(child: TextField(controller: controller)),

View File

@ -17,6 +17,7 @@ void main() {
view: tester.view,
configuration: TestViewConfiguration.fromView(view: tester.view, size: const Size(400, 200)),
);
addTearDown(renderView.dispose);
// The configuration above defines a view with a resolution of 2400x1800
// physical pixels. With a device pixel ratio of 3x, this yields a

View File

@ -119,6 +119,7 @@ void main() {
});
testWidgets('can reset all values', (WidgetTester tester) async {
addTearDown(tester.binding.resetLayers);
final initial = DisplaySnapshot(tester.view.display);
tester.view.display.devicePixelRatio = 7;

View File

@ -569,6 +569,7 @@ void main() {
// Regression test for https://github.com/flutter/flutter/issues/133955.
final events = <RawKeyEvent>[];
final focusNode = FocusNode();
addTearDown(focusNode.dispose);
await tester.pumpWidget(
RawKeyboardListener(focusNode: focusNode, onKey: events.add, child: Container()),

View File

@ -16,11 +16,13 @@ const List<Widget> fooBarTexts = <Text>[
void main() {
group('image', () {
testWidgets('finds Image widgets', (WidgetTester tester) async {
addTearDown(imageCache.clear);
await tester.pumpWidget(_boilerplate(Image(image: FileImage(File('test')))));
expect(find.image(FileImage(File('test'))), findsOneWidget);
});
testWidgets('finds Button widgets with Image', (WidgetTester tester) async {
addTearDown(imageCache.clear);
await tester.pumpWidget(
_boilerplate(ElevatedButton(onPressed: null, child: Image(image: FileImage(File('test'))))),
);
@ -166,13 +168,11 @@ void main() {
});
testWidgets('finds EditableText widgets', (WidgetTester tester) async {
final controller = TextEditingController()..text = 'this is test';
addTearDown(controller.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: _boilerplate(
TextField(controller: TextEditingController()..text = 'this is test'),
),
),
home: Scaffold(body: _boilerplate(TextField(controller: controller))),
),
);
@ -780,11 +780,16 @@ void main() {
});
testWidgets('finds only static text for now', (WidgetTester tester) async {
final focusNode = FocusNode();
addTearDown(focusNode.dispose);
final controller = TextEditingController(text: 'text');
addTearDown(controller.dispose);
await tester.pumpWidget(
_boilerplate(
EditableText(
controller: TextEditingController(text: 'text'),
focusNode: FocusNode(),
controller: controller,
focusNode: focusNode,
style: const TextStyle(),
cursorColor: const Color(0x00000000),
backgroundCursorColor: const Color(0x00000000),
@ -826,6 +831,7 @@ void main() {
});
testWidgets('finds multiple subtypes', (WidgetTester tester) async {
addTearDown(imageCache.clear);
await tester.pumpWidget(
_boilerplate(
Row(
@ -1498,6 +1504,7 @@ void main() {
group('scrollable', () {
testWidgets('can find node that can scroll up', (WidgetTester tester) async {
final controller = ScrollController();
addTearDown(controller.dispose);
await tester.pumpWidget(
MaterialApp(
home: SingleChildScrollView(
@ -1515,6 +1522,7 @@ void main() {
testWidgets('can find node that can scroll down', (WidgetTester tester) async {
final controller = ScrollController(initialScrollOffset: 400);
addTearDown(controller.dispose);
await tester.pumpWidget(
MaterialApp(
home: SingleChildScrollView(
@ -1532,6 +1540,7 @@ void main() {
testWidgets('can find node that can scroll left', (WidgetTester tester) async {
final controller = ScrollController();
addTearDown(controller.dispose);
await tester.pumpWidget(
MaterialApp(
home: SingleChildScrollView(
@ -1550,6 +1559,7 @@ void main() {
testWidgets('can find node that can scroll right', (WidgetTester tester) async {
final controller = ScrollController(initialScrollOffset: 200);
addTearDown(controller.dispose);
await tester.pumpWidget(
MaterialApp(
home: SingleChildScrollView(

View File

@ -4,6 +4,7 @@
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
// This file is for testings that require a `LiveTestWidgetsFlutterBinding`
@ -73,6 +74,7 @@ void main() {
});
testWidgets('setSurfaceSize works', (WidgetTester tester) async {
addTearDown(binding.resetLayers);
await tester.pumpWidget(const MaterialApp(home: Center(child: Text('Test'))));
final Size windowCenter = tester.view.physicalSize / tester.view.devicePixelRatio / 2;
@ -128,6 +130,36 @@ void main() {
binding.shouldPropagateDevicePointerEvents = false;
},
);
testWidgets('resetLayers resets configuration and replaces root layer', (
WidgetTester tester,
) async {
// Ensure cleanup. This statement is not part of the test.
addTearDown(binding.resetLayers);
final ViewConfiguration currentConfig = binding.renderView.configuration;
await binding.setSurfaceSize(const Size(400, 400));
binding.renderView.configuration = const ViewConfiguration(devicePixelRatio: 10.0);
final Layer? currentRootLayer = binding.renderView.debugLayer;
await binding.resetLayers(); // This statement is the testee.
// Verify that all properties have been reset
expect(
binding.renderView.configuration.logicalConstraints,
equals(currentConfig.logicalConstraints),
);
expect(
binding.renderView.configuration.physicalConstraints,
equals(currentConfig.physicalConstraints),
);
expect(
binding.renderView.configuration.devicePixelRatio,
equals(currentConfig.devicePixelRatio),
);
// Verify that root layer has been replaced
expect(binding.renderView.debugLayer, isNot(same(currentRootLayer)));
});
}
/// A widget that shows the number of times it has been tapped.

View File

@ -16,18 +16,17 @@ void main() {
testWidgets('paints.circle is not affected by mutated colors', (WidgetTester tester) async {
final Key customPaintKey = UniqueKey();
final painter = _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawCircle(Offset.zero, 10.0, paint);
},
);
addTearDown(painter.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Center(
child: CustomPaint(
key: customPaintKey,
painter: _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawCircle(Offset.zero, 10.0, paint);
},
),
),
child: CustomPaint(key: customPaintKey, painter: painter),
),
),
),
@ -41,18 +40,17 @@ void main() {
testWidgets('paints.rect is not affected by mutated colors', (WidgetTester tester) async {
final Key customPaintKey = UniqueKey();
final painter = _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawRect(const Rect.fromLTRB(0.0, 0.0, 100.0, 100.0), paint);
},
);
addTearDown(painter.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Center(
child: CustomPaint(
key: customPaintKey,
painter: _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawRect(const Rect.fromLTRB(0.0, 0.0, 100.0, 100.0), paint);
},
),
),
child: CustomPaint(key: customPaintKey, painter: painter),
),
),
),
@ -67,19 +65,18 @@ void main() {
testWidgets('paints.drawRRect is not affected by mutated colors', (WidgetTester tester) async {
final Key customPaintKey = UniqueKey();
const rect = Rect.fromLTRB(0.0, 0.0, 100.0, 100.0);
final painter = _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawRRect(RRect.fromRectXY(rect, 4.0, 4.0), paint);
},
);
addTearDown(painter.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Center(
child: CustomPaint(
key: customPaintKey,
painter: _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawRRect(RRect.fromRectXY(rect, 4.0, 4.0), paint);
},
),
),
child: CustomPaint(key: customPaintKey, painter: painter),
),
),
),
@ -94,25 +91,20 @@ void main() {
testWidgets('paints.drawDRRect is not affected by mutated colors', (WidgetTester tester) async {
final Key customPaintKey = UniqueKey();
const rect = Rect.fromLTRB(0.0, 0.0, 100.0, 100.0);
final painter = _MutantPainter(
painter: (Canvas canvas, Paint paint) {
final rRect = RRect.fromRectXY(rect, 4.0, 4.0);
final innerRRect = RRect.fromRectXY(const Rect.fromLTRB(10.0, 10.0, 80.0, 80.0), 4.0, 4.0);
canvas.drawDRRect(rRect, innerRRect, paint);
},
);
addTearDown(painter.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Center(
child: CustomPaint(
key: customPaintKey,
painter: _MutantPainter(
painter: (Canvas canvas, Paint paint) {
final rRect = RRect.fromRectXY(rect, 4.0, 4.0);
final innerRRect = RRect.fromRectXY(
const Rect.fromLTRB(10.0, 10.0, 80.0, 80.0),
4.0,
4.0,
);
canvas.drawDRRect(rRect, innerRRect, paint);
},
),
),
child: CustomPaint(key: customPaintKey, painter: painter),
),
),
),
@ -126,19 +118,18 @@ void main() {
testWidgets('paints.drawPath is not affected by mutated colors', (WidgetTester tester) async {
final Key customPaintKey = UniqueKey();
final painter = _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawPath(Path(), paint);
},
);
addTearDown(painter.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Center(
child: CustomPaint(
key: customPaintKey,
painter: _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawPath(Path(), paint);
},
),
),
child: CustomPaint(key: customPaintKey, painter: painter),
),
),
),
@ -152,19 +143,18 @@ void main() {
testWidgets('paints.drawLine is not affected by mutated colors', (WidgetTester tester) async {
final Key customPaintKey = UniqueKey();
final painter = _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawLine(Offset.zero, const Offset(10.0, 10.0), paint);
},
);
addTearDown(painter.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Center(
child: CustomPaint(
key: customPaintKey,
painter: _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawLine(Offset.zero, const Offset(10.0, 10.0), paint);
},
),
),
child: CustomPaint(key: customPaintKey, painter: painter),
),
),
),
@ -178,20 +168,19 @@ void main() {
testWidgets('paints.drawArc is not affected by mutated colors', (WidgetTester tester) async {
final Key customPaintKey = UniqueKey();
final painter = _MutantPainter(
painter: (Canvas canvas, Paint paint) {
const rect = Rect.fromLTRB(0.0, 0.0, 100.0, 100.0);
canvas.drawArc(rect, 10.0, 10.0, true, paint);
},
);
addTearDown(painter.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Center(
child: CustomPaint(
key: customPaintKey,
painter: _MutantPainter(
painter: (Canvas canvas, Paint paint) {
const rect = Rect.fromLTRB(0.0, 0.0, 100.0, 100.0);
canvas.drawArc(rect, 10.0, 10.0, true, paint);
},
),
),
child: CustomPaint(key: customPaintKey, painter: painter),
),
),
),
@ -205,19 +194,18 @@ void main() {
testWidgets('paints.drawPaint is not affected by mutated colors', (WidgetTester tester) async {
final Key customPaintKey = UniqueKey();
final painter = _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawPaint(paint);
},
);
addTearDown(painter.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Center(
child: CustomPaint(
key: customPaintKey,
painter: _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawPaint(paint);
},
),
),
child: CustomPaint(key: customPaintKey, painter: painter),
),
),
),
@ -238,22 +226,21 @@ void main() {
WidgetTester tester,
) async {
final Key customPaintKey = UniqueKey();
final painter = _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawRSuperellipse(
RSuperellipse.fromLTRBR(0.0, 0.0, 100.0, 100.0, const Radius.circular(10.0)),
paint,
);
},
);
addTearDown(painter.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Center(
child: CustomPaint(
key: customPaintKey,
painter: _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawRSuperellipse(
RSuperellipse.fromLTRBR(0.0, 0.0, 100.0, 100.0, const Radius.circular(10.0)),
paint,
);
},
),
),
child: CustomPaint(key: customPaintKey, painter: painter),
),
),
),
@ -267,20 +254,19 @@ void main() {
testWidgets('paints.drawOval is not affected by mutated colors', (WidgetTester tester) async {
final Key customPaintKey = UniqueKey();
final painter = _MutantPainter(
painter: (Canvas canvas, Paint paint) {
const rect = Rect.fromLTRB(0.0, 0.0, 100.0, 100.0);
canvas.drawOval(rect, paint);
},
);
addTearDown(painter.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Center(
child: CustomPaint(
key: customPaintKey,
painter: _MutantPainter(
painter: (Canvas canvas, Paint paint) {
const rect = Rect.fromLTRB(0.0, 0.0, 100.0, 100.0);
canvas.drawOval(rect, paint);
},
),
),
child: CustomPaint(key: customPaintKey, painter: painter),
),
),
),
@ -299,18 +285,17 @@ void main() {
testWidgets('paints.image is not affected by mutated colors', (WidgetTester tester) async {
final Key customPaintKey = UniqueKey();
final painter = _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawImage(image, Offset.zero, paint);
},
);
addTearDown(painter.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Center(
child: CustomPaint(
key: customPaintKey,
painter: _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawImage(image, Offset.zero, paint);
},
),
),
child: CustomPaint(key: customPaintKey, painter: painter),
),
),
),
@ -332,19 +317,18 @@ void main() {
) async {
final Key customPaintKey = UniqueKey();
const rect = Rect.fromLTRB(0.0, 0.0, 100.0, 100.0);
final painter = _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawImageRect(image, rect, rect, paint);
},
);
addTearDown(painter.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Center(
child: CustomPaint(
key: customPaintKey,
painter: _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawImageRect(image, rect, rect, paint);
},
),
),
child: CustomPaint(key: customPaintKey, painter: painter),
),
),
),
@ -361,19 +345,18 @@ void main() {
) async {
final Key customPaintKey = UniqueKey();
const rect = Rect.fromLTRB(0.0, 0.0, 100.0, 100.0);
final painter = _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawImageNine(image, rect, rect, paint);
},
);
addTearDown(painter.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Center(
child: CustomPaint(
key: customPaintKey,
painter: _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawImageNine(image, rect, rect, paint);
},
),
),
child: CustomPaint(key: customPaintKey, painter: painter),
),
),
),
@ -392,22 +375,21 @@ void main() {
testWidgets('paints.drawPoints is not affected by mutated colors', (WidgetTester tester) async {
final Key customPaintKey = UniqueKey();
final painter = _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawPoints(ui.PointMode.lines, <Offset>[
Offset.zero,
const Offset(10.0, 10.0),
], paint);
},
);
addTearDown(painter.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Center(
child: CustomPaint(
key: customPaintKey,
painter: _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawPoints(ui.PointMode.lines, <Offset>[
Offset.zero,
const Offset(10.0, 10.0),
], paint);
},
),
),
child: CustomPaint(key: customPaintKey, painter: painter),
),
),
),
@ -428,23 +410,22 @@ void main() {
WidgetTester tester,
) async {
final Key customPaintKey = UniqueKey();
final painter = _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawRawPoints(
ui.PointMode.lines,
Float32List.fromList(<double>[0.0, 0.0, 10.0, 10.0]),
paint,
);
},
);
addTearDown(painter.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Center(
child: CustomPaint(
key: customPaintKey,
painter: _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawRawPoints(
ui.PointMode.lines,
Float32List.fromList(<double>[0.0, 0.0, 10.0, 10.0]),
paint,
);
},
),
),
child: CustomPaint(key: customPaintKey, painter: painter),
),
),
),
@ -463,27 +444,26 @@ void main() {
testWidgets('paints.drawVertices is not affected by mutated colors', (WidgetTester tester) async {
final Key customPaintKey = UniqueKey();
final painter = _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawVertices(
ui.Vertices(ui.VertexMode.triangles, <Offset>[
Offset.zero,
const Offset(0.0, 10.0),
const Offset(10.0, 10.0),
]),
BlendMode.src,
paint,
);
},
);
addTearDown(painter.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Center(
child: CustomPaint(
key: customPaintKey,
painter: _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawVertices(
ui.Vertices(ui.VertexMode.triangles, <Offset>[
Offset.zero,
const Offset(0.0, 10.0),
const Offset(10.0, 10.0),
]),
BlendMode.src,
paint,
);
},
),
),
child: CustomPaint(key: customPaintKey, painter: painter),
),
),
),
@ -502,27 +482,26 @@ void main() {
testWidgets('paints.drawAtlas is not affected by mutated colors', (WidgetTester tester) async {
final Key customPaintKey = UniqueKey();
final painter = _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawAtlas(
image,
<RSTransform>[],
<Rect>[],
<Color>[],
BlendMode.src,
const Rect.fromLTRB(0.0, 0.0, 100.0, 100.0),
paint,
);
},
);
addTearDown(painter.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Center(
child: CustomPaint(
key: customPaintKey,
painter: _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawAtlas(
image,
<RSTransform>[],
<Rect>[],
<Color>[],
BlendMode.src,
const Rect.fromLTRB(0.0, 0.0, 100.0, 100.0),
paint,
);
},
),
),
child: CustomPaint(key: customPaintKey, painter: painter),
),
),
),
@ -541,27 +520,26 @@ void main() {
testWidgets('paints.drawRawAtlas is not affected by mutated colors', (WidgetTester tester) async {
final Key customPaintKey = UniqueKey();
final painter = _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawRawAtlas(
image,
Float32List.fromList(<double>[]),
Float32List.fromList(<double>[]),
Int32List.fromList(<int>[]),
BlendMode.src,
const Rect.fromLTRB(0.0, 0.0, 100.0, 100.0),
paint,
);
},
);
addTearDown(painter.dispose);
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: Center(
child: CustomPaint(
key: customPaintKey,
painter: _MutantPainter(
painter: (Canvas canvas, Paint paint) {
canvas.drawRawAtlas(
image,
Float32List.fromList(<double>[]),
Float32List.fromList(<double>[]),
Int32List.fromList(<int>[]),
BlendMode.src,
const Rect.fromLTRB(0.0, 0.0, 100.0, 100.0),
paint,
);
},
),
),
child: CustomPaint(key: customPaintKey, painter: painter),
),
),
),

View File

@ -103,4 +103,11 @@ class _RestorableWidgetState extends State<_RestorableWidget> with RestorationMi
@override
String? get restorationId => widget.restorationId;
@override
void dispose() {
stringValue.dispose();
intValue.dispose();
super.dispose();
}
}

View File

@ -116,6 +116,7 @@ void main() {
final client = _PerformSelectorInputClient();
final focusNode = FocusNode();
addTearDown(focusNode.dispose);
await tester.pumpWidget(
Directionality(

View File

@ -292,6 +292,8 @@ void main() {
});
testWidgets('can clear out fake properties all at once', (WidgetTester tester) async {
addTearDown(tester.binding.resetLayers);
final initial = FlutterViewSnapshot(tester.view);
tester.view.devicePixelRatio = 7;

View File

@ -78,6 +78,8 @@ No widgets found at Offset(1.0, 1.0).
testWidgets('Should print message on pointer events with setSurfaceSize', (
WidgetTester tester,
) async {
addTearDown(tester.binding.resetLayers);
final printedMessages = <String?>[];
var invocations = 0;

View File

@ -13,6 +13,7 @@ import 'package:flutter/rendering.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
import 'package:matcher/expect.dart' as matcher;
import 'package:matcher/src/expect/async_matcher.dart';
@ -88,6 +89,7 @@ void main() {
int count;
final test = AnimationController(duration: const Duration(milliseconds: 5100), vsync: tester);
addTearDown(test.dispose);
count = await tester.pumpAndSettle(const Duration(seconds: 1));
expect(count, 1); // it always pumps at least one frame
@ -242,6 +244,7 @@ void main() {
duration: const Duration(seconds: 1),
vsync: const TestVSync(),
);
addTearDown(controller.dispose);
expect(tester.hasRunningAnimations, isFalse);
controller.forward();
expect(tester.hasRunningAnimations, isTrue);
@ -258,6 +261,7 @@ void main() {
duration: const Duration(minutes: 525600),
vsync: const TestVSync(),
);
addTearDown(controller.dispose);
expect(await tester.pumpAndSettle(), 1);
controller.forward();
try {
@ -522,6 +526,7 @@ void main() {
testWidgets('verifyTickersWereDisposed control test', (WidgetTester tester) async {
late FlutterError error;
final Ticker ticker = tester.createTicker((Duration duration) {});
addTearDown(ticker.dispose);
ticker.start();
try {
tester.verifyTickersWereDisposed('');
@ -792,19 +797,22 @@ void main() {
expect(find.byType(View), findsNothing);
});
testWidgets('passing a view to pumpWidget with wrapWithView: true throws', (
WidgetTester tester,
) async {
await tester.pumpWidget(View(view: FakeView(tester.view), child: const SizedBox.shrink()));
expect(
tester.takeException(),
isFlutterError.having(
(FlutterError e) => e.message,
'message',
contains('consider setting the "wrapWithView" parameter of that method to false'),
),
);
});
testWidgets(
'passing a view to pumpWidget with wrapWithView: true throws',
experimentalLeakTesting: LeakTesting.settings
.withIgnoredAll(), // leaking by design because of exception
(WidgetTester tester) async {
await tester.pumpWidget(View(view: FakeView(tester.view), child: const SizedBox.shrink()));
expect(
tester.takeException(),
isFlutterError.having(
(FlutterError e) => e.message,
'message',
contains('consider setting the "wrapWithView" parameter of that method to false'),
),
);
},
);
testWidgets('can pass a View to pumpWidget when wrapWithView: false', (
WidgetTester tester,