From d0e03173bdd7a7d28be9998491398b1890887ce7 Mon Sep 17 00:00:00 2001 From: Taha Tesser Date: Tue, 21 Jan 2025 10:41:24 +0200 Subject: [PATCH] Update `ListTile` test to prevent log dump and test error message. (#161811) Fixes [`ListTile` widget tests dump logs in Flutter CI](https://github.com/flutter/flutter/issues/161810) ### Before Screenshot 2025-01-17 at 17 37 30 ### After Screenshot 2025-01-17 at 17 36 50 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [x] 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. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [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/material/list_tile_test.dart | 54 +++++++++++++++++-- 1 file changed, 50 insertions(+), 4 deletions(-) diff --git a/packages/flutter/test/material/list_tile_test.dart b/packages/flutter/test/material/list_tile_test.dart index ab5fb5313a..1fb1aaadc9 100644 --- a/packages/flutter/test/material/list_tile_test.dart +++ b/packages/flutter/test/material/list_tile_test.dart @@ -2621,6 +2621,12 @@ void main() { testWidgets('Leading/Trailing exceeding list tile width throws exception', ( WidgetTester tester, ) async { + List exceptions = []; + FlutterExceptionHandler? oldHandler = FlutterError.onError; + FlutterError.onError = (FlutterErrorDetails details) { + exceptions.add(details.exception); + }; + Widget buildListTile({Widget? leading, Widget? trailing}) { return MaterialApp( home: Material( @@ -2636,16 +2642,56 @@ void main() { // List tile width is 100 as a result, an exception should be thrown. await tester.pumpWidget(buildListTile(leading: const SizedBox(width: 61))); - // Error message cannot be tested as there too many errors thrown. - expect(tester.takeException(), isNotNull); + FlutterError.onError = oldHandler; + expect(exceptions.first.runtimeType, FlutterError); + FlutterError error = exceptions.first as FlutterError; + expect(error.diagnostics.length, 3); + expect( + error.diagnostics[0].toStringDeep(), + 'Leading widget consumes the entire tile width (including\nListTile.contentPadding).\n', + ); + expect( + error.diagnostics[1].toStringDeep(), + 'Either resize the tile width so that the leading widget plus any\n' + 'content padding do not exceed the tile width, or use a sized\n' + 'widget, or consider replacing ListTile with a custom widget.\n', + ); + expect( + error.diagnostics[2].toStringDeep(), + 'See also:\n' + 'https://api.flutter.dev/flutter/material/ListTile-class.html#material.ListTile.4\n', + ); + + exceptions = []; + oldHandler = FlutterError.onError; + FlutterError.onError = (FlutterErrorDetails details) { + exceptions.add(details.exception); + }; // Test a trailing widget that exceeds the list tile width. // 16 (content padding) + 61 (trailing width) + 24 (content padding) = 101. // List tile width is 100 as a result, an exception should be thrown. await tester.pumpWidget(buildListTile(trailing: const SizedBox(width: 61))); - // Error message cannot tested be as there too many errors thrown. - expect(tester.takeException(), isNotNull); + FlutterError.onError = oldHandler; + expect(exceptions.first.runtimeType, FlutterError); + error = exceptions.first as FlutterError; + expect(error.diagnostics.length, 3); + expect( + error.diagnostics[0].toStringDeep(), + 'Trailing widget consumes the entire tile width (including\nListTile.contentPadding).\n', + ); + expect( + error.diagnostics[1].toStringDeep(), + 'Either resize the tile width so that the trailing widget plus any\n' + 'content padding do not exceed the tile width, or use a sized\n' + 'widget, or consider replacing ListTile with a custom widget.\n', + ); + expect( + error.diagnostics[2].toStringDeep(), + 'See also:\n' + 'https://api.flutter.dev/flutter/material/ListTile-class.html#material.ListTile.4\n', + ); }); group('Material 2', () {