Repository navigation
Open
Labels
P3Issues that are less important to the Flutter projectIssues that are less important to the Flutter projecta: tests"flutter test", flutter_test, or one of our tests"flutter test", flutter_test, or one of our testsc: new featureNothing broken; request for a new capabilityNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to FlutterA detailed proposal for a change to Flutterframeworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.team-frameworkOwned by Framework teamOwned by Framework teamtriaged-frameworkTriaged by Framework teamTriaged by Framework team
Description
Use case
I'm trying to test a widget that changes color when hovering. Altough, it's not easy to do it with flutter_test. This is what I currently use:
testWidgets('MyWidget should be red by default', (WidgetTester tester) async {
await tester.pumpWidget(
MyWidget(),
);
expect(find.byWidgetPredicate((Widget widget) => widget is Container && widget.color == Colors.red), findsOneWidget);
expect(find.byWidgetPredicate((Widget widget) => widget is Container && widget.color == Colors.blue), findsNothing);
final gesture = await tester.createGesture(kind: PointerDeviceKind.mouse);
await gesture.addPointer(location: Offset.zero);
addTearDown(gesture.removePointer);
await tester.pump();
await gesture.moveTo(tester.getCenter(find.byType(MyWidget)));
await tester.pumpAndSettle();
expect(find.byWidgetPredicate((Widget widget) => widget is Container && widget.color == Colors.red), findsNothing);
expect(find.byWidgetPredicate((Widget widget) => widget is Container && widget.color == Colors.blue), findsOneWidget);
});Proposal
Add something like tester.tap, but for hover:
// Starts the hover
await tester.startHover(location: tester.getCenter(find.byType(MyWidget)));
// Do my tests
expect(.., ..);
// Ends the hover
await tester.endHover();Reacted by Tricked, Darren Baldwin, Sander in 't Hout, Kássio, Julian Rex, Bruno D'Luka, Luccas Clezar, Jan Oopkaup, Daniel Saier, Paulo Henrique Nonaka and 9 more
Metadata
Metadata
Assignees
Labels
P3Issues that are less important to the Flutter projectIssues that are less important to the Flutter projecta: tests"flutter test", flutter_test, or one of our tests"flutter test", flutter_test, or one of our testsc: new featureNothing broken; request for a new capabilityNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to FlutterA detailed proposal for a change to Flutterframeworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.team-frameworkOwned by Framework teamOwned by Framework teamtriaged-frameworkTriaged by Framework teamTriaged by Framework team
Activity
jankuss commentedon Feb 15, 2023
I also bumped into this, it feels unnecessarily verbose manually adding the pointers to test hovering. The code you would need to write is also not very self-explanatory. I wonder though how an API around this could look.
Idea
We could handle the creation and setup of the gesture in a
tester.hover()method, and return the internally usedTestGesture.Then the user could end the hover by calling
gesture.removePointer()or moving the cursor to another location throughgesture.moveTo()Example
feat(flutter_test): add startHover and startHoverAt helper to WidgetT…