Adds SemanticsNode Finders for searching the semantics tree (#127137)
* Pulled `FinderBase` out of `Finder` * `FinderBase` can be used for any object, not just elements * Terminology was updated to be more "find" related * Re-implemented `Finder` using `FinderBase<Element>` * Backwards compatibility maintained with `_LegacyFinderMixin` * Introduced base classes for SemanticsNode finders * Introduced basic SemanticsNode finders through `find.semantics` * Updated some relevant matchers to make use of the more generic `FinderBase` Closes #123634 Closes #115874
This commit is contained in:
@@ -9,11 +9,11 @@ import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import 'all_elements.dart';
|
||||
import 'event_simulation.dart';
|
||||
import 'finders.dart';
|
||||
import 'test_async_utils.dart';
|
||||
import 'test_pointer.dart';
|
||||
import 'tree_traversal.dart';
|
||||
import 'window.dart';
|
||||
|
||||
/// The default drag touch slop used to break up a large drag into multiple
|
||||
@@ -74,7 +74,7 @@ class SemanticsController {
|
||||
///
|
||||
/// Will throw a [StateError] if the finder returns more than one element or
|
||||
/// if no semantics are found or are not enabled.
|
||||
SemanticsNode find(Finder finder) {
|
||||
SemanticsNode find(FinderBase<Element> finder) {
|
||||
TestAsyncUtils.guardSync();
|
||||
if (!_controller.binding.semanticsEnabled) {
|
||||
throw StateError('Semantics are not enabled.');
|
||||
@@ -149,7 +149,7 @@ class SemanticsController {
|
||||
/// parts of the traversal.
|
||||
/// * [orderedEquals], which can be given an [Iterable<Matcher>] to exactly
|
||||
/// match the order of the traversal.
|
||||
Iterable<SemanticsNode> simulatedAccessibilityTraversal({Finder? start, Finder? end, FlutterView? view}) {
|
||||
Iterable<SemanticsNode> simulatedAccessibilityTraversal({FinderBase<Element>? start, FinderBase<Element>? end, FlutterView? view}) {
|
||||
TestAsyncUtils.guardSync();
|
||||
FlutterView? startView;
|
||||
FlutterView? endView;
|
||||
@@ -158,7 +158,7 @@ class SemanticsController {
|
||||
if (view != null && startView != view) {
|
||||
throw StateError(
|
||||
'The start node is not part of the provided view.\n'
|
||||
'Finder: ${start.description}\n'
|
||||
'Finder: ${start.toString(describeSelf: true)}\n'
|
||||
'View of start node: $startView\n'
|
||||
'Specified view: $view'
|
||||
);
|
||||
@@ -169,7 +169,7 @@ class SemanticsController {
|
||||
if (view != null && endView != view) {
|
||||
throw StateError(
|
||||
'The end node is not part of the provided view.\n'
|
||||
'Finder: ${end.description}\n'
|
||||
'Finder: ${end.toString(describeSelf: true)}\n'
|
||||
'View of end node: $endView\n'
|
||||
'Specified view: $view'
|
||||
);
|
||||
@@ -178,8 +178,8 @@ class SemanticsController {
|
||||
if (endView != null && startView != null && endView != startView) {
|
||||
throw StateError(
|
||||
'The start and end node are in different views.\n'
|
||||
'Start finder: ${start!.description}\n'
|
||||
'End finder: ${end!.description}\n'
|
||||
'Start finder: ${start!.toString(describeSelf: true)}\n'
|
||||
'End finder: ${end!.toString(describeSelf: true)}\n'
|
||||
'View of start node: $startView\n'
|
||||
'View of end node: $endView'
|
||||
);
|
||||
@@ -200,7 +200,7 @@ class SemanticsController {
|
||||
if (startIndex == -1) {
|
||||
throw StateError(
|
||||
'The expected starting node was not found.\n'
|
||||
'Finder: ${start.description}\n\n'
|
||||
'Finder: ${start.toString(describeSelf: true)}\n\n'
|
||||
'Expected Start Node: $startNode\n\n'
|
||||
'Traversal: [\n ${traversal.join('\n ')}\n]');
|
||||
}
|
||||
@@ -212,7 +212,7 @@ class SemanticsController {
|
||||
if (endIndex == -1) {
|
||||
throw StateError(
|
||||
'The expected ending node was not found.\n'
|
||||
'Finder: ${end.description}\n\n'
|
||||
'Finder: ${end.toString(describeSelf: true)}\n\n'
|
||||
'Expected End Node: $endNode\n\n'
|
||||
'Traversal: [\n ${traversal.join('\n ')}\n]');
|
||||
}
|
||||
@@ -342,11 +342,11 @@ abstract class WidgetController {
|
||||
///
|
||||
/// * [view] which returns the [TestFlutterView] used when only a single
|
||||
/// view is being used.
|
||||
TestFlutterView viewOf(Finder finder) {
|
||||
TestFlutterView viewOf(FinderBase<Element> finder) {
|
||||
return _viewOf(finder) as TestFlutterView;
|
||||
}
|
||||
|
||||
FlutterView _viewOf(Finder finder) {
|
||||
FlutterView _viewOf(FinderBase<Element> finder) {
|
||||
return firstWidget<View>(
|
||||
find.ancestor(
|
||||
of: finder,
|
||||
@@ -356,7 +356,7 @@ abstract class WidgetController {
|
||||
}
|
||||
|
||||
/// Checks if `finder` exists in the tree.
|
||||
bool any(Finder finder) {
|
||||
bool any(FinderBase<Element> finder) {
|
||||
TestAsyncUtils.guardSync();
|
||||
return finder.evaluate().isNotEmpty;
|
||||
}
|
||||
@@ -377,7 +377,7 @@ abstract class WidgetController {
|
||||
///
|
||||
/// * Use [firstWidget] if you expect to match several widgets but only want the first.
|
||||
/// * Use [widgetList] if you expect to match several widgets and want all of them.
|
||||
T widget<T extends Widget>(Finder finder) {
|
||||
T widget<T extends Widget>(FinderBase<Element> finder) {
|
||||
TestAsyncUtils.guardSync();
|
||||
return finder.evaluate().single.widget as T;
|
||||
}
|
||||
@@ -388,7 +388,7 @@ abstract class WidgetController {
|
||||
/// Throws a [StateError] if `finder` is empty.
|
||||
///
|
||||
/// * Use [widget] if you only expect to match one widget.
|
||||
T firstWidget<T extends Widget>(Finder finder) {
|
||||
T firstWidget<T extends Widget>(FinderBase<Element> finder) {
|
||||
TestAsyncUtils.guardSync();
|
||||
return finder.evaluate().first.widget as T;
|
||||
}
|
||||
@@ -397,7 +397,7 @@ abstract class WidgetController {
|
||||
///
|
||||
/// * Use [widget] if you only expect to match one widget.
|
||||
/// * Use [firstWidget] if you expect to match several but only want the first.
|
||||
Iterable<T> widgetList<T extends Widget>(Finder finder) {
|
||||
Iterable<T> widgetList<T extends Widget>(FinderBase<Element> finder) {
|
||||
TestAsyncUtils.guardSync();
|
||||
return finder.evaluate().map<T>((Element element) {
|
||||
final T result = element.widget as T;
|
||||
@@ -408,7 +408,7 @@ abstract class WidgetController {
|
||||
/// Find all layers that are children of the provided [finder].
|
||||
///
|
||||
/// The [finder] must match exactly one element.
|
||||
Iterable<Layer> layerListOf(Finder finder) {
|
||||
Iterable<Layer> layerListOf(FinderBase<Element> finder) {
|
||||
TestAsyncUtils.guardSync();
|
||||
final Element element = finder.evaluate().single;
|
||||
final RenderObject object = element.renderObject!;
|
||||
@@ -437,7 +437,7 @@ abstract class WidgetController {
|
||||
///
|
||||
/// * Use [firstElement] if you expect to match several elements but only want the first.
|
||||
/// * Use [elementList] if you expect to match several elements and want all of them.
|
||||
T element<T extends Element>(Finder finder) {
|
||||
T element<T extends Element>(FinderBase<Element> finder) {
|
||||
TestAsyncUtils.guardSync();
|
||||
return finder.evaluate().single as T;
|
||||
}
|
||||
@@ -448,7 +448,7 @@ abstract class WidgetController {
|
||||
/// Throws a [StateError] if `finder` is empty.
|
||||
///
|
||||
/// * Use [element] if you only expect to match one element.
|
||||
T firstElement<T extends Element>(Finder finder) {
|
||||
T firstElement<T extends Element>(FinderBase<Element> finder) {
|
||||
TestAsyncUtils.guardSync();
|
||||
return finder.evaluate().first as T;
|
||||
}
|
||||
@@ -457,7 +457,7 @@ abstract class WidgetController {
|
||||
///
|
||||
/// * Use [element] if you only expect to match one element.
|
||||
/// * Use [firstElement] if you expect to match several but only want the first.
|
||||
Iterable<T> elementList<T extends Element>(Finder finder) {
|
||||
Iterable<T> elementList<T extends Element>(FinderBase<Element> finder) {
|
||||
TestAsyncUtils.guardSync();
|
||||
return finder.evaluate().cast<T>();
|
||||
}
|
||||
@@ -479,7 +479,7 @@ abstract class WidgetController {
|
||||
///
|
||||
/// * Use [firstState] if you expect to match several states but only want the first.
|
||||
/// * Use [stateList] if you expect to match several states and want all of them.
|
||||
T state<T extends State>(Finder finder) {
|
||||
T state<T extends State>(FinderBase<Element> finder) {
|
||||
TestAsyncUtils.guardSync();
|
||||
return _stateOf<T>(finder.evaluate().single, finder);
|
||||
}
|
||||
@@ -491,7 +491,7 @@ abstract class WidgetController {
|
||||
/// matching widget has no state.
|
||||
///
|
||||
/// * Use [state] if you only expect to match one state.
|
||||
T firstState<T extends State>(Finder finder) {
|
||||
T firstState<T extends State>(FinderBase<Element> finder) {
|
||||
TestAsyncUtils.guardSync();
|
||||
return _stateOf<T>(finder.evaluate().first, finder);
|
||||
}
|
||||
@@ -503,17 +503,17 @@ abstract class WidgetController {
|
||||
///
|
||||
/// * Use [state] if you only expect to match one state.
|
||||
/// * Use [firstState] if you expect to match several but only want the first.
|
||||
Iterable<T> stateList<T extends State>(Finder finder) {
|
||||
Iterable<T> stateList<T extends State>(FinderBase<Element> finder) {
|
||||
TestAsyncUtils.guardSync();
|
||||
return finder.evaluate().map<T>((Element element) => _stateOf<T>(element, finder));
|
||||
}
|
||||
|
||||
T _stateOf<T extends State>(Element element, Finder finder) {
|
||||
T _stateOf<T extends State>(Element element, FinderBase<Element> finder) {
|
||||
TestAsyncUtils.guardSync();
|
||||
if (element is StatefulElement) {
|
||||
return element.state as T;
|
||||
}
|
||||
throw StateError('Widget of type ${element.widget.runtimeType}, with ${finder.description}, is not a StatefulWidget.');
|
||||
throw StateError('Widget of type ${element.widget.runtimeType}, with ${finder.describeMatch(Plurality.many)}, is not a StatefulWidget.');
|
||||
}
|
||||
|
||||
/// Render objects of all the widgets currently in the widget tree
|
||||
@@ -535,7 +535,7 @@ abstract class WidgetController {
|
||||
///
|
||||
/// * Use [firstRenderObject] if you expect to match several render objects but only want the first.
|
||||
/// * Use [renderObjectList] if you expect to match several render objects and want all of them.
|
||||
T renderObject<T extends RenderObject>(Finder finder) {
|
||||
T renderObject<T extends RenderObject>(FinderBase<Element> finder) {
|
||||
TestAsyncUtils.guardSync();
|
||||
return finder.evaluate().single.renderObject! as T;
|
||||
}
|
||||
@@ -546,7 +546,7 @@ abstract class WidgetController {
|
||||
/// Throws a [StateError] if `finder` is empty.
|
||||
///
|
||||
/// * Use [renderObject] if you only expect to match one render object.
|
||||
T firstRenderObject<T extends RenderObject>(Finder finder) {
|
||||
T firstRenderObject<T extends RenderObject>(FinderBase<Element> finder) {
|
||||
TestAsyncUtils.guardSync();
|
||||
return finder.evaluate().first.renderObject! as T;
|
||||
}
|
||||
@@ -555,7 +555,7 @@ abstract class WidgetController {
|
||||
///
|
||||
/// * Use [renderObject] if you only expect to match one render object.
|
||||
/// * Use [firstRenderObject] if you expect to match several but only want the first.
|
||||
Iterable<T> renderObjectList<T extends RenderObject>(Finder finder) {
|
||||
Iterable<T> renderObjectList<T extends RenderObject>(FinderBase<Element> finder) {
|
||||
TestAsyncUtils.guardSync();
|
||||
return finder.evaluate().map<T>((Element element) {
|
||||
final T result = element.renderObject! as T;
|
||||
@@ -603,7 +603,7 @@ abstract class WidgetController {
|
||||
/// For example, a test that verifies that tapping a disabled button does not
|
||||
/// trigger the button would set `warnIfMissed` to false, because the button
|
||||
/// would ignore the tap.
|
||||
Future<void> tap(Finder finder, {int? pointer, int buttons = kPrimaryButton, bool warnIfMissed = true}) {
|
||||
Future<void> tap(FinderBase<Element> finder, {int? pointer, int buttons = kPrimaryButton, bool warnIfMissed = true}) {
|
||||
return tapAt(getCenter(finder, warnIfMissed: warnIfMissed, callee: 'tap'), pointer: pointer, buttons: buttons);
|
||||
}
|
||||
|
||||
@@ -628,7 +628,7 @@ abstract class WidgetController {
|
||||
/// * [tap], which presses and releases a pointer at the given location.
|
||||
/// * [longPress], which presses and releases a pointer with a gap in
|
||||
/// between long enough to trigger the long-press gesture.
|
||||
Future<TestGesture> press(Finder finder, {int? pointer, int buttons = kPrimaryButton, bool warnIfMissed = true}) {
|
||||
Future<TestGesture> press(FinderBase<Element> finder, {int? pointer, int buttons = kPrimaryButton, bool warnIfMissed = true}) {
|
||||
return TestAsyncUtils.guard<TestGesture>(() {
|
||||
return startGesture(getCenter(finder, warnIfMissed: warnIfMissed, callee: 'press'), pointer: pointer, buttons: buttons);
|
||||
});
|
||||
@@ -646,7 +646,7 @@ abstract class WidgetController {
|
||||
/// later verify that long-pressing the same location (using the same finder)
|
||||
/// has no effect (since the widget is now obscured), setting `warnIfMissed`
|
||||
/// to false on that second call.
|
||||
Future<void> longPress(Finder finder, {int? pointer, int buttons = kPrimaryButton, bool warnIfMissed = true}) {
|
||||
Future<void> longPress(FinderBase<Element> finder, {int? pointer, int buttons = kPrimaryButton, bool warnIfMissed = true}) {
|
||||
return longPressAt(getCenter(finder, warnIfMissed: warnIfMissed, callee: 'longPress'), pointer: pointer, buttons: buttons);
|
||||
}
|
||||
|
||||
@@ -707,7 +707,7 @@ abstract class WidgetController {
|
||||
/// A fling is essentially a drag that ends at a particular speed. If you
|
||||
/// just want to drag and end without a fling, use [drag].
|
||||
Future<void> fling(
|
||||
Finder finder,
|
||||
FinderBase<Element> finder,
|
||||
Offset offset,
|
||||
double speed, {
|
||||
int? pointer,
|
||||
@@ -787,7 +787,7 @@ abstract class WidgetController {
|
||||
/// A fling is essentially a drag that ends at a particular speed. If you
|
||||
/// just want to drag and end without a fling, use [drag].
|
||||
Future<void> trackpadFling(
|
||||
Finder finder,
|
||||
FinderBase<Element> finder,
|
||||
Offset offset,
|
||||
double speed, {
|
||||
int? pointer,
|
||||
@@ -952,7 +952,7 @@ abstract class WidgetController {
|
||||
/// should be left to their default values.
|
||||
/// {@endtemplate}
|
||||
Future<void> drag(
|
||||
Finder finder,
|
||||
FinderBase<Element> finder,
|
||||
Offset offset, {
|
||||
int? pointer,
|
||||
int buttons = kPrimaryButton,
|
||||
@@ -1085,7 +1085,7 @@ abstract class WidgetController {
|
||||
/// more accurate time control.
|
||||
/// {@endtemplate}
|
||||
Future<void> timedDrag(
|
||||
Finder finder,
|
||||
FinderBase<Element> finder,
|
||||
Offset offset,
|
||||
Duration duration, {
|
||||
int? pointer,
|
||||
@@ -1282,14 +1282,14 @@ abstract class WidgetController {
|
||||
/// this method is being called from another that is forwarding its own
|
||||
/// `warnIfMissed` parameter (see e.g. the implementation of [tap]).
|
||||
/// {@endtemplate}
|
||||
Offset getCenter(Finder finder, { bool warnIfMissed = false, String callee = 'getCenter' }) {
|
||||
Offset getCenter(FinderBase<Element> finder, { bool warnIfMissed = false, String callee = 'getCenter' }) {
|
||||
return _getElementPoint(finder, (Size size) => size.center(Offset.zero), warnIfMissed: warnIfMissed, callee: callee);
|
||||
}
|
||||
|
||||
/// Returns the point at the top left of the given widget.
|
||||
///
|
||||
/// {@macro flutter.flutter_test.WidgetController.getCenter.warnIfMissed}
|
||||
Offset getTopLeft(Finder finder, { bool warnIfMissed = false, String callee = 'getTopLeft' }) {
|
||||
Offset getTopLeft(FinderBase<Element> finder, { bool warnIfMissed = false, String callee = 'getTopLeft' }) {
|
||||
return _getElementPoint(finder, (Size size) => Offset.zero, warnIfMissed: warnIfMissed, callee: callee);
|
||||
}
|
||||
|
||||
@@ -1297,7 +1297,7 @@ abstract class WidgetController {
|
||||
/// point is not inside the object's hit test area.
|
||||
///
|
||||
/// {@macro flutter.flutter_test.WidgetController.getCenter.warnIfMissed}
|
||||
Offset getTopRight(Finder finder, { bool warnIfMissed = false, String callee = 'getTopRight' }) {
|
||||
Offset getTopRight(FinderBase<Element> finder, { bool warnIfMissed = false, String callee = 'getTopRight' }) {
|
||||
return _getElementPoint(finder, (Size size) => size.topRight(Offset.zero), warnIfMissed: warnIfMissed, callee: callee);
|
||||
}
|
||||
|
||||
@@ -1305,7 +1305,7 @@ abstract class WidgetController {
|
||||
/// point is not inside the object's hit test area.
|
||||
///
|
||||
/// {@macro flutter.flutter_test.WidgetController.getCenter.warnIfMissed}
|
||||
Offset getBottomLeft(Finder finder, { bool warnIfMissed = false, String callee = 'getBottomLeft' }) {
|
||||
Offset getBottomLeft(FinderBase<Element> finder, { bool warnIfMissed = false, String callee = 'getBottomLeft' }) {
|
||||
return _getElementPoint(finder, (Size size) => size.bottomLeft(Offset.zero), warnIfMissed: warnIfMissed, callee: callee);
|
||||
}
|
||||
|
||||
@@ -1313,7 +1313,7 @@ abstract class WidgetController {
|
||||
/// point is not inside the object's hit test area.
|
||||
///
|
||||
/// {@macro flutter.flutter_test.WidgetController.getCenter.warnIfMissed}
|
||||
Offset getBottomRight(Finder finder, { bool warnIfMissed = false, String callee = 'getBottomRight' }) {
|
||||
Offset getBottomRight(FinderBase<Element> finder, { bool warnIfMissed = false, String callee = 'getBottomRight' }) {
|
||||
return _getElementPoint(finder, (Size size) => size.bottomRight(Offset.zero), warnIfMissed: warnIfMissed, callee: callee);
|
||||
}
|
||||
|
||||
@@ -1340,7 +1340,7 @@ abstract class WidgetController {
|
||||
/// in the documentation for the [flutter_test] library.
|
||||
static bool hitTestWarningShouldBeFatal = false;
|
||||
|
||||
Offset _getElementPoint(Finder finder, Offset Function(Size size) sizeToPoint, { required bool warnIfMissed, required String callee }) {
|
||||
Offset _getElementPoint(FinderBase<Element> finder, Offset Function(Size size) sizeToPoint, { required bool warnIfMissed, required String callee }) {
|
||||
TestAsyncUtils.guardSync();
|
||||
final Iterable<Element> elements = finder.evaluate();
|
||||
if (elements.isEmpty) {
|
||||
@@ -1411,7 +1411,7 @@ abstract class WidgetController {
|
||||
|
||||
/// Returns the size of the given widget. This is only valid once
|
||||
/// the widget's render object has been laid out at least once.
|
||||
Size getSize(Finder finder) {
|
||||
Size getSize(FinderBase<Element> finder) {
|
||||
TestAsyncUtils.guardSync();
|
||||
final Element element = finder.evaluate().single;
|
||||
final RenderBox box = element.renderObject! as RenderBox;
|
||||
@@ -1579,7 +1579,7 @@ 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) => Rect.fromPoints(getTopLeft(finder), getBottomRight(finder));
|
||||
Rect getRect(FinderBase<Element> finder) => Rect.fromPoints(getTopLeft(finder), getBottomRight(finder));
|
||||
|
||||
/// Attempts to find the [SemanticsNode] of first result from `finder`.
|
||||
///
|
||||
@@ -1596,7 +1596,7 @@ abstract class WidgetController {
|
||||
/// Will throw a [StateError] if the finder returns more than one element or
|
||||
/// if no semantics are found or are not enabled.
|
||||
// TODO(pdblasi-google): Deprecate this and point references to semantics.find. See https://github.com/flutter/flutter/issues/112670.
|
||||
SemanticsNode getSemantics(Finder finder) => semantics.find(finder);
|
||||
SemanticsNode getSemantics(FinderBase<Element> finder) => semantics.find(finder);
|
||||
|
||||
/// Enable semantics in a test by creating a [SemanticsHandle].
|
||||
///
|
||||
@@ -1620,7 +1620,7 @@ abstract class WidgetController {
|
||||
///
|
||||
/// * [Scrollable.ensureVisible], which is the production API used to
|
||||
/// implement this method.
|
||||
Future<void> ensureVisible(Finder finder) => Scrollable.ensureVisible(element(finder));
|
||||
Future<void> ensureVisible(FinderBase<Element> finder) => Scrollable.ensureVisible(element(finder));
|
||||
|
||||
/// Repeatedly scrolls a [Scrollable] by `delta` in the
|
||||
/// [Scrollable.axisDirection] direction until a widget matching `finder` is
|
||||
@@ -1645,9 +1645,9 @@ abstract class WidgetController {
|
||||
///
|
||||
/// * [dragUntilVisible], which implements the body of this method.
|
||||
Future<void> scrollUntilVisible(
|
||||
Finder finder,
|
||||
FinderBase<Element> finder,
|
||||
double delta, {
|
||||
Finder? scrollable,
|
||||
FinderBase<Element>? scrollable,
|
||||
int maxScrolls = 50,
|
||||
Duration duration = const Duration(milliseconds: 50),
|
||||
}
|
||||
@@ -1688,8 +1688,8 @@ abstract class WidgetController {
|
||||
/// * [scrollUntilVisible], which wraps this method with an API that is more
|
||||
/// convenient when dealing with a [Scrollable].
|
||||
Future<void> dragUntilVisible(
|
||||
Finder finder,
|
||||
Finder view,
|
||||
FinderBase<Element> finder,
|
||||
FinderBase<Element> view,
|
||||
Offset moveStep, {
|
||||
int maxIteration = 50,
|
||||
Duration duration = const Duration(milliseconds: 50),
|
||||
|
||||
Reference in New Issue
Block a user