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,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