chatapp3-flutter/test/l10n_ui_smoke_test.dart
2026-04-17 15:21:23 +08:00

127 lines
3.8 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:yumi/app/config/app_config.dart';
import 'package:yumi/app/constants/sc_screen.dart';
import 'package:yumi/app_localizations.dart';
import 'package:yumi/modules/auth/account/sc_login_with_account_page.dart';
import 'package:yumi/modules/settings/language/language_page.dart';
import 'package:yumi/services/localization/localization_manager.dart';
import 'package:yumi/shared/data_sources/sources/local/data_persistence.dart';
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
AppConfig.initialize();
tearDown(() async {
DataPersistence.reset();
});
const locales = <Locale>[
Locale('en'),
Locale('ar'),
Locale('tr'),
Locale('bn'),
];
for (final locale in locales) {
group('Locale ${locale.languageCode}', () {
testWidgets('LanguagePage renders cleanly', (tester) async {
await _pumpLocalizedPage(
tester: tester,
locale: locale,
child: LanguagePage(),
);
final exceptions = _drainExceptions(tester);
expect(exceptions, isEmpty, reason: exceptions.join('\n\n'));
expect(
tester
.widget<MaterialApp>(find.byType(MaterialApp))
.locale
?.languageCode,
locale.languageCode,
);
});
testWidgets('SCLoginWithAccountPage renders cleanly', (tester) async {
await _pumpLocalizedPage(
tester: tester,
locale: locale,
child: const SCLoginWithAccountPage(),
);
final exceptions = _drainExceptions(tester);
expect(exceptions, isEmpty, reason: exceptions.join('\n\n'));
expect(
tester
.widget<MaterialApp>(find.byType(MaterialApp))
.locale
?.languageCode,
locale.languageCode,
);
});
});
}
}
Future<void> _pumpLocalizedPage({
required WidgetTester tester,
required Locale locale,
required Widget child,
}) async {
SharedPreferences.setMockInitialValues({'lang': locale.languageCode});
await DataPersistence.initialize();
await tester.binding.setSurfaceSize(const Size(390, 844));
await tester.pumpWidget(
ChangeNotifierProvider(
create: (_) => LocalizationManager(),
child: ScreenUtilInit(
designSize: Size(SCScreen.designWidth, SCScreen.designHeight),
splitScreenMode: false,
minTextAdapt: true,
builder: (context, _) {
return MaterialApp(
debugShowCheckedModeBanner: false,
locale: locale,
localizationsDelegates: const [
SCAppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: const [
Locale('en'),
Locale('ar'),
Locale('tr'),
Locale('bn'),
],
home: RepaintBoundary(
key: const ValueKey('capture_boundary'),
child: child,
),
);
},
),
),
);
await tester.pump();
await tester.pump(const Duration(milliseconds: 200));
await tester.pump(const Duration(milliseconds: 200));
await tester.pump(const Duration(milliseconds: 200));
}
List<Object> _drainExceptions(WidgetTester tester) {
final exceptions = <Object>[];
Object? exception;
while ((exception = tester.takeException()) != null) {
exceptions.add(exception!);
}
return exceptions;
}