64 lines
2.2 KiB
Dart
64 lines
2.2 KiB
Dart
import 'package:flutter/widgets.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
|
|
import 'package:maibu_satabot_v2/core/localization/locale_cubit.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
class _FailingPreferences implements SharedPreferences {
|
|
@override
|
|
Object? get(String key) => null;
|
|
|
|
@override
|
|
Future<bool> setString(String key, String value) async {
|
|
throw StateError('Preferences write unavailable');
|
|
}
|
|
|
|
@override
|
|
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
|
|
}
|
|
|
|
void main() {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
|
|
test('applies the selection immediately even if saving fails', () async {
|
|
final cubit = LocaleCubit(_FailingPreferences());
|
|
addTearDown(cubit.close);
|
|
|
|
final saving = cubit.setLocale(const Locale('zh'));
|
|
expect(cubit.state, AppLocalizations.chinese);
|
|
expect(cubit.hasChosenLanguage, isTrue);
|
|
|
|
await saving;
|
|
expect(cubit.state, AppLocalizations.chinese);
|
|
});
|
|
|
|
test('restores legacy settings and persists the latest selection', () async {
|
|
SharedPreferences.setMockInitialValues({
|
|
'app_locale_chosen': true,
|
|
'app_locale_lang': 'zh',
|
|
});
|
|
final preferences = await SharedPreferences.getInstance();
|
|
final cubit = LocaleCubit(preferences);
|
|
addTearDown(cubit.close);
|
|
expect(cubit.state, AppLocalizations.chinese);
|
|
|
|
final first = cubit.setLocale(AppLocalizations.chinese);
|
|
final last = cubit.setLocale(AppLocalizations.english);
|
|
expect(cubit.state, AppLocalizations.english);
|
|
|
|
await Future.wait([first, last]);
|
|
expect(preferences.getString('app_locale_lang'), 'en');
|
|
expect(preferences.getString('app_locale_country'), 'US');
|
|
});
|
|
|
|
test('loads the actual language assets without a country code', () async {
|
|
final chinese = AppLocalizations(const Locale('zh'));
|
|
final english = AppLocalizations(const Locale('en'));
|
|
|
|
expect(await chinese.load(), isTrue);
|
|
expect(await english.load(), isTrue);
|
|
expect(chinese.translate('common.cancel'), '取消');
|
|
expect(english.translate('common.cancel'), 'Cancel');
|
|
});
|
|
}
|