Unverified Commit c9cf9c99 authored by Ming Lyu (CareF)'s avatar Ming Lyu (CareF) Committed by GitHub

Promote WidgetTester.ensureVisible to WidgetController (#61540)

* Promtoe WidgetTester.ensureVisible

* modify according to tvolkert@
parent 9e665e1d
......@@ -676,6 +676,20 @@ abstract class WidgetController {
/// Returns the rect of the given widget. This is only valid once
/// the widget's render object has been laid out at least once.
Rect getRect(Finder finder) => getTopLeft(finder) & getSize(finder);
/// Given a widget `W` specified by [finder] and a [Scrollable] widget `S` in
/// its ancestry tree, this scrolls `S` so as to make `W` visible.
///
/// Usually the `finder` for this method should be labeled
/// `skipOffstage: false`, so that [Finder] deals with widgets that's out of
/// the screen correctly.
///
/// This does not work when the `S` is long and `W` far away from the
/// dispalyed part does not have a cached element yet. See
/// https://github.com/flutter/flutter/issues/61458
///
/// Shorthand for `Scrollable.ensureVisible(element(finder))`
Future<void> ensureVisible(Finder finder) => Scrollable.ensureVisible(element(finder));
}
/// Variant of [WidgetController] that can be used in tests running
......
......@@ -1128,12 +1128,6 @@ class WidgetTester extends WidgetController implements HitTestDispatcher, Ticker
SemanticsHandle ensureSemantics() {
return binding.pipelineOwner.ensureSemantics();
}
/// Given a widget `W` specified by [finder] and a [Scrollable] widget `S` in
/// its ancestry tree, this scrolls `S` so as to make `W` visible.
///
/// Shorthand for `Scrollable.ensureVisible(tester.element(finder))`
Future<void> ensureVisible(Finder finder) => Scrollable.ensureVisible(element(finder));
}
typedef _TickerDisposeCallback = void Function(_TestTicker ticker);
......
......@@ -414,4 +414,29 @@ void main() {
}
},
);
testWidgets(
'ensureVisibl: scrolls to make widget visible',
(WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: ListView.builder(
itemCount: 20,
shrinkWrap: true,
itemBuilder: (BuildContext context, int i) => ListTile(title: Text('Item $i')),
),
),
),
);
// Make sure widget isn't on screen
expect(find.text('Item 15', skipOffstage: true), findsNothing);
await tester.ensureVisible(find.text('Item 15', skipOffstage: false));
await tester.pumpAndSettle();
expect(find.text('Item 15', skipOffstage: true), findsOneWidget);
},
);
}
......@@ -158,30 +158,6 @@ void main() {
});
});
group('ensureVisible', () {
testWidgets('scrolls to make widget visible', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: ListView.builder(
itemCount: 20,
shrinkWrap: true,
itemBuilder: (BuildContext context, int i) => ListTile(title: Text('Item $i')),
),
),
),
);
// Make sure widget isn't on screen
expect(find.text('Item 15', skipOffstage: true), findsNothing);
await tester.ensureVisible(find.text('Item 15', skipOffstage: false));
await tester.pumpAndSettle();
expect(find.text('Item 15', skipOffstage: true), findsOneWidget);
});
});
group('expectLater', () {
testWidgets('completes when matcher completes', (WidgetTester tester) async {
final Completer<void> completer = Completer<void>();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment