From 0161fc7cafca3d31fecc8bfef5f7ec7425b8c0c0 Mon Sep 17 00:00:00 2001 From: Tong Mu Date: Thu, 22 Jan 2026 16:37:19 -0800 Subject: [PATCH] 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. [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 --- .../flutter_test/lib/src/accessibility.dart | 57 +-- packages/flutter_test/lib/src/binding.dart | 30 ++ .../flutter_test/test/accessibility_test.dart | 18 +- .../flutter_test/test/controller_test.dart | 8 +- .../test/coordinate_translation_test.dart | 1 + packages/flutter_test/test/display_test.dart | 1 + .../test/event_simulation_test.dart | 1 + packages/flutter_test/test/finders_test.dart | 24 +- .../flutter_test/test/live_binding_test.dart | 32 ++ .../test/recording_canvas_test.dart | 350 ++++++++---------- .../flutter_test/test/restoration_test.dart | 7 + .../test/test_text_input_test.dart | 1 + packages/flutter_test/test/view_test.dart | 2 + .../test/widget_tester_live_device_test.dart | 2 + .../flutter_test/test/widget_tester_test.dart | 34 +- 15 files changed, 328 insertions(+), 240 deletions(-) diff --git a/packages/flutter_test/lib/src/accessibility.dart b/packages/flutter_test/lib/src/accessibility.dart index 9a40da59e10..e91e662d6a3 100644 --- a/packages/flutter_test/lib/src/accessibility.dart +++ b/packages/flutter_test/lib/src/accessibility.dart @@ -596,37 +596,44 @@ class CustomMinimumContrastGuideline extends AccessibilityGuideline { @override Future evaluate(WidgetTester tester) async { - // Compute elements to be evaluated. - final List elements = finder.evaluate().toList(); - final images = {}; - final byteDatas = {}; - // 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 = {}; + try { + // Compute elements to be evaluated. + final List elements = finder.evaluate().toList(); + final byteDatas = {}; - 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; } diff --git a/packages/flutter_test/lib/src/binding.dart b/packages/flutter_test/lib/src/binding.dart index 1d9288b52d3..1dca5d33f02 100644 --- a/packages/flutter_test/lib/src/binding.dart +++ b/packages/flutter_test/lib/src/binding.dart @@ -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 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); diff --git a/packages/flutter_test/test/accessibility_test.dart b/packages/flutter_test/test/accessibility_test.dart index 8ea2a92a119..3beb38bfdd5 100644 --- a/packages/flutter_test/test/accessibility_test.dart +++ b/packages/flutter_test/test/accessibility_test.dart @@ -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: [ 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( diff --git a/packages/flutter_test/test/controller_test.dart b/packages/flutter_test/test/controller_test.dart index 434b61c382b..536375fcfac 100644 --- a/packages/flutter_test/test/controller_test.dart +++ b/packages/flutter_test/test/controller_test.dart @@ -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: [ 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)), diff --git a/packages/flutter_test/test/coordinate_translation_test.dart b/packages/flutter_test/test/coordinate_translation_test.dart index bc5416d03ec..ca10da7b4aa 100644 --- a/packages/flutter_test/test/coordinate_translation_test.dart +++ b/packages/flutter_test/test/coordinate_translation_test.dart @@ -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 diff --git a/packages/flutter_test/test/display_test.dart b/packages/flutter_test/test/display_test.dart index 81e3801468d..96023f09ecc 100644 --- a/packages/flutter_test/test/display_test.dart +++ b/packages/flutter_test/test/display_test.dart @@ -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; diff --git a/packages/flutter_test/test/event_simulation_test.dart b/packages/flutter_test/test/event_simulation_test.dart index 301dfba4d93..59d83611df6 100644 --- a/packages/flutter_test/test/event_simulation_test.dart +++ b/packages/flutter_test/test/event_simulation_test.dart @@ -569,6 +569,7 @@ void main() { // Regression test for https://github.com/flutter/flutter/issues/133955. final events = []; final focusNode = FocusNode(); + addTearDown(focusNode.dispose); await tester.pumpWidget( RawKeyboardListener(focusNode: focusNode, onKey: events.add, child: Container()), diff --git a/packages/flutter_test/test/finders_test.dart b/packages/flutter_test/test/finders_test.dart index 87c2941eff6..219304e1d0c 100644 --- a/packages/flutter_test/test/finders_test.dart +++ b/packages/flutter_test/test/finders_test.dart @@ -16,11 +16,13 @@ const List fooBarTexts = [ 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( diff --git a/packages/flutter_test/test/live_binding_test.dart b/packages/flutter_test/test/live_binding_test.dart index 62f9c10011a..b8e6e3586d4 100644 --- a/packages/flutter_test/test/live_binding_test.dart +++ b/packages/flutter_test/test/live_binding_test.dart @@ -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. diff --git a/packages/flutter_test/test/recording_canvas_test.dart b/packages/flutter_test/test/recording_canvas_test.dart index 93fb6fa4552..852cc6939b0 100644 --- a/packages/flutter_test/test/recording_canvas_test.dart +++ b/packages/flutter_test/test/recording_canvas_test.dart @@ -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.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.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([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([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.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.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, + [], + [], + [], + 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, - [], - [], - [], - 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([]), + Float32List.fromList([]), + Int32List.fromList([]), + 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([]), - Float32List.fromList([]), - Int32List.fromList([]), - BlendMode.src, - const Rect.fromLTRB(0.0, 0.0, 100.0, 100.0), - paint, - ); - }, - ), - ), + child: CustomPaint(key: customPaintKey, painter: painter), ), ), ), diff --git a/packages/flutter_test/test/restoration_test.dart b/packages/flutter_test/test/restoration_test.dart index fe3c13704bb..ffc20a7a217 100644 --- a/packages/flutter_test/test/restoration_test.dart +++ b/packages/flutter_test/test/restoration_test.dart @@ -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(); + } } diff --git a/packages/flutter_test/test/test_text_input_test.dart b/packages/flutter_test/test/test_text_input_test.dart index 59a14330be8..35ee62253b9 100644 --- a/packages/flutter_test/test/test_text_input_test.dart +++ b/packages/flutter_test/test/test_text_input_test.dart @@ -116,6 +116,7 @@ void main() { final client = _PerformSelectorInputClient(); final focusNode = FocusNode(); + addTearDown(focusNode.dispose); await tester.pumpWidget( Directionality( diff --git a/packages/flutter_test/test/view_test.dart b/packages/flutter_test/test/view_test.dart index 51de084e16e..e84f02d327d 100644 --- a/packages/flutter_test/test/view_test.dart +++ b/packages/flutter_test/test/view_test.dart @@ -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; diff --git a/packages/flutter_test/test/widget_tester_live_device_test.dart b/packages/flutter_test/test/widget_tester_live_device_test.dart index 6f28b21689b..936b541a478 100644 --- a/packages/flutter_test/test/widget_tester_live_device_test.dart +++ b/packages/flutter_test/test/widget_tester_live_device_test.dart @@ -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 = []; var invocations = 0; diff --git a/packages/flutter_test/test/widget_tester_test.dart b/packages/flutter_test/test/widget_tester_test.dart index 98fb167d63e..f4c0becf4a4 100644 --- a/packages/flutter_test/test/widget_tester_test.dart +++ b/packages/flutter_test/test/widget_tester_test.dart @@ -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,