feat(flutter): 重构四 Tab 与浏览页

This commit is contained in:
Zhou Weidong committed 2026-10-20 09:49:42 +08:00
1 parent 99c72e2c32
commit 959edc0a32
41 files changed
+2148 -1179

No files matched your search

+12 -8
View File
@@ -66,22 +66,20 @@ void main() {
logic.openProfile();
expect(logic.tab, 2);
expect(logic.tab, 3);
logic.dispose();
});
test('should_return_to_discover_and_handle_immersive_back', () {
test('should_return_home_and_handle_immersive_back', () {
final logic = HomeLogic(dramas);
logic.onSystemBack();
expect(logic.tab, 1);
logic.onSystemBack();
expect(logic.tab, 1);
expect(logic.tab, 0);
expect(logic.backRequest, 0);
logic.select(0);
logic.select(1);
logic.onSystemBack();
expect(logic.tab, 1);
expect(logic.tab, 0);
logic.select(0);
logic.onImmersiveChanged(true);
@@ -91,7 +89,7 @@ void main() {
logic.onImmersiveChanged(false);
logic.onSystemBack();
expect(logic.tab, 1);
expect(logic.tab, 0);
logic.dispose();
});
@@ -130,6 +128,9 @@ void main() {
expect(logic.tab, 1);
expect(logic.searchRequest, 1);
logic.myAction(MyPageAction.create);
expect(logic.tab, 2);
logic.myAction(MyPageAction.resume);
expect(logic.tab, 0);
expect(logic.resumeRequest, 1);
@@ -144,6 +145,9 @@ void main() {
final logic = HomeLogic(dramas);
logic.discoverAction(DiscoverPageAction.my);
expect(logic.tab, 3);
logic.discoverAction(DiscoverPageAction.create);
expect(logic.tab, 2);
logic.discoverAction(DiscoverPageAction.home);
+24 -16
View File
@@ -19,8 +19,9 @@ import '../support/widget_player.dart';
const _keys = [
Key('home-tabs-bar'),
Key('tab-home'),
Key('tab-discover'),
Key('tab-my'),
Key('tab-theater'),
Key('tab-create'),
Key('tab-mine'),
];
void main() {
@@ -33,7 +34,7 @@ void main() {
});
});
testWidgets('should_theme_three_tabs_and_switch_selected_state_when_tapped', (
testWidgets('should_theme_four_tabs_and_switch_selected_state_when_tapped', (
tester,
) async {
final haptics = FakeHaptics();
@@ -68,20 +69,20 @@ void main() {
expect(
find.descendant(
of: find.byKey(const Key('tab-home')),
matching: find.byIcon(Icons.play_circle_fill_rounded),
matching: find.byIcon(Icons.home_rounded),
),
findsOneWidget,
);
await tester.tap(find.byKey(const Key('tab-discover')));
await tester.tap(find.byKey(const Key('tab-theater')));
await tester.pumpAndSettle();
expect(haptics.selectionClicks, 1);
await tester.tap(find.byKey(const Key('tab-discover')));
await tester.tap(find.byKey(const Key('tab-theater')));
await tester.pumpAndSettle();
expect(haptics.selectionClicks, 1);
expect(
find.descendant(
of: find.byKey(const Key('tab-discover')),
of: find.byKey(const Key('tab-theater')),
matching: find.byIcon(Icons.explore_rounded),
),
findsOneWidget,
@@ -89,25 +90,32 @@ void main() {
expect(
find.descendant(
of: find.byKey(const Key('tab-home')),
matching: find.byIcon(Icons.play_circle_outline_rounded),
matching: find.byIcon(Icons.home_outlined),
),
findsOneWidget,
);
});
testWidgets('should_map_three_tabs_to_home_discover_and_me', (tester) async {
testWidgets('should_map_four_tabs_to_home_theater_create_and_me', (
tester,
) async {
await _pumpApp(tester);
expect(find.byKey(const Key('tab-home')), findsOneWidget);
expect(find.byKey(const Key('tab-discover')), findsOneWidget);
expect(find.byKey(const Key('tab-my')), findsOneWidget);
expect(find.byKey(const Key('home-feed-info-card')), findsOneWidget);
expect(find.byKey(const Key('tab-theater')), findsOneWidget);
expect(find.byKey(const Key('tab-create')), findsOneWidget);
expect(find.byKey(const Key('tab-mine')), findsOneWidget);
expect(find.byKey(const Key('home-browse-page')), findsOneWidget);
await tester.tap(find.byKey(const Key('tab-discover')));
await tester.tap(find.byKey(const Key('tab-theater')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('home-all-dramas-grid')), findsOneWidget);
await tester.tap(find.byKey(const Key('tab-my')));
await tester.tap(find.byKey(const Key('tab-create')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('create-page')), findsOneWidget);
await tester.tap(find.byKey(const Key('tab-mine')));
await tester.pumpAndSettle();
expect(find.text('Viewer'), findsOneWidget);
expect(find.byKey(const Key('mine-menu')), findsOneWidget);
@@ -116,7 +124,7 @@ void main() {
await tester.tap(find.byKey(const Key('tab-home')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('home-feed-info-card')), findsOneWidget);
expect(find.byKey(const Key('home-browse-page')), findsOneWidget);
});
testWidgets('should_not_haptic_when_ordinary_list_item_is_opened', (
@@ -124,7 +132,7 @@ void main() {
) async {
final haptics = FakeHaptics();
await _pumpApp(tester, haptics);
await tester.tap(find.byKey(const Key('tab-my')));
await tester.tap(find.byKey(const Key('tab-mine')));
await tester.pumpAndSettle();
haptics.reset();
+21 -33
View File
@@ -7,6 +7,7 @@ import 'package:shared_preferences_platform_interface/shared_preferences_async_p
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/data/repositories/favorite_repository.dart';
import 'package:fruitshorts/core/data/repositories/playback_repository.dart';
import 'package:fruitshorts/core/data/repositories/watch_history_repository.dart';
@@ -22,7 +23,7 @@ import '../support/fake_notice_repository.dart';
import '../support/widget_player.dart';
void main() {
testWidgets('should_render_preview_home_card_without_hot_or_follow_avatar', (
testWidgets('should_render_browse_home_with_header_channels_and_cards', (
tester,
) async {
SharedPreferencesAsyncPlatform.instance =
@@ -32,7 +33,16 @@ void main() {
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
final catalog = FakeCatalogRepository();
final catalog = FakeCatalogRepository(
banners: const [
FeedBanner(
id: 'banner-1',
imageUrl: '/files/banner.jpg',
linkType: 'DRAMA',
linkValue: 'the-signal-demo',
),
],
);
await tester.pumpWidget(
LumaDramaApp(
playerFactory: createWidgetPlayer,
@@ -51,40 +61,18 @@ void main() {
await tester.tap(find.byKey(const Key('tab-home')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('home-feed-title')), findsOneWidget);
expect(find.byKey(const Key('home-feed-info-card')), findsOneWidget);
expect(find.byKey(const Key('home-feed-episode')), findsOneWidget);
final cardRect = tester.getRect(
find.byKey(const Key('home-feed-info-card')),
);
final episodeRect = tester.getRect(
find.byKey(const Key('home-feed-episode')),
);
expect(episodeRect.left, lessThan(cardRect.center.dx));
final railCenter = tester.getCenter(find.byKey(const Key('rail-like')));
final railBottom = tester.getBottomLeft(find.byKey(const Key('rail-save')));
expect(railCenter.dy, greaterThan(844 * 0.4));
expect(railBottom.dy, lessThan(cardRect.top));
expect(find.byKey(const Key('home-feed-follow-series')), findsOneWidget);
expect(find.byKey(const Key('home-feed-episodes')), findsOneWidget);
for (final key in const [Key('home-feed-search'), Key('home-feed-more')]) {
expect(tester.getSize(find.byKey(key)), const Size(36, 36));
expect(
tester
.widget<Icon>(
find.descendant(of: find.byKey(key), matching: find.byType(Icon)),
)
.size,
18,
);
expect(find.byKey(const Key('home-search')), findsOneWidget);
expect(find.byKey(const Key('home-profile')), findsOneWidget);
expect(find.byKey(const Key('home-weekday')), findsOneWidget);
for (final channel in ['hot', 'free', 'newReleases', 'ranking']) {
expect(find.byKey(Key('home-channel-$channel')), findsOneWidget);
}
expect(find.byKey(const Key('home-hero-banner')), findsOneWidget);
expect(
tester.getSize(find.byKey(const Key('home-feed-follow-series'))).height,
34,
find.byKey(const Key('home-drama-card-the-signal-demo')),
findsOneWidget,
);
expect(find.text('HOT'), findsNothing);
expect(find.textContaining('Season'), findsNothing);
expect(find.byKey(const Key('home-feed-follow')), findsNothing);
expect(find.byKey(const Key('home-feed-info-card')), findsNothing);
expect(tester.takeException(), isNull);
});
+206
View File
@@ -0,0 +1,206 @@
// Author: fruit.com.cn
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:fruitshorts/app.dart';
import 'package:fruitshorts/core/data/models/home_feed.dart';
import 'package:fruitshorts/core/data/models/watch_progress.dart';
import 'package:fruitshorts/core/data/repositories/favorite_repository.dart';
import 'package:fruitshorts/core/data/repositories/playback_repository.dart';
import 'package:fruitshorts/core/data/repositories/watch_history_repository.dart';
import 'package:fruitshorts/pages/player/player_view.dart';
import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart';
import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
import '../support/fake_catalog.dart';
import '../support/fake_category.dart';
import '../support/fake_haptics.dart';
import '../support/fake_interaction_repositories.dart';
import '../support/widget_player.dart';
void main() {
setUp(() {
SharedPreferencesAsyncPlatform.instance =
InMemorySharedPreferencesAsync.empty();
});
testWidgets('should_render_four_tabs_and_open_create_placeholder', (
tester,
) async {
await _pumpApp(tester);
for (final key in _tabKeys) {
expect(find.byKey(key), findsOneWidget);
}
await tester.tap(find.byKey(const Key('tab-create')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('create-page')), findsOneWidget);
expect(find.text('Create'), findsWidgets);
expect(find.text('Stories become short dramas'), findsOneWidget);
});
testWidgets('should_switch_home_channel_when_chip_is_tapped', (tester) async {
await _pumpApp(tester);
expect(_chip(tester, 'home-channel-hot').selected, isTrue);
expect(_chip(tester, 'home-channel-free').selected, isFalse);
await tester.tap(find.byKey(const Key('home-channel-free')));
await tester.pumpAndSettle();
expect(_chip(tester, 'home-channel-hot').selected, isFalse);
expect(_chip(tester, 'home-channel-free').selected, isTrue);
});
testWidgets('should_open_theater_search_when_home_search_is_tapped', (
tester,
) async {
await _pumpApp(tester);
await tester.tap(find.byKey(const Key('home-search')));
await tester.pumpAndSettle();
final field = tester.widget<TextField>(
find.byKey(const Key('discover-search')),
);
expect(field.focusNode?.hasFocus, isTrue);
});
testWidgets('should_select_theater_language_without_filtering_catalog', (
tester,
) async {
await _pumpApp(tester);
await tester.tap(find.byKey(const Key('tab-theater')));
await tester.pumpAndSettle();
expect(_chip(tester, 'theater-language-all').selected, isTrue);
expect(
find.byKey(const Key('home-drama-card-the-signal-demo')),
findsOneWidget,
);
await tester.tap(find.byKey(const Key('theater-language-id')));
await tester.pumpAndSettle();
expect(_chip(tester, 'theater-language-all').selected, isFalse);
expect(_chip(tester, 'theater-language-id').selected, isTrue);
expect(
find.byKey(const Key('home-drama-card-the-signal-demo')),
findsOneWidget,
);
});
testWidgets('should_open_fullscreen_player_when_home_card_is_tapped', (
tester,
) async {
await _pumpApp(tester);
await tester.tap(find.byKey(const Key('home-drama-card-the-signal-demo')));
await tester.pumpAndSettle();
final player = tester.widget<PlayerPage>(find.byType(PlayerPage));
expect(player.homeMode, isFalse);
});
testWidgets('should_open_fullscreen_player_when_theater_card_is_tapped', (
tester,
) async {
await _pumpApp(tester);
await tester.tap(find.byKey(const Key('tab-theater')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('home-drama-card-the-signal-demo')));
await tester.pumpAndSettle();
final player = tester.widget<PlayerPage>(find.byType(PlayerPage));
expect(player.homeMode, isFalse);
});
testWidgets('should_resume_continue_watching_at_saved_episode', (
tester,
) async {
await _pumpApp(
tester,
history: InMemoryWatchHistoryRepository(
seed: const [
WatchProgress(
dramaId: 'the-signal-demo',
episodeId: 'the-signal-demo-3',
position: Duration(minutes: 2),
duration: Duration(minutes: 10),
),
],
),
);
expect(find.byKey(const Key('home-continue-watching')), findsOneWidget);
await tester.tap(
find.byKey(const Key('home-continue-play-the-signal-demo')),
);
await tester.pumpAndSettle();
final player = tester.widget<PlayerPage>(find.byType(PlayerPage));
expect(player.homeMode, isFalse);
expect(player.initialIndex, 2);
});
testWidgets('should_fit_home_and_theater_on_320_width', (tester) async {
await _pumpApp(tester, size: const Size(320, 568));
expect(find.byKey(const Key('home-search')), findsOneWidget);
expect(find.byKey(const Key('home-hero-title-0')), findsOneWidget);
expect(tester.takeException(), isNull);
await tester.tap(find.byKey(const Key('tab-theater')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('theater-language-chips')), findsOneWidget);
expect(tester.takeException(), isNull);
});
}
const _tabKeys = [
Key('tab-home'),
Key('tab-theater'),
Key('tab-create'),
Key('tab-mine'),
];
ChoiceChip _chip(WidgetTester tester, String key) =>
tester.widget<ChoiceChip>(find.byKey(Key(key)));
Future<void> _pumpApp(
WidgetTester tester, {
WatchHistoryRepository? history,
Size size = const Size(390, 844),
}) async {
tester.view.physicalSize = size;
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
final catalog = FakeCatalogRepository(
banners: const [
FeedBanner(
id: 'banner-1',
imageUrl: '/files/banner.jpg',
linkType: 'DRAMA',
linkValue: 'the-signal-demo',
),
],
);
await tester.pumpWidget(
LumaDramaApp(
playerFactory: createWidgetPlayer,
catalogRepository: catalog,
categoryRepository: FakeCategoryRepository(catalog: catalog),
watchHistoryRepository: history ?? InMemoryWatchHistoryRepository(),
favoriteRepository: InMemoryFavoriteRepository(cards: catalog.dramas),
playbackRepository: FakePlaybackRepository(),
commentRepository: FakeCommentRepository(),
interactionRepository: FakeInteractionRepository(),
haptics: FakeHaptics(),
),
);
await tester.pumpAndSettle();
}