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 {
|
||||
|
||||
Reference in New Issue
Block a user