Make markdown tests more resiliant (#2959)

diff --git a/packages/flutter_markdown/test/header_test.dart b/packages/flutter_markdown/test/header_test.dart
index 6bb1b6a..22b1f4b 100644
--- a/packages/flutter_markdown/test/header_test.dart
+++ b/packages/flutter_markdown/test/header_test.dart
@@ -17,9 +17,11 @@
         const String data = '# Header';
         await tester.pumpWidget(boilerplate(const MarkdownBody(data: data)));
 
-        final Iterable<Widget> widgets = tester.allWidgets;
+        final Iterable<Widget> widgets = selfAndDescendantWidgetsOf(
+          find.byType(MarkdownBody),
+          tester,
+        );
         expectWidgetTypes(widgets, <Type>[
-          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 ff94672..380cb2f 100644
--- a/packages/flutter_markdown/test/horizontal_rule_test.dart
+++ b/packages/flutter_markdown/test/horizontal_rule_test.dart
@@ -17,9 +17,11 @@
         const String data = '---';
         await tester.pumpWidget(boilerplate(const MarkdownBody(data: data)));
 
-        final Iterable<Widget> widgets = tester.allWidgets;
+        final Iterable<Widget> widgets = selfAndDescendantWidgetsOf(
+          find.byType(MarkdownBody),
+          tester,
+        );
         expectWidgetTypes(widgets, <Type>[
-          Directionality,
           MarkdownBody,
           Container,
           DecoratedBox,
@@ -36,9 +38,11 @@
         const String data = '-----';
         await tester.pumpWidget(boilerplate(const MarkdownBody(data: data)));
 
-        final Iterable<Widget> widgets = tester.allWidgets;
+        final Iterable<Widget> widgets = selfAndDescendantWidgetsOf(
+          find.byType(MarkdownBody),
+          tester,
+        );
         expectWidgetTypes(widgets, <Type>[
-          Directionality,
           MarkdownBody,
           Container,
           DecoratedBox,
@@ -55,9 +59,11 @@
         const String data = '* * *';
         await tester.pumpWidget(boilerplate(const MarkdownBody(data: data)));
 
-        final Iterable<Widget> widgets = tester.allWidgets;
+        final Iterable<Widget> widgets = selfAndDescendantWidgetsOf(
+          find.byType(MarkdownBody),
+          tester,
+        );
         expectWidgetTypes(widgets, <Type>[
-          Directionality,
           MarkdownBody,
           Container,
           DecoratedBox,
@@ -74,9 +80,11 @@
         const String data = '# h1\n ## h2\n* * *';
         await tester.pumpWidget(boilerplate(const MarkdownBody(data: data)));
 
-        final Iterable<Widget> widgets = tester.allWidgets;
+        final Iterable<Widget> widgets = selfAndDescendantWidgetsOf(
+          find.byType(MarkdownBody),
+          tester,
+        );
         expectWidgetTypes(widgets, <Type>[
-          Directionality,
           MarkdownBody,
           Column,
           Column,
diff --git a/packages/flutter_markdown/test/scrollable_test.dart b/packages/flutter_markdown/test/scrollable_test.dart
index a0cdfe9..429f066 100644
--- a/packages/flutter_markdown/test/scrollable_test.dart
+++ b/packages/flutter_markdown/test/scrollable_test.dart
@@ -65,9 +65,11 @@
           ),
         );
 
-        final List<Widget> widgets = tester.allWidgets.toList();
-        expectWidgetTypes(widgets.take(3), <Type>[
-          Directionality,
+        final List<Widget> widgets = selfAndDescendantWidgetsOf(
+          find.byType(Markdown),
+          tester,
+        ).toList();
+        expectWidgetTypes(widgets.take(2), <Type>[
           Markdown,
           ListView,
         ]);
diff --git a/packages/flutter_markdown/test/style_sheet_test.dart b/packages/flutter_markdown/test/style_sheet_test.dart
index 89f5e13..0236e43 100644
--- a/packages/flutter_markdown/test/style_sheet_test.dart
+++ b/packages/flutter_markdown/test/style_sheet_test.dart
@@ -317,9 +317,11 @@
           styleSheet: style,
         )));
 
-        final Iterable<Widget> widgets = tester.allWidgets;
+        final Iterable<Widget> widgets = selfAndDescendantWidgetsOf(
+          find.byType(MarkdownBody),
+          tester,
+        );
         expectWidgetTypes(widgets, <Type>[
-          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 1cacb34..d0e777f 100644
--- a/packages/flutter_markdown/test/text_alignment_test.dart
+++ b/packages/flutter_markdown/test/text_alignment_test.dart
@@ -32,9 +32,11 @@
           ),
         );
 
-        final Iterable<Widget> widgets = tester.allWidgets;
+        final Iterable<Widget> widgets = selfAndDescendantWidgetsOf(
+          find.byType(MarkdownBody),
+          tester,
+        );
         expectWidgetTypes(widgets, <Type>[
-          Directionality,
           MarkdownBody,
           Column,
           Column,
diff --git a/packages/flutter_markdown/test/text_test.dart b/packages/flutter_markdown/test/text_test.dart
index 0cb1324..b61a298 100644
--- a/packages/flutter_markdown/test/text_test.dart
+++ b/packages/flutter_markdown/test/text_test.dart
@@ -43,9 +43,14 @@
           ),
         );
 
-        final Iterable<Widget> widgets = tester.allWidgets;
-        expectWidgetTypes(
-            widgets, <Type>[Directionality, MarkdownBody, Column]);
+        final Iterable<Widget> widgets = selfAndDescendantWidgetsOf(
+          find.byType(MarkdownBody),
+          tester,
+        );
+        expectWidgetTypes(widgets, <Type>[
+          MarkdownBody,
+          Column,
+        ]);
       },
     );
 
@@ -58,9 +63,16 @@
           ),
         );
 
-        final Iterable<Widget> widgets = tester.allWidgets;
-        expectWidgetTypes(widgets,
-            <Type>[Directionality, MarkdownBody, Column, Wrap, RichText]);
+        final Iterable<Widget> widgets = selfAndDescendantWidgetsOf(
+          find.byType(MarkdownBody),
+          tester,
+        );
+        expectWidgetTypes(widgets, <Type>[
+          MarkdownBody,
+          Column,
+          Wrap,
+          RichText,
+        ]);
         expectTextStrings(widgets, <String>['Hello']);
       },
     );
@@ -77,9 +89,16 @@
         ),
       );
 
