Settings: per-device interface font choice
System default plus Inter, IBM Plex Sans, Source Sans 3 and Atkinson Hyperlegible Next (OFL 1.1, bundled as on-demand assets and loaded with FontLoader, so the web fetches only the previewed/chosen files). Stored in shared_preferences, applied live to Material and Cupertino text themes; RobotoMono styles unchanged. Licences registered with LicenseRegistry.
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
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';
|
||||
|
||||
class _Harness {
|
||||
_Harness({this.hold = false});
|
||||
|
||||
final bool hold;
|
||||
final store = MemoryAppFontStore();
|
||||
final loads = <String>[];
|
||||
final held = <Completer<void>>[];
|
||||
|
||||
late final fonts = AppFontController(
|
||||
store: store,
|
||||
loader: (family, assets) {
|
||||
loads.addAll(assets);
|
||||
if (!hold || assets.length == 1) return Future<void>.value();
|
||||
final done = Completer<void>();
|
||||
held.add(done);
|
||||
return done.future;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _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<Text>(
|
||||
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, <String?>[
|
||||
for (final font in AppFont.values)
|
||||
if (font.previewAsset != null) font.previewAsset,
|
||||
]);
|
||||
final group = tester.widget<RadioGroup<AppFont>>(
|
||||
find.byType(RadioGroup<AppFont>),
|
||||
);
|
||||
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.font, AppFont.sourceSans3);
|
||||
expect(
|
||||
tester
|
||||
.widget<RadioGroup<AppFont>>(find.byType(RadioGroup<AppFont>))
|
||||
.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<AppFont>), 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.font, AppFont.inter);
|
||||
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);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter_test/flutter_test.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';
|
||||
|
||||
/// Records each load and completes it when the test says so.
|
||||
class _Loads {
|
||||
final calls = <(String, List<String>)>[];
|
||||
final pending = <Completer<void>>[];
|
||||
bool immediate = true;
|
||||
|
||||
Future<void> call(String family, List<String> assets) {
|
||||
calls.add((family, assets));
|
||||
final done = Completer<void>();
|
||||
pending.add(done);
|
||||
if (immediate) done.complete();
|
||||
return done.future;
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
test('starts on the system default with no download', () async {
|
||||
final loads = _Loads();
|
||||
final fonts = AppFontController(
|
||||
store: MemoryAppFontStore(),
|
||||
loader: loads.call,
|
||||
);
|
||||
await fonts.restore();
|
||||
expect(fonts.selected, AppFont.system);
|
||||
expect(fonts.family, isNull);
|
||||
expect(loads.calls, isEmpty);
|
||||
});
|
||||
|
||||
test('restore loads only the stored family, then applies it', () async {
|
||||
final loads = _Loads()..immediate = false;
|
||||
final fonts = AppFontController(
|
||||
store: MemoryAppFontStore(AppFont.inter),
|
||||
loader: loads.call,
|
||||
);
|
||||
final restoring = fonts.restore();
|
||||
await pumpEventQueue();
|
||||
expect(fonts.selected, AppFont.inter);
|
||||
expect(fonts.family, isNull, reason: 'system face until loaded');
|
||||
expect(fonts.loading, isTrue);
|
||||
expect(loads.calls.single.$1, 'Inter');
|
||||
expect(loads.calls.single.$2, AppFont.inter.assets);
|
||||
loads.pending.single.complete();
|
||||
await restoring;
|
||||
expect(fonts.family, 'Inter');
|
||||
expect(fonts.loading, isFalse);
|
||||
});
|
||||
|
||||
test(
|
||||
'restore with a wait returns at the deadline and applies later',
|
||||
() async {
|
||||
final loads = _Loads()..immediate = false;
|
||||
final fonts = AppFontController(
|
||||
store: MemoryAppFontStore(AppFont.sourceSans3),
|
||||
loader: loads.call,
|
||||
);
|
||||
await fonts.restore(wait: const Duration(milliseconds: 10));
|
||||
expect(fonts.family, isNull);
|
||||
loads.pending.single.complete();
|
||||
await pumpEventQueue();
|
||||
expect(fonts.family, 'Source Sans 3');
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'select stores the choice and applies it; system needs no load',
|
||||
() async {
|
||||
final store = MemoryAppFontStore();
|
||||
final loads = _Loads();
|
||||
final fonts = AppFontController(store: store, loader: loads.call);
|
||||
await fonts.select(AppFont.atkinsonHyperlegible);
|
||||
expect(store.font, AppFont.atkinsonHyperlegible);
|
||||
expect(fonts.family, 'Atkinson Hyperlegible Next');
|
||||
await fonts.select(AppFont.system);
|
||||
expect(store.font, AppFont.system);
|
||||
expect(fonts.family, isNull);
|
||||
expect(loads.calls, hasLength(1));
|
||||
},
|
||||
);
|
||||
|
||||
test('a newer choice wins over a slower earlier load', () async {
|
||||
final loads = _Loads()..immediate = false;
|
||||
final fonts = AppFontController(
|
||||
store: MemoryAppFontStore(),
|
||||
loader: loads.call,
|
||||
);
|
||||
final first = fonts.select(AppFont.inter);
|
||||
await pumpEventQueue();
|
||||
final second = fonts.select(AppFont.ibmPlexSans);
|
||||
await pumpEventQueue();
|
||||
loads.pending[1].complete();
|
||||
await second;
|
||||
expect(fonts.family, 'IBM Plex Sans');
|
||||
loads.pending[0].complete();
|
||||
await first;
|
||||
expect(fonts.family, 'IBM Plex Sans');
|
||||
});
|
||||
|
||||
test(
|
||||
'preview loads the regular weight once; choosing loads the rest',
|
||||
() async {
|
||||
final loads = _Loads();
|
||||
final fonts = AppFontController(
|
||||
store: MemoryAppFontStore(),
|
||||
loader: loads.call,
|
||||
);
|
||||
expect(fonts.canPreview(AppFont.inter), isFalse);
|
||||
await fonts.preview(AppFont.inter);
|
||||
await fonts.preview(AppFont.inter);
|
||||
await fonts.preview(AppFont.system);
|
||||
expect(fonts.canPreview(AppFont.inter), isTrue);
|
||||
expect(fonts.canPreview(AppFont.system), isTrue);
|
||||
expect(loads.calls.single.$1, 'Inter');
|
||||
expect(loads.calls.single.$2, <String>['fonts/inter/Inter-Regular.ttf']);
|
||||
await fonts.select(AppFont.inter);
|
||||
expect(loads.calls.last.$2, AppFont.inter.assets.skip(1).toList());
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'a failed load keeps the current face, says so, and can retry',
|
||||
() async {
|
||||
var fail = true;
|
||||
final fonts = AppFontController(
|
||||
store: MemoryAppFontStore(),
|
||||
loader: (family, assets) async {
|
||||
if (fail) throw StateError('offline');
|
||||
},
|
||||
);
|
||||
await fonts.select(AppFont.inter);
|
||||
expect(fonts.family, isNull);
|
||||
expect(fonts.loadFailed, AppFont.inter);
|
||||
expect(fonts.loading, isFalse);
|
||||
fail = false;
|
||||
await fonts.select(AppFont.inter);
|
||||
expect(fonts.loadFailed, isNull);
|
||||
expect(fonts.family, 'Inter');
|
||||
},
|
||||
);
|
||||
|
||||
test('a failed save is reported but the font still applies', () async {
|
||||
final fonts = AppFontController(
|
||||
store: MemoryAppFontStore()..failSaves = true,
|
||||
loader: _Loads().call,
|
||||
);
|
||||
await fonts.select(AppFont.inter);
|
||||
expect(fonts.saveFailed, isTrue);
|
||||
expect(fonts.family, 'Inter');
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/ui/theme/app_font.dart';
|
||||
import 'package:pipelines_app/ui/theme/app_font_store.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
void main() {
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
setUp(() => SharedPreferences.setMockInitialValues(<String, Object>{}));
|
||||
|
||||
test('nothing stored is the system default', () async {
|
||||
expect(await const SharedPreferencesAppFontStore().load(), AppFont.system);
|
||||
});
|
||||
|
||||
test(
|
||||
'a choice round-trips under its stable id, on the device only',
|
||||
() async {
|
||||
const store = SharedPreferencesAppFontStore();
|
||||
expect(await store.save(AppFont.ibmPlexSans), isTrue);
|
||||
final preferences = await SharedPreferences.getInstance();
|
||||
expect(preferences.getKeys(), <String>{appFontKey});
|
||||
expect(preferences.getString(appFontKey), 'ibm-plex-sans');
|
||||
expect(await store.load(), AppFont.ibmPlexSans);
|
||||
|
||||
expect(await store.save(AppFont.system), isTrue);
|
||||
expect(await store.load(), AppFont.system);
|
||||
},
|
||||
);
|
||||
|
||||
test('an unknown stored id falls back to the system default', () async {
|
||||
SharedPreferences.setMockInitialValues(<String, Object>{
|
||||
appFontKey: 'comic-sans',
|
||||
});
|
||||
expect(await const SharedPreferencesAppFontStore().load(), AppFont.system);
|
||||
});
|
||||
|
||||
test('every bundled option has its four weights and its licence', () {
|
||||
for (final font in AppFont.values) {
|
||||
if (font == AppFont.system) {
|
||||
expect(font.family, isNull);
|
||||
expect(font.assets, isEmpty);
|
||||
continue;
|
||||
}
|
||||
expect(font.assets, hasLength(4));
|
||||
expect(font.previewAsset, font.assets.first);
|
||||
for (final asset in <String>[...font.assets, font.licenseAsset!]) {
|
||||
expect(File(asset).existsSync(), isTrue, reason: asset);
|
||||
}
|
||||
expect(
|
||||
File(font.licenseAsset!).readAsStringSync(),
|
||||
contains('SIL OPEN FONT LICENSE Version 1.1'),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('the optional fonts are assets, not startup fonts', () {
|
||||
final pubspec = File('pubspec.yaml').readAsStringSync();
|
||||
final fonts = pubspec.substring(pubspec.indexOf('\n fonts:'));
|
||||
for (final font in AppFont.values.where((f) => f.family != null)) {
|
||||
expect(fonts, isNot(contains('family: ${font.family}')));
|
||||
expect(
|
||||
pubspec,
|
||||
contains(' - ${font.licenseAsset!.replaceAll('OFL.txt', '')}\n'),
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/application/app.dart';
|
||||
import 'package:pipelines_app/shell/destination.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/app_theme.dart';
|
||||
|
||||
void main() {
|
||||
test('Material themes take the family and keep sizes and weights', () {
|
||||
for (final build in <ThemeData Function({String? fontFamily})>[
|
||||
appLightTheme,
|
||||
appDarkTheme,
|
||||
]) {
|
||||
final system = build().textTheme;
|
||||
final inter = build(fontFamily: 'Inter').textTheme;
|
||||
expect(build().textTheme.bodyMedium?.fontFamily, isNot('Inter'));
|
||||
for (final (a, b) in <(TextStyle?, TextStyle?)>[
|
||||
(system.bodyMedium, inter.bodyMedium),
|
||||
(system.titleMedium, inter.titleMedium),
|
||||
(system.labelLarge, inter.labelLarge),
|
||||
(system.headlineSmall, inter.headlineSmall),
|
||||
]) {
|
||||
expect(b?.fontFamily, 'Inter');
|
||||
expect(b?.fontSize, a?.fontSize);
|
||||
expect(b?.fontWeight, a?.fontWeight);
|
||||
}
|
||||
expect(
|
||||
build(
|
||||
fontFamily: 'Inter',
|
||||
).cupertinoOverrideTheme?.textTheme?.textStyle.fontFamily,
|
||||
'Inter',
|
||||
);
|
||||
expect(build().cupertinoOverrideTheme, isNull);
|
||||
}
|
||||
});
|
||||
|
||||
test('the Cupertino theme takes the family in every text role', () {
|
||||
expect(appCupertinoThemeWith(), same(appCupertinoTheme));
|
||||
final text = appCupertinoThemeWith(fontFamily: 'Inter').textTheme;
|
||||
const stock = CupertinoTextThemeData();
|
||||
for (final (a, b) in <(TextStyle, TextStyle)>[
|
||||
(stock.textStyle, text.textStyle),
|
||||
(stock.navTitleTextStyle, text.navTitleTextStyle),
|
||||
(stock.navLargeTitleTextStyle, text.navLargeTitleTextStyle),
|
||||
(stock.tabLabelTextStyle, text.tabLabelTextStyle),
|
||||
(stock.actionTextStyle, text.actionTextStyle),
|
||||
(stock.pickerTextStyle, text.pickerTextStyle),
|
||||
]) {
|
||||
expect(b.fontFamily, 'Inter');
|
||||
expect(b.fontSize, a.fontSize);
|
||||
expect(b.fontWeight, a.fontWeight);
|
||||
}
|
||||
expect(
|
||||
appCupertinoThemeWith(fontFamily: 'Inter').scaffoldBackgroundColor,
|
||||
AppColors.background,
|
||||
);
|
||||
});
|
||||
|
||||
for (final cupertino in <bool>[false, true]) {
|
||||
testWidgets('the ${cupertino ? 'Cupertino' : 'Material'} host switches '
|
||||
'font live, without a restart', (tester) async {
|
||||
final fonts = AppFontController(
|
||||
store: MemoryAppFontStore(),
|
||||
loader: (family, assets) async {},
|
||||
);
|
||||
String? family;
|
||||
await tester.pumpWidget(
|
||||
MyApp(
|
||||
cupertino: cupertino,
|
||||
fonts: fonts,
|
||||
destination: AppDestination.settings,
|
||||
destinationBodies: <AppDestination, WidgetBuilder>{
|
||||
AppDestination.settings: (context) => Builder(
|
||||
builder: (context) {
|
||||
family = DefaultTextStyle.of(context).style.fontFamily;
|
||||
return const Text('body');
|
||||
},
|
||||
),
|
||||
},
|
||||
),
|
||||
);
|
||||
final before = family;
|
||||
expect(before, isNot('IBM Plex Sans'));
|
||||
await fonts.select(AppFont.ibmPlexSans);
|
||||
await tester.pumpAndSettle();
|
||||
expect(family, 'IBM Plex Sans');
|
||||
await fonts.select(AppFont.system);
|
||||
await tester.pumpAndSettle();
|
||||
expect(family, before);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user