Add find.backButton finder and StandardComponentType enum to find components in tests. (#149349)
## Description This adds `find.backButton()` in the Common Finders to allow finding different types of standard UI elements. It works by attaching a key made from an enum value in a new enum called `StandardComponentType` to all of the standard widgets that perform the associated function. I also substituted the finder in several places where it is useful in tests. This allows writing tests that want to find the "back" button without having to know exactly which icon the back button uses under what circumstances. To do it correctly is actually quite complicated, since there are several adaptations that occur (based on platform, and whether it is web or not). ## Tests - Added tests.
This commit is contained in:
@@ -468,11 +468,77 @@ class CommonFinders {
|
||||
return _AncestorWidgetFinder(of, matching, matchLeaves: matchRoot);
|
||||
}
|
||||
|
||||
/// Finds a standard "back" button.
|
||||
///
|
||||
/// A common element on many user interfaces is the "back" button. This is the
|
||||
/// button which takes the user back to the previous page/screen/state.
|
||||
///
|
||||
/// It is useful in tests to be able to find these buttons, both for tapping
|
||||
/// them or verifying their existence, but because different platforms and
|
||||
/// locales have different icons representing them with different labels and
|
||||
/// tooltips, it's not desirable to have to look them up by these attributes.
|
||||
///
|
||||
/// This finder uses the [StandardComponentType] enum to look for buttons that
|
||||
/// have the key associated with [StandardComponentType.backButton]. If
|
||||
/// another widget is assigned that key, then it too will be considered an
|
||||
/// "official" back button in the widget tree, allowing this matcher to still
|
||||
/// find it even though it might use a different icon or tooltip.
|
||||
///
|
||||
/// ## Sample code
|
||||
///
|
||||
/// ```dart
|
||||
/// expect(find.backButton(), findsOneWidget);
|
||||
/// ```
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// * [StandardComponentType], the enum that enumerates components that are
|
||||
/// both common in user interfaces, but which also can vary slightly in
|
||||
/// presentation across different platforms, locales, and devices.
|
||||
/// * [BackButton], the Flutter Material widget that represents the back
|
||||
/// button.
|
||||
Finder backButton() {
|
||||
return byKey(StandardComponentType.backButton.key);
|
||||
}
|
||||
|
||||
/// Finds a standard "close" button.
|
||||
///
|
||||
/// A common element on many user interfaces is the "close" button. This is
|
||||
/// the button which closes or cancels whatever it is attached to.
|
||||
///
|
||||
/// It is useful in tests to be able to find these buttons, both for tapping
|
||||
/// them or verifying their existence, but because different platforms and
|
||||
/// locales have different icons representing them with different labels and
|
||||
/// tooltips, it's not desirable to have to look them up by these attributes.
|
||||
///
|
||||
/// This finder uses the [StandardComponentType] enum to look for buttons that
|
||||
/// have the key associated with [StandardComponentType.closeButton]. If
|
||||
/// another widget is assigned that key, then it too will be considered an
|
||||
/// "official" close button in the widget tree, allowing this matcher to still
|
||||
/// find it even though it might use a different icon or tooltip.
|
||||
///
|
||||
/// ## Sample code
|
||||
///
|
||||
/// ```dart
|
||||
/// expect(find.closeButton(), findsOneWidget);
|
||||
/// ```
|
||||
///
|
||||
/// See also:
|
||||
///
|
||||
/// * [StandardComponentType], the enum that enumerates components that are
|
||||
/// both common in user interfaces, but which also can vary slightly in
|
||||
/// presentation across different platforms, locales, and devices.
|
||||
/// * [CloseButton], the Flutter Material widget that represents a close
|
||||
/// button.
|
||||
Finder closeButton() {
|
||||
return byKey(StandardComponentType.closeButton.key);
|
||||
}
|
||||
|
||||
/// Finds [Semantics] widgets matching the given `label`, either by
|
||||
/// [RegExp.hasMatch] or string equality.
|
||||
///
|
||||
/// The framework may combine semantics labels in certain scenarios, such as
|
||||
/// when multiple [Text] widgets are in a [MaterialButton] widget. In such a
|
||||
/// when multiple [Text] widgets are in a [TextButton] widget. In such a
|
||||
/// case, it may be preferable to match by regular expression. Consumers of
|
||||
/// this API __must not__ introduce unsuitable content into the semantics tree
|
||||
/// for the purposes of testing; in particular, you should prefer matching by
|
||||
@@ -515,7 +581,6 @@ class CommonFinders {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Provides lightweight syntax for getting frequently used semantics finders.
|
||||
///
|
||||
/// This class is instantiated once, as [CommonFinders.semantics], under [find].
|
||||
|
||||
Reference in New Issue
Block a user