Skip to content

[flutter_test] Simulating hover should be easier #102754

Open

Description

@bdlukaa

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();

Activity

  1. added
    in triagePresently being triaged by the triage team
    a: tests"flutter test", flutter_test, or one of our tests
    c: new featureNothing broken; request for a new capability
    frameworkflutter/packages/flutter repository. See also f: labels.
    c: proposalA detailed proposal for a change to Flutter
    and removed
    in triagePresently being triaged by the triage team
    on Apr 29, 2022
  2. added
    P3Issues that are less important to the Flutter project
    on May 4, 2022
  3. jankuss commented on Feb 15, 2023

    @jankuss
    Contributor

    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 used TestGesture.
    Then the user could end the hover by calling gesture.removePointer() or moving the cursor to another location through gesture.moveTo()

    Example

    final gesture = await tester.hover(location: tester.getCenter(find.byType(MyWidget)));
    await gesture.removePointer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projecta: tests"flutter test", flutter_test, or one of our testsc: new featureNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to Flutterframeworkflutter/packages/flutter repository. See also f: labels.team-frameworkOwned by Framework teamtriaged-frameworkTriaged by Framework team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions