|
| 1 | +import 'package:bb_mobile/core/widgets/bb_pullable_body.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:flutter_test/flutter_test.dart'; |
| 4 | + |
| 5 | +void main() { |
| 6 | + const lastItemKey = Key('last-item'); |
| 7 | + const lastItemHeight = 40.0; |
| 8 | + // Comfortably taller than the 600dp test viewport, so the content overflows |
| 9 | + // and the scroll extent is what decides whether the last item is reachable. |
| 10 | + const contentHeight = 1200.0; |
| 11 | + |
| 12 | + Future<void> pumpBody( |
| 13 | + WidgetTester tester, { |
| 14 | + required double bottomInset, |
| 15 | + }) async { |
| 16 | + await tester.pumpWidget( |
| 17 | + MaterialApp( |
| 18 | + home: Scaffold( |
| 19 | + body: BBPullableBody( |
| 20 | + onRefresh: () async {}, |
| 21 | + bottomInset: bottomInset, |
| 22 | + slivers: const [ |
| 23 | + SliverToBoxAdapter( |
| 24 | + child: SizedBox(height: contentHeight - lastItemHeight), |
| 25 | + ), |
| 26 | + SliverToBoxAdapter( |
| 27 | + child: SizedBox(key: lastItemKey, height: lastItemHeight), |
| 28 | + ), |
| 29 | + ], |
| 30 | + ), |
| 31 | + ), |
| 32 | + ), |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + /// Distance from the bottom of the last item to the bottom of the viewport, |
| 37 | + /// once scrolled as far as the body allows. Zero means the item is flush with |
| 38 | + /// the viewport edge — where a pinned footer would cover it. |
| 39 | + Future<double> clearanceAtEndOfScroll(WidgetTester tester) async { |
| 40 | + await tester.drag(find.byType(CustomScrollView), const Offset(0, -2000)); |
| 41 | + await tester.pumpAndSettle(); |
| 42 | + |
| 43 | + final viewportBottom = tester.getRect(find.byType(CustomScrollView)).bottom; |
| 44 | + final lastItemBottom = tester.getRect(find.byKey(lastItemKey)).bottom; |
| 45 | + |
| 46 | + return viewportBottom - lastItemBottom; |
| 47 | + } |
| 48 | + |
| 49 | + testWidgets('leaves the last item flush with the viewport by default', ( |
| 50 | + tester, |
| 51 | + ) async { |
| 52 | + await pumpBody(tester, bottomInset: 0); |
| 53 | + |
| 54 | + expect(await clearanceAtEndOfScroll(tester), moreOrLessEquals(0)); |
| 55 | + }); |
| 56 | + |
| 57 | + testWidgets('scrolls the last item clear of the reserved bottom inset', ( |
| 58 | + tester, |
| 59 | + ) async { |
| 60 | + await pumpBody(tester, bottomInset: 84); |
| 61 | + |
| 62 | + // Without the reservation this is 0 and a footer pinned over the body hides |
| 63 | + // the last item for good — the wallet home regression this guards against. |
| 64 | + expect(await clearanceAtEndOfScroll(tester), moreOrLessEquals(84)); |
| 65 | + }); |
| 66 | + |
| 67 | + testWidgets('adds the bottom inset to the scrollable extent', (tester) async { |
| 68 | + await pumpBody(tester, bottomInset: 84); |
| 69 | + final withInset = tester |
| 70 | + .state<ScrollableState>(find.byType(Scrollable)) |
| 71 | + .position |
| 72 | + .maxScrollExtent; |
| 73 | + |
| 74 | + await pumpBody(tester, bottomInset: 0); |
| 75 | + final withoutInset = tester |
| 76 | + .state<ScrollableState>(find.byType(Scrollable)) |
| 77 | + .position |
| 78 | + .maxScrollExtent; |
| 79 | + |
| 80 | + expect(withInset - withoutInset, moreOrLessEquals(84)); |
| 81 | + }); |
| 82 | + |
| 83 | + testWidgets('keeps short content unscrollable when an inset is reserved', ( |
| 84 | + tester, |
| 85 | + ) async { |
| 86 | + await tester.pumpWidget( |
| 87 | + MaterialApp( |
| 88 | + home: Scaffold( |
| 89 | + body: BBPullableBody( |
| 90 | + onRefresh: () async {}, |
| 91 | + bottomInset: 84, |
| 92 | + slivers: const [SliverToBoxAdapter(child: SizedBox(height: 100))], |
| 93 | + ), |
| 94 | + ), |
| 95 | + ), |
| 96 | + ); |
| 97 | + |
| 98 | + final position = tester |
| 99 | + .state<ScrollableState>(find.byType(Scrollable)) |
| 100 | + .position; |
| 101 | + |
| 102 | + expect(position.maxScrollExtent, 0); |
| 103 | + }); |
| 104 | +} |
0 commit comments