修复用户端个人头像展示
This commit is contained in:
71
apps/user_app/test/ui/profile_avatar_test.dart
Normal file
71
apps/user_app/test/ui/profile_avatar_test.dart
Normal file
@@ -0,0 +1,71 @@
|
||||
// 功能描述:验证用户头像的真实图片、空值占位和加载失败回退状态。
|
||||
// 版本:1.0.0
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:user_app/data/services/api_client.dart';
|
||||
import 'package:user_app/ui/features/profile/profile_avatar.dart';
|
||||
|
||||
/// 验证头像组件在不同图片状态下保持可用。
|
||||
void main() {
|
||||
/// 构建统一头像测试宿主。
|
||||
Widget testHost({
|
||||
required String name,
|
||||
required Uint8List? imageBytes,
|
||||
}) => MaterialApp(
|
||||
home: Scaffold(
|
||||
body: ProfileAvatar(
|
||||
name: name,
|
||||
imageBytes: imageBytes,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
testWidgets('有头像时使用圆形裁剪图片', (tester) async {
|
||||
final imageBytes = Uint8List.fromList(
|
||||
base64Decode(
|
||||
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M/wHwAF/gL+X8l7WQAAAABJRU5ErkJggg==',
|
||||
),
|
||||
);
|
||||
await tester.pumpWidget(
|
||||
testHost(
|
||||
name: 'cyyy',
|
||||
imageBytes: imageBytes,
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final image = tester.widget<Image>(find.byKey(const Key('profile-avatar-image')));
|
||||
expect(image.fit, BoxFit.cover);
|
||||
expect(image.image, isA<MemoryImage>());
|
||||
});
|
||||
|
||||
testWidgets('头像为空时显示用户名首字母', (tester) async {
|
||||
await tester.pumpWidget(testHost(name: 'cyyy', imageBytes: null));
|
||||
|
||||
expect(find.text('c'), findsOneWidget);
|
||||
expect(find.byKey(const Key('profile-avatar-image')), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('头像加载失败时回退用户名首字母', (tester) async {
|
||||
await tester.pumpWidget(
|
||||
testHost(
|
||||
name: 'cyyy',
|
||||
imageBytes: Uint8List.fromList(const [0]),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('c'), findsOneWidget);
|
||||
});
|
||||
|
||||
test('头像接口失败时安全返回空值', () async {
|
||||
final result = await loadProfileAvatarSafely(
|
||||
() => throw const ApiException(500, '头像加载失败'),
|
||||
);
|
||||
|
||||
expect(result, isNull);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user