From 7df68b906ccacc5aed7e5d84fe533ae27f15a94d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=98=9F=E5=9B=94?= Date: Fri, 27 Aug 2021 13:11:02 +0800 Subject: [PATCH] feat: widget finders add widgetWithImage method (#89020) --- packages/flutter_test/lib/src/finders.dart | 24 ++++++++++++++++++++ packages/flutter_test/test/finders_test.dart | 7 ++++++ 2 files changed, 31 insertions(+) diff --git a/packages/flutter_test/lib/src/finders.dart b/packages/flutter_test/lib/src/finders.dart index 0972de4d1c..6ec09ec9a2 100644 --- a/packages/flutter_test/lib/src/finders.dart +++ b/packages/flutter_test/lib/src/finders.dart @@ -183,6 +183,30 @@ class CommonFinders { ); } + /// Looks for widgets that contain an [Image] descendant displaying [ImageProvider] + /// `image` in it. + /// + /// ## Sample code + /// + /// ```dart + /// // Suppose you have a button with image in it: + /// Button( + /// child: Image.file(filePath) + /// ) + /// + /// // You can find and tap on it like this: + /// tester.tap(find.widgetWithImage(Button, FileImage(filePath))); + /// ``` + /// + /// If the `skipOffstage` argument is true (the default), then this skips + /// nodes that are [Offstage] or that are from inactive [Route]s. + Finder widgetWithImage(Type widgetType, ImageProvider image, { bool skipOffstage = true }) { + return find.ancestor( + of: find.image(image), + matching: find.byType(widgetType), + ); + } + /// Finds widgets by searching for elements with a particular type. /// /// This does not do subclass tests, so for example diff --git a/packages/flutter_test/test/finders_test.dart b/packages/flutter_test/test/finders_test.dart index 2ce4abecd4..fc8923b6ed 100644 --- a/packages/flutter_test/test/finders_test.dart +++ b/packages/flutter_test/test/finders_test.dart @@ -16,6 +16,13 @@ void main() { )); expect(find.image(FileImage(File('test'), scale: 1.0)), findsOneWidget); }); + + testWidgets('finds Button widgets with Image', (WidgetTester tester) async { + await tester.pumpWidget(_boilerplate( + ElevatedButton(onPressed: null, child: Image(image: FileImage(File('test'), scale: 1.0)),) + )); + expect(find.widgetWithImage(ElevatedButton, FileImage(File('test'), scale: 1.0)), findsOneWidget); + }); }); group('text', () {