diff --git a/packages/flutter_markdown/test/header_test.dart b/packages/flutter_markdown/test/header_test.dart index 6bb1b6a518..22b1f4bbb3 100644 --- a/packages/flutter_markdown/test/header_test.dart +++ b/packages/flutter_markdown/test/header_test.dart @@ -17,9 +17,11 @@ void defineTests() { const String data = '# Header'; await tester.pumpWidget(boilerplate(const MarkdownBody(data: data))); - final Iterable widgets = tester.allWidgets; + final Iterable widgets = selfAndDescendantWidgetsOf( + find.byType(MarkdownBody), + tester, + ); expectWidgetTypes(widgets, [ - Directionality, MarkdownBody, Column, Wrap, diff --git a/packages/flutter_markdown/test/horizontal_rule_test.dart b/packages/flutter_markdown/test/horizontal_rule_test.dart index ff94672cea..380cb2f04b 100644 --- a/packages/flutter_markdown/test/horizontal_rule_test.dart +++ b/packages/flutter_markdown/test/horizontal_rule_test.dart @@ -17,9 +17,11 @@ void defineTests() { const String data = '---'; await tester.pumpWidget(boilerplate(const MarkdownBody(data: data))); - final Iterable widgets = tester.allWidgets; + final Iterable widgets = selfAndDescendantWidgetsOf( + find.byType(MarkdownBody), + tester, + ); expectWidgetTypes(widgets, [ - Directionality, MarkdownBody, Container, DecoratedBox, @@ -36,9 +38,11 @@ void defineTests() { const String data = '-----'; await tester.pumpWidget(boilerplate(const MarkdownBody(data: data))); - final Iterable widgets = tester.allWidgets; + final Iterable widgets = selfAndDescendantWidgetsOf( + find.byType(MarkdownBody), + tester, + ); expectWidgetTypes(widgets, [ - Directionality, MarkdownBody, Container, DecoratedBox, @@ -55,9 +59,11 @@ void defineTests() { const String data = '* * *'; await tester.pumpWidget(boilerplate(const MarkdownBody(data: data))); - final Iterable widgets = tester.allWidgets; + final Iterable widgets = selfAndDescendantWidgetsOf( + find.byType(MarkdownBody), + tester, + ); expectWidgetTypes(widgets, [ - Directionality, MarkdownBody, Container, DecoratedBox, @@ -74,9 +80,11 @@ void defineTests() { const String data = '# h1\n ## h2\n* * *'; await tester.pumpWidget(boilerplate(const MarkdownBody(data: data))); - final Iterable widgets = tester.allWidgets; + final Iterable widgets = selfAndDescendantWidgetsOf( + find.byType(MarkdownBody), + tester, + ); expectWidgetTypes(widgets, [ - Directionality, MarkdownBody, Column, Column, diff --git a/packages/flutter_markdown/test/scrollable_test.dart b/packages/flutter_markdown/test/scrollable_test.dart index a0cdfe94f4..429f0661a5 100644 --- a/packages/flutter_markdown/test/scrollable_test.dart +++ b/packages/flutter_markdown/test/scrollable_test.dart @@ -65,9 +65,11 @@ void defineTests() { ), ); - final List widgets = tester.allWidgets.toList(); - expectWidgetTypes(widgets.take(3), [ - Directionality, + final List widgets = selfAndDescendantWidgetsOf( + find.byType(Markdown), + tester, + ).toList(); + expectWidgetTypes(widgets.take(2), [ Markdown, ListView, ]); diff --git a/packages/flutter_markdown/test/style_sheet_test.dart b/packages/flutter_markdown/test/style_sheet_test.dart index 89f5e13cc3..0236e439dc 100644 --- a/packages/flutter_markdown/test/style_sheet_test.dart +++ b/packages/flutter_markdown/test/style_sheet_test.dart @@ -317,9 +317,11 @@ void defineTests() { styleSheet: style, ))); - final Iterable widgets = tester.allWidgets; + final Iterable widgets = selfAndDescendantWidgetsOf( + find.byType(MarkdownBody), + tester, + ); expectWidgetTypes(widgets, [ - Directionality, MarkdownBody, Column, Padding, diff --git a/packages/flutter_markdown/test/text_alignment_test.dart b/packages/flutter_markdown/test/text_alignment_test.dart index 1cacb34fdf..d0e777f7c7 100644 --- a/packages/flutter_markdown/test/text_alignment_test.dart +++ b/packages/flutter_markdown/test/text_alignment_test.dart @@ -32,9 +32,11 @@ void defineTests() { ), ); - final Iterable widgets = tester.allWidgets; + final Iterable widgets = selfAndDescendantWidgetsOf( + find.byType(MarkdownBody), + tester, + ); expectWidgetTypes(widgets, [ - Directionality, MarkdownBody, Column, Column, diff --git a/packages/flutter_markdown/test/text_test.dart b/packages/flutter_markdown/test/text_test.dart index 0cb1324703..b61a298d26 100644 --- a/packages/flutter_markdown/test/text_test.dart +++ b/packages/flutter_markdown/test/text_test.dart @@ -43,9 +43,14 @@ void defineTests() { ), ); - final Iterable widgets = tester.allWidgets; - expectWidgetTypes( - widgets, [Directionality, MarkdownBody, Column]); + final Iterable widgets = selfAndDescendantWidgetsOf( + find.byType(MarkdownBody), + tester, + ); + expectWidgetTypes(widgets, [ + MarkdownBody, + Column, + ]); }, ); @@ -58,9 +63,16 @@ void defineTests() { ), ); - final Iterable widgets = tester.allWidgets; - expectWidgetTypes(widgets, - [Directionality, MarkdownBody, Column, Wrap, RichText]); + final Iterable widgets = selfAndDescendantWidgetsOf( + find.byType(MarkdownBody), + tester, + ); + expectWidgetTypes(widgets, [ + MarkdownBody, + Column, + Wrap, + RichText, + ]); expectTextStrings(widgets, ['Hello']); }, ); @@ -77,9 +89,16 @@ void defineTests() { ), ); - final Iterable widgets = tester.allWidgets; - expectWidgetTypes(widgets, - [Directionality, MarkdownBody, Column, Wrap, RichText]); + final Iterable widgets = selfAndDescendantWidgetsOf( + find.byType(MarkdownBody), + tester, + ); + expectWidgetTypes(widgets, [ + MarkdownBody, + Column, + Wrap, + RichText, + ]); expectTextStrings(widgets, ['aaa bbb']); }); }); @@ -96,9 +115,12 @@ void defineTests() { ), ); - final Iterable widgets = tester.allWidgets; - expectWidgetTypes(widgets, - [Directionality, MarkdownBody, Column, Wrap, RichText]); + final Iterable widgets = selfAndDescendantWidgetsOf( + find.byType(MarkdownBody), + tester, + ); + expectWidgetTypes( + widgets, [MarkdownBody, Column, Wrap, RichText]); expectTextStrings(widgets, ['line 1\nline 2']); }, ); @@ -114,9 +136,12 @@ void defineTests() { ), ); - final Iterable widgets = tester.allWidgets; - expectWidgetTypes(widgets, - [Directionality, MarkdownBody, Column, Wrap, RichText]); + final Iterable widgets = selfAndDescendantWidgetsOf( + find.byType(MarkdownBody), + tester, + ); + expectWidgetTypes( + widgets, [MarkdownBody, Column, Wrap, RichText]); expectTextStrings(widgets, ['line 1\nline 2']); }, ); @@ -131,9 +156,16 @@ void defineTests() { ), ); - final Iterable widgets = tester.allWidgets; - expectWidgetTypes(widgets, - [Directionality, MarkdownBody, Column, Wrap, RichText]); + final Iterable widgets = selfAndDescendantWidgetsOf( + find.byType(MarkdownBody), + tester, + ); + expectWidgetTypes(widgets, [ + MarkdownBody, + Column, + Wrap, + RichText, + ]); expectTextStrings(widgets, ['line 1. line 2.']); }, ); @@ -148,9 +180,16 @@ void defineTests() { ), ); - final Iterable widgets = tester.allWidgets; - expectWidgetTypes(widgets, - [Directionality, MarkdownBody, Column, Wrap, RichText]); + final Iterable widgets = selfAndDescendantWidgetsOf( + find.byType(MarkdownBody), + tester, + ); + expectWidgetTypes(widgets, [ + MarkdownBody, + Column, + Wrap, + RichText, + ]); expectTextStrings(widgets, ['line 1. line 2.']); }, ); @@ -168,9 +207,12 @@ void defineTests() { ), ); - final Iterable widgets = tester.allWidgets; - expectWidgetTypes(widgets, - [Directionality, MarkdownBody, Column, Wrap, RichText]); + final Iterable widgets = selfAndDescendantWidgetsOf( + find.byType(MarkdownBody), + tester, + ); + expectWidgetTypes( + widgets, [MarkdownBody, Column, Wrap, RichText]); expectTextStrings(widgets, ['line 1.\nline 2.']); }, ); @@ -247,9 +289,16 @@ void defineTests() { ), ); - final Iterable widgets = tester.allWidgets; - expectWidgetTypes(widgets, - [Directionality, MarkdownBody, Column, Wrap, RichText]); + final Iterable widgets = selfAndDescendantWidgetsOf( + find.byType(MarkdownBody), + tester, + ); + expectWidgetTypes(widgets, [ + MarkdownBody, + Column, + Wrap, + RichText, + ]); expectTextStrings(widgets, ['strikethrough']); }); }); diff --git a/packages/flutter_markdown/test/utils.dart b/packages/flutter_markdown/test/utils.dart index 889b12729c..f28ef29ba9 100644 --- a/packages/flutter_markdown/test/utils.dart +++ b/packages/flutter_markdown/test/utils.dart @@ -18,6 +18,17 @@ final TextTheme textTheme = Typography.material2018() .black .merge(const TextTheme(bodyMedium: TextStyle(fontSize: 12.0))); +Iterable selfAndDescendantWidgetsOf(Finder start, WidgetTester tester) { + final Element startElement = tester.element(start); + final Iterable descendants = + collectAllElementsFrom(startElement, skipOffstage: false) + .map((Element e) => e.widget); + return [ + startElement.widget, + ...descendants, + ]; +} + void expectWidgetTypes(Iterable widgets, List expected) { final List actual = widgets.map((Widget w) => w.runtimeType).toList(); expect(actual, expected);