// 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/models/auth_session.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/data/services/auth_session_store.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_account_repository.dart'; import '../support/fake_catalog.dart'; import '../support/fake_category.dart'; import '../support/fake_interaction_repositories.dart'; import '../support/fake_notice_repository.dart'; import '../support/widget_player.dart'; void main() { testWidgets('should_hide_account_entries_when_signed_out', (tester) async { SharedPreferencesAsyncPlatform.instance = InMemorySharedPreferencesAsync.empty(); await _pumpApp(tester); await tester.tap(find.byKey(const Key('tab-mine'))); await tester.pumpAndSettle(); await tester.ensureVisible(find.byKey(const Key('mine-settings'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('mine-settings'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('mine-account-card')), findsNothing); // 未登录时设置页不再提供登录入口,也不显示退出登录按钮。 expect(find.byKey(const Key('settings-page')), findsOneWidget); expect(find.byKey(const Key('settings-sign-out')), findsNothing); }); testWidgets('should_show_four_account_entries_when_signed_in', ( tester, ) async { SharedPreferencesAsyncPlatform.instance = InMemorySharedPreferencesAsync.empty(); await AuthSessionStore().save( const AuthSession( accessToken: 'access', refreshToken: 'refresh', user: AuthUser(uid: 'U-K19', nickname: 'Nadia'), ), ); await _pumpApp(tester); await tester.tap(find.byKey(const Key('tab-mine'))); await tester.pumpAndSettle(); await tester.ensureVisible(find.byKey(const Key('mine-settings'))); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('mine-settings'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('mine-account-card')), findsOneWidget); expect(find.byKey(const Key('mine-account-profile')), findsOneWidget); expect(find.byKey(const Key('mine-account-devices')), findsOneWidget); expect(find.byKey(const Key('mine-account-export')), findsOneWidget); expect(find.byKey(const Key('mine-account-deactivate')), findsOneWidget); expect(find.byKey(const Key('settings-notices')), findsNothing); final profileTop = tester .getTopLeft(find.byKey(const Key('mine-account-profile'))) .dy; final devicesTop = tester .getTopLeft(find.byKey(const Key('mine-account-devices'))) .dy; final exportTop = tester .getTopLeft(find.byKey(const Key('mine-account-export'))) .dy; final deactivateTop = tester .getTopLeft(find.byKey(const Key('mine-account-deactivate'))) .dy; expect(devicesTop, greaterThan(profileTop)); expect(exportTop, greaterThan(devicesTop)); expect(deactivateTop, greaterThan(exportTop)); }); } Future _pumpApp(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(), playbackRepository: FakePlaybackRepository(), commentRepository: FakeCommentRepository(), interactionRepository: FakeInteractionRepository(), noticeRepository: FakeNoticeRepository(), accountRepository: FakeAccountRepository(), ), ); await tester.pumpAndSettle(); }