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', () {