Files
ReelShort/test/recommend/recommend_sections_test.dart
T

227 lines
9.2 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);
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), findsOneWidget);
expect(find.byKey(const Key('recommend-section-hits')), findsNothing);
expect(find.byType(AppBottomNav), 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 cover = tester.widget<AspectRatio>(
find.descendant(of: card, matching: find.byType(AspectRatio)),
);
expect(cover.aspectRatio, AppDimens.cardCoverAspectRatio);
final cardTitle = tester.widget<Text>(
find.byKey(const Key('recommend-title-hits-the-signal-demo')),
);
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.cardViewsFont);
expect(views.style?.color, AppColors.textPrimary);
final tag = find.byKey(const Key('recommend-tag-hits-the-signal-demo-0'));
expect(tag, findsOneWidget);
expect(tester.getSize(tag).height, AppDimens.cardTagHeight);
final tagText = tester.widget<Text>(
find.descendant(of: tag, matching: find.byType(Text)),
);
expect(tagText.style?.fontSize, AppDimens.cardTagFont);
expect(tagText.style?.color, AppColors.textTertiary);
expect(
(tester.widget<Container>(tag).decoration! as BoxDecoration).borderRadius,
BorderRadius.circular(AppDimens.cardTagRadius),
);
});
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')), findsNothing);
expect(find.byKey(const Key('all-filter-subtitles')), findsNothing);
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')), findsNothing);
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_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, isTrue);
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();
}