feat(flutter): 增加榜单新剧分页与预约刷新

This commit is contained in:
Zhou Weidong committed 2026-10-20 16:53:07 +08:00
1 parent 805762f761
commit c4dcb714ab
12 files changed
+611 -96

No files matched your search

+136 -2
View File
@@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:fruitshorts/app.dart';
import 'package:fruitshorts/core/data/models/drama.dart';
import 'package:fruitshorts/core/data/models/home_feed.dart';
import 'package:fruitshorts/core/theme/app_colors.dart';
import 'package:fruitshorts/core/data/repositories/favorite_repository.dart';
import 'package:fruitshorts/core/data/repositories/playback_repository.dart';
@@ -172,14 +173,119 @@ void main() {
expect(find.byKey(const Key('ranking-page')), findsNothing);
expect(find.byKey(const Key('discover-search')), findsOneWidget);
});
testWidgets('should_page_ranking_and_reset_after_refresh', (tester) async {
final repository = _CountingCatalogRepository(dramas: _rankDramas(40));
await _pumpTheater(tester, catalogRepository: repository);
await tester.tap(find.byKey(const Key('theater-toolbar-ranking')));
await tester.pumpAndSettle();
final list = find.byKey(const Key('ranking-list'));
await tester.drag(list, const Offset(0, -8000));
await tester.pump();
expect(find.byKey(const Key('ranking-row-19')), findsOneWidget);
expect(find.byKey(const Key('ranking-row-20')), findsNothing);
expect(find.byKey(const Key('ranking-load-more')), findsOneWidget);
await tester.pump(const Duration(milliseconds: 299));
expect(find.byKey(const Key('ranking-load-more')), findsOneWidget);
await tester.pump(const Duration(milliseconds: 1));
await tester.drag(list, const Offset(0, -8000));
await tester.pump();
expect(find.byKey(const Key('ranking-row-39')), findsOneWidget);
expect(find.byKey(const Key('ranking-row-40')), findsNothing);
expect(find.byKey(const Key('ranking-no-more')), findsOneWidget);
final position = _scrollPosition(tester, const Key('ranking-list'));
position.jumpTo(0);
await tester.pump();
final beforeRefresh = repository.fetchCalls;
await tester.drag(list, const Offset(0, 120));
await tester.pump();
await tester.pumpAndSettle();
expect(repository.fetchCalls, beforeRefresh + 1);
expect(position.pixels, 0);
await tester.drag(list, const Offset(0, -8000));
await tester.pump();
expect(find.byKey(const Key('ranking-load-more')), findsOneWidget);
expect(find.byKey(const Key('ranking-no-more')), findsNothing);
await tester.pumpAndSettle();
});
testWidgets('should_page_new_releases_and_reset_after_refresh', (
tester,
) async {
final repository = _CountingCatalogRepository(dramas: _rankDramas(40));
await _pumpTheater(tester, catalogRepository: repository);
await tester.tap(find.byKey(const Key('theater-toolbar-new')));
await tester.pumpAndSettle();
final list = find.byKey(const Key('new-releases-list'));
await tester.drag(list, const Offset(0, -400));
await tester.pump();
expect(_countKeys('home-drama-card-'), 20);
await tester.drag(list, const Offset(0, -8000));
await tester.pump();
expect(find.byKey(const Key('new-releases-load-more')), findsOneWidget);
await tester.pump(const Duration(milliseconds: 299));
expect(_countKeys('home-drama-card-'), 20);
await tester.pump(const Duration(milliseconds: 1));
await tester.pump();
expect(_countKeys('home-drama-card-'), 40);
await tester.pumpAndSettle();
final position = _scrollPosition(tester, const Key('new-releases-list'));
position.jumpTo(position.maxScrollExtent);
await tester.pumpAndSettle();
expect(find.byKey(const Key('new-releases-no-more')), findsOneWidget);
position.jumpTo(0);
await tester.pump();
final beforeRefresh = repository.fetchCalls;
await tester.drag(list, const Offset(0, 120));
await tester.pump();
await tester.pumpAndSettle();
expect(repository.fetchCalls, beforeRefresh + 1);
expect(position.pixels, 0);
await tester.drag(list, const Offset(0, -400));
await tester.pump();
expect(_countKeys('home-drama-card-'), 20);
});
testWidgets('should_refresh_reserve_without_load_more', (tester) async {
final repository = _CountingCatalogRepository(dramas: _rankDramas(3));
await _pumpTheater(tester, catalogRepository: repository);
await tester.tap(find.byKey(const Key('theater-toolbar-reserve')));
await tester.pumpAndSettle();
final beforeRefresh = repository.fetchCalls;
await tester.drag(
find.byKey(const Key('reserve-list')),
const Offset(0, 120),
);
await tester.pump();
await tester.pumpAndSettle();
expect(repository.fetchCalls, beforeRefresh + 1);
expect(find.byKey(const Key('reserve-load-more')), findsNothing);
expect(find.byKey(const Key('reserve-no-more')), findsNothing);
});
}
Future<void> _pumpTheater(WidgetTester tester, {List<Drama>? dramas}) async {
Future<void> _pumpTheater(
WidgetTester tester, {
List<Drama>? dramas,
FakeCatalogRepository? catalogRepository,
}) async {
tester.view.physicalSize = const Size(390, 844);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
final catalog = FakeCatalogRepository(dramas: dramas);
final catalog = catalogRepository ?? FakeCatalogRepository(dramas: dramas);
await tester.pumpWidget(
LumaDramaApp(
playerFactory: createWidgetPlayer,
@@ -199,6 +305,34 @@ Future<void> _pumpTheater(WidgetTester tester, {List<Drama>? dramas}) async {
await tester.pumpAndSettle();
}
ScrollPosition _scrollPosition(WidgetTester tester, Key listKey) {
final scrollable = find.descendant(
of: find.byKey(listKey),
matching: find.byType(Scrollable),
);
return tester.state<ScrollableState>(scrollable.first).position;
}
int _countKeys(String prefix) => find
.byWidgetPredicate((widget) {
final key = widget.key;
return key is ValueKey<String> && key.value.startsWith(prefix);
})
.evaluate()
.length;
class _CountingCatalogRepository extends FakeCatalogRepository {
_CountingCatalogRepository({super.dramas});
int fetchCalls = 0;
@override
Future<HomeFeed> fetchHomeFeed() {
fetchCalls++;
return super.fetchHomeFeed();
}
}
List<Drama> _rankDramas(int count) => [
for (var index = 0; index < count; index++)
Drama(