WidgetTester enterText() and showKeyboard() can specify an EditableText ancestor (#9398)

This commit is contained in:
Hans Muller
2017-04-14 15:41:30 -07:00
committed by GitHub
parent dbfa747b37
commit 80a8c5629e
5 changed files with 92 additions and 40 deletions

View File

@@ -189,10 +189,13 @@ class CommonFinders {
/// of: find.widgetWithText(Row, 'label_1'), matching: find.text('value_1')
/// ), findsOneWidget);
///
/// If the [matchRoot] argument is true then the widget(s) specified by [of]
/// will be matched along with the descendants.
///
/// If the [skipOffstage] argument is true (the default), then nodes that are
/// [Offstage] or that are from inactive [Route]s are skipped.
Finder descendant({ Finder of, Finder matching, bool skipOffstage: true }) {
return new _DescendantFinder(of, matching, skipOffstage: skipOffstage);
Finder descendant({ Finder of, Finder matching, bool matchRoot: false, bool skipOffstage: true }) {
return new _DescendantFinder(of, matching, matchRoot: matchRoot, skipOffstage: skipOffstage);
}
}
@@ -488,13 +491,21 @@ class _ElementPredicateFinder extends MatchFinder {
}
class _DescendantFinder extends Finder {
_DescendantFinder(this.ancestor, this.descendant, { bool skipOffstage: true }) : super(skipOffstage: skipOffstage);
_DescendantFinder(this.ancestor, this.descendant, {
this.matchRoot: false,
bool skipOffstage: true,
}) : super(skipOffstage: skipOffstage);
final Finder ancestor;
final Finder descendant;
final bool matchRoot;
@override
String get description => '${descendant.description} that has ancestor(s) with ${ancestor.description} ';
String get description {
if (matchRoot)
return '${descendant.description} in the subtree(s) beginning with ${ancestor.description}';
return '${descendant.description} that has ancestor(s) with ${ancestor.description}';
}
@override
Iterable<Element> apply(Iterable<Element> candidates) {
@@ -503,8 +514,12 @@ class _DescendantFinder extends Finder {
@override
Iterable<Element> get allCandidates {
return ancestor.evaluate().expand(
final Iterable<Element> ancestorElements = ancestor.evaluate();
final List<Element> candidates = ancestorElements.expand(
(Element element) => collectAllElementsFrom(element, skipOffstage: skipOffstage)
).toSet().toList();
if (matchRoot)
candidates.insertAll(0, ancestorElements);
return candidates;
}
}

View File

@@ -436,19 +436,25 @@ class WidgetTester extends WidgetController implements HitTestDispatcher, Ticker
/// Returns the TestTextInput singleton.
///
/// Typical app tests will not need to use this value. To add text to widgets
/// like [Input] or [TextField], call [enterText].
/// like [TextField] or [FormTextField], call [enterText].
TestTextInput get testTextInput => binding.testTextInput;
/// Give the EditableText widget specified by [finder] the focus, as if the
/// Give the text input widget specified by [finder] the focus, as if the
/// onscreen keyboard had appeared.
///
/// Tests that just need to add text to widgets like [Input] or [TextField]
/// only need to call [enterText].
/// The widget specified by [finder] must be an [EditableText] or have
/// an [EditableText] descendant. For example `find.byType(TextField)`
/// or `find.byType(FormTextField)`, or `find.byType(EditableText)`.
///
/// Tests that just need to add text to widgets like [TextField]
/// or [FormTextField] only need to call [enterText].
Future<Null> showKeyboard(Finder finder) async {
return TestAsyncUtils.guard(() async {
// TODO(hansmuller): Once find.descendant (#7789) lands replace the following
// RHS with state(find.descendant(finder), find.byType(EditableText)).
final EditableTextState editable = state(finder);
final EditableTextState editable = state(find.descendant(
of: finder,
matching: find.byType(EditableText),
matchRoot: true,
));
if (editable != binding.focusedEditable) {
binding.focusedEditable = editable;
await pump();
@@ -456,8 +462,15 @@ class WidgetTester extends WidgetController implements HitTestDispatcher, Ticker
});
}
/// Give the EditableText widget specified by [finder] the focus and
/// Give the text input widget specified by [finder] the focus and
/// enter [text] as if it been provided by the onscreen keyboard.
///
/// The widget specified by [finder] must be an [EditableText] or have
/// an [EditableText] descendant. For example `find.byType(TextField)`
/// or `find.byType(FormTextField)`, or `find.byType(EditableText)`.
///
/// To just give [finder] the focus without entering any text,
/// see [showKeyboard].
Future<Null> enterText(Finder finder, String text) async {
return TestAsyncUtils.guard(() async {
await showKeyboard(finder);

View File

@@ -184,6 +184,30 @@ void main() {
contains('Actual: ?:<zero widgets with text "bar" that has ancestor(s) with type Column with text "foo"')
);
});
testWidgets('Root not matched by default', (WidgetTester tester) async {
await tester.pumpWidget(new Row(children: <Widget>[
new Column(children: <Text>[const Text('foo'), const Text('bar')])
]));
expect(find.descendant(
of: find.widgetWithText(Row, 'foo'),
matching: find.byType(Row),
), findsNothing);
});
testWidgets('Match the root', (WidgetTester tester) async {
await tester.pumpWidget(new Row(children: <Widget>[
new Column(children: <Text>[const Text('foo'), const Text('bar')])
]));
expect(find.descendant(
of: find.widgetWithText(Row, 'foo'),
matching: find.byType(Row),
matchRoot: true,
), findsOneWidget);
});
});
testWidgets('hasRunningAnimations control test', (WidgetTester tester) async {