feat(flutter): 优化登录页与底部导航

This commit is contained in:
Zhou Weidong committed 2026-10-17 16:11:01 +08:00
1 parent e52d81881c
commit e609e3ad8d
12 files changed
+702 -189

No files matched your search

+223 -21
View File
@@ -1,9 +1,12 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:provider/provider.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 'package:fruitshorts/core/data/models/auth_session.dart';
import 'package:fruitshorts/core/data/remote/auth_api.dart';
import 'package:fruitshorts/core/data/remote/api_client.dart';
import 'package:fruitshorts/core/data/services/auth_controller.dart';
@@ -20,15 +23,22 @@ void main() {
InMemorySharedPreferencesAsync.empty();
});
Future<void> pumpAuthPage(WidgetTester tester, {bool signUp = false}) async {
Future<void> pumpAuthPage(
WidgetTester tester, {
bool signUp = false,
AuthApi? api,
}) async {
final auth = AuthController(
AuthApi(
// 这个测试不联网:请求一律 500,正好用于验证"本地校验没过就不发请求"
ApiClient(
baseUrl: 'http://127.0.0.1:1',
adapter: FakeHttpAdapter((_) async => FakeHttpResponse('{}', 500)),
),
),
api ??
AuthApi(
// 这个测试不联网:请求一律 500,正好用于验证"本地校验没过就不发请求"
ApiClient(
baseUrl: 'http://127.0.0.1:1',
adapter: FakeHttpAdapter(
(_) async => FakeHttpResponse('{}', 500),
),
),
),
AuthSessionStore(),
);
await tester.pumpWidget(
@@ -45,7 +55,9 @@ void main() {
await tester.pumpAndSettle();
}
testWidgets('should_show_email_error_and_stay_on_page_when_form_is_empty', (tester) async {
testWidgets('should_show_email_error_and_stay_on_page_when_form_is_empty', (
tester,
) async {
await pumpAuthPage(tester);
expect(find.text('Welcome back'), findsOneWidget);
@@ -57,25 +69,215 @@ void main() {
expect(find.text('Welcome back'), findsOneWidget);
});
testWidgets('should_reject_weak_password_locally_when_only_letters_are_typed', (tester) async {
testWidgets(
'should_reject_weak_password_locally_when_only_letters_are_typed',
(tester) async {
await pumpAuthPage(tester);
await tester.enterText(find.byType(TextField).first, 'a@b.com');
await tester.enterText(find.byType(TextField).last, 'abcdefgh');
await tester.tap(find.byType(FilledButton));
await tester.pumpAndSettle();
expect(
find.text('Password needs both letters and numbers'),
findsOneWidget,
);
},
);
testWidgets(
'should_flip_between_sign_in_and_sign_up_when_switch_link_is_tapped',
(tester) async {
await pumpAuthPage(tester);
expect(find.text('Welcome back'), findsOneWidget);
await tester.tap(find.text('New here? Create an account'));
await tester.pumpAndSettle();
expect(find.text('Create your account'), findsOneWidget);
expect(find.text('Already have an account? Sign in'), findsOneWidget);
},
);
testWidgets('should_show_brand_and_form_accessibility_when_page_opens', (
tester,
) async {
await pumpAuthPage(tester);
await tester.enterText(find.byType(TextField).first, 'a@b.com');
await tester.enterText(find.byType(TextField).last, 'abcdefgh');
await tester.tap(find.byType(FilledButton));
await tester.pumpAndSettle();
expect(find.text('Password needs both letters and numbers'), findsOneWidget);
expect(find.text('FruitShorts'), findsOneWidget);
expect(find.text('Short dramas made for Indonesia'), findsOneWidget);
final email = tester.widget<TextField>(find.byKey(const Key('auth-email')));
final password = tester.widget<TextField>(
find.byKey(const Key('auth-password')),
);
expect(email.autofillHints, contains(AutofillHints.email));
expect(password.autofillHints, contains(AutofillHints.password));
expect(
tester.getSize(find.byKey(const Key('auth-submit'))).height,
greaterThanOrEqualTo(44),
);
expect(
tester.getSize(find.byKey(const Key('auth-password-visibility'))).height,
greaterThanOrEqualTo(44),
);
expect(
tester.getSize(find.byKey(const Key('auth-switch-mode'))).height,
greaterThanOrEqualTo(44),
);
});
testWidgets('should_flip_between_sign_in_and_sign_up_when_switch_link_is_tapped', (tester) async {
testWidgets('should_toggle_password_visibility_when_eye_button_is_tapped', (
tester,
) async {
await pumpAuthPage(tester);
expect(find.text('Welcome back'), findsOneWidget);
expect(
tester
.widget<TextField>(find.byKey(const Key('auth-password')))
.obscureText,
isTrue,
);
await tester.tap(find.text('New here? Create an account'));
await tester.tap(find.byKey(const Key('auth-password-visibility')));
await tester.pump();
expect(
tester
.widget<TextField>(find.byKey(const Key('auth-password')))
.obscureText,
isFalse,
);
expect(find.byTooltip('Hide password'), findsOneWidget);
});
testWidgets('should_move_focus_from_email_to_password_when_next_is_pressed', (
tester,
) async {
await pumpAuthPage(tester);
await tester.tap(find.byKey(const Key('auth-email')));
await tester.testTextInput.receiveAction(TextInputAction.next);
await tester.pump();
expect(
FocusManager.instance.primaryFocus?.debugLabel,
'auth-password-focus',
);
});
testWidgets('should_disable_form_and_submit_while_done_action_is_pending', (
tester,
) async {
final api = _PendingAuthApi();
await pumpAuthPage(tester, api: api);
await tester.enterText(find.byKey(const Key('auth-email')), 'a@b.com');
await tester.enterText(find.byKey(const Key('auth-password')), 'abcd1234');
await tester.tap(find.byKey(const Key('auth-password')));
await tester.testTextInput.receiveAction(TextInputAction.done);
await tester.pump();
expect(api.loginCalls, 1);
expect(
tester.widget<TextField>(find.byKey(const Key('auth-email'))).enabled,
isFalse,
);
expect(
tester.widget<TextField>(find.byKey(const Key('auth-password'))).enabled,
isFalse,
);
expect(
tester
.widget<IconButton>(find.byKey(const Key('auth-password-visibility')))
.onPressed,
isNull,
);
expect(
tester
.widget<FilledButton>(find.byKey(const Key('auth-submit')))
.onPressed,
isNull,
);
expect(
tester
.widget<TextButton>(find.byKey(const Key('auth-switch-mode')))
.onPressed,
isNull,
);
api.release();
await tester.pumpAndSettle();
});
testWidgets('should_render_validation_error_with_theme_semantic_color', (
tester,
) async {
await pumpAuthPage(tester);
await tester.enterText(find.byKey(const Key('auth-email')), 'a@b.com');
await tester.enterText(find.byKey(const Key('auth-password')), 'abcdefgh');
await tester.tap(find.byKey(const Key('auth-submit')));
await tester.pumpAndSettle();
expect(find.text('Create your account'), findsOneWidget);
expect(find.text('Already have an account? Sign in'), findsOneWidget);
final errorFinder = find.byKey(const Key('auth-error'));
final error = tester.widget<Text>(errorFinder);
final expected = Theme.of(tester.element(errorFinder)).colorScheme.error;
expect(error.style?.color, expected);
});
testWidgets(
'should_keep_submit_reachable_when_keyboard_opens_on_small_screen',
(tester) async {
tester.view.physicalSize = const Size(320, 568);
tester.view.devicePixelRatio = 1;
tester.view.viewInsets = const FakeViewPadding(bottom: 260);
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
addTearDown(tester.view.resetViewInsets);
await pumpAuthPage(tester);
await tester.scrollUntilVisible(
find.byKey(const Key('auth-submit')),
120,
scrollable: find
.descendant(
of: find.byKey(const Key('auth-scroll')),
matching: find.byType(Scrollable),
)
.first,
);
await tester.pumpAndSettle();
expect(
find.byKey(const Key('auth-submit')).hitTestable(),
findsOneWidget,
);
expect(tester.takeException(), isNull);
},
);
}
class _PendingAuthApi extends AuthApi {
_PendingAuthApi()
: super(
ApiClient(
baseUrl: 'http://127.0.0.1:1',
adapter: FakeHttpAdapter((_) async => FakeHttpResponse('{}', 500)),
),
);
final _gate = Completer<void>();
int loginCalls = 0;
void release() => _gate.complete();
@override
Future<AuthSession> login({
required String email,
required String password,
required String deviceId,
String platform = '',
String appVersion = '',
}) async {
loginCalls++;
await _gate.future;
throw const ApiException(500, 'pending failure');
}
}
+9
View File
@@ -45,4 +45,13 @@ void main() {
expect(l10n.episodeProgress(2, 10), contains('2'));
expect(l10n.episodeProgress(2, 10), contains('10'));
});
test('should_keep_auth_ui_copy_in_all_supported_locales', () async {
for (final locale in const [Locale('en'), Locale('zh'), Locale('id')]) {
final l10n = await AppLocalizations.delegate.load(locale);
expect(l10n.authBrandTagline, isNotEmpty);
expect(l10n.showPassword, isNotEmpty);
expect(l10n.hidePassword, isNotEmpty);
}
});
}
+126
View File
@@ -0,0 +1,126 @@
// 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/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/theme/app_colors.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_catalog.dart';
import '../support/fake_category.dart';
import '../support/fake_interaction_repositories.dart';
import '../support/widget_player.dart';
const _keys = [
Key('home-tabs-bar'),
Key('tab-discover'),
Key('tab-home'),
Key('tab-my-list'),
Key('tab-my'),
];
void main() {
setUp(() {
SharedPreferencesAsyncPlatform.instance =
InMemorySharedPreferencesAsync.withData({
'auth.accessToken': 'test-access',
'auth.refreshToken': 'test-refresh',
'auth.user': '{"uid":"9","nickname":"Viewer","avatarUrl":""}',
});
});
testWidgets('should_theme_four_tabs_and_switch_selected_state_when_tapped', (
tester,
) async {
await _pumpApp(tester);
for (final key in _keys) {
expect(find.byKey(key), findsOneWidget);
}
for (final key in _keys.skip(1)) {
expect(tester.getSize(find.byKey(key)).height, greaterThanOrEqualTo(48));
}
final barFinder = find.byKey(const Key('home-tabs-bar'));
final bar = tester.widget<Container>(barFinder);
final decoration = bar.decoration as BoxDecoration;
final border = decoration.border as Border;
final context = tester.element(barFinder);
expect(decoration.color, AppColors.bar);
expect(border.top.color, Theme.of(context).colorScheme.outlineVariant);
expect(
tester
.widget<AnimatedContainer>(
find.descendant(
of: find.byKey(const Key('tab-home')),
matching: find.byType(AnimatedContainer),
),
)
.duration,
lessThanOrEqualTo(const Duration(milliseconds: 200)),
);
expect(
find.descendant(
of: find.byKey(const Key('tab-home')),
matching: find.byIcon(Icons.movie_rounded),
),
findsOneWidget,
);
await tester.tap(find.byKey(const Key('tab-discover')));
await tester.pumpAndSettle();
expect(
find.descendant(
of: find.byKey(const Key('tab-discover')),
matching: find.byIcon(Icons.home_rounded),
),
findsOneWidget,
);
expect(
find.descendant(
of: find.byKey(const Key('tab-home')),
matching: find.byIcon(Icons.movie_outlined),
),
findsOneWidget,
);
});
testWidgets('should_hide_themed_navigation_when_immersive_mode_is_enabled', (
tester,
) async {
await _pumpApp(tester);
await tester.tap(find.byTooltip('Clear screen'));
await tester.pumpAndSettle();
for (final key in _keys) {
expect(find.byKey(key), findsNothing);
}
});
}
Future<void> _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(cards: catalog.dramas),
playbackRepository: FakePlaybackRepository(),
commentRepository: FakeCommentRepository(),
interactionRepository: FakeInteractionRepository(),
),
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('tab-home')));
await tester.pumpAndSettle();
}