import 'dart:async'; 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/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.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? next; late final fonts = AppFontController( store: store, library: CustomFontLibrary(bytes: bytes), loader: (family, sources) async {}, bytesLoader: (family, faces) async {}, ); Future?> pick() async => next; } Future _pump( WidgetTester tester, _Harness harness, { bool cupertino = false, FontFilePicker? pick, }) async { final section = SingleChildScrollView( child: SettingsFontSection( controller: harness.fonts, pickFiles: pick ?? harness.pick, ), ); await tester.pumpWidget( cupertino ? CupertinoApp(home: CupertinoPageScaffold(child: section)) : MaterialApp(home: Scaffold(body: section)), ); await tester.pumpAndSettle(); } void main() { setUp(() => SharedPreferences.setMockInitialValues({})); 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 = [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(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( 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(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 = [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 expectProblem(WidgetTester tester, Matcher message) async { expect(find.text(addCustomFontFailedTitle), findsOneWidget); expect( find.byWidgetPredicate( (w) => w is Text && message.matches(w.data, {}), ), 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(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 = [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?>(); 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(find.byKey(settingsAddFontKey)); expect(row.enabled, isTrue); }); testWidgets('Cupertino: an added font has the checkmark and options', ( tester, ) async { final harness = _Harness()..next = [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); }); }