import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.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/font_choice.dart'; class _Harness { _Harness({this.hold = false}); final bool hold; final store = MemoryAppFontStore(); final loads = []; final held = >[]; late final fonts = AppFontController( store: store, loader: (family, assets) { loads.addAll(assets); if (!hold || assets.length == 1) return Future.value(); final done = Completer(); held.add(done); return done.future; }, ); } Future _pump( WidgetTester tester, _Harness harness, { bool cupertino = false, }) async { final section = SingleChildScrollView( child: SettingsFontSection(controller: harness.fonts), ); await tester.pumpWidget( cupertino ? CupertinoApp(home: CupertinoPageScaffold(child: section)) : MaterialApp(home: Scaffold(body: section)), ); await tester.pumpAndSettle(); } String? _familyOf(WidgetTester tester, AppFont font) => tester .widget( find.descendant( of: find.byKey(settingsFontKey(font)), matching: find.text(font.label), ), ) .style ?.fontFamily; void main() { testWidgets('Material: every option is a radio named in its own face', ( tester, ) async { final harness = _Harness(); await _pump(tester, harness); expect(find.text(fontSectionTitle), findsOneWidget); for (final font in AppFont.values) { expect(find.byKey(settingsFontKey(font)), findsOneWidget); expect(_familyOf(tester, font), font.family ?? 'Roboto'); } // Showing the picker fetched one regular weight per option, no more. expect(harness.loads, [ for (final font in AppFont.values) if (font.previewAsset != null) font.previewAsset, ]); final group = tester.widget>( find.byType(RadioGroup), ); expect(group.groupValue, AppFont.system); }); testWidgets('Material: choosing a font stores it and shows it loading', ( tester, ) async { final harness = _Harness(hold: true); await _pump(tester, harness); await tester.tap(find.text('Source Sans 3')); await tester.pump(); expect(harness.store.id, AppFont.sourceSans3.id); expect( tester .widget>(find.byType(RadioGroup)) .groupValue, AppFont.sourceSans3, ); expect(find.text(fontLoadingText), findsOneWidget); expect(harness.fonts.family, isNull); harness.held.single.complete(); await tester.pumpAndSettle(); expect(find.text(fontLoadingText), findsNothing); expect(harness.fonts.family, 'Source Sans 3'); }); testWidgets('Cupertino: the chosen row carries the checkmark', ( tester, ) async { final harness = _Harness(); await _pump(tester, harness, cupertino: true); expect(find.byType(RadioListTile), findsNothing); expect(_familyOf(tester, AppFont.system), 'CupertinoSystemText'); Finder check(AppFont font) => find.descendant( of: find.byKey(settingsFontKey(font)), matching: find.byIcon(CupertinoIcons.check_mark), ); expect(check(AppFont.system), findsOneWidget); await tester.tap(find.text('Inter')); await tester.pumpAndSettle(); expect(check(AppFont.system), findsNothing); expect(check(AppFont.inter), findsOneWidget); expect(harness.store.id, AppFont.inter.id); expect(harness.fonts.family, 'Inter'); }); testWidgets('a failed save is said under the group', (tester) async { final harness = _Harness(); harness.store.failSaves = true; await _pump(tester, harness); await tester.tap(find.text('Inter')); await tester.pumpAndSettle(); expect(find.text(fontSaveFailedText), findsOneWidget); }); }