// Author: fruit.com.cn import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.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 '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/pages/player/widgets/player_center_toggle.dart'; import '../support/fake_catalog.dart'; import '../support/fake_category.dart'; import '../support/fake_haptics.dart'; import '../support/fake_interaction_repositories.dart'; import '../support/fake_notice_repository.dart'; import '../support/widget_player.dart'; void main() { setUp(() { SharedPreferencesAsyncPlatform.instance = InMemorySharedPreferencesAsync.empty(); }); testWidgets('should_open_fullscreen_overlay_from_discover_card', ( tester, ) async { await _pumpApp(tester); await tester.tap(find.byKey(const Key('tab-discover'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('home-drama-card-city-lights-demo'))); await tester.pumpAndSettle(); _expectFullscreenOverlay(tester); expect(find.byType(PageView), findsOneWidget); await tester.tap(find.byKey(const Key('player-more'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('player-more-download')), findsOneWidget); expect(find.byKey(const Key('player-more-pip')), findsOneWidget); expect(tester.takeException(), isNull); }); testWidgets('should_open_fullscreen_overlay_from_detail_cta_and_chip', ( tester, ) async { await _pumpApp(tester); await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('drawer-open-series'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('detail-cta'))); await tester.pumpAndSettle(); _expectFullscreenOverlay(tester); await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('detail-episode-chip-2'))); await tester.pumpAndSettle(); _expectFullscreenOverlay(tester); expect( tester.widget(find.byKey(const Key('player-title'))).data, contains('Episode 2'), ); expect(tester.takeException(), isNull); }); testWidgets('should_show_play_toggle_only_when_playback_is_paused', ( tester, ) async { await _pumpApp(tester); await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('drawer-open-series'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('detail-cta'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('player-play-toggle')), findsNothing); await tester.tap(find.byKey(const Key('player-video-tap'))); await tester.pump(); expect(find.byKey(const Key('player-play-toggle')), findsOneWidget); expect(find.byKey(const Key('player-controls-top')), findsOneWidget); expect(find.byKey(const Key('player-controls-bottom')), findsOneWidget); await tester.tap(find.byKey(const Key('player-play-toggle'))); await tester.pump(); expect(find.byKey(const Key('player-play-toggle')), findsNothing); }); testWidgets('should_keep_controls_visible_until_clear_screen_is_tapped', ( tester, ) async { await _pumpApp(tester); await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('drawer-open-series'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('detail-cta'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('player-controls-top')), findsOneWidget); expect(find.byKey(const Key('player-controls-bottom')), findsOneWidget); expect(find.byType(PlayerCenterToggle), findsNothing); await tester.tap(find.byKey(const Key('player-video-tap'))); await tester.pump(); expect(find.byType(PlayerCenterToggle), findsOneWidget); await tester.pump(const Duration(seconds: 5)); await tester.pump(); expect(find.byKey(const Key('player-controls-top')), findsOneWidget); expect(find.byKey(const Key('player-controls-bottom')), findsOneWidget); expect(find.byType(PlayerCenterToggle), findsOneWidget); await tester.tap(find.byKey(const Key('player-clear-screen'))); await tester.pump(); expect(find.byKey(const Key('player-controls-top')), findsNothing); expect(find.byKey(const Key('player-controls-bottom')), findsNothing); expect(find.byType(PlayerCenterToggle), findsNothing); expect(find.byKey(const Key('player-video-tap')), findsOneWidget); expect(find.byKey(const Key('player-exit-clear-screen')), findsOneWidget); expect( tester.getSize(find.byKey(const Key('player-exit-clear-screen'))), const Size(36, 36), ); await tester.tap(find.byKey(const Key('player-exit-clear-screen'))); await tester.pump(); expect(find.byKey(const Key('player-controls-top')), findsOneWidget); expect(find.byKey(const Key('player-controls-bottom')), findsOneWidget); expect(find.byType(PlayerCenterToggle), findsOneWidget); expect(find.byKey(const Key('player-exit-clear-screen')), findsNothing); await tester.tap(find.byKey(const Key('player-clear-screen'))); await tester.pump(); expect(find.byKey(const Key('player-exit-clear-screen')), findsOneWidget); await tester.tap(find.byKey(const Key('player-video-tap'))); await tester.pump(); expect(find.byKey(const Key('player-controls-top')), findsOneWidget); expect(find.byKey(const Key('player-exit-clear-screen')), findsNothing); }); testWidgets('should_fit_player_header_on_320_width', (tester) async { await _pumpApp(tester, size: const Size(320, 568)); await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('drawer-open-series'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('detail-cta'))); await tester.pumpAndSettle(); expect( tester.getSize(find.byKey(const Key('player-back'))), const Size(36, 36), ); expect(tester.takeException(), isNull); }); testWidgets('should_clear_and_restore_home_feed_chrome', (tester) async { await _pumpApp(tester); await tester.tap(find.byKey(const Key('tab-home'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('home-feed-title')), findsOneWidget); expect(find.byKey(const Key('home-feed-info-card')), findsOneWidget); expect(find.byKey(const Key('home-feed-scrim')), findsOneWidget); await tester.tap(find.byKey(const Key('home-feed-clear-screen'))); await tester.pump(); expect(find.byKey(const Key('home-feed-title')), findsNothing); expect(find.byKey(const Key('home-feed-info-card')), findsNothing); expect(find.byKey(const Key('home-feed-scrim')), findsNothing); expect(find.byKey(const Key('home-feed-video-tap')), findsOneWidget); expect( find.byKey(const Key('home-feed-exit-clear-screen')), findsOneWidget, ); expect( tester.getSize(find.byKey(const Key('home-feed-exit-clear-screen'))), const Size(36, 36), ); await tester.tap(find.byKey(const Key('home-feed-exit-clear-screen'))); await tester.pump(); expect(find.byKey(const Key('home-feed-title')), findsOneWidget); expect(find.byKey(const Key('home-feed-info-card')), findsOneWidget); expect(find.byKey(const Key('home-feed-scrim')), findsOneWidget); expect(find.byKey(const Key('home-feed-exit-clear-screen')), findsNothing); await tester.tap(find.byKey(const Key('home-feed-clear-screen'))); await tester.pump(); expect( find.byKey(const Key('home-feed-exit-clear-screen')), findsOneWidget, ); await tester.tap(find.byKey(const Key('home-feed-video-tap'))); await tester.pump(); expect(find.byKey(const Key('home-feed-title')), findsOneWidget); expect(find.byKey(const Key('home-feed-exit-clear-screen')), findsNothing); }); testWidgets('should_show_home_play_toggle_only_when_paused', (tester) async { await _pumpApp(tester); await tester.tap(find.byKey(const Key('tab-home'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('home-feed-center-play')), findsNothing); await tester.tap(find.byKey(const Key('home-feed-video-toggle'))); await tester.pump(); expect(find.byKey(const Key('home-feed-center-play')), findsOneWidget); await tester.tap(find.byKey(const Key('home-feed-center-play'))); await tester.pump(); expect(find.byKey(const Key('home-feed-center-play')), findsNothing); }); } void _expectFullscreenOverlay(WidgetTester tester) { expect(find.byKey(const Key('player-back')), findsOneWidget); expect(find.byKey(const Key('player-title')), findsOneWidget); expect(find.byKey(const Key('player-more')), findsOneWidget); for (final key in const [Key('player-back'), Key('player-more')]) { expect(tester.getSize(find.byKey(key)), const Size(36, 36)); } expect(find.byKey(const Key('player-play-toggle')), findsNothing); expect(find.byKey(const Key('player-episodes')), findsOneWidget); expect(find.byKey(const Key('player-comments')), findsOneWidget); expect(find.byKey(const Key('player-next')), findsOneWidget); expect(find.byType(Slider), findsOneWidget); expect(find.byKey(const Key('rail-like')), findsNothing); expect(find.byKey(const Key('rail-save')), findsNothing); } Future _pumpApp( WidgetTester tester, { Size size = const Size(390, 844), }) async { tester.view.physicalSize = size; 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(), noticeRepository: FakeNoticeRepository(), haptics: FakeHaptics(), ), ); await tester.pumpAndSettle(); }