|
| 1 | +import 'package:checks/checks.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | +import 'package:flutter_test/flutter_test.dart'; |
| 4 | +import 'package:zulip/api/model/model.dart'; |
| 5 | +import 'package:zulip/api/route/messages.dart'; |
| 6 | +import 'package:zulip/model/narrow.dart'; |
| 7 | +import 'package:zulip/widgets/message_list.dart'; |
| 8 | +import 'package:zulip/widgets/sticky_header.dart'; |
| 9 | +import 'package:zulip/widgets/store.dart'; |
| 10 | + |
| 11 | +import '../api/fake_api.dart'; |
| 12 | +import '../example_data.dart' as eg; |
| 13 | +import '../model/binding.dart'; |
| 14 | + |
| 15 | +Future<void> setupMessageListPage(WidgetTester tester, { |
| 16 | + required Narrow narrow, |
| 17 | +}) async { |
| 18 | + addTearDown(TestZulipBinding.instance.reset); |
| 19 | + addTearDown(tester.view.resetPhysicalSize); |
| 20 | + |
| 21 | + tester.view.physicalSize = const Size(600, 800); |
| 22 | + |
| 23 | + await TestZulipBinding.instance.globalStore.add(eg.selfAccount, eg.initialSnapshot()); |
| 24 | + final store = await TestZulipBinding.instance.globalStore.perAccount(eg.selfAccount.id); |
| 25 | + final connection = store.connection as FakeApiConnection; |
| 26 | + |
| 27 | + // prepare message list data |
| 28 | + final List<StreamMessage> messages = List.generate(10, (index) { |
| 29 | + return eg.streamMessage(id: index); |
| 30 | + }); |
| 31 | + connection.prepare(json: GetMessagesResult( |
| 32 | + anchor: messages[0].id, |
| 33 | + foundNewest: true, |
| 34 | + foundOldest: true, |
| 35 | + foundAnchor: true, |
| 36 | + historyLimited: false, |
| 37 | + messages: messages, |
| 38 | + ).toJson()); |
| 39 | + |
| 40 | + await tester.pumpWidget( |
| 41 | + MaterialApp( |
| 42 | + home: GlobalStoreWidget( |
| 43 | + child: PerAccountStoreWidget( |
| 44 | + accountId: eg.selfAccount.id, |
| 45 | + child: MessageListPage(narrow: narrow))))); |
| 46 | + |
| 47 | + // global store, per-account store, and message list get loaded |
| 48 | + await tester.pumpAndSettle(); |
| 49 | +} |
| 50 | + |
| 51 | +void main() { |
| 52 | + TestZulipBinding.ensureInitialized(); |
| 53 | + |
| 54 | + group('ScrollToBottomButton interactions', () { |
| 55 | + ScrollController? findMessageListScrollController(WidgetTester tester) { |
| 56 | + final stickyHeaderListView = tester.widget<StickyHeaderListView>(find.byType(StickyHeaderListView)); |
| 57 | + return stickyHeaderListView.controller; |
| 58 | + } |
| 59 | + |
| 60 | + bool isButtonVisible(WidgetTester tester) { |
| 61 | + return tester.any(find.descendant( |
| 62 | + of: find.byType(ScrollToBottomButton), |
| 63 | + matching: find.byTooltip("Scroll to bottom"))); |
| 64 | + } |
| 65 | + |
| 66 | + testWidgets('scrolling changes visibility', (WidgetTester tester) async { |
| 67 | + final stream = eg.stream(); |
| 68 | + await setupMessageListPage(tester, narrow: StreamNarrow(stream.streamId)); |
| 69 | + |
| 70 | + final scrollController = findMessageListScrollController(tester)!; |
| 71 | + |
| 72 | + // Initial state should be not visible, as the message list renders with latest message in view |
| 73 | + check(isButtonVisible(tester)).equals(false); |
| 74 | + |
| 75 | + scrollController.jumpTo(600); |
| 76 | + await tester.pump(); |
| 77 | + check(isButtonVisible(tester)).equals(true); |
| 78 | + |
| 79 | + scrollController.jumpTo(0); |
| 80 | + await tester.pump(); |
| 81 | + check(isButtonVisible(tester)).equals(false); |
| 82 | + }); |
| 83 | + |
| 84 | + testWidgets('dimension updates changes visibility', (WidgetTester tester) async { |
| 85 | + final stream = eg.stream(); |
| 86 | + await setupMessageListPage(tester, narrow: StreamNarrow(stream.streamId)); |
| 87 | + |
| 88 | + final scrollController = findMessageListScrollController(tester)!; |
| 89 | + |
| 90 | + // Initial state should be not visible, as the message list renders with latest message in view |
| 91 | + check(isButtonVisible(tester)).equals(false); |
| 92 | + |
| 93 | + scrollController.jumpTo(600); |
| 94 | + await tester.pump(); |
| 95 | + check(isButtonVisible(tester)).equals(true); |
| 96 | + |
| 97 | + tester.view.physicalSize = const Size(2000, 40000); |
| 98 | + await tester.pump(); |
| 99 | + // Dimension changes use NotificationListener<ScrollMetricsNotification |
| 100 | + // which has a one frame lag. If that ever gets resolved this extra pump |
| 101 | + // would ideally be removed |
| 102 | + await tester.pump(); |
| 103 | + check(isButtonVisible(tester)).equals(false); |
| 104 | + }); |
| 105 | + |
| 106 | + testWidgets('button functionality', (WidgetTester tester) async { |
| 107 | + final stream = eg.stream(); |
| 108 | + await setupMessageListPage(tester, narrow: StreamNarrow(stream.streamId)); |
| 109 | + |
| 110 | + final scrollController = findMessageListScrollController(tester)!; |
| 111 | + |
| 112 | + // Initial state should be not visible, as the message list renders with latest message in view |
| 113 | + check(isButtonVisible(tester)).equals(false); |
| 114 | + |
| 115 | + scrollController.jumpTo(600); |
| 116 | + await tester.pump(); |
| 117 | + check(isButtonVisible(tester)).equals(true); |
| 118 | + |
| 119 | + await tester.tap(find.byType(ScrollToBottomButton)); |
| 120 | + await tester.pumpAndSettle(); |
| 121 | + check(isButtonVisible(tester)).equals(false); |
| 122 | + check(scrollController.position.pixels).equals(0); |
| 123 | + }); |
| 124 | + }); |
| 125 | +} |
0 commit comments