// Author: fruit.com.cn import 'package:flutter/material.dart'; import 'package:flutter/services.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_four_tabs_and_open_following_page', ( tester, ) async { await _pumpApp(tester); for (final key in _tabKeys) { expect(find.byKey(key), findsOneWidget); } expect(find.byKey(const Key('tab-create')), findsNothing); await tester.tap(find.byKey(const Key('tab-follow'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('following-page')), findsOneWidget); expect(find.byType(AppBottomNav), findsOneWidget); }); testWidgets('should_start_home_on_explorer_grid', (tester) async { await _pumpApp(tester); expect(find.byKey(const Key('home-grid')), findsOneWidget); expect(find.byKey(const Key('home-grid-cards')), findsOneWidget); expect(find.byType(PageView), findsNothing); expect(find.byKey(const Key('home-page-gradient')), findsNothing); expect(_hasLightStatusBar(tester), isTrue); }); testWidgets('should_keep_home_grid_when_keyboard_insets_change', ( tester, ) async { addTearDown(tester.view.resetViewInsets); await _pumpApp(tester); final grid = find.byKey(const Key('home-grid')); final before = tester.getRect(grid); tester.view.viewInsets = const FakeViewPadding(bottom: 300); await tester.pump(); expect( tester .widget(find.byType(Scaffold).first) .resizeToAvoidBottomInset, isFalse, ); expect(tester.getRect(grid), before); }); testWidgets('should_render_recommend_immersive_player_and_inner_top_nav', ( tester, ) async { await _pumpApp(tester); await tester.tap(find.byKey(const Key('tab-recommend'))); await tester.pumpAndSettle(); 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); _expectNoFloatingOverlays(); 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); _expectNoFloatingOverlays(); }); testWidgets('should_switch_playlist_and_all_dramas_in_recommend', ( tester, ) async { await _pumpApp(tester); await tester.tap(find.byKey(const Key('tab-recommend'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('recommend-tab-playlist'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('recommend-section-hits')), findsOneWidget); expect(find.byType(AppBottomNav), findsOneWidget); _expectNoFloatingOverlays(); 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); } _expectNoFloatingOverlays(); }); testWidgets('should_open_search_from_home_and_recommend', (tester) async { await _pumpApp(tester); await tester.tap(find.byKey(const Key('home-search-entry'))); await tester.pumpAndSettle(); expect( tester .widget(find.byKey(const Key('discover-search'))) .focusNode ?.hasFocus, isTrue, ); await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('tab-recommend'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('recommend-search'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('discover-search')), findsOneWidget); }); testWidgets('should_open_player_from_home_grid_card', (tester) async { await _pumpApp(tester); await tester.tap(find.byKey(const Key('home-feed-card-the-signal-demo'))); await tester.pumpAndSettle(); final player = tester.widget(find.byType(PlayerPage).last); expect(player.homeMode, isFalse); }); testWidgets('should_fit_home_and_recommend_on_320_width', (tester) async { await _pumpApp(tester, size: const Size(320, 568)); expect(find.byKey(const Key('home-grid-cards')), findsOneWidget); expect(tester.takeException(), isNull); await tester.tap(find.byKey(const Key('tab-recommend'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('recommend-feed-player')), findsOneWidget); expect(tester.takeException(), isNull); }); } const _tabKeys = [ Key('tab-home'), Key('tab-recommend'), Key('tab-follow'), Key('tab-mine'), ]; void _expectNoFloatingOverlays() { expect(find.byKey(const Key('mine-mini-player')), findsNothing); expect(find.byType(FloatingActionButton), findsNothing); } bool _hasLightStatusBar(WidgetTester tester) => tester .widgetList>( find.byType(AnnotatedRegion), ) .any((region) => region.value.statusBarIconBrightness == Brightness.light); 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(), haptics: FakeHaptics(), ), ); await tester.pumpAndSettle(); }