Merge: add your own fonts in Settings

This commit is contained in:
2026-11-04 18:45:25 +00:00
28 changed files with 2329 additions and 122 deletions
@@ -0,0 +1,201 @@
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);
});
}
@@ -7,6 +7,7 @@ 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/font_choice.dart';
class _Harness {
_Harness({this.hold = false});
@@ -71,8 +72,8 @@ void main() {
for (final font in AppFont.values)
if (font.previewAsset != null) font.previewAsset,
]);
final group = tester.widget<RadioGroup<AppFont>>(
find.byType(RadioGroup<AppFont>),
final group = tester.widget<RadioGroup<FontChoice>>(
find.byType(RadioGroup<FontChoice>),
);
expect(group.groupValue, AppFont.system);
});
@@ -85,10 +86,10 @@ void main() {
await tester.tap(find.text('Source Sans 3'));
await tester.pump();
expect(harness.store.font, AppFont.sourceSans3);
expect(harness.store.id, AppFont.sourceSans3.id);
expect(
tester
.widget<RadioGroup<AppFont>>(find.byType(RadioGroup<AppFont>))
.widget<RadioGroup<FontChoice>>(find.byType(RadioGroup<FontChoice>))
.groupValue,
AppFont.sourceSans3,
);
@@ -107,7 +108,7 @@ void main() {
final harness = _Harness();
await _pump(tester, harness, cupertino: true);
expect(find.byType(RadioListTile<AppFont>), findsNothing);
expect(find.byType(RadioListTile<FontChoice>), findsNothing);
expect(_familyOf(tester, AppFont.system), 'CupertinoSystemText');
Finder check(AppFont font) => find.descendant(
of: find.byKey(settingsFontKey(font)),
@@ -119,7 +120,7 @@ void main() {
await tester.pumpAndSettle();
expect(check(AppFont.system), findsNothing);
expect(check(AppFont.inter), findsOneWidget);
expect(harness.store.font, AppFont.inter);
expect(harness.store.id, AppFont.inter.id);
expect(harness.fonts.family, 'Inter');
});