// 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( 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( 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(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( 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); 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)); await scrub.up(); await tester.pumpAndSettle(); expect(find.byKey(const Key('recommend-top-nav')), findsOneWidget); 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(find.text("This Week's Hits")); expect(sectionTitle.style?.fontSize, AppDimens.recommendSectionTitle); expect(sectionTitle.style?.fontWeight, FontWeight.w700); final sectionSubtitle = tester.widget( 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( 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( 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(coverFinder); expect(cover.aspectRatio, AppDimens.cardCoverAspectRatio); final cardTitleFinder = find.byKey( const Key('recommend-title-hits-the-signal-demo'), ); final cardTitle = tester.widget(cardTitleFinder); expect(cardTitle.style?.fontSize, AppDimens.cardTitleFont); expect(cardTitle.style?.fontWeight, FontWeight.w500); expect(cardTitle.style?.height, AppDimens.cardTitleLineHeight); final views = tester.widget( 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( 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(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( 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(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( find.byKey(const Key('discover-search')), ); expect(search.focusNode?.hasFocus, isTrue); }); } TextStyle _textStyle(WidgetTester tester, String text) { return tester .widget( find.descendant( of: find.byKey(const Key('all-sort-new')), matching: find.text(text), ), ) .style!; } Future _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(); }