[flutter_test] perf: find.ancestor (#108868)

This commit is contained in:
Pascal Welsch 2022-08-03 23:59:05 +02:00 committed by GitHub
parent 7270c48bd7
commit 80679f0b42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 2 deletions

View File

@ -923,7 +923,8 @@ class _DescendantFinder extends Finder {
@override
Iterable<Element> apply(Iterable<Element> candidates) {
return candidates.where((Element element) => descendant.evaluate().contains(element));
final Iterable<Element> descendants = descendant.evaluate();
return candidates.where((Element element) => descendants.contains(element));
}
@override
@ -956,7 +957,8 @@ class _AncestorFinder extends Finder {
@override
Iterable<Element> apply(Iterable<Element> candidates) {
return candidates.where((Element element) => ancestor.evaluate().contains(element));
final Iterable<Element> ancestors = ancestor.evaluate();
return candidates.where((Element element) => ancestors.contains(element));
}
@override

View File

@ -361,6 +361,30 @@ void main() {
matchRoot: true,
), findsOneWidget);
});
testWidgets('is fast in deep tree', (WidgetTester tester) async {
await tester.pumpWidget(
Directionality(
textDirection: TextDirection.ltr,
child: _deepWidgetTree(
depth: 1000,
child: Row(
children: <Widget>[
_deepWidgetTree(
depth: 1000,
child: Column(children: fooBarTexts),
),
],
),
),
),
);
expect(find.ancestor(
of: find.text('bar'),
matching: find.byType(Row),
), findsOneWidget);
});
});
group('pageBack', () {
@ -854,3 +878,12 @@ class _AlwaysRepaint extends CustomPainter {
onPaint();
}
}
/// Wraps [child] in [depth] layers of [SizedBox]
Widget _deepWidgetTree({required int depth, required Widget child}) {
Widget tree = child;
for (int i = 0; i < depth; i += 1) {
tree = SizedBox(child: tree);
}
return tree;
}