Skip to content

Commit

Permalink
Add regression test
Browse files Browse the repository at this point in the history
  • Loading branch information
fujidaiti committed Jul 19, 2024
1 parent 9bec46a commit 424e289
Showing 1 changed file with 66 additions and 1 deletion.
67 changes: 66 additions & 1 deletion package/test/scrollable/scrollable_sheet_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ class _TestSheetContent extends StatelessWidget {
const _TestSheetContent({
super.key,
this.height = 500,
this.itemCount = 30,
// Disable the snapping effect by default in tests.
this.scrollPhysics = const ClampingScrollPhysics(),
this.onTapItem,
});

final double? height;
final int itemCount;
final ScrollPhysics? scrollPhysics;
final void Function(int index)? onTapItem;

@override
Widget build(BuildContext context) {
Expand All @@ -54,9 +58,10 @@ class _TestSheetContent extends StatelessWidget {
child: ListView(
physics: scrollPhysics,
children: List.generate(
30,
itemCount,
(index) => ListTile(
title: Text('Item $index'),
onTap: onTapItem != null ? () => onTapItem!(index) : null,
),
),
),
Expand Down Expand Up @@ -135,6 +140,66 @@ void main() {
);
});

// Regression test for https://github.com/fujidaiti/smooth_sheets/issues/190
testWidgets(
'Press and hold scrollable view should stop momentum scrolling',
(tester) async {
const targetKey = Key('Target');
final controller = SheetController();
late ScrollController scrollController;

await tester.pumpWidget(
_TestApp(
child: ScrollableSheet(
controller: controller,
child: Builder(
builder: (context) {
// TODO(fujita): Refactor this line after #116 is resolved.
scrollController = PrimaryScrollController.of(context);
return _TestSheetContent(
key: targetKey,
itemCount: 1000,
height: null,
// The items need to be clickable to cause the reported issue.
onTapItem: (index) {},
);
},
),
),
),
);

const dragDistance = 200.0;
const flingSpeed = 2000.0;
await tester.fling(
find.byKey(targetKey),
const Offset(0, -1 * dragDistance), // Fling up
flingSpeed,
);

final offsetAfterFling = scrollController.offset;
// Don't know why, but we need to call `pump` at least 2 times
// to forward the animation clock.
await tester.pump();
await tester.pump(const Duration(milliseconds: 250));
final offsetBeforePress = scrollController.offset;
expect(offsetBeforePress, greaterThan(offsetAfterFling),
reason: 'Momentum scrolling should be in progress.');

// Press and hold the finger on the target widget.
await tester.press(find.byKey(targetKey));
// Wait for the momentum scrolling to stop.
await tester.pumpAndSettle();
final offsetAfterPress = scrollController.offset;
expect(
offsetAfterPress,
equals(offsetBeforePress),
reason: 'Momentum scrolling should be stopped immediately'
'by pressing and holding.',
);
},
);

group('SheetKeyboardDismissible', () {
late FocusNode focusNode;
late Widget testWidget;
Expand Down

0 comments on commit 424e289

Please sign in to comment.