Files
ReelShort/test/recommend/recommend_sections_test.dart
T

404 lines
15 KiB
Dart

// 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/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/core/theme/app_colors.dart';
import 'package:fruitshorts/core/theme/app_dimens.dart';
import 'package:fruitshorts/core/widgets/app_bottom_nav.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_immersive_player_as_recommend_tab', (
tester,
) async {
await _pumpRecommend(tester);
expect(find.byKey(const Key('recommend-page')), findsOneWidget);
expect(find.byKey(const Key('recommend-tab-recommend')), findsOneWidget);
expect(find.byKey(const Key('recommend-tab-playlist')), findsOneWidget);
expect(find.byKey(const Key('recommend-tab-all')), findsOneWidget);
expect(find.byKey(const Key('recommend-feed-player')), findsOneWidget);
expect(find.byKey(const Key('recommend-top-gradient')), findsOneWidget);
final recommendText = tester.widget<Text>(
find.descendant(
of: find.byKey(const Key('recommend-tab-recommend')),
matching: find.text('Recommend'),
),
);
expect(recommendText.style?.fontSize, 17);
expect(recommendText.style?.fontWeight, FontWeight.w700);
expect(recommendText.style?.color, AppColors.textPrimary);
final playlistText = tester.widget<Text>(
find.descendant(
of: find.byKey(const Key('recommend-tab-playlist')),
matching: find.text('Playlists'),
),
);
expect(playlistText.style?.fontSize, 17);
expect(playlistText.style?.fontWeight, FontWeight.w400);
expect(playlistText.style?.color, AppColors.textTabInactive);
expect(
tester
.widget<IconButton>(find.byKey(const Key('recommend-search')))
.iconSize,
26,
);
final topNavRect = tester.getRect(
find.byKey(const Key('recommend-top-nav')),
);
expect(
tester.getRect(find.byKey(const Key('recommend-search'))).right,
closeTo(topNavRect.right - AppDimens.feedPageHorizontal, 0.1),
);
final tabGroupLeft = tester
.getRect(find.byKey(const Key('recommend-tab-recommend')))
.left;
final tabGroupRight = tester
.getRect(find.byKey(const Key('recommend-tab-all')))
.right;
expect(
(tabGroupLeft + tabGroupRight) / 2,
closeTo(topNavRect.center.dx, 0.1),
);
expect(
tester
.getRect(find.byKey(const Key('recommend-top-nav')))
.overlaps(
tester.getRect(find.byKey(const Key('recommend-feed-player'))),
),
isTrue,
);
final player = tester.widget<PlayerPage>(
find.byKey(const Key('recommend-feed-player')),
);
expect(player.homeMode, isTrue);
expect(player.embedded, isTrue);
expect(player.showHomeFeedHeader, isFalse);
expect(find.byType(PageView), findsNWidgets(2));
expect(find.byKey(const Key('recommend-section-hits')), findsNothing);
expect(find.byType(AppBottomNav), findsOneWidget);
final progress = find.byKey(const Key('home-feed-progress'));
final progressCenterY = tester.getCenter(progress).dy;
final scrub = await tester.startGesture(tester.getCenter(progress));
await scrub.moveBy(const Offset(40, 0));
await tester.pump(const Duration(milliseconds: 130));
expect(find.byKey(const Key('recommend-top-nav')), findsOneWidget);
expect(tester.getCenter(progress).dy, closeTo(progressCenterY, 0.1));
expect(find.byType(AppBottomNav), findsOneWidget);
expect(
tester.getSize(find.byKey(const Key('home-tabs-bar'))).height,
AppDimens.navHeight,
);
await scrub.up();
await tester.pumpAndSettle();
expect(find.byKey(const Key('recommend-top-nav')), findsOneWidget);
expect(find.byType(AppBottomNav), findsOneWidget);
expect(
tester.getSize(find.byKey(const Key('home-tabs-bar'))).height,
AppDimens.navHeight,
);
await tester.tap(find.byKey(const Key('recommend-tab-playlist')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('recommend-section-hits')), findsOneWidget);
});
testWidgets('should_render_playlist_sections_and_horizontal_cards', (
tester,
) async {
await _pumpRecommend(tester);
await tester.tap(find.byKey(const Key('recommend-tab-playlist')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('recommend-section-hits')), findsOneWidget);
expect(find.byKey(const Key('recommend-section-new')), findsOneWidget);
expect(find.byKey(const Key('recommend-section-costume')), findsOneWidget);
expect(find.byKey(const Key('recommend-more-hits')), findsOneWidget);
expect(find.byKey(const Key('recommend-more-new')), findsOneWidget);
expect(find.byKey(const Key('recommend-more-costume')), findsOneWidget);
expect(find.byKey(const Key('recommend-row-hits')), findsOneWidget);
expect(
find.byKey(const Key('recommend-card-hits-the-signal-demo')),
findsOneWidget,
);
expect(find.text("This Week's Hits"), findsOneWidget);
expect(find.text('New This Week'), findsOneWidget);
expect(find.text('Costume Drama Awards'), findsOneWidget);
expect(find.byType(AppBottomNav), findsOneWidget);
final sectionTitle = tester.widget<Text>(find.text("This Week's Hits"));
expect(sectionTitle.style?.fontSize, AppDimens.recommendSectionTitle);
expect(sectionTitle.style?.fontWeight, FontWeight.w700);
final sectionSubtitle = tester.widget<Text>(
find.text('Binge the hottest stories of the week!'),
);
expect(sectionSubtitle.style?.fontSize, AppDimens.recommendSectionSubtitle);
expect(sectionSubtitle.style?.color, AppColors.textSecondary);
final moreText = tester.widget<Text>(
find.descendant(
of: find.byKey(const Key('recommend-more-hits')),
matching: find.text('More'),
),
);
expect(moreText.style?.fontSize, AppDimens.recommendSectionMore);
expect(moreText.style?.color, AppColors.textSecondary);
final card = find.byKey(const Key('recommend-card-hits-the-signal-demo'));
final coverClip = tester.widget<ClipRRect>(
find.descendant(of: card, matching: find.byType(ClipRRect)),
);
expect(
coverClip.borderRadius,
BorderRadius.circular(AppDimens.cardCoverRadius),
);
final coverFinder = find.descendant(
of: card,
matching: find.byType(AspectRatio),
);
final cover = tester.widget<AspectRatio>(coverFinder);
expect(cover.aspectRatio, AppDimens.cardCoverAspectRatio);
final cardTitleFinder = find.byKey(
const Key('recommend-title-hits-the-signal-demo'),
);
final cardTitle = tester.widget<Text>(cardTitleFinder);
expect(cardTitle.style?.fontSize, AppDimens.cardTitleFont);
expect(cardTitle.style?.fontWeight, FontWeight.w500);
expect(cardTitle.style?.height, AppDimens.cardTitleLineHeight);
final views = tester.widget<Text>(
find.descendant(
of: find.byKey(const Key('recommend-views-hits-the-signal-demo')),
matching: find.byType(Text),
),
);
expect(views.style?.fontSize, AppDimens.cardPlayCountFont);
expect(views.style?.fontWeight, FontWeight.w400);
expect(
find.descendant(
of: coverFinder,
matching: find.byKey(const Key('recommend-views-hits-the-signal-demo')),
),
findsOneWidget,
);
expect(views.style?.color, AppColors.textPrimary);
final viewIcon = find.descendant(
of: find.byKey(const Key('recommend-views-hits-the-signal-demo')),
matching: find.byIcon(Icons.local_fire_department_rounded),
);
expect(viewIcon, findsOneWidget);
final badge = find.byKey(
const Key('drama-cover-badge-ribbon-hits-the-signal-demo-hot'),
);
expect(badge, findsOneWidget);
final badgeText = tester.widget<Text>(
find.descendant(of: badge, matching: find.byType(Text)),
);
expect(badgeText.data, 'Hit');
expect(badgeText.style?.fontSize, AppDimens.cardBadgeFont);
expect(badgeText.style?.color, AppColors.textPrimary);
final tagsFinder = find.byKey(
const Key('recommend-tags-hits-the-signal-demo'),
);
final tags = tester.widget<Text>(tagsFinder);
expect(tags.data, 'Suspense | Mystery');
expect(tags.style?.fontSize, AppDimens.cardTagFont);
expect(tags.style?.color, AppColors.textTertiary);
expect(
tester.getTopLeft(cardTitleFinder).dx,
tester.getTopLeft(coverFinder).dx,
);
expect(tester.getTopLeft(tagsFinder).dx, tester.getTopLeft(coverFinder).dx);
});
testWidgets('should_switch_inner_tabs_without_losing_grid_filter_state', (
tester,
) async {
await _pumpRecommend(tester);
await tester.tap(find.byKey(const Key('recommend-tab-playlist')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('recommend-section-hits')), findsOneWidget);
await tester.tap(find.byKey(const Key('recommend-tab-all')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('all-dramas-grid')), findsOneWidget);
for (var row = 0; row < 4; row++) {
expect(find.byKey(Key('all-filter-row-$row')), findsOneWidget);
}
expect(find.byKey(const Key('all-filter-local')), findsOneWidget);
expect(find.byKey(const Key('all-filter-subtitles')), findsOneWidget);
await tester.tap(find.byKey(const Key('all-filter-local')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('all-dramas-empty')), findsOneWidget);
await tester.tap(find.byKey(const Key('all-filter-all')).first);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('all-sort-new')));
await tester.pump();
expect(_textStyle(tester, 'New').fontWeight, FontWeight.w700);
expect(_textStyle(tester, 'New').fontSize, AppDimens.filterChipFont);
expect(
tester.getSize(find.byKey(const Key('all-sort-new'))).height,
AppDimens.filterChipHeight,
);
final chipContainer = tester.widget<Container>(
find.descendant(
of: find.byKey(const Key('all-sort-new')),
matching: find.byType(Container),
),
);
expect(
(chipContainer.decoration! as BoxDecoration).borderRadius,
BorderRadius.circular(AppDimens.filterChipRadius),
);
await tester.tap(find.byKey(const Key('recommend-tab-playlist')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('recommend-section-hits')), findsOneWidget);
expect(find.byKey(const Key('recommend-feed-player')), findsOneWidget);
expect(find.byType(AppBottomNav), findsOneWidget);
await tester.tap(find.byKey(const Key('recommend-tab-all')));
await tester.pumpAndSettle();
expect(_textStyle(tester, 'New').fontWeight, FontWeight.w700);
expect(find.byKey(const Key('all-dramas-grid')), findsOneWidget);
});
testWidgets('should_switch_inner_tabs_with_horizontal_page_swipes', (
tester,
) async {
await _pumpRecommend(tester);
final pages = find.byKey(const Key('recommend-tab-page-view'));
final topNavBefore = tester.getRect(
find.byKey(const Key('recommend-top-nav')),
);
await tester.drag(pages, const Offset(-320, 0));
await tester.pumpAndSettle();
expect(find.byKey(const Key('recommend-section-hits')), findsOneWidget);
expect(find.byKey(const Key('recommend-feed-player')), findsOneWidget);
expect(
tester
.widget<Text>(
find.descendant(
of: find.byKey(const Key('recommend-tab-playlist')),
matching: find.text('Playlists'),
),
)
.style
?.color,
AppColors.textPrimary,
);
expect(
tester.getRect(find.byKey(const Key('recommend-top-nav'))),
topNavBefore,
);
await tester.drag(pages, const Offset(-320, 0));
await tester.pumpAndSettle();
expect(find.byKey(const Key('all-dramas-grid')), findsOneWidget);
await tester.drag(pages, const Offset(640, 0));
await tester.pumpAndSettle();
expect(find.byKey(const Key('recommend-feed-player')), findsOneWidget);
expect(find.byKey(const Key('recommend-tab-recommend')), findsOneWidget);
});
testWidgets('should_open_standalone_player_from_title_and_watch_all', (
tester,
) async {
await _pumpRecommend(tester);
await tester.tap(find.byKey(const Key('home-feed-title-open')));
await tester.pumpAndSettle();
expect(
tester.widget<PlayerPage>(find.byType(PlayerPage).last).homeMode,
isFalse,
);
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
expect(find.byKey(const Key('recommend-page')), findsOneWidget);
expect(find.byKey(const Key('recommend-tab-recommend')), findsOneWidget);
await tester.tap(find.byKey(const Key('home-feed-full-series')));
await tester.pumpAndSettle();
expect(
tester.widget<PlayerPage>(find.byType(PlayerPage).last).homeMode,
isFalse,
);
});
testWidgets('should_open_player_and_search_from_recommend_page', (
tester,
) async {
await _pumpRecommend(tester);
await tester.tap(find.byKey(const Key('recommend-tab-playlist')));
await tester.pumpAndSettle();
await tester.tap(
find.byKey(const Key('recommend-card-hits-the-signal-demo')),
);
await tester.pumpAndSettle();
final player = tester.widget<PlayerPage>(find.byType(PlayerPage).last);
expect(player.homeMode, isFalse);
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('recommend-search')));
await tester.pumpAndSettle();
final search = tester.widget<TextField>(
find.byKey(const Key('discover-search')),
);
expect(search.focusNode?.hasFocus, isTrue);
});
}
TextStyle _textStyle(WidgetTester tester, String text) {
return tester
.widget<Text>(
find.descendant(
of: find.byKey(const Key('all-sort-new')),
matching: find.text(text),
),
)
.style!;
}
Future<void> _pumpRecommend(WidgetTester tester) async {
tester.view.physicalSize = const Size(390, 844);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
final catalog = FakeCatalogRepository();
await tester.pumpWidget(
LumaDramaApp(
playerFactory: createWidgetPlayer,
catalogRepository: catalog,
categoryRepository: FakeCategoryRepository(catalog: catalog),
watchHistoryRepository: InMemoryWatchHistoryRepository(),
favoriteRepository: InMemoryFavoriteRepository(cards: catalog.dramas),
playbackRepository: FakePlaybackRepository(),
commentRepository: FakeCommentRepository(),
interactionRepository: FakeInteractionRepository(),
haptics: FakeHaptics(),
),
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('tab-recommend')));
await tester.pumpAndSettle();
}