Add custom themes, holiday themes, chat backgrounds and holiday animations
- Themes: name + background + accent, derived surfaces/text/bubbles; editor with live preview; share codes and .jsonl import/export - Theme icons: swap tab, send, settings, node avatar and map marker icons for named icons or pictures (lossless JPEG XL) - Chat backgrounds per channel/DM (color or JPEG XL picture with fade); themes can set one, optionally for every chat - Map follows custom themes (panels, accent, tile tint, markers) - Holiday themes (Halloween, Thanksgiving, Christmas, New Year's) with generated backgrounds, icons and map markers; automatic switching from 17 days before each holiday - Holiday greeting animations (bats, turkeys, Santa, fireworks) matched across all app languages via translation strings
This commit is contained in:
1 parent
780183b586
commit
4a574185e8
124 files changed
+12686
-292
No files matched your search
@@ -0,0 +1,246 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/l10n/app_localizations.dart';
|
||||
import 'package:meshcore_open/models/app_settings.dart';
|
||||
import 'package:meshcore_open/models/custom_theme.dart';
|
||||
import 'package:meshcore_open/screens/theme_editor_screen.dart';
|
||||
import 'package:meshcore_open/services/app_settings_service.dart';
|
||||
import 'package:meshcore_open/storage/prefs_manager.dart';
|
||||
import 'package:meshcore_open/theme/mesh_theme.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
void main() {
|
||||
group('hex colors', () {
|
||||
test('parse long, short and bare forms', () {
|
||||
expect(parseHexColor('#0EA5E9'), const Color(0xFF0EA5E9));
|
||||
expect(parseHexColor('0ea5e9'), const Color(0xFF0EA5E9));
|
||||
expect(parseHexColor('#fff'), const Color(0xFFFFFFFF));
|
||||
expect(parseHexColor(' #000000 '), const Color(0xFF000000));
|
||||
});
|
||||
|
||||
test('reject invalid input', () {
|
||||
expect(parseHexColor('#12'), isNull);
|
||||
expect(parseHexColor('#GGGGGG'), isNull);
|
||||
expect(parseHexColor(42), isNull);
|
||||
expect(parseHexColor(null), isNull);
|
||||
});
|
||||
|
||||
test('format as uppercase #RRGGBB', () {
|
||||
expect(colorToHex(const Color(0xFF0ea5e9)), '#0EA5E9');
|
||||
expect(colorToHex(const Color(0x800000FF)), '#0000FF');
|
||||
});
|
||||
});
|
||||
|
||||
group('CustomTheme', () {
|
||||
const theme = CustomTheme(
|
||||
id: 'custom:1',
|
||||
name: 'Forest',
|
||||
background: Color(0xFF0E1A13),
|
||||
accent: Color(0xFF4ADE80),
|
||||
);
|
||||
|
||||
test('brightness comes from the background', () {
|
||||
expect(theme.isDark, isTrue);
|
||||
expect(theme.copyWith(background: Colors.white).isDark, isFalse);
|
||||
});
|
||||
|
||||
test('JSON round-trips', () {
|
||||
expect(CustomTheme.fromJson(theme.toJson()), theme);
|
||||
});
|
||||
|
||||
test('share code round-trips under a new ID', () {
|
||||
final imported = CustomTheme.fromShareCode(
|
||||
theme.toShareCode(),
|
||||
newId: 'custom:2',
|
||||
);
|
||||
expect(imported, theme.copyWith(id: 'custom:2'));
|
||||
});
|
||||
|
||||
test('hand-written share code is accepted', () {
|
||||
final imported = CustomTheme.fromShareCode(
|
||||
'{"name": "Mine", "background": "#fff", "accent": "1d4ed8"}',
|
||||
newId: 'x',
|
||||
);
|
||||
expect(imported?.name, 'Mine');
|
||||
expect(imported?.isDark, isFalse);
|
||||
});
|
||||
|
||||
test('bad share codes are rejected', () {
|
||||
for (final code in [
|
||||
'',
|
||||
'not json',
|
||||
'[]',
|
||||
'{"name": "", "background": "#000", "accent": "#fff"}',
|
||||
'{"name": "X", "background": "nope", "accent": "#fff"}',
|
||||
'{"name": "X", "background": "#000"}',
|
||||
]) {
|
||||
expect(
|
||||
CustomTheme.fromShareCode(code, newId: 'x'),
|
||||
isNull,
|
||||
reason: code,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('preset IDs are unique and resolvable', () {
|
||||
final ids = CustomTheme.presets.map((p) => p.id).toSet();
|
||||
expect(ids.length, CustomTheme.presets.length);
|
||||
for (final preset in [
|
||||
...CustomTheme.presets,
|
||||
...CustomTheme.holidayPresets,
|
||||
]) {
|
||||
expect(preset.isPreset, isTrue);
|
||||
expect(CustomTheme.presetById(preset.id), preset);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
group('MeshTheme.fromCustom', () {
|
||||
test('every preset yields readable text and buttons', () {
|
||||
for (final preset in [
|
||||
...CustomTheme.presets,
|
||||
...CustomTheme.holidayPresets,
|
||||
]) {
|
||||
final data = MeshTheme.fromCustom(preset);
|
||||
final scheme = data.colorScheme;
|
||||
expect(
|
||||
data.brightness,
|
||||
preset.isDark ? Brightness.dark : Brightness.light,
|
||||
);
|
||||
expect(scheme.surface, preset.background);
|
||||
expect(scheme.primary, preset.accent);
|
||||
expect(
|
||||
_contrast(scheme.onSurface, scheme.surface),
|
||||
greaterThan(7),
|
||||
reason: preset.name,
|
||||
);
|
||||
expect(
|
||||
_contrast(scheme.onPrimary, scheme.primary),
|
||||
greaterThan(3),
|
||||
reason: preset.name,
|
||||
);
|
||||
final bubble = data.extension<MeshBubbleTheme>()!;
|
||||
expect(
|
||||
_contrast(bubble.meInk, bubble.me),
|
||||
greaterThan(4.5),
|
||||
reason: preset.name,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('stock themes keep the standard bubble', () {
|
||||
expect(
|
||||
MeshTheme.dark().extension<MeshBubbleTheme>(),
|
||||
MeshBubbleTheme.standard,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('settings', () {
|
||||
setUp(() async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
await PrefsManager.initialize();
|
||||
});
|
||||
|
||||
test('custom themes and the active ID persist', () {
|
||||
const theme = CustomTheme(
|
||||
id: 'custom:1',
|
||||
name: 'Mine',
|
||||
background: Color(0xFF000000),
|
||||
accent: Color(0xFFFACC15),
|
||||
);
|
||||
final settings = AppSettings(
|
||||
customThemes: const [theme],
|
||||
activeThemeId: theme.id,
|
||||
);
|
||||
final restored = AppSettings.fromJson(settings.toJson());
|
||||
expect(restored.customThemes, [theme]);
|
||||
expect(restored.activeThemeId, theme.id);
|
||||
expect(AppSettings.fromJson(const {}).customThemes, isEmpty);
|
||||
});
|
||||
|
||||
test('service saves, applies, resolves and deletes themes', () async {
|
||||
final service = AppSettingsService();
|
||||
const theme = CustomTheme(
|
||||
id: 'custom:1',
|
||||
name: 'Mine',
|
||||
background: Color(0xFF000000),
|
||||
accent: Color(0xFFFACC15),
|
||||
);
|
||||
await service.saveCustomTheme(theme, apply: true);
|
||||
expect(service.activeCustomTheme, theme);
|
||||
|
||||
await service.saveCustomTheme(theme.copyWith(name: 'Renamed'));
|
||||
expect(service.settings.customThemes.single.name, 'Renamed');
|
||||
|
||||
await service.setActiveTheme(CustomTheme.presets.first.id);
|
||||
expect(service.activeCustomTheme, CustomTheme.presets.first);
|
||||
|
||||
await service.setThemeMode('light');
|
||||
expect(service.activeCustomTheme, isNull);
|
||||
|
||||
await service.setActiveTheme(theme.id);
|
||||
await service.deleteCustomTheme(theme.id);
|
||||
expect(service.settings.customThemes, isEmpty);
|
||||
expect(service.activeCustomTheme, isNull);
|
||||
});
|
||||
});
|
||||
|
||||
testWidgets('editor saves and applies a new theme', (tester) async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
await PrefsManager.initialize();
|
||||
final service = AppSettingsService();
|
||||
await tester.pumpWidget(
|
||||
ChangeNotifierProvider.value(
|
||||
value: service,
|
||||
child: const MaterialApp(
|
||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
home: ThemeEditorScreen(),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
final list = find.byType(Scrollable).first;
|
||||
final save = find.text('Save and use');
|
||||
Future<void> tapSave() async {
|
||||
await tester.scrollUntilVisible(save, 200, scrollable: list);
|
||||
await tester.ensureVisible(save);
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(save);
|
||||
}
|
||||
|
||||
// Saving without a name is refused.
|
||||
await tapSave();
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Give your theme a name'), findsOneWidget);
|
||||
expect(service.settings.customThemes, isEmpty);
|
||||
|
||||
await tester.scrollUntilVisible(
|
||||
find.byType(TextField).first,
|
||||
-200,
|
||||
scrollable: list,
|
||||
);
|
||||
await tester.enterText(find.byType(TextField).first, 'Night Owl');
|
||||
final accentHex = find.widgetWithText(TextField, '#0EA5E9');
|
||||
await tester.scrollUntilVisible(accentHex, 200, scrollable: list);
|
||||
await tester.enterText(accentHex, '#FACC15');
|
||||
await tester.pump();
|
||||
await tapSave();
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final saved = service.settings.customThemes.single;
|
||||
expect(saved.name, 'Night Owl');
|
||||
expect(saved.accent, const Color(0xFFFACC15));
|
||||
expect(service.activeCustomTheme, saved);
|
||||
});
|
||||
}
|
||||
|
||||
double _contrast(Color a, Color b) {
|
||||
final la = a.computeLuminance();
|
||||
final lb = b.computeLuminance();
|
||||
final hi = la > lb ? la : lb;
|
||||
final lo = la > lb ? lb : la;
|
||||
return (hi + 0.05) / (lo + 0.05);
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:koni_jxl_flutter/koni_jxl_flutter.dart';
|
||||
import 'package:meshcore_open/connector/meshcore_protocol.dart';
|
||||
import 'package:meshcore_open/models/chat_background.dart';
|
||||
import 'package:meshcore_open/models/custom_theme.dart';
|
||||
import 'package:meshcore_open/theme/mesh_theme.dart';
|
||||
import 'package:meshcore_open/theme/theme_icons.dart';
|
||||
import 'package:meshcore_open/widgets/mesh_ui.dart';
|
||||
|
||||
/// Every bundled picture a holiday theme points at.
|
||||
List<String> _assetsOf(CustomTheme theme) => [
|
||||
for (final icon in theme.icons.values) ?icon.asset,
|
||||
?theme.chatBackground?.asset,
|
||||
];
|
||||
|
||||
void main() {
|
||||
test('map marker slots cover every node type', () {
|
||||
expect(mapMarkerSlotForNodeType(advTypeChat), ThemeIconSlot.mapChat);
|
||||
expect(
|
||||
mapMarkerSlotForNodeType(advTypeRepeater),
|
||||
ThemeIconSlot.mapRepeater,
|
||||
);
|
||||
expect(mapMarkerSlotForNodeType(advTypeRoom), ThemeIconSlot.mapRoom);
|
||||
expect(mapMarkerSlotForNodeType(advTypeSensor), ThemeIconSlot.mapSensor);
|
||||
expect(mapMarkerSlotForNodeType(99), isNull);
|
||||
expect(ThemeIconSlot.values.where((s) => s.isMapMarker).map((s) => s.key), [
|
||||
'map_chat',
|
||||
'map_repeater',
|
||||
'map_room',
|
||||
'map_sensor',
|
||||
'map_self',
|
||||
]);
|
||||
});
|
||||
|
||||
test('list avatars and map markers share a node type icon', () {
|
||||
const marker = ThemeIconOverride.icon('castle');
|
||||
const theme = MeshIconTheme({'map_repeater': marker});
|
||||
expect(theme[ThemeIconSlot.repeater], marker);
|
||||
expect(theme[ThemeIconSlot.mapRepeater], marker);
|
||||
expect(theme[ThemeIconSlot.room], isNull);
|
||||
|
||||
// A slot's own setting wins over its partner's.
|
||||
const own = ThemeIconOverride.icon('home');
|
||||
const both = MeshIconTheme({'map_room': marker, 'room': own});
|
||||
expect(both[ThemeIconSlot.room], own);
|
||||
expect(both[ThemeIconSlot.mapRoom], marker);
|
||||
|
||||
for (final slot in ThemeIconSlot.values) {
|
||||
expect(slot.partner?.partner ?? slot, slot, reason: slot.key);
|
||||
}
|
||||
});
|
||||
|
||||
testWidgets('holiday list avatars show the map marker picture', (
|
||||
tester,
|
||||
) async {
|
||||
final theme = CustomTheme.holidayPresets.first;
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
theme: MeshTheme.fromCustom(theme),
|
||||
home: const Column(
|
||||
children: [
|
||||
AvatarCircle(name: 'Tower', iconSlot: ThemeIconSlot.repeater),
|
||||
AvatarCircle(name: 'Zed'),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
final image = tester.widget<Image>(find.byType(Image));
|
||||
expect(
|
||||
image.image,
|
||||
JxlImageProvider.asset(theme.icons['map_repeater']!.asset!),
|
||||
);
|
||||
// Chat contacts keep their initials.
|
||||
expect(find.text('ZE'), findsOneWidget);
|
||||
});
|
||||
|
||||
test('pictures and named icons resolve to the right provider', () {
|
||||
expect(themePictureOf(null), isNull);
|
||||
expect(themePictureOf(const ThemeIconOverride.icon('park')), isNull);
|
||||
expect(
|
||||
themePictureOf(
|
||||
const ThemeIconOverride.asset('assets/themes/halloween/map_chat.jxl'),
|
||||
),
|
||||
isA<JxlImageProvider>(),
|
||||
);
|
||||
});
|
||||
|
||||
test('holiday themes have unique IDs that resolve', () {
|
||||
final all = [...CustomTheme.presets, ...CustomTheme.holidayPresets];
|
||||
expect(all.map((t) => t.id).toSet(), hasLength(all.length));
|
||||
for (final theme in CustomTheme.holidayPresets) {
|
||||
expect(CustomTheme.presetById(theme.id), theme);
|
||||
expect(theme.isPreset, isTrue);
|
||||
expect(theme.chatBackground?.asset, isNotNull);
|
||||
expect(theme.icons.keys, containsAll(['contacts', 'channels', 'map']));
|
||||
expect(
|
||||
theme.icons.keys,
|
||||
containsAll([
|
||||
for (final slot in ThemeIconSlot.values)
|
||||
if (slot.isMapMarker) slot.key,
|
||||
]),
|
||||
reason: '${theme.name} has every map marker',
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('every holiday picture is a bundled, declared JPEG XL file', () {
|
||||
final pubspec = File('pubspec.yaml').readAsStringSync();
|
||||
for (final theme in CustomTheme.holidayPresets) {
|
||||
for (final path in _assetsOf(theme)) {
|
||||
expect(isThemeAssetPath(path), isTrue, reason: path);
|
||||
final file = File(path);
|
||||
expect(file.existsSync(), isTrue, reason: path);
|
||||
expect(looksLikeJxl(file.readAsBytesSync()), isTrue, reason: path);
|
||||
final dir = path.substring(0, path.lastIndexOf('/') + 1);
|
||||
expect(pubspec, contains('- $dir'), reason: 'pubspec lists $dir');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
testWidgets('holiday pictures decode, icons keep transparency', (
|
||||
tester,
|
||||
) async {
|
||||
await tester.runAsync(() async {
|
||||
for (final theme in CustomTheme.holidayPresets) {
|
||||
final background = await decodeJxlToUiImage(
|
||||
File(theme.chatBackground!.asset!).readAsBytesSync(),
|
||||
);
|
||||
expect(background.height, greaterThan(background.width));
|
||||
background.dispose();
|
||||
|
||||
final icon = await decodeJxlToUiImage(
|
||||
File(theme.icons['contacts']!.asset!).readAsBytesSync(),
|
||||
);
|
||||
final pixels = (await icon.toByteData())!;
|
||||
// Top-left corner lies outside the round badge.
|
||||
expect(pixels.getUint8(3), 0, reason: theme.name);
|
||||
icon.dispose();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
test('asset references survive JSON and share codes', () {
|
||||
final theme = CustomTheme.holidayPresets.first;
|
||||
expect(CustomTheme.fromJson(theme.toJson()), theme);
|
||||
final copy = CustomTheme.fromShareCode(theme.toShareCode(), newId: 'x')!;
|
||||
expect(copy.chatBackground, theme.chatBackground);
|
||||
expect(copy.icons, theme.icons);
|
||||
});
|
||||
|
||||
test('shared codes cannot point outside the theme assets', () {
|
||||
for (final bad in [
|
||||
'assets/images/logo.png',
|
||||
'assets/themes/../fonts/Inter.ttf',
|
||||
'assets/themes/halloween/background.png',
|
||||
'/etc/passwd',
|
||||
]) {
|
||||
expect(isThemeAssetPath(bad), isFalse, reason: bad);
|
||||
expect(ThemeIconOverride.fromJson({'asset': bad}), isNull);
|
||||
expect(ChatBackground.fromJson({'asset': bad}), isNull);
|
||||
}
|
||||
});
|
||||
|
||||
testWidgets('a holiday theme shows its icon in place of the default', (
|
||||
tester,
|
||||
) async {
|
||||
final theme = CustomTheme.holidayPresets.first;
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
theme: MeshTheme.fromCustom(theme),
|
||||
home: const ThemedIcon(ThemeIconSlot.map),
|
||||
),
|
||||
);
|
||||
expect(find.byType(Image), findsOneWidget);
|
||||
expect(find.byIcon(Icons.map_outlined), findsNothing);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
import 'dart:typed_data';
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:koni_jxl_flutter/koni_jxl_flutter.dart';
|
||||
import 'package:meshcore_open/models/custom_theme.dart';
|
||||
import 'package:meshcore_open/services/app_settings_service.dart';
|
||||
import 'package:meshcore_open/storage/prefs_manager.dart';
|
||||
import 'package:meshcore_open/theme/mesh_theme.dart';
|
||||
import 'package:meshcore_open/theme/theme_icons.dart';
|
||||
import 'package:meshcore_open/utils/theme_file.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
void main() {
|
||||
final png = Uint8List.fromList([137, 80, 78, 71, 1, 2, 3]);
|
||||
final theme = CustomTheme(
|
||||
id: 'custom:1',
|
||||
name: 'Iconic',
|
||||
background: const Color(0xFF000000),
|
||||
accent: const Color(0xFFFACC15),
|
||||
icons: {
|
||||
'map': const ThemeIconOverride.icon('park'),
|
||||
'send': ThemeIconOverride.image(png),
|
||||
},
|
||||
);
|
||||
|
||||
group('theme icons', () {
|
||||
test('icons survive JSON and share codes', () {
|
||||
expect(CustomTheme.fromJson(theme.toJson()), theme);
|
||||
final imported = CustomTheme.fromShareCode(
|
||||
theme.toShareCode(),
|
||||
newId: 'x',
|
||||
)!;
|
||||
expect(imported.icons['send']!.imageBytes, png);
|
||||
expect(imported.icons['map']!.icon, 'park');
|
||||
});
|
||||
|
||||
test('unknown slots, unknown icons and bad pictures are dropped', () {
|
||||
final icons = parseThemeIcons({
|
||||
'map': {'icon': 'not_an_icon'},
|
||||
'nope': {'icon': 'park'},
|
||||
'send': {'image': '%%%'},
|
||||
'contacts': {'icon': 'rocket_launch'},
|
||||
});
|
||||
expect(icons.keys, ['contacts']);
|
||||
});
|
||||
|
||||
test('slot keys are unique and catalog names resolve', () {
|
||||
final keys = ThemeIconSlot.values.map((s) => s.key).toSet();
|
||||
expect(keys.length, ThemeIconSlot.values.length);
|
||||
});
|
||||
|
||||
test('custom theme carries its icons into ThemeData', () {
|
||||
final data = MeshTheme.fromCustom(theme);
|
||||
expect(data.extension<MeshIconTheme>()![ThemeIconSlot.map]!.icon, 'park');
|
||||
expect(MeshTheme.dark().extension<MeshIconTheme>()!.overrides, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('ThemedIcon shows the override or the default', (tester) async {
|
||||
Widget host(ThemeData data) => MaterialApp(
|
||||
theme: data,
|
||||
home: const Row(
|
||||
children: [
|
||||
ThemedIcon(ThemeIconSlot.map),
|
||||
ThemedIcon(ThemeIconSlot.channels),
|
||||
],
|
||||
),
|
||||
);
|
||||
await tester.pumpWidget(host(MeshTheme.fromCustom(theme)));
|
||||
expect(find.byIcon(Icons.park), findsOneWidget);
|
||||
expect(find.byIcon(Icons.tag), findsOneWidget);
|
||||
|
||||
await tester.pumpWidget(host(MeshTheme.dark()));
|
||||
await tester.pumpAndSettle(); // theme change animates
|
||||
expect(find.byIcon(Icons.map_outlined), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('pictures are shrunk to a lossless JPEG XL', (tester) async {
|
||||
final big = await tester.runAsync(() async {
|
||||
final recorder = ui.PictureRecorder();
|
||||
Canvas(recorder).drawRect(
|
||||
const Rect.fromLTWH(0, 0, 400, 200),
|
||||
Paint()..color = const Color(0xFFFF0000),
|
||||
);
|
||||
final image = await recorder.endRecording().toImage(400, 200);
|
||||
final data = await image.toByteData(format: ui.ImageByteFormat.png);
|
||||
return data!.buffer.asUint8List();
|
||||
});
|
||||
final shrunk = await tester.runAsync(() => shrinkThemeIconImage(big!));
|
||||
expect(looksLikeJxl(shrunk!), isTrue);
|
||||
final size = await tester.runAsync(() async {
|
||||
final image = await decodeJxlToUiImage(shrunk);
|
||||
return Size(image.width.toDouble(), image.height.toDouble());
|
||||
});
|
||||
expect(size, const Size(128, 64));
|
||||
expect(
|
||||
await tester.runAsync(
|
||||
() => shrinkThemeIconImage(Uint8List.fromList([1, 2, 3])),
|
||||
),
|
||||
isNull,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group('jsonl files', () {
|
||||
var n = 0;
|
||||
String newId() => 'id${n++}';
|
||||
|
||||
test('one theme per line round-trips', () {
|
||||
final second = CustomTheme.presets.first;
|
||||
final text = encodeThemesJsonl([theme, second]);
|
||||
expect(text.trim().split('\n'), hasLength(2));
|
||||
final decoded = decodeThemesJsonl(text, newId: newId);
|
||||
expect(decoded, hasLength(2));
|
||||
expect(decoded[0], theme.copyWith(id: decoded[0].id));
|
||||
expect(decoded[1].name, second.name);
|
||||
expect(decoded[0].id, isNot(decoded[1].id));
|
||||
});
|
||||
|
||||
test('bad and blank lines are skipped', () {
|
||||
final text =
|
||||
'\n{"name":"A","background":"#000","accent":"#fff"}\n'
|
||||
'garbage\n\r\n{"name":"B","background":"#fff","accent":"#000"}\r\n';
|
||||
expect(decodeThemesJsonl(text, newId: newId).map((t) => t.name), [
|
||||
'A',
|
||||
'B',
|
||||
]);
|
||||
});
|
||||
|
||||
test('a pretty-printed single theme is accepted', () {
|
||||
const text =
|
||||
'{\n "name": "Pretty",\n "background": "#000",\n'
|
||||
' "accent": "#fff"\n}\n';
|
||||
expect(decodeThemesJsonl(text, newId: newId).single.name, 'Pretty');
|
||||
});
|
||||
|
||||
test('file stems are safe', () {
|
||||
expect(themeFileStem('My Cool Theme!'), 'my-cool-theme');
|
||||
expect(themeFileStem(' '), 'theme');
|
||||
});
|
||||
|
||||
test('import skips duplicates and assigns unique IDs', () async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
await PrefsManager.initialize();
|
||||
final service = AppSettingsService();
|
||||
final themes = decodeThemesJsonl(
|
||||
encodeThemesJsonl([theme, CustomTheme.presets.first]),
|
||||
newId: service.newCustomThemeId,
|
||||
);
|
||||
expect(await service.importCustomThemes(themes), 2);
|
||||
final again = decodeThemesJsonl(
|
||||
encodeThemesJsonl([theme]),
|
||||
newId: service.newCustomThemeId,
|
||||
);
|
||||
expect(await service.importCustomThemes(again), 0);
|
||||
final ids = service.settings.customThemes.map((t) => t.id).toSet();
|
||||
expect(ids, hasLength(2));
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user