284 lines
8.6 KiB
Dart
284 lines
8.6 KiB
Dart
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';
|
|
import 'package:fruitshorts/core/data/services/auth_session_store.dart';
|
|
import 'package:fruitshorts/l10n/app_localizations.dart';
|
|
import 'package:fruitshorts/pages/auth/auth_view.dart';
|
|
|
|
import '../support/fake_http_adapter.dart';
|
|
|
|
/// 登录/注册页的可读证据:校验提示 + 登录/注册切换 + 未登录时不会去发请求。
|
|
void main() {
|
|
setUp(() {
|
|
SharedPreferencesAsyncPlatform.instance =
|
|
InMemorySharedPreferencesAsync.empty();
|
|
});
|
|
|
|
Future<void> pumpAuthPage(
|
|
WidgetTester tester, {
|
|
bool signUp = false,
|
|
AuthApi? api,
|
|
}) async {
|
|
final auth = AuthController(
|
|
api ??
|
|
AuthApi(
|
|
// 这个测试不联网:请求一律 500,正好用于验证"本地校验没过就不发请求"
|
|
ApiClient(
|
|
baseUrl: 'http://127.0.0.1:1',
|
|
adapter: FakeHttpAdapter(
|
|
(_) async => FakeHttpResponse('{}', 500),
|
|
),
|
|
),
|
|
),
|
|
AuthSessionStore(),
|
|
);
|
|
await tester.pumpWidget(
|
|
ChangeNotifierProvider<AuthController>.value(
|
|
value: auth,
|
|
child: MaterialApp(
|
|
locale: const Locale('en'),
|
|
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
|
supportedLocales: AppLocalizations.supportedLocales,
|
|
home: AuthPage(signUpMode: signUp),
|
|
),
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
}
|
|
|
|
testWidgets('should_show_email_error_and_stay_on_page_when_form_is_empty', (
|
|
tester,
|
|
) async {
|
|
await pumpAuthPage(tester);
|
|
|
|
expect(find.text('Welcome back'), findsOneWidget);
|
|
|
|
await tester.tap(find.byType(FilledButton));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(find.text('Enter your email'), findsOneWidget);
|
|
expect(find.text('Welcome back'), findsOneWidget);
|
|
});
|
|
|
|
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);
|
|
|
|
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_toggle_password_visibility_when_eye_button_is_tapped', (
|
|
tester,
|
|
) async {
|
|
await pumpAuthPage(tester);
|
|
expect(
|
|
tester
|
|
.widget<TextField>(find.byKey(const Key('auth-password')))
|
|
.obscureText,
|
|
isTrue,
|
|
);
|
|
|
|
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();
|
|
|
|
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');
|
|
}
|
|
}
|