import 'dart:async'; import 'package:cupertino_ui/cupertino_ui.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:go_router/go_router.dart'; import 'package:ttstd_family_care/app/app.dart'; import 'package:ttstd_family_care/features/device/data/device_providers.dart'; import 'package:ttstd_family_care/features/device/domain/device_models.dart'; import 'package:ttstd_family_care/features/profile/data/profile_repository_impl.dart'; import 'package:ttstd_family_care/features/profile/domain/profile_models.dart'; import 'package:ttstd_family_care/features/profile/domain/profile_repository.dart'; import 'package:ttstd_family_care/features/profile/presentation/profile_edit_page.dart'; import 'package:ttstd_family_care/features/profile/presentation/profile_tab.dart'; import 'package:ttstd_family_care/l10n/app_localizations.dart'; class _ProfileRepository implements ProfileRepository { Completer? pendingLoad; UserProfile profile = const UserProfile(id: '7', nickname: 'Old name'); @override Future load() async => pendingLoad == null ? profile : await pendingLoad!.future; @override Future save(String nickname, String signature) async { profile = profile.copyWith(nickname: nickname, signature: signature); return profile; } @override Future chooseAvatar() async => null; } void main() { for (final signature in ['', 'First line\nSecond line\nMore text']) { testWidgets( 'profile and devices load without shifting sections ($signature)', (tester) async { final repository = _ProfileRepository() ..pendingLoad = Completer(); final devices = Completer>(); final router = GoRouter( routes: [GoRoute(path: '/', builder: (_, _) => const ProfileTab())], ); addTearDown(router.dispose); await tester.pumpWidget( ProviderScope( overrides: [ profileRepositoryProvider.overrideWithValue(repository), myDevicesProvider.overrideWith((_) => devices.future), ], child: App(router: router), ), ); await tester.pump(); final l10n = AppLocalizations.of( tester.element(find.byType(ProfileTab)), ); final boundBefore = tester.getTopLeft(find.text(l10n.deviceBound)); final settingsBefore = tester.getTopLeft(find.text(l10n.settingsTitle)); repository.pendingLoad!.complete( UserProfile( id: '123456789012345678901234567890', nickname: 'A very long nickname that must stay on one line', signature: signature, ), ); await tester.pump(); await tester.pump(const Duration(milliseconds: 100)); expect(tester.getTopLeft(find.text(l10n.deviceBound)), boundBefore); expect( tester.getTopLeft(find.text(l10n.settingsTitle)), settingsBefore, ); devices.complete([ const DeviceBrief( serialno: 'one', snName: 'A very long device name that stays on one line', snModel: 'Tablet model', online: 1, ), ]); await tester.pumpAndSettle(); expect( tester.getTopLeft(find.text(l10n.settingsTitle)), settingsBefore, ); expect(tester.takeException(), isNull); }, ); } testWidgets('editing from profile updates the header after saving', ( tester, ) async { final repository = _ProfileRepository(); final router = GoRouter( initialLocation: '/profile', routes: [ GoRoute(path: '/profile', builder: (_, _) => const ProfileTab()), GoRoute( path: '/profile/edit', builder: (_, _) => const ProfileEditPage(), ), ], ); addTearDown(router.dispose); await tester.pumpWidget( ProviderScope( overrides: [ profileRepositoryProvider.overrideWithValue(repository), myDevicesProvider.overrideWith((_) async => []), ], child: App(router: router), ), ); await tester.pumpAndSettle(); expect(find.text('Old name'), findsOneWidget); final l10n = AppLocalizations.of(tester.element(find.byType(ProfileTab))); await tester.tap(find.text(l10n.profileEdit)); await tester.pumpAndSettle(); final fields = find.byType(CupertinoTextField); await tester.enterText(fields.at(0), 'New name'); await tester.enterText(fields.at(1), 'My signature'); await tester.tap(find.text(l10n.commonSave)); await tester.pumpAndSettle(); expect(router.routeInformationProvider.value.uri.path, '/profile'); expect(find.text('New name'), findsOneWidget); expect(find.text('My signature'), findsOneWidget); expect(repository.profile.nickname, 'New name'); }); }