import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:nontonki/app.dart'; import 'package:nontonki/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/widget_player.dart'; void main() { /// 默认按「已登录」起测:账号体系接上后,"登录"不再是本地演示开关, /// 而是本地会话(这里直接给 shared_preferences 的内存实现塞一份会话)。 /// 昵称/uid 与演示文案保持一致,避免为显示细节改写既有断言。 void seedSignedInSession() { SharedPreferencesAsyncPlatform.instance = InMemorySharedPreferencesAsync.withData({ 'auth.accessToken': 'test-access', 'auth.refreshToken': 'test-refresh', 'auth.user': '{"uid":"M1CZWVYE","nickname":"Vicky sheng","avatarUrl":""}', }); } setUp(() { seedSignedInSession(); }); testWidgets( 'home swipe promotes next without recreating it or showing loading', (tester) async { final created = []; await tester.pumpWidget( LumaDramaApp( playerFactory: (episode) { created.add(episode.id); return WidgetPlayer(); }, catalogRepository: FakeCatalogRepository(), ), ); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('tab-home'))); await tester.pumpAndSettle(); expect(created, hasLength(2)); expect(find.byType(CircularProgressIndicator), findsNothing); await tester.drag(find.byType(PageView), const Offset(0, -550)); await tester.pump(); expect(find.byType(CircularProgressIndicator), findsNothing); await tester.pumpAndSettle(); expect(find.text('City Lights & Velvet Nights'), findsOneWidget); expect(created, hasLength(3)); expect(created.toSet(), hasLength(3)); await tester.pumpWidget(const SizedBox.shrink()); await tester.pumpAndSettle(); }, ); testWidgets('immersive home opens the series detail', (tester) async { tester.view.physicalSize = const Size(390, 844); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget( LumaDramaApp(playerFactory: createWidgetPlayer, catalogRepository: FakeCatalogRepository()), ); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('tab-home'))); await tester.pumpAndSettle(); expect(find.text('The Signal'), findsOneWidget); expect(find.byType(Slider), findsOneWidget); expect(find.text('Home'), findsWidgets); expect(find.textContaining('Playing now'), findsOneWidget); expect(find.text('Recommended'), findsNothing); expect(find.text('Following'), findsNothing); expect(find.byTooltip('Episodes'), findsWidgets); expect(find.byTooltip('Clear screen'), findsOneWidget); expect(find.byTooltip('Picture in picture'), findsOneWidget); expect(find.text('Watch Full'), findsNothing); expect(find.byTooltip('Explore series'), findsNothing); }); testWidgets('clear screen hides tabs and restores them on tap', ( tester, ) async { tester.view.physicalSize = const Size(390, 844); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget( LumaDramaApp(playerFactory: createWidgetPlayer, catalogRepository: FakeCatalogRepository()), ); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('tab-home'))); await tester.pumpAndSettle(); await tester.tap(find.byTooltip('Clear screen')); await tester.pumpAndSettle(); expect(find.byKey(const Key('home-tabs-bar')), findsNothing); expect(find.byTooltip('Exit clear screen'), findsOneWidget); await tester.tap(find.byTooltip('Exit clear screen')); await tester.pumpAndSettle(); expect(find.byKey(const Key('home-tabs-bar')), findsOneWidget); expect(find.byTooltip('Clear screen'), findsOneWidget); expect(tester.takeException(), isNull); }); testWidgets('home quick drawer opens current series and switches root tabs', ( tester, ) async { tester.view.physicalSize = const Size(360, 720); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget( LumaDramaApp(playerFactory: createWidgetPlayer, catalogRepository: FakeCatalogRepository()), ); await tester.pumpAndSettle(); expect(find.text('Continue Watching'), findsOneWidget); await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); expect( tester .widget(find.byType(PlayerPage, skipOffstage: false)) .active, isFalse, ); expect(find.text('Quick access'), findsOneWidget); expect( find.descendant( of: find.byType(Drawer), matching: find.text('7 EPISODES'), ), findsOneWidget, ); expect(tester.takeException(), isNull); await tester.tap(find.byKey(const Key('drawer-open-series'))); await tester.pumpAndSettle(); expect(find.text('Watch episode 1'), findsOneWidget); await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); expect( tester .widget(find.byType(PlayerPage, skipOffstage: false)) .active, isFalse, ); expect(find.text('Continue Watching'), findsOneWidget); await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('drawer-discover'))); await tester.pumpAndSettle(); expect(find.text('Continue Watching'), findsOneWidget); expect( Navigator.of(tester.element(find.text('Continue Watching'))).canPop(), isFalse, ); }); testWidgets('interface language changes without a caption control', ( tester, ) async { tester.view.physicalSize = const Size(390, 844); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget( LumaDramaApp(playerFactory: createWidgetPlayer, catalogRepository: FakeCatalogRepository()), ); await tester.pumpAndSettle(); expect(find.byTooltip('Captions'), findsNothing); await tester.tap(find.byKey(const Key('tab-my'))); await tester.pumpAndSettle(); expect(find.text('Vicky sheng'), findsOneWidget); expect(find.text('Coins'), findsOneWidget); expect(find.text('PREFERENCES'), findsOneWidget); await tester.tap(find.text('App Language')); await tester.pumpAndSettle(); await tester.tap(find.text('简体中文').last); await tester.pumpAndSettle(); expect(find.text('我的'), findsWidgets); expect(find.text('我的账户'), findsOneWidget); expect(find.text('首页'), findsOneWidget); await tester.tap(find.text('首页')); await tester.pumpAndSettle(); expect(find.text('为你推荐'), findsOneWidget); expect(find.text('热门'), findsOneWidget); }); testWidgets('home controls fit a compact Android viewport', (tester) async { tester.view.physicalSize = const Size(360, 720); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget( LumaDramaApp(playerFactory: createWidgetPlayer, catalogRepository: FakeCatalogRepository()), ); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('tab-home'))); await tester.pumpAndSettle(); expect(find.text('The Signal'), findsOneWidget); await tester.drag(find.byType(PageView), const Offset(0, -500)); await tester.pumpAndSettle(); expect(find.text('City Lights & Velvet Nights'), findsOneWidget); await tester.tap(find.byKey(const Key('tab-discover'))); await tester.pumpAndSettle(); await tester.tap(find.text('City Lights & Velvet Nights')); await tester.pumpAndSettle(); expect(find.byType(Slider), findsOneWidget); expect(tester.takeException(), isNull); }); testWidgets('My page fits a compact Android viewport and returns home', ( tester, ) async { tester.view.physicalSize = const Size(360, 720); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget( LumaDramaApp(playerFactory: createWidgetPlayer, catalogRepository: FakeCatalogRepository()), ); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('tab-my'))); await tester.pumpAndSettle(); expect(tester.takeException(), isNull); expect(find.text('Vicky sheng'), findsOneWidget); expect(find.text('Coins'), findsOneWidget); expect(tester.takeException(), isNull); await tester.tap(find.text('Shorts')); await tester.pumpAndSettle(); expect(find.text('The Signal'), findsOneWidget); }); testWidgets( 'Discover shows three mock playable dramas and one pending poster', (tester) async { tester.view.physicalSize = const Size(360, 720); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget( LumaDramaApp(playerFactory: createWidgetPlayer, catalogRepository: FakeCatalogRepository()), ); await tester.pumpAndSettle(); await tester.tap(find.text('Home')); await tester.pumpAndSettle(); expect(find.text('For You'), findsOneWidget); expect(find.text('Continue Watching'), findsOneWidget); expect(find.text('Rewards'), findsOneWidget); expect(find.text(r'$ 0'), findsOneWidget); expect(find.text('Urban Romance', skipOffstage: false), findsOneWidget); expect( find.text('Action · Thriller', skipOffstage: false), findsOneWidget, ); expect(find.byTooltip('Search'), findsNothing); expect(tester.takeException(), isNull); await tester.tap(find.byKey(const Key('tab-my-list'))); await tester.pumpAndSettle(); expect(find.text('No shows here yet.'), findsOneWidget); await tester.tap(find.text('Shorts')); await tester.pumpAndSettle(); expect(find.text('The Signal'), findsOneWidget); }, ); testWidgets('bottom tabs stay on one route', (tester) async { tester.view.physicalSize = const Size(390, 844); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget( LumaDramaApp(playerFactory: createWidgetPlayer, catalogRepository: FakeCatalogRepository()), ); await tester.pumpAndSettle(); await tester.tap(find.text('Home')); await tester.pumpAndSettle(); expect(find.text('Continue Watching'), findsOneWidget); expect(find.byTooltip('Search'), findsNothing); expect( Navigator.of(tester.element(find.text('Continue Watching'))).canPop(), isFalse, ); await tester.tap(find.byKey(const Key('tab-my-list'))); await tester.pumpAndSettle(); expect(find.text('History'), findsOneWidget); expect( Navigator.of(tester.element(find.text('History'))).canPop(), isFalse, ); await tester.tap(find.text('Watchlist')); await tester.pumpAndSettle(); expect(find.text('No shows here yet.'), findsOneWidget); await tester.tap(find.byKey(const Key('tab-discover'))); await tester.pumpAndSettle(); expect(find.text('Continue Watching'), findsOneWidget); expect(find.byTooltip('Search'), findsNothing); expect( Navigator.of(tester.element(find.text('Continue Watching'))).canPop(), isFalse, ); await tester.tap(find.byKey(const Key('tab-my-list'))); await tester.pumpAndSettle(); expect(find.text('No shows here yet.'), findsOneWidget); await tester.tap(find.byKey(const Key('tab-home'))); await tester.pumpAndSettle(); expect(find.text('The Signal'), findsOneWidget); expect( Navigator.of(tester.element(find.text('The Signal'))).canPop(), isFalse, ); }); testWidgets( 'home swipe changes drama, entered player swipe changes episode', (tester) async { tester.view.physicalSize = const Size(390, 844); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget( LumaDramaApp(playerFactory: createWidgetPlayer, catalogRepository: FakeCatalogRepository()), ); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('tab-home'))); await tester.pumpAndSettle(); expect(find.text('The Signal'), findsOneWidget); await tester.drag(find.byType(PageView), const Offset(0, -550)); await tester.pumpAndSettle(); expect(find.text('City Lights & Velvet Nights'), findsOneWidget); expect(find.textContaining('Episode 1'), findsOneWidget); await tester.tap(find.byKey(const Key('tab-discover'))); await tester.pumpAndSettle(); await tester.tap(find.text('City Lights & Velvet Nights')); await tester.pumpAndSettle(); expect(find.byType(Slider), findsOneWidget); await tester.drag(find.byType(PageView), const Offset(0, -550)); await tester.pumpAndSettle(); expect(find.text('Episode 2 · Playing now'), findsOneWidget); expect(find.text('City Lights & Velvet Nights'), findsWidgets); }, ); testWidgets('shorts page has no category navigation', (tester) async { tester.view.physicalSize = const Size(390, 844); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget( LumaDramaApp(playerFactory: createWidgetPlayer, catalogRepository: FakeCatalogRepository()), ); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('tab-home'))); await tester.pumpAndSettle(); expect(find.text('Following'), findsNothing); expect(find.text('Animated'), findsNothing); expect(find.text('Live Action'), findsNothing); expect(find.text('Recommended'), findsNothing); expect(find.text('The Signal'), findsOneWidget); await tester.drag(find.byType(PageView), const Offset(0, -550)); await tester.pumpAndSettle(); expect(find.text('City Lights & Velvet Nights'), findsOneWidget); expect(tester.takeException(), isNull); }); testWidgets('right rail saves, comments, likes and invokes share in order', ( tester, ) async { tester.view.physicalSize = const Size(390, 844); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); String? sharedText; const channel = MethodChannel('com.stormg.lumadrama/share'); tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, ( call, ) async { sharedText = (call.arguments as Map)['text'] as String; return null; }); addTearDown( () => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler( channel, null, ), ); await tester.pumpWidget( LumaDramaApp(playerFactory: createWidgetPlayer, catalogRepository: FakeCatalogRepository()), ); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('tab-home'))); await tester.pumpAndSettle(); final keys = [ const Key('rail-like'), const Key('rail-save'), const Key('rail-comments'), const Key('rail-share'), ]; final tops = keys .map((key) => tester.getTopLeft(find.byKey(key)).dy) .toList(); expect(tops, orderedEquals(tops.toList()..sort())); await tester.tap(find.byKey(const Key('rail-save'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('rail-comments'))); await tester.pumpAndSettle(); expect(find.text('Only visible on this device'), findsOneWidget); await tester.enterText(find.byKey(const Key('comment-input')), 'Nice demo'); await tester.tap(find.byKey(const Key('comment-post'))); await tester.pumpAndSettle(); expect(find.text('Nice demo'), findsOneWidget); await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('rail-like'))); await tester.tap(find.byKey(const Key('rail-share'))); await tester.pumpAndSettle(); expect(sharedText, contains('The Signal')); expect(tester.takeException(), isNull); }); testWidgets('system back opens the drawer and stays on home', (tester) async { tester.view.physicalSize = const Size(390, 844); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget( LumaDramaApp(playerFactory: createWidgetPlayer, catalogRepository: FakeCatalogRepository()), ); await tester.pumpAndSettle(); expect(find.text('Continue Watching'), findsOneWidget); await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); expect(find.text('Quick access'), findsOneWidget); expect(find.byKey(const Key('home-tabs-bar')), findsOneWidget); await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); expect(find.text('Quick access'), findsNothing); expect(find.text('Continue Watching'), findsOneWidget); expect( Navigator.of( tester.element(find.byKey(const Key('home-tabs-bar'))), ).canPop(), isFalse, ); }); testWidgets('My watchlist lists saved dramas across the home feed', ( tester, ) async { tester.view.physicalSize = const Size(390, 844); tester.view.devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget( LumaDramaApp(playerFactory: createWidgetPlayer, catalogRepository: FakeCatalogRepository()), ); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('tab-home'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('rail-save'))); await tester.pumpAndSettle(); await tester.drag(find.byType(PageView), const Offset(0, -550)); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('rail-save'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('tab-my-list'))); await tester.pumpAndSettle(); await tester.tap(find.text('Watchlist')); await tester.pumpAndSettle(); expect(find.text('The Signal'), findsOneWidget); expect(find.text('City Lights & Velvet Nights'), findsWidgets); await tester.tap(find.text('The Signal')); await tester.pumpAndSettle(); expect(find.text('Watch episode 1'), findsOneWidget); }); }