Files
ReelShort/test/recommend/recommend_sections_test.dart
T

589 lines
22 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'), skipOffstage: false), 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'), skipOffstage: false)),
),
isTrue,
);
final player = tester.widget<PlayerPage>(
find.byKey(const Key('recommend-feed-player'), skipOffstage: false),
);
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 playlistSurface = tester.widget<DecoratedBox>(
find.byKey(const Key('recommend-playlist-surface')),
);
final playlistGradient =
(playlistSurface.decoration as BoxDecoration).gradient!
as LinearGradient;
expect(playlistGradient.begin, Alignment.topCenter);
expect(playlistGradient.end, Alignment.bottomCenter);
expect(playlistGradient.colors, const [
AppColors.mineGradientTop,
AppColors.mineGradientMiddle,
AppColors.mineGradientBottom,
]);
expect(playlistGradient.stops, const [0, 0.4, 0.75]);
final playlistGlow = tester.widget<DecoratedBox>(
find.byKey(const Key('recommend-playlist-glow')),
);
final glowGradient =
(playlistGlow.decoration as BoxDecoration).gradient! as RadialGradient;
expect(glowGradient.center, const Alignment(0, -0.95));
expect(glowGradient.radius, 0.9);
expect(glowGradient.colors, const [
AppColors.recommendPlaylistGlow,
AppColors.transparent,
]);
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-tag'),
);
expect(badge, findsNothing);
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_open_playlist_detail_with_collage_and_two_columns', (
tester,
) async {
await _pumpRecommend(tester);
await tester.tap(find.byKey(const Key('recommend-tab-playlist')));
await tester.pumpAndSettle();
await tester.ensureVisible(find.byKey(const Key('recommend-more-hits')));
await tester.tap(find.byKey(const Key('recommend-more-hits')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('playlist-detail-page')), findsOneWidget);
expect(find.byKey(const Key('playlist-detail-collage')), findsOneWidget);
expect(
tester.widget<Text>(find.byKey(const Key('playlist-detail-title'))).data,
"This Week's Hits",
);
expect(
tester
.widget<Text>(find.byKey(const Key('playlist-detail-subtitle')))
.data,
'Binge the hottest stories of the week!',
);
final grid = tester.widget<SliverGrid>(
find.byKey(const Key('playlist-detail-grid')),
);
final delegate =
grid.gridDelegate as SliverGridDelegateWithFixedCrossAxisCount;
expect(delegate.crossAxisCount, 2);
final first = tester.getRect(
find.byKey(const Key('home-feed-card-the-signal-demo')),
);
final second = tester.getRect(
find.byKey(const Key('home-feed-card-city-lights-demo')),
);
expect(second.top, closeTo(first.top, 1));
expect(second.left, greaterThan(first.right));
while (tester.takeException() != null) {}
});
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);
final grid = tester.widget<SliverGrid>(
find.byKey(const Key('all-dramas-grid')),
);
final delegate =
grid.gridDelegate as SliverGridDelegateWithFixedCrossAxisCount;
expect(delegate.crossAxisCount, 3);
expect(delegate.crossAxisSpacing, AppDimens.allDramasGridGap);
expect(delegate.mainAxisSpacing, AppDimens.allDramasGridRowGap);
for (var row = 0; row < 3; row++) {
expect(find.byKey(Key('all-filter-row-$row')), findsOneWidget);
final scroll = tester.widget<SingleChildScrollView>(
find.byKey(Key('all-filter-scroll-$row')),
);
expect(scroll.scrollDirection, Axis.horizontal);
expect(scroll.physics, isA<BouncingScrollPhysics>());
}
final filterStarts = [
for (var row = 0; row < 3; row++)
tester.getRect(find.byKey(Key('all-filter-row-$row'))).left,
];
expect(
filterStarts,
everyElement(closeTo(AppDimens.allDramasHorizontal, 0.1)),
);
final pageRect = tester.getRect(find.byKey(const Key('recommend-page')));
for (var row = 0; row < 3; row++) {
final rowRect = tester.getRect(find.byKey(Key('all-filter-row-$row')));
expect(
rowRect.left,
closeTo(pageRect.left + AppDimens.allDramasHorizontal, 0.1),
);
expect(rowRect.right, closeTo(pageRect.right - 12, 0.1));
}
final firstTexts = [
find.descendant(
of: find.byKey(const Key('all-filter-row-0')),
matching: find.byType(Text),
),
find.descendant(
of: find.byKey(const Key('all-filter-row-1')),
matching: find.byType(Text),
),
find.descendant(
of: find.byKey(const Key('all-filter-row-2')),
matching: find.byType(Text),
),
];
final firstTextX = firstTexts
.map((finder) => tester.getTopLeft(finder.first).dx)
.toList();
expect(firstTextX, everyElement(closeTo(firstTextX.first, 0.1)));
expect(
firstTextX.first,
closeTo(
pageRect.left +
AppDimens.allDramasHorizontal +
AppDimens.filterChipHorizontalPadding,
0.1,
),
);
for (var row = 0; row < 2; row++) {
expect(
tester.getSize(find.byKey(Key('all-filter-row-$row'))).height,
AppDimens.filterChipHeight + AppDimens.filterRowGap,
);
}
expect(
tester.getSize(find.byKey(const Key('all-filter-row-2'))).height,
AppDimens.filterChipHeight,
);
expect(
tester.getRect(find.byKey(const Key('all-dramas-scroll'))).top -
tester.getRect(find.byKey(const Key('all-dramas-filters'))).bottom,
closeTo(AppDimens.allDramasFilterToGridGap, 0.1),
);
expect(
find.descendant(
of: find.byKey(const Key('all-dramas-refresh')),
matching: find.byKey(const Key('all-dramas-filters')),
),
findsNothing,
);
expect(
find.descendant(
of: find.byKey(const Key('all-dramas-scroll')),
matching: find.byKey(const Key('all-dramas-grid')),
),
findsOneWidget,
);
final fixedFilterTop = tester
.getRect(find.byKey(const Key('all-dramas-filters')))
.top;
await tester.drag(
find.byKey(const Key('all-dramas-scroll')),
const Offset(0, -120),
);
await tester.pump();
expect(
tester.getRect(find.byKey(const Key('all-dramas-filters'))).top,
closeTo(fixedFilterTop, 0.1),
);
expect(find.byKey(const Key('all-filter-classification-all')), findsOneWidget);
expect(find.byKey(const Key('all-filter-category-all')), findsOneWidget);
expect(find.byKey(const Key('all-filter-tag-all')), findsOneWidget);
expect(
tester
.widget<Text>(
find.descendant(
of: find.byKey(const Key('all-filter-classification-all')),
matching: find.text('All'),
),
)
.style
?.color,
AppColors.filterChipTextSelected,
);
expect(find.byKey(const Key('all-dramas-filter-page')), findsNothing);
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'), skipOffstage: false), findsOneWidget);
expect(find.byType(AppBottomNav), findsOneWidget);
await tester.tap(find.byKey(const Key('recommend-tab-all')));
await tester.pumpAndSettle();
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'), skipOffstage: false), 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'), skipOffstage: false), findsOneWidget);
expect(find.byKey(const Key('recommend-tab-recommend')), findsOneWidget);
});
testWidgets('should_open_full_player_presentation_from_title_and_watch_all', (
tester,
) async {
await _pumpRecommend(tester);
await tester.tap(find.byKey(const Key('home-feed-series-name')));
await _waitForFullPlayer(tester);
await tester.pumpAndSettle();
expect(find.byType(PlayerPage).last, findsOneWidget);
expect(
tester.widget<PlayerPage>(find.byType(PlayerPage).last).homeMode,
isFalse,
);
expect(find.byKey(const Key('player-back')), findsOneWidget);
expect(find.byKey(const Key('home-tabs-bar')).hitTestable(), findsNothing);
await tester.tap(find.byKey(const Key('player-back')));
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 _waitForFullPlayer(tester);
await tester.pumpAndSettle();
expect(find.byType(PlayerPage).last, findsOneWidget);
expect(
tester.widget<PlayerPage>(find.byType(PlayerPage).last).homeMode,
isFalse,
);
expect(find.byKey(const Key('player-back')), findsOneWidget);
expect(find.byKey(const Key('home-tabs-bar')).hitTestable(), findsNothing);
await tester.tap(find.byKey(const Key('player-back')));
await tester.pumpAndSettle();
});
testWidgets('should_pull_refresh_recommend_feed_from_the_first_video', (
tester,
) async {
await _pumpRecommend(tester);
final gesture = await tester.startGesture(
tester.getCenter(find.byKey(const Key('recommend-feed-player'), skipOffstage: false)),
);
await gesture.moveBy(const Offset(0, 80));
await tester.pump();
expect(find.byKey(const Key('pull-refresh-indicator')), findsOneWidget);
final feedScroll = tester.state<ScrollableState>(
find
.descendant(
of: find.byKey(const Key('recommend-feed-player'), skipOffstage: false),
matching: find.byType(Scrollable),
)
.first,
);
expect(feedScroll.position.pixels, 0);
await gesture.up();
await tester.pump();
expect(feedScroll.position.pixels, 0);
await tester.pump(const Duration(milliseconds: 400));
while (tester.takeException() != null) {}
});
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.pump();
await tester.pump(const Duration(milliseconds: 400));
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);
});
}
Future<void> _waitForFullPlayer(WidgetTester tester) async {
for (var attempt = 0; attempt < 120; attempt++) {
await tester.runAsync(
() => Future<void>.delayed(const Duration(milliseconds: 10)),
);
await tester.pump(const Duration(milliseconds: 16));
if (find.byType(PlayerPage).evaluate().length > 1) return;
}
fail('full player route was not pushed after frame capture');
}
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();
}