Files
pipelines/app/test/features/settings/settings_custom_font_test.dart
T
alan c24e7f40b4 Settings: add your own fonts (.ttf/.otf) per device
Add font… picks one or more files, checks them with a bounded sfnt reader
(name table family, OS/2 weight), registers them with FontLoader before
storing, and chooses the new family. Added fonts list below the bundled ones
with Rename and Remove; removing the chosen one falls back to System default.
Bytes live in the app support directory natively and in IndexedDB on the web
(localStorage is too small); a shared_preferences index names them. Limits:
10 MB per file, 40 MB total. The capture kit gains a files= step.
2026-11-04 18:45:01 +00:00

202 lines
7.2 KiB
Dart

import 'dart:convert';
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/settings/settings_custom_font_actions.dart';
import 'package:pipelines_app/features/settings/settings_custom_font_dialogs.dart';
import 'package:pipelines_app/features/settings/settings_font_section.dart';
import 'package:pipelines_app/ui/theme/app_font.dart';
import 'package:pipelines_app/ui/theme/app_font_controller.dart';
import 'package:pipelines_app/ui/theme/app_font_store.dart';
import 'package:pipelines_app/ui/theme/custom_fonts/custom_font.dart';
import 'package:pipelines_app/ui/theme/custom_fonts/custom_font_draft.dart';
import 'package:pipelines_app/ui/theme/custom_fonts/custom_font_library.dart';
import 'package:pipelines_app/ui/theme/custom_fonts/font_bytes_store.dart';
import 'package:shared_preferences/shared_preferences.dart';
PickedFontFile _pick(String path) => PickedFontFile(
name: path.split('/').last,
bytes: File(path).readAsBytesSync(),
);
class _Harness {
final store = MemoryAppFontStore();
final bytes = MemoryFontBytesStore();
List<PickedFontFile>? next;
late final fonts = AppFontController(
store: store,
library: CustomFontLibrary(bytes: bytes),
loader: (family, sources) async {},
bytesLoader: (family, faces) async {},
);
Future<List<PickedFontFile>?> pick() async => next;
}
Future<void> _pump(
WidgetTester tester,
_Harness harness, {
bool cupertino = false,
}) async {
final section = SingleChildScrollView(
child: SettingsFontSection(
controller: harness.fonts,
pickFiles: harness.pick,
),
);
await tester.pumpWidget(
cupertino
? CupertinoApp(home: CupertinoPageScaffold(child: section))
: MaterialApp(home: Scaffold(body: section)),
);
await tester.pumpAndSettle();
}
void main() {
setUp(() => SharedPreferences.setMockInitialValues(<String, Object>{}));
final regular = _pick('fonts/inter/Inter-Regular.ttf');
final bold = _pick('fonts/inter/Inter-Bold.ttf');
testWidgets('Material: add, name, rename and remove an added font', (
tester,
) async {
final harness = _Harness()..next = <PickedFontFile>[regular, bold];
await _pump(tester, harness);
await tester.tap(find.text(addCustomFontText));
await tester.pumpAndSettle();
// The family name comes from the font; the files are listed.
expect(find.text(addCustomFontTitle), findsOneWidget);
expect(
find.text('Regular · Inter-Regular.ttf\nBold · Inter-Bold.ttf'),
findsOneWidget,
);
final field = find.byType(TextField);
expect(tester.widget<TextField>(field).controller!.text, 'Inter');
await tester.enterText(field, 'My Inter');
await tester.tap(find.text('Add'));
await tester.pumpAndSettle();
final font = harness.fonts.customFonts.single;
expect(font.name, 'My Inter');
expect(harness.store.id, font.id, reason: 'a new font is chosen');
final row = find.byKey(settingsFontKey(font));
expect(row, findsOneWidget);
// Below the bundled options, named in its own face.
expect(
tester.getTopLeft(row).dy,
greaterThan(
tester.getTopLeft(find.byKey(settingsFontKey(AppFont.values.last))).dy,
),
);
final name = tester.widget<Text>(
find.descendant(of: row, matching: find.text('My Inter')),
);
expect(name.style?.fontFamily, font.family);
expect(
find.descendant(of: row, matching: find.text('Regular, Bold')),
findsOneWidget,
);
// Its own control, not merged into the radio tile.
final options = tester.getSemantics(
find.byTooltip(customFontActionsTooltip),
);
expect(options.tooltip, customFontActionsTooltip);
expect(options.label, isNot(contains('My Inter')));
expect(find.bySemanticsLabel(addCustomFontText), findsOneWidget);
await tester.tap(find.byTooltip(customFontActionsTooltip));
await tester.pumpAndSettle();
await tester.tap(find.text(renameCustomFontText));
await tester.pumpAndSettle();
await tester.enterText(find.byType(TextField), 'Body');
await tester.tap(find.widgetWithText(TextButton, renameCustomFontText));
await tester.pumpAndSettle();
expect(find.text('Body'), findsOneWidget);
expect(harness.fonts.selected.label, 'Body');
await tester.tap(find.byTooltip(customFontActionsTooltip));
await tester.pumpAndSettle();
await tester.tap(find.text(removeCustomFontText));
await tester.pumpAndSettle();
expect(find.text('Remove Body?'), findsOneWidget);
await tester.tap(find.widgetWithText(TextButton, 'Remove'));
await tester.pumpAndSettle();
expect(find.text('Body'), findsNothing);
expect(harness.fonts.selected, AppFont.system);
expect(harness.store.id, AppFont.system.id);
expect(harness.bytes.files, isEmpty);
});
testWidgets('an invalid file is refused with a message, nothing stored', (
tester,
) async {
final harness = _Harness()
..next = <PickedFontFile>[
PickedFontFile(name: 'fake.ttf', bytes: utf8.encode('not a font')),
];
await _pump(tester, harness);
await tester.tap(find.text(addCustomFontText));
await tester.pumpAndSettle();
expect(find.text(addCustomFontFailedTitle), findsOneWidget);
expect(find.textContaining('“fake.ttf” cannot be used'), findsOneWidget);
expect(find.text(addCustomFontTitle), findsNothing);
await tester.tap(find.text('OK'));
await tester.pumpAndSettle();
expect(harness.fonts.customFonts, isEmpty);
expect(harness.bytes.files, isEmpty);
});
testWidgets('a cancelled pick or name dialog adds nothing', (tester) async {
final harness = _Harness();
await _pump(tester, harness);
await tester.tap(find.text(addCustomFontText));
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsNothing);
harness.next = <PickedFontFile>[regular];
await tester.tap(find.text(addCustomFontText));
await tester.pumpAndSettle();
await tester.tap(find.text('Cancel'));
await tester.pumpAndSettle();
expect(harness.fonts.customFonts, isEmpty);
});
testWidgets('Cupertino: an added font has the checkmark and options', (
tester,
) async {
final harness = _Harness()..next = <PickedFontFile>[regular];
await _pump(tester, harness, cupertino: true);
await tester.tap(find.text(addCustomFontText));
await tester.pumpAndSettle();
await tester.tap(find.text('Add'));
await tester.pumpAndSettle();
final CustomFont font = harness.fonts.customFonts.single;
final row = find.byKey(settingsFontKey(font));
expect(
find.descendant(
of: row,
matching: find.byIcon(CupertinoIcons.check_mark),
),
findsOneWidget,
);
// The options button is its own control beside the row.
expect(find.bySemanticsLabel(customFontActionsTooltip), findsOneWidget);
await tester.tap(find.byIcon(CupertinoIcons.ellipsis_circle));
await tester.pumpAndSettle();
await tester.tap(find.text(removeCustomFontText));
await tester.pumpAndSettle();
await tester.tap(find.widgetWithText(CupertinoDialogAction, 'Remove'));
await tester.pumpAndSettle();
expect(harness.fonts.customFonts, isEmpty);
expect(harness.fonts.selected, AppFont.system);
});
}