Skip to content

Commit

Permalink
update: unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
LinXunFeng committed Nov 25, 2023
1 parent b36169a commit 282c7ee
Showing 1 changed file with 114 additions and 0 deletions.
114 changes: 114 additions & 0 deletions test/chat_scroll_observer_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* @Author: LinXunFeng [email protected]
* @Repo: https://github.com/fluttercandies/flutter_scrollview_observer
* @Date: 2023-11-25 19:04:30
*/
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:scrollview_observer/scrollview_observer.dart';

void main() {
// Regression test for https://github.com/fluttercandies/flutter_scrollview_observer/issues/64.
testWidgets('Keeping position', (tester) async {
final scrollController = ScrollController();
final observerController =
ListObserverController(controller: scrollController);
final chatScrollObserver = ChatScrollObserver(observerController)
..fixedPositionOffset = -1;

int receiveScrollNotificationCount = 0;

Widget widget = ChatListView(
scrollController: scrollController,
observerController: observerController,
chatScrollObserver: chatScrollObserver,
onReceiveScrollNotification: () {
receiveScrollNotificationCount += 1;
},
);
await tester.pumpWidget(widget);
await tester.tap(find.byType(FloatingActionButton));
await tester.pumpAndSettle();

final result = await observerController.dispatchOnceObserve(
isForce: true,
isDependObserveCallback: false,
);
expect(result.observeResult?.firstChild?.index, 4);
expect(receiveScrollNotificationCount, 0);

scrollController.dispose();
});
}

class ChatListView extends StatefulWidget {
const ChatListView({
Key? key,
required this.scrollController,
required this.observerController,
required this.chatScrollObserver,
required this.onReceiveScrollNotification,
}) : super(key: key);

final ScrollController scrollController;
final ListObserverController observerController;
final ChatScrollObserver chatScrollObserver;
final Function()? onReceiveScrollNotification;

@override
State<ChatListView> createState() => _ChatListViewState();
}

class _ChatListViewState extends State<ChatListView> {
List<String> dataList =
List.generate(100, (index) => index.toString()).toList();

@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(),
body: _buildListView(),
floatingActionButton: FloatingActionButton(
onPressed: () {
widget.chatScrollObserver.standby(changeCount: 4);
setState(() {
dataList.insert(0, '-1');
dataList.insert(0, '-2');
dataList.insert(0, '-3');
dataList.insert(0, '-4');
});
},
child: const Icon(Icons.add),
),
),
);
}

Widget _buildListView() {
Widget resultWidget = ListViewObserver(
controller: widget.observerController,
child: ListView.builder(
itemCount: dataList.length,
physics: ChatObserverBouncingScrollPhysics(
observer: widget.chatScrollObserver,
),
controller: widget.scrollController,
itemBuilder: (context, index) {
return SizedBox(
height: 100,
child: Center(child: Text(dataList[index])),
);
},
),
);
resultWidget = NotificationListener<ScrollNotification>(
child: resultWidget,
onNotification: (notification) {
widget.onReceiveScrollNotification?.call();
return false;
},
);
return resultWidget;
}
}

0 comments on commit 282c7ee

Please sign in to comment.