修复用户端个人头像展示

This commit is contained in:
czl231
2026-09-02 22:48:47 +08:00
parent 2033a9407e
commit 7cc56f955f
12 changed files with 334 additions and 23 deletions

View File

@@ -0,0 +1,54 @@
// 功能描述:验证受保护头像二进制接口的鉴权读取和异常处理。
// 版本1.1.0
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
import 'package:user_app/data/services/api_client.dart';
/// 验证头像字节、鉴权请求头和资源不存在状态。
void main() {
test('头像接口携带令牌并返回二进制内容', () async {
final expectedBytes = Uint8List.fromList(const [1, 2, 3]);
final client = ApiClient(
() => 'JWT test-token',
baseUrl: 'https://api.example.com',
client: MockClient((request) async {
expect(request.url.path, '/heqi/client/v1/user/auth/avatar');
expect(request.headers['authorization'], 'JWT test-token');
return http.Response.bytes(expectedBytes, 200);
}),
);
expect(
await client.getBytes('/heqi/client/v1/user/auth/avatar'),
expectedBytes,
);
});
test('头像不存在时返回空值', () async {
final client = ApiClient(
() => 'JWT test-token',
baseUrl: 'https://api.example.com',
client: MockClient((request) async => http.Response('', 404)),
);
expect(await client.getBytes('/avatar'), isNull);
});
test('头像接口异常时返回中文错误', () async {
final client = ApiClient(
() => 'JWT test-token',
baseUrl: 'https://api.example.com',
client: MockClient((request) async => http.Response('', 500)),
);
expect(
() => client.getBytes('/avatar'),
throwsA(
isA<ApiException>().having((error) => error.message, 'message', '头像加载失败'),
),
);
});
}

View 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);
});
}