Add ancestor and descendant finders to Driver (#32410)

This commit is contained in:
Michael Goderbauer
2019-05-10 18:21:19 +02:00
committed by GitHub
parent 705143855f
commit b37c3be0fa
5 changed files with 355 additions and 0 deletions

View File

@@ -148,6 +148,8 @@ abstract class SerializableFinder {
case 'BySemanticsLabel': return BySemanticsLabel.deserialize(json);
case 'ByText': return ByText.deserialize(json);
case 'PageBack': return const PageBack();
case 'Descendant': return Descendant.deserialize(json);
case 'Ancestor': return Ancestor.deserialize(json);
}
throw DriverError('Unsupported search specification type $finderType');
}
@@ -317,6 +319,120 @@ class PageBack extends SerializableFinder {
String get finderType => 'PageBack';
}
/// A Flutter Driver finder that finds a descendant of [of] that matches
/// [matching].
///
/// If the `matchRoot` argument is true, then the widget specified by [of] will
/// be considered for a match. The argument defaults to false.
class Descendant extends SerializableFinder {
/// Creates a descendant finder.
const Descendant({
@required this.of,
@required this.matching,
this.matchRoot = false,
});
/// The finder specifying the widget of which the descendant is to be found.
final SerializableFinder of;
/// Only a descendant of [of] matching this finder will be found.
final SerializableFinder matching;
/// Whether the widget matching [of] will be considered for a match.
final bool matchRoot;
@override
String get finderType => 'Descendant';
@override
Map<String, String> serialize() {
return super.serialize()
..addAll(of.serialize().map((String key, String value) => MapEntry<String, String>('of_$key', value)))
..addAll(matching.serialize().map((String key, String value) => MapEntry<String, String>('matching_$key', value)))
..addAll(<String, String>{
'matchRoot': matchRoot ? 'true' : 'false',
});
}
/// Deserializes the finder from JSON generated by [serialize].
static Descendant deserialize(Map<String, String> json) {
final Map<String, String> of = <String, String>{};
final Map<String, String> matching = <String, String>{};
final Map<String, String> other = <String, String>{};
for (String key in json.keys) {
if (key.startsWith('of_')) {
of[key.substring('of_'.length)] = json[key];
} else if (key.startsWith('matching_')) {
matching[key.substring('matching_'.length)] = json[key];
} else {
other[key] = json[key];
}
}
return Descendant(
of: SerializableFinder.deserialize(of),
matching: SerializableFinder.deserialize(matching),
matchRoot: other['matchRoot'] == 'true',
);
}
}
/// A Flutter Driver finder that finds an ancestor of [of] that matches
/// [matching].
///
/// If the `matchRoot` argument is true, then the widget specified by [of] will
/// be considered for a match. The argument defaults to false.
class Ancestor extends SerializableFinder {
/// Creates an ancestor finder.
const Ancestor({
@required this.of,
@required this.matching,
this.matchRoot = false,
});
/// The finder specifying the widget of which the ancestor is to be found.
final SerializableFinder of;
/// Only an ancestor of [of] matching this finder will be found.
final SerializableFinder matching;
/// Whether the widget matching [of] will be considered for a match.
final bool matchRoot;
@override
String get finderType => 'Ancestor';
@override
Map<String, String> serialize() {
return super.serialize()
..addAll(of.serialize().map((String key, String value) => MapEntry<String, String>('of_$key', value)))
..addAll(matching.serialize().map((String key, String value) => MapEntry<String, String>('matching_$key', value)))
..addAll(<String, String>{
'matchRoot': matchRoot ? 'true' : 'false',
});
}
/// Deserializes the finder from JSON generated by [serialize].
static Ancestor deserialize(Map<String, String> json) {
final Map<String, String> of = <String, String>{};
final Map<String, String> matching = <String, String>{};
final Map<String, String> other = <String, String>{};
for (String key in json.keys) {
if (key.startsWith('of_')) {
of[key.substring('of_'.length)] = json[key];
} else if (key.startsWith('matching_')) {
matching[key.substring('matching_'.length)] = json[key];
} else {
other[key] = json[key];
}
}
return Ancestor(
of: SerializableFinder.deserialize(of),
matching: SerializableFinder.deserialize(matching),
matchRoot: other['matchRoot'] == 'true',
);
}
}
/// A Flutter driver command that retrieves a semantics id using a specified finder.
///
/// This command requires assertions to be enabled on the device.

View File

@@ -1017,6 +1017,28 @@ class CommonFinders {
/// Finds the back button on a Material or Cupertino page's scaffold.
SerializableFinder pageBack() => const PageBack();
/// Finds the widget that is an ancestor of the `of` parameter and that
/// matches the `matching` parameter.
///
/// If the `matchRoot` argument is true then the widget specified by `of` will
/// be considered for a match. The argument defaults to false.
SerializableFinder ancestor({
@required SerializableFinder of,
@required SerializableFinder matching,
bool matchRoot = false,
}) => Ancestor(of: of, matching: matching, matchRoot: matchRoot);
/// Finds the widget that is an descendant of the `of` parameter and that
/// matches the `matching` parameter.
///
/// If the `matchRoot` argument is true then the widget specified by `of` will
/// be considered for a match. The argument defaults to false.
SerializableFinder descendant({
@required SerializableFinder of,
@required SerializableFinder matching,
bool matchRoot = false,
}) => Descendant(of: of, matching: matching, matchRoot: matchRoot);
}
/// An immutable 2D floating-point offset used by Flutter Driver.

View File

@@ -142,6 +142,8 @@ class FlutterDriverExtension {
'ByValueKey': (SerializableFinder finder) => _createByValueKeyFinder(finder),
'ByType': (SerializableFinder finder) => _createByTypeFinder(finder),
'PageBack': (SerializableFinder finder) => _createPageBackFinder(),
'Ancestor': (SerializableFinder finder) => _createAncestorFinder(finder),
'Descendant': (SerializableFinder finder) => _createDescendantFinder(finder),
});
}
@@ -310,6 +312,22 @@ class FlutterDriverExtension {
}, description: 'Material or Cupertino back button');
}
Finder _createAncestorFinder(Ancestor arguments) {
return find.ancestor(
of: _createFinder(arguments.of),
matching: _createFinder(arguments.matching),
matchRoot: arguments.matchRoot,
);
}
Finder _createDescendantFinder(Descendant arguments) {
return find.descendant(
of: _createFinder(arguments.of),
matching: _createFinder(arguments.matching),
matchRoot: arguments.matchRoot,
);
}
Finder _createFinder(SerializableFinder finder) {
final FinderConstructor constructor = _finders[finder.finderType];