-      final Iterable<Widget> widgets = tester.allWidgets;
-      expectWidgetTypes(widgets,
-          <Type>[Directionality, MarkdownBody, Column, Wrap, RichText]);
+      final Iterable<Widget> widgets = selfAndDescendantWidgetsOf(
+        find.byType(MarkdownBody),
+        tester,
+      );
+      expectWidgetTypes(widgets, <Type>[
+        MarkdownBody,
+        Column,
+        Wrap,
+        RichText,
+      ]);
       expectTextStrings(widgets, <String>['aaa bbb']);
     });
   });
@@ -96,9 +115,12 @@
           ),
         );
 
-        final Iterable<Widget> widgets = tester.allWidgets;
-        expectWidgetTypes(widgets,
-            <Type>[Directionality, MarkdownBody, Column, Wrap, RichText]);
+        final Iterable<Widget> widgets = selfAndDescendantWidgetsOf(
+          find.byType(MarkdownBody),
+          tester,
+        );
+        expectWidgetTypes(
+            widgets, <Type>[MarkdownBody, Column, Wrap, RichText]);
         expectTextStrings(widgets, <String>['line 1\nline 2']);
       },
     );
@@ -114,9 +136,12 @@
           ),
         );
 
-        final Iterable<Widget> widgets = tester.allWidgets;
-        expectWidgetTypes(widgets,
-            <Type>[Directionality, MarkdownBody, Column, Wrap, RichText]);
+        final Iterable<Widget> widgets = selfAndDescendantWidgetsOf(
+          find.byType(MarkdownBody),
+          tester,
+        );
+        expectWidgetTypes(
+            widgets, <Type>[MarkdownBody, Column, Wrap, RichText]);
         expectTextStrings(widgets, <String>['line 1\nline 2']);
       },
     );
