How to simulate onDoubleTap in flutter test

Issue

I’m trying to write a flutter test and simulate a double tab. But I cannot manage to find a way.

Here is what I have done for now:

void main() {
  testWidgets('It should trigger onDoubleTap', (tester) async {
    await tester.pumpWidget(MaterialApp(
      home: GestureDetector(
        child: const Text('button'),
        onDoubleTap: () {
          print('double tapped');
        },
      ),
    ));

    await tester.pumpAndSettle();
    await tester.tap(find.text('button')); // <- Tried with tester.press too
    await tester.tap(find.text('button')); // <- Tried with tester.press too
    await tester.pumpAndSettle();
  });
}

When I run the test, this is was I get:

00:03 +1: All tests passed!                                                                                              

But I don’t see any double tapped in the console.


How can I trigger the double-tap?

Solution

The solution is to wait kDoubleTapMinTime between both taps.

void main() {
  testWidgets('It should trigger onDoubleTap', (tester) async {
    await tester.pumpWidget(MaterialApp(
      home: GestureDetector(
        child: const Text('button'),
        onDoubleTap: () {
          print('double tapped');
        },
      ),
    ));

    await tester.pumpAndSettle();
    await tester.tap(find.text('button'));
    await tester.pump(kDoubleTapMinTime); // <- Add this
    await tester.tap(find.text('button'));
    await tester.pumpAndSettle();
  });
}

I get the double tapped in the console:

double tapped
00:03 +1: All tests passed!

Answered By – Valentin Vignal

Answer Checked By – Cary Denson (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *