Merge: Add font works with a real file dialog and always reports failures
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
@@ -7,6 +8,7 @@ import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/settings/settings_custom_font_actions.dart';
|
||||
import 'package:pipelines_app/features/settings/settings_custom_font_dialogs.dart';
|
||||
import 'package:pipelines_app/features/settings/settings_font_section.dart';
|
||||
import 'package:pipelines_app/features/settings/settings_row.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';
|
||||
@@ -14,6 +16,7 @@ import 'package:pipelines_app/ui/theme/custom_fonts/custom_font.dart';
|
||||
import 'package:pipelines_app/ui/theme/custom_fonts/custom_font_draft.dart';
|
||||
import 'package:pipelines_app/ui/theme/custom_fonts/custom_font_library.dart';
|
||||
import 'package:pipelines_app/ui/theme/custom_fonts/font_bytes_store.dart';
|
||||
import 'package:pipelines_app/ui/theme/custom_fonts/font_file_picker.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
PickedFontFile _pick(String path) => PickedFontFile(
|
||||
@@ -40,11 +43,12 @@ Future<void> _pump(
|
||||
WidgetTester tester,
|
||||
_Harness harness, {
|
||||
bool cupertino = false,
|
||||
FontFilePicker? pick,
|
||||
}) async {
|
||||
final section = SingleChildScrollView(
|
||||
child: SettingsFontSection(
|
||||
controller: harness.fonts,
|
||||
pickFiles: harness.pick,
|
||||
pickFiles: pick ?? harness.pick,
|
||||
),
|
||||
);
|
||||
await tester.pumpWidget(
|
||||
@@ -168,6 +172,78 @@ void main() {
|
||||
expect(harness.fonts.customFonts, isEmpty);
|
||||
});
|
||||
|
||||
Future<void> expectProblem(WidgetTester tester, Matcher message) async {
|
||||
expect(find.text(addCustomFontFailedTitle), findsOneWidget);
|
||||
expect(
|
||||
find.byWidgetPredicate(
|
||||
(w) => w is Text && message.matches(w.data, <Object?, Object?>{}),
|
||||
),
|
||||
findsOneWidget,
|
||||
);
|
||||
await tester.tap(find.text('OK'));
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
testWidgets('every failure of the picker is said, never silent', (
|
||||
tester,
|
||||
) async {
|
||||
Object? failure;
|
||||
final harness = _Harness();
|
||||
await _pump(
|
||||
tester,
|
||||
harness,
|
||||
pick: () async => failure == null ? harness.next : throw failure,
|
||||
);
|
||||
|
||||
// A file the host could not read (null bytes).
|
||||
failure = const CustomFontRejected('“A.ttf” could not be read.');
|
||||
await tester.tap(find.text(addCustomFontText));
|
||||
await tester.pumpAndSettle();
|
||||
await expectProblem(tester, equals('“A.ttf” could not be read.'));
|
||||
|
||||
// Any other picker exception.
|
||||
failure = StateError('boom');
|
||||
await tester.tap(find.text(addCustomFontText));
|
||||
await tester.pumpAndSettle();
|
||||
await expectProblem(tester, contains('The file chooser failed'));
|
||||
|
||||
// The wrong type, now that mobile pickers offer every file.
|
||||
failure = null;
|
||||
harness.next = <PickedFontFile>[
|
||||
PickedFontFile(name: 'photo.png', bytes: utf8.encode('png')),
|
||||
];
|
||||
await tester.tap(find.text(addCustomFontText));
|
||||
await tester.pumpAndSettle();
|
||||
await expectProblem(tester, contains('is not a .ttf or .otf font file'));
|
||||
expect(harness.fonts.customFonts, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('a font that fails to save is said, nothing stored', (
|
||||
tester,
|
||||
) async {
|
||||
final harness = _Harness()..next = <PickedFontFile>[regular];
|
||||
harness.bytes.failWrites = true;
|
||||
await _pump(tester, harness);
|
||||
await tester.tap(find.text(addCustomFontText));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text('Add'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text(addCustomFontFailedTitle), findsOneWidget);
|
||||
expect(harness.fonts.customFonts, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('an open picker does not disable Add font…', (tester) async {
|
||||
final harness = _Harness();
|
||||
final never = Completer<List<PickedFontFile>?>();
|
||||
await _pump(tester, harness, pick: () => never.future);
|
||||
await tester.tap(find.text(addCustomFontText));
|
||||
await tester.pump();
|
||||
// A browser that never reports the cancel must not leave it stuck.
|
||||
expect(find.text(addingCustomFontText), findsNothing);
|
||||
final row = tester.widget<SettingsRow>(find.byKey(settingsAddFontKey));
|
||||
expect(row.enabled, isTrue);
|
||||
});
|
||||
|
||||
testWidgets('Cupertino: an added font has the checkmark and options', (
|
||||
tester,
|
||||
) async {
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/ui/theme/custom_fonts/custom_font_draft.dart';
|
||||
import 'package:pipelines_app/ui/theme/custom_fonts/font_file_picker.dart';
|
||||
|
||||
FontFileSource _source(
|
||||
String name, {
|
||||
int? size,
|
||||
Future<Uint8List?> Function()? read,
|
||||
}) => FontFileSource(
|
||||
name: name,
|
||||
size: size,
|
||||
read: read ?? () async => Uint8List.fromList(<int>[1, 2, 3]),
|
||||
);
|
||||
|
||||
Matcher _rejected(String message) => throwsA(
|
||||
isA<CustomFontRejected>().having((e) => e.message, 'message', message),
|
||||
);
|
||||
|
||||
void main() {
|
||||
test('an empty selection is a cancel', () async {
|
||||
expect(await readFontSelection(const <FontFileSource>[]), isNull);
|
||||
});
|
||||
|
||||
test('chosen files come back with their bytes, in order', () async {
|
||||
final files = await readFontSelection(<FontFileSource>[
|
||||
_source('A.ttf', size: 3),
|
||||
_source('B.otf'),
|
||||
]);
|
||||
expect(files!.map((f) => f.name), <String>['A.ttf', 'B.otf']);
|
||||
expect(files.first.bytes, <int>[1, 2, 3]);
|
||||
});
|
||||
|
||||
test('a file whose bytes the host cannot provide is refused', () async {
|
||||
await expectLater(
|
||||
readFontSelection(<FontFileSource>[
|
||||
_source('A.ttf', read: () async => null),
|
||||
]),
|
||||
_rejected('“A.ttf” could not be read.'),
|
||||
);
|
||||
});
|
||||
|
||||
test('a read that throws is refused by name, not swallowed', () async {
|
||||
await expectLater(
|
||||
readFontSelection(<FontFileSource>[
|
||||
_source('A.ttf', read: () async => throw StateError('NotReadable')),
|
||||
]),
|
||||
_rejected('“A.ttf” could not be read.'),
|
||||
);
|
||||
});
|
||||
|
||||
test('a file over the limit is refused before it is read', () async {
|
||||
var read = false;
|
||||
await expectLater(
|
||||
readFontSelection(<FontFileSource>[
|
||||
_source(
|
||||
'Huge.ttf',
|
||||
size: CustomFontLimits.maxFileBytes + 1,
|
||||
read: () async {
|
||||
read = true;
|
||||
return Uint8List(0);
|
||||
},
|
||||
),
|
||||
]),
|
||||
_rejected('“Huge.ttf” is larger than 10 MB.'),
|
||||
);
|
||||
expect(read, isFalse);
|
||||
});
|
||||
|
||||
test('another file type passes through for inspect to refuse', () async {
|
||||
final files = await readFontSelection(<FontFileSource>[
|
||||
_source('photo.png'),
|
||||
]);
|
||||
expect(
|
||||
() => CustomFontDraft.inspect(files!),
|
||||
_rejected('“photo.png” is not a .ttf or .otf font file.'),
|
||||
);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user