mirror of
https://github.com/zarzet/SpotiFLAC-Mobile.git
synced 2026-10-21 15:02:21 +02:00
115 lines
3.9 KiB
Dart
115 lines
3.9 KiB
Dart
import 'dart:io';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:spotiflac_android/models/theme_settings.dart';
|
|
import 'package:spotiflac_android/providers/theme_provider.dart';
|
|
import 'package:spotiflac_android/services/custom_font_service.dart';
|
|
import 'package:spotiflac_android/theme/app_theme.dart';
|
|
import 'package:spotiflac_android/theme/mornye_theme.dart';
|
|
|
|
void main() {
|
|
TestWidgetsFlutterBinding.ensureInitialized();
|
|
|
|
test(
|
|
'import persists a private copy, restores after restart, rejects damage',
|
|
() async {
|
|
final directory = await Directory.systemTemp.createTemp('font-import-');
|
|
addTearDown(() => directory.delete(recursive: true));
|
|
final bytes = await File('assets/fonts/InterVariable.ttf').readAsBytes();
|
|
var loads = 0;
|
|
CustomFontService service() => CustomFontService(
|
|
directory: () async => directory,
|
|
loadFont: (_, data) async {
|
|
loads++;
|
|
expect(data, bytes);
|
|
},
|
|
);
|
|
final importer = service();
|
|
final id = await importer.import(bytes);
|
|
expect(await importer.restore(id), 'ImportedFont_$id');
|
|
expect(loads, 1);
|
|
expect(await service().restore(id), 'ImportedFont_$id');
|
|
expect(loads, 2);
|
|
await File('${directory.path}/$id.font').writeAsBytes([1, 2]);
|
|
expect(await service().restore(id), isNull);
|
|
expect(await service().restore('../outside'), isNull);
|
|
expect(
|
|
() => CustomFontService.validate(Uint8List(12)),
|
|
throwsFormatException,
|
|
);
|
|
expect(
|
|
() => CustomFontService.validate(bytes.sublist(0, 32)),
|
|
throwsFormatException,
|
|
);
|
|
},
|
|
);
|
|
|
|
test('font choice survives bootstrap, export and reset', () async {
|
|
SharedPreferences.setMockInitialValues({kUseSystemFontKey: true});
|
|
final container = ProviderContainer();
|
|
addTearDown(container.dispose);
|
|
final notifier = container.read(themeProvider.notifier);
|
|
await notifier.setCustomFont('selected-font', 'My font.ttf');
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final theme = loadBootstrapThemeSettings(prefs);
|
|
expect(theme.customFontId, 'selected-font');
|
|
expect(theme.customFontName, 'My font.ttf');
|
|
expect(ThemeSettings.fromJson(theme.toJson()), theme);
|
|
await notifier.setCustomFont('', '');
|
|
expect(loadBootstrapThemeSettings(prefs).customFontId, isEmpty);
|
|
expect(loadBootstrapThemeSettings(prefs).useSystemFont, isTrue);
|
|
});
|
|
|
|
testWidgets(
|
|
'custom typography survives Mornye local surface reconstruction',
|
|
(tester) async {
|
|
final theme = MornyeTheme.build(
|
|
Brightness.dark,
|
|
fontFamily: 'ImportedFont_test',
|
|
);
|
|
await tester.pumpWidget(
|
|
MaterialApp(
|
|
theme: theme,
|
|
home: Builder(
|
|
builder: (context) {
|
|
final surface = MornyeTheme.fromContext(
|
|
context,
|
|
chromeSurface: Colors.red,
|
|
);
|
|
expect(
|
|
surface.textTheme.bodyLarge!.fontFamily,
|
|
'ImportedFont_test',
|
|
);
|
|
expect(
|
|
surface.textTheme.headlineSmall!.fontFamily,
|
|
'ImportedFont_test',
|
|
);
|
|
return const SizedBox();
|
|
},
|
|
),
|
|
),
|
|
);
|
|
expect(
|
|
AppTheme.light(
|
|
fontFamily: 'ImportedFont_test',
|
|
).textTheme.bodyMedium!.fontFamily,
|
|
'ImportedFont_test',
|
|
);
|
|
expect(
|
|
AppTheme.dark(
|
|
fontFamily: 'ImportedFont_test',
|
|
).textTheme.bodyMedium!.fontFamily,
|
|
'ImportedFont_test',
|
|
);
|
|
expect(
|
|
MornyeTheme.build(Brightness.dark).textTheme.bodyLarge!.fontFamily,
|
|
isNot('ImportedFont_test'),
|
|
);
|
|
},
|
|
);
|
|
}
|