1

I've wrapped up PageView widget inside a Listener widget. There I check in onPointerSignal property if pointerSignal is PointerScrollEvent to detect if the user used the mouse wheel to scroll on the widget. Then instead of doing the weird behaviour it produces I animateToPage and I set physics property of the PageView to NeverScrollableScrollPhysics() so it does not interfere with animateToPage transition.

That is working really cool! It jumps from one page to another with a smooth animation by moving mouse wheel.

But fun comes when testing the behaviour through widget testing. Does someone know how to emit/dispatch a PointerScrollEvent? I've tried the following without any success.

void main() {
  testWidgets(
    'when mouse wheel moved forward then next page is shown',
    (WidgetTester tester) async {
      await tester.pumpWidget(
        Test(
          child: PointerAwarePageView(
            children: [
              Text('1'),
              Text('2'),
            ],
          ),
        ),
      );

      expect(find.text('1'), findsOneWidget);

      tester.dispatchEvent(
        PointerScrollEvent(scrollDelta: Offset(0.0, 1.0)),
        HitTestResult(),
      );
      await tester.pump(Duration(seconds: 3));

      expect(find.text('2'), findsOneWidget);
    },
  );
}
2
  • Does it help you? github.com/flutter/flutter/blob/master/packages/flutter/test/… Commented Oct 17, 2020 at 19:59
  • @Akif unfortunately not, in that test they are not actually triggering the event. I need to know whcih specific function from WidgetTester allows me to fire a PointerScrollEvent Commented Oct 18, 2020 at 6:00

1 Answer 1

2

Finally, I found a way to do it. This flutter test inspired me: https://github.com/flutter/flutter/blob/master/packages/flutter/test/widgets/scrollable_test.dart#L316

We need to create a TestPointer with mouse device kind and perform scroll. Then send the event though WidgetTester.

I will write down here my working resulting test:

void main() {
  testWidgets(
    'when mouse wheel is scrolled then next page is shown',
    (WidgetTester tester) async {
      await tester.pumpWidget(
        Test(
          child: PointerAwarePageView(
            children: [
              Text('1'),
              Text('2'),
            ],
          ),
        ),
      );

      expect(find.text('1'), findsOneWidget);

      final Offset scrollEventLocation = tester.getCenter(find.byType(PointerAwarePageView));
      final TestPointer testPointer = TestPointer(1, PointerDeviceKind.mouse);
      testPointer.hover(scrollEventLocation);
      final HitTestResult result = tester.hitTestOnBinding(scrollEventLocation);
      await tester.sendEventToBinding(testPointer.scroll(const Offset(0.0, 1.0)), result);
      await tester.pumpAndSettle();

      expect(find.text('2'), findsOneWidget);
    },
  );
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.