mobile-eda/test/unit/localization_test.dart

112 lines
3.4 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
void main() {
group('Localization Tests', () {
test('AppLocalizations should support required locales', () {
final supportedLocales = AppLocalizations.supportedLocales;
// 检查是否支持中文(简体)
expect(
supportedLocales.any((locale) =>
locale.languageCode == 'zh' && locale.countryCode == 'CN',
),
true,
);
// 检查是否支持英文
expect(
supportedLocales.any((locale) =>
locale.languageCode == 'en',
),
true,
);
});
testWidgets('Chinese locale should load Chinese translations', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
locale: const Locale('zh', 'CN'),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: Builder(
builder: (context) {
return Scaffold(
body: Text(AppLocalizations.of(context)!.appTitle),
);
},
),
),
);
expect(find.text('Mobile EDA'), findsOneWidget);
});
testWidgets('English locale should load English translations', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
locale: const Locale('en', 'US'),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: Builder(
builder: (context) {
return Scaffold(
body: Text(AppLocalizations.of(context)!.homeScreen),
);
},
),
),
);
expect(find.text('Home'), findsOneWidget);
});
});
group('RTL Support Tests', () {
test('Arabic locale should be RTL', () {
const arabicLocale = Locale('ar', 'SA');
expect(arabicLocale.languageCode, 'ar');
});
testWidgets('RTL layout should be applied for Arabic', (WidgetTester tester) async {
await tester.pumpWidget(
MaterialApp(
locale: const Locale('ar', 'SA'),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
builder: (context, child) {
return Directionality(
textDirection: TextDirection.rtl,
child: child!,
);
},
home: const Scaffold(
body: Row(
children: [
Expanded(child: Text('First')),
Expanded(child: Text('Second')),
],
),
),
),
);
// RTL 布局中,第一个元素应该在右侧
final firstFinder = find.text('First');
final secondFinder = find.text('Second');
expect(firstFinder, findsOneWidget);
expect(secondFinder, findsOneWidget);
});
});
group('Translation Completeness Tests', () {
test('All locales should have same keys', () {
// 这个测试确保所有语言文件有相同的键
// 在实际项目中,可以通过解析 ARB 文件来验证
expect(true, true); // 占位符测试
});
});
}