149 lines
5.5 KiB
Dart
149 lines
5.5 KiB
Dart
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_avatar.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<UserProfile>? pendingLoad;
|
|
UserProfile profile = const UserProfile(id: '7', nickname: 'Old name');
|
|
|
|
@override
|
|
Future<UserProfile> load() async =>
|
|
pendingLoad == null ? profile : await pendingLoad!.future;
|
|
|
|
@override
|
|
Future<UserProfile> save(String nickname, String signature) async {
|
|
profile = profile.copyWith(nickname: nickname, signature: signature);
|
|
return profile;
|
|
}
|
|
|
|
@override
|
|
Future<UserProfile?> chooseAvatar() async => null;
|
|
}
|
|
|
|
void main() {
|
|
for (final signature in ['', 'First line\nSecond line\nMore text']) {
|
|
testWidgets(
|
|
'profile stays centered and device loading preserves layout ($signature)',
|
|
(tester) async {
|
|
final repository = _ProfileRepository()
|
|
..pendingLoad = Completer<UserProfile>();
|
|
final devices = Completer<List<DeviceBrief>>();
|
|
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)),
|
|
);
|
|
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.getCenter(find.byKey(const ValueKey('profileIdentity'))).dy,
|
|
tester.getCenter(find.byType(ProfileAvatar)).dy,
|
|
);
|
|
final signatureFinder = find.byKey(const ValueKey('profileSignature'));
|
|
expect(tester.widget<Text>(signatureFinder).data, signature);
|
|
final identity = tester.getRect(
|
|
find.byKey(const ValueKey('profileIdentity')),
|
|
);
|
|
final avatar = tester.getRect(find.byType(ProfileAvatar));
|
|
expect(
|
|
tester.getRect(signatureFinder).bottom,
|
|
lessThanOrEqualTo(identity.bottom),
|
|
);
|
|
final settingsBefore = tester.getTopLeft(find.text(l10n.settingsTitle));
|
|
// Empty signatures occupy one text line, never a separate reserved block.
|
|
if (signature.isEmpty) {
|
|
expect(identity.height, lessThanOrEqualTo(avatar.height + 4));
|
|
}
|
|
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');
|
|
});
|
|
}
|