import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/ui/layout/wheel_scroll_forwarding.dart'; Future _wheel( WidgetTester tester, Offset at, Offset delta, { PointerDeviceKind kind = PointerDeviceKind.trackpad, }) async { final pointer = TestPointer(1, kind); await tester.sendEventToBinding(pointer.hover(at)); await tester.sendEventToBinding(pointer.scroll(delta)); await tester.pump(); } void main() { final list = ScrollController(); tearDownAll(list.dispose); // A vertical list holding one wide horizontally scrolling row, under a // floating button that is not part of the list. Widget page({bool passThrough = true}) => MaterialApp( home: Scaffold( body: VerticalWheelForwarder( controller: list, child: Stack( children: [ ListView( controller: list, children: [ const SizedBox(height: 200), SizedBox( height: 60, child: passThrough ? const HorizontalScrollView( key: Key('wide'), child: SizedBox(width: 3000, height: 60), ) : const SingleChildScrollView( key: Key('wide'), scrollDirection: Axis.horizontal, child: SizedBox(width: 3000, height: 60), ), ), const SizedBox(height: 2000), ], ), Positioned( right: 8, bottom: 8, child: FilledButton( key: const Key('floating'), onPressed: () {}, child: const Text('Floating'), ), ), ], ), ), ), ); ScrollPosition sideways(WidgetTester tester) => tester .state( find.descendant( of: find.byKey(const Key('wide')), matching: find.byType(Scrollable), ), ) .position; testWidgets('a mostly vertical trackpad gesture over a wide row scrolls ' 'the list', (tester) async { await tester.pumpWidget(page()); final at = tester.getCenter(find.byKey(const Key('wide'))); await _wheel(tester, at, const Offset(5, 40)); expect(list.offset, 40); expect(sideways(tester).pixels, 0); }); testWidgets('a plain horizontal scroller takes the whole gesture (the ' 'defect this guards against)', (tester) async { await tester.pumpWidget(page(passThrough: false)); list.jumpTo(0); final at = tester.getCenter(find.byKey(const Key('wide'))); await _wheel(tester, at, const Offset(5, 40)); expect(list.offset, 0); expect(sideways(tester).pixels, 5); }); testWidgets('a mostly sideways gesture or Shift + wheel scrolls the row', ( tester, ) async { await tester.pumpWidget(page()); list.jumpTo(0); final at = tester.getCenter(find.byKey(const Key('wide'))); await _wheel(tester, at, const Offset(40, 5)); expect(sideways(tester).pixels, 40); expect(list.offset, 0); await tester.sendKeyDownEvent(LogicalKeyboardKey.shiftLeft); await _wheel( tester, at, const Offset(0, 30), kind: PointerDeviceKind.mouse, ); await tester.sendKeyUpEvent(LogicalKeyboardKey.shiftLeft); expect(sideways(tester).pixels, 70); expect(list.offset, 0); }); testWidgets('the wheel over a control floating above the list scrolls it', ( tester, ) async { await tester.pumpWidget(page()); list.jumpTo(0); final at = tester.getCenter(find.byKey(const Key('floating'))); await _wheel( tester, at, const Offset(0, 50), kind: PointerDeviceKind.mouse, ); expect(list.offset, 50); // At the start, an upward wheel has nothing to move and is not claimed. list.jumpTo(0); await _wheel( tester, at, const Offset(0, -50), kind: PointerDeviceKind.mouse, ); expect(list.offset, 0); }); testWidgets('a multi-line field that can scroll its own text keeps the ' 'wheel', (tester) async { final field = ScrollController(); addTearDown(field.dispose); await tester.pumpWidget( MaterialApp( home: Scaffold( body: VerticalWheelForwarder( controller: list, child: Column( children: [ Expanded( child: ListView( controller: list, children: const [SizedBox(height: 3000)], ), ), TextField( key: const Key('field'), scrollController: field, maxLines: 3, controller: TextEditingController( text: List.generate(20, (i) => 'line $i').join('\n'), ), ), ], ), ), ), ), ); list.jumpTo(0); await _wheel( tester, tester.getCenter(find.byKey(const Key('field'))), const Offset(0, 20), kind: PointerDeviceKind.mouse, ); expect(field.offset, greaterThan(0)); expect(list.offset, 0); }); }