Files
flutterApp/test/_verify_logout_dialog_test.dart

133 lines
4.4 KiB
Dart

import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:get_it/get_it.dart';
import 'package:go_router/go_router.dart';
import 'package:maibu_satabot_v2/core/app/app_user_cubit.dart';
import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
import 'package:maibu_satabot_v2/core/router/route_paths.dart';
import 'package:maibu_satabot_v2/features/auth/presentation/bloc/auth_cubit.dart';
import 'package:maibu_satabot_v2/features/v2/my/presentation/pages/my_page_v2.dart';
class _ProfileTestAssets extends CachingAssetBundle {
@override
Future<ByteData> load(String key) => rootBundle.load(
key == 'assets/images/logo_me_bg.png' ? 'assets/images/app_logo.png' : key,
);
}
class _FakeAuth implements AuthCubit {
final completion = Completer<void>();
int logoutCalls = 0;
@override
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
@override
Future<void> logout() {
logoutCalls++;
return completion.future;
}
}
class _LoadedStrings extends LocalizationsDelegate<AppLocalizations> {
const _LoadedStrings(this.strings);
final AppLocalizations strings;
@override
bool isSupported(Locale locale) => locale == strings.locale;
@override
Future<AppLocalizations> load(Locale locale) => SynchronousFuture(strings);
@override
bool shouldReload(_LoadedStrings old) => old.strings != strings;
}
void main() {
final strings = <Locale, AppLocalizations>{};
setUpAll(() async {
for (final locale in AppLocalizations.supportedLocales) {
final loaded = AppLocalizations(locale);
expect(await loaded.load(), isTrue);
strings[locale] = loaded;
}
});
for (final locale in AppLocalizations.supportedLocales) {
testWidgets('logout dialog cancels and confirms in $locale', (tester) async {
tester.view.devicePixelRatio = 1;
tester.view.physicalSize = const Size(480, 800);
addTearDown(tester.view.resetDevicePixelRatio);
addTearDown(tester.view.resetPhysicalSize);
final auth = _FakeAuth();
GetIt.I.registerSingleton<AuthCubit>(auth);
addTearDown(() => GetIt.I.unregister<AuthCubit>());
final router = GoRouter(
initialLocation: RoutePaths.my,
routes: [
GoRoute(
path: RoutePaths.my,
builder: (_, _) => BlocProvider(
create: (_) => AppUserCubit(),
child: DefaultAssetBundle(
bundle: _ProfileTestAssets(),
child: const MyPageV2(),
),
),
),
GoRoute(
path: RoutePaths.login,
builder: (_, _) => const Scaffold(body: Text('Login page')),
),
],
);
addTearDown(router.dispose);
final l10n = strings[locale]!;
await tester.pumpWidget(MaterialApp.router(
routerConfig: router,
locale: locale,
supportedLocales: AppLocalizations.supportedLocales,
localizationsDelegates: [
_LoadedStrings(l10n),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
));
await tester.pumpAndSettle();
final label = l10n.translate('my_v2.logout');
final dialog = find.byType(Dialog);
final button = find.text(label);
await tester.ensureVisible(button);
await tester.tap(button);
await tester.pumpAndSettle();
expect(dialog, findsOneWidget);
expect(find.byType(AlertDialog), findsNothing);
expect(find.byIcon(Icons.logout_rounded), findsOneWidget);
expect(find.text(l10n.translate('my_v2.logout_confirm')), findsOneWidget);
await tester.tap(find.text(l10n.translate('my_v2.cancel')));
await tester.pumpAndSettle();
expect(dialog, findsNothing);
expect(auth.logoutCalls, 0);
await tester.tap(button);
await tester.pumpAndSettle();
await tester.tap(find.descendant(of: dialog, matching: button).last);
await tester.pumpAndSettle();
expect(auth.logoutCalls, 1);
expect(find.text('Login page'), findsNothing);
auth.completion.complete();
await tester.pumpAndSettle();
expect(find.text('Login page'), findsOneWidget);
});
}
}