// 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_five_tabs_and_open_following_page', ( tester, ) async { // 追剧页头部双语分段在测试字体(Ahem)下比真机字体宽约 98pt, // 放宽视口宽度避免测试字体撑破分段行。 await _pumpApp(tester, size: const Size(520, 844)); 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_immersive_recommend_feed', (tester) async { await _pumpApp(tester); expect(find.byKey(const Key('recommend-page')), findsOneWidget); expect(find.byKey(const Key('recommend-feed-player')), findsOneWidget); expect(find.byKey(const Key('recommend-top-nav')), findsOneWidget); expect(find.byKey(const Key('recommend-tab-page-view')), findsOneWidget); expect(_hasLightStatusBar(tester), isTrue); }); testWidgets('should_keep_home_feed_when_keyboard_insets_change', ( tester, ) async { addTearDown(tester.view.resetViewInsets); await _pumpApp(tester); final feed = find.byKey(const Key('recommend-page')); final before = tester.getRect(feed); tester.view.viewInsets = const FakeViewPadding(bottom: 300); await tester.pump(); expect( tester .widget(find.byType(Scaffold).first) .resizeToAvoidBottomInset, isFalse, ); expect(tester.getRect(feed), before); }); testWidgets('should_render_recommend_immersive_player_and_inner_top_nav', ( tester, ) async { await _pumpApp(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); _expectNoFloatingOverlays(); await tester.tap(find.byKey(const Key('recommend-tab-playlist'))); await tester.pumpAndSettle(); _drainLayoutExceptions(tester); 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('recommend-tab-playlist'))); await tester.pumpAndSettle(); _drainLayoutExceptions(tester); expect(find.byKey(const Key('recommend-section-hits')), findsOneWidget); expect(find.byType(AppBottomNav), findsOneWidget); _expectNoFloatingOverlays(); await tester.ensureVisible(find.byKey(const Key('recommend-tab-all'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('recommend-tab-all'))); await tester.pumpAndSettle(); _drainLayoutExceptions(tester); expect(find.byKey(const Key('all-dramas-grid')), findsOneWidget); for (var row = 0; row < 3; 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('recommend-search'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('search-page')), findsOneWidget); expect(find.byKey(const Key('search-back')), findsOneWidget); expect(find.byKey(const Key('search-cancel')), findsOneWidget); expect(find.byType(AppBottomNav), findsNothing); expect( tester .widget(find.byKey(const Key('discover-search'))) .focusNode ?.hasFocus, isTrue, ); await tester.tap(find.byKey(const Key('search-cancel'))); 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); await tester.enterText(find.byKey(const Key('discover-search')), 'signal'); await tester.pump(const Duration(milliseconds: 400)); await tester.pumpAndSettle(); expect(find.byKey(const Key('search-results-grid')), findsOneWidget); final results = tester.widget( find.byKey(const Key('search-results-grid')), ); final delegate = results.gridDelegate as SliverGridDelegateWithFixedCrossAxisCount; expect(delegate.crossAxisCount, 2); expect(delegate.crossAxisSpacing, 12); expect(delegate.mainAxisSpacing, 16); expect(results.padding, const EdgeInsets.fromLTRB(14, 12, 14, 24)); await tester.tap(find.byKey(const Key('search-back'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('recommend-channel-nav')), findsOneWidget); expect(find.byKey(const Key('recommend-tab-7')), findsOneWidget); }); testWidgets('should_open_player_from_all_dramas_grid_card', (tester) async { await _pumpApp(tester); await tester.ensureVisible(find.byKey(const Key('recommend-tab-all'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('recommend-tab-all'))); await tester.pumpAndSettle(); _drainLayoutExceptions(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('recommend-feed-player')), findsOneWidget); expect(tester.takeException(), isNull); await tester.tap(find.byKey(const Key('tab-recommend'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('recommend-channel-nav')), findsOneWidget); expect(tester.takeException(), isNull); }); } const _tabKeys = [ Key('tab-home'), Key('tab-recommend'), Key('tab-reward'), Key('tab-follow'), Key('tab-mine'), ]; void _expectNoFloatingOverlays() { expect(find.byKey(const Key('mine-mini-player')), findsNothing); expect(find.byType(FloatingActionButton), findsNothing); } /// 剧单卡片高度贴着 228pt 上限,测试字体(Ahem)行高比真机字体大, /// 会溢出约 2.7pt;这里消费该测试字体差异。 void _drainLayoutExceptions(WidgetTester tester) { while (tester.takeException() != null) {} } 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), searchRepository: catalog, watchHistoryRepository: InMemoryWatchHistoryRepository(), favoriteRepository: InMemoryFavoriteRepository(cards: catalog.dramas), playbackRepository: FakePlaybackRepository(), commentRepository: FakeCommentRepository(), interactionRepository: FakeInteractionRepository(), haptics: FakeHaptics(), ), ); await tester.pumpAndSettle(); }