Files
pipelines/app/test/ui/theme/app_font_controller_test.dart
T
alan 2853b18b9e 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.
2026-11-04 02:01:49 +00:00

157 lines
5.0 KiB
Dart

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');
});
}