如何在flutter测试中模拟onDoubleTap

Val*_*nal 4 gesturedetector dart flutter flutter-test single-vs-double-tap

我正在尝试编写一个颤振测试并模拟双选项卡。但我无法找到办法。

这是我现在所做的:

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();
  });
}
Run Code Online (Sandbox Code Playgroud)

当我运行测试时,我得到的是:

00:03 +1: All tests passed!                                                                                              
Run Code Online (Sandbox Code Playgroud)

double tapped但我在控制台中没有看到任何内容。


如何触发双击?

Val*_*nal 7

解决方案是在两次点击之间等待kDoubleTapMinTime

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();
  });
}
Run Code Online (Sandbox Code Playgroud)

double tapped我在控制台中得到:

double tapped
00:03 +1: All tests passed!
Run Code Online (Sandbox Code Playgroud)