@@ -131,9 +156,16 @@
           ),
         );
 
-        final Iterable<Widget> widgets = tester.allWidgets;
-        expectWidgetTypes(widgets,
-            <Type>[Directionality, MarkdownBody, Column, Wrap, RichText]);
+        final Iterable<Widget> widgets = selfAndDescendantWidgetsOf(
+          find.byType(MarkdownBody),
+          tester,
+        );
+        expectWidgetTypes(widgets, <Type>[
+          MarkdownBody,
+          Column,
+          Wrap,
+          RichText,
+        ]);
         expectTextStrings(widgets, <String>['line 1. line 2.']);
       },
     );
@@ -148,9 +180,16 @@
           ),
         );
 
-        final Iterable<Widget> widgets = tester.allWidgets;
-        expectWidgetTypes(widgets,
-            <Type>[Directionality, MarkdownBody, Column, Wrap, RichText]);
+        final Iterable<Widget> widgets = selfAndDescendantWidgetsOf(
+          find.byType(MarkdownBody),
+          tester,
+        );
+        expectWidgetTypes(widgets, <Type>[
+          MarkdownBody,
+          Column,
+          Wrap,
+          RichText,
+        ]);
         expectTextStrings(widgets, <String>['line 1. line 2.']);
       },
     );
@@ -168,9 +207,12 @@
           ),
         );
 
-        final Iterable<Widget> widgets = tester.allWidgets;
-        expectWidgetTypes(widgets,
-            <Type>[Directionality, MarkdownBody, Column, Wrap, RichText]);
+        final Iterable<Widget> widgets = selfAndDescendantWidgetsOf(
+          find.byType(MarkdownBody),
+          tester,
+        );
+        expectWidgetTypes(
+            widgets, <Type>[MarkdownBody, Column, Wrap, RichText]);
         expectTextStrings(widgets, <String>['line 1.\nline 2.']);
       },
     );
@@ -247,9 +289,16 @@
         ),
       );
 
-      final Iterable<Widget> widgets = tester.allWidgets;
-      expectWidgetTypes(widgets,
-          <Type>[Directionality, MarkdownBody, Column, Wrap, RichText]);
+      final Iterable<Widget> widgets = selfAndDescendantWidgetsOf(
+        find.byType(MarkdownBody),
+        tester,
+      );
+      expectWidgetTypes(widgets, <Type>[
+        MarkdownBody,
+        Column,
+        Wrap,
+        RichText,
+      ]);
       expectTextStrings(widgets, <String>['strikethrough']);
     });
   });
diff --git a/packages/flutter_markdown/test/utils.dart b/packages/flutter_markdown/test/utils.dart
index 889b127..f28ef29 100644
--- a/packages/flutter_markdown/test/utils.dart
+++ b/packages/flutter_markdown/test/utils.dart
@@ -18,6 +18,17 @@
     .black
     .merge(const TextTheme(bodyMedium: TextStyle(fontSize: 12.0)));
 
+Iterable<Widget> selfAndDescendantWidgetsOf(Finder start, WidgetTester tester) {
+  final Element startElement = tester.element(start);
+  final Iterable<Widget> descendants =
+      collectAllElementsFrom(startElement, skipOffstage: false)
+          .map((Element e) => e.widget);
+  return <Widget>[
+    startElement.widget,
+    ...descendants,
+  ];
+}
+
 void expectWidgetTypes(Iterable<Widget> widgets, List<Type> expected) {
   final List<Type> actual = widgets.map((Widget w) => w.runtimeType).toList();
   expect(actual, expected);