// 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/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( 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); }); 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); 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(); }