Files
pipelines/app/test/features/settings/settings_custom_font_test.dart
alan a7c41063fa Remove Cupertino branches from Settings
Keep the Material-only settings implementation and its focused tests while preserving public cupertino parameters for later cleanup. This removes unreachable platform-specific widgets and test variants without changing the Material path.
2026-11-11 05:05:57 +00:00

240 lines
8.6 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'dart:io';
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/features/settings/settings_row.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_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:pipelines_app/ui/theme/custom_fonts/font_file_picker.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, {
FontFilePicker? pick,
}) async {
final section = SingleChildScrollView(
child: SettingsFontSection(
controller: harness.fonts,
pickFiles: pick ?? harness.pick,
),
);
await tester.pumpWidget(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);
});
Future<void> expectProblem(WidgetTester tester, Matcher message) async {
expect(find.text(addCustomFontFailedTitle), findsOneWidget);
expect(
find.byWidgetPredicate(
(w) => w is Text && message.matches(w.data, <Object?, Object?>{}),
),
findsOneWidget,
);
await tester.tap(find.text('OK'));
await tester.pumpAndSettle();
}
testWidgets('every failure of the picker is said, never silent', (
tester,
) async {
Object? failure;
final harness = _Harness();
await _pump(
tester,
harness,
pick: () async => failure == null ? harness.next : throw failure,
);
// A file the host could not read (null bytes).
failure = const CustomFontRejected('“A.ttf” could not be read.');
await tester.tap(find.text(addCustomFontText));
await tester.pumpAndSettle();
await expectProblem(tester, equals('“A.ttf” could not be read.'));
// Any other picker exception.
failure = StateError('boom');
await tester.tap(find.text(addCustomFontText));
await tester.pumpAndSettle();
await expectProblem(tester, contains('The file chooser failed'));
// The wrong type, now that mobile pickers offer every file.
failure = null;
harness.next = <PickedFontFile>[
PickedFontFile(name: 'photo.png', bytes: utf8.encode('png')),
];
await tester.tap(find.text(addCustomFontText));
await tester.pumpAndSettle();
await expectProblem(tester, contains('is not a .ttf or .otf font file'));
expect(harness.fonts.customFonts, isEmpty);
});
testWidgets('a font that fails to save is said, nothing stored', (
tester,
) async {
final harness = _Harness()..next = <PickedFontFile>[regular];
harness.bytes.failWrites = true;
await _pump(tester, harness);
await tester.tap(find.text(addCustomFontText));
await tester.pumpAndSettle();
await tester.tap(find.text('Add'));
await tester.pumpAndSettle();
expect(find.text(addCustomFontFailedTitle), findsOneWidget);
expect(harness.fonts.customFonts, isEmpty);
});
testWidgets('an open picker does not disable Add font…', (tester) async {
final harness = _Harness();
final never = Completer<List<PickedFontFile>?>();
await _pump(tester, harness, pick: () => never.future);
await tester.tap(find.text(addCustomFontText));
await tester.pump();
// A browser that never reports the cancel must not leave it stuck.
expect(find.text(addingCustomFontText), findsNothing);
final row = tester.widget<SettingsRow>(find.byKey(settingsAddFontKey));
expect(row.enabled, isTrue);
});
}