c24e7f40b4
Add font… picks one or more files, checks them with a bounded sfnt reader (name table family, OS/2 weight), registers them with FontLoader before storing, and chooses the new family. Added fonts list below the bundled ones with Rename and Remove; removing the chosen one falls back to System default. Bytes live in the app support directory natively and in IndexedDB on the web (localStorage is too small); a shared_preferences index names them. Limits: 10 MB per file, 40 MB total. The capture kit gains a files= step.
157 lines
5.0 KiB
Dart
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.id),
|
|
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.id),
|
|
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.id, AppFont.atkinsonHyperlegible.id);
|
|
expect(fonts.family, 'Atkinson Hyperlegible Next');
|
|
await fonts.select(AppFont.system);
|
|
expect(store.id, AppFont.system.id);
|
|
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');
|
|
});
|
|
}
|