From 7d6731ca4010720bcb764ae077b14da8df38c790 Mon Sep 17 00:00:00 2001 From: Ellet Date: Fri, 21 Jun 2024 16:01:28 +0300 Subject: [PATCH] test(localizations): write a few tests for FlutterQuillLocalizationsWidget --- lib/src/l10n/extensions/localizations.dart | 19 ++++-- test/widgets/editor_test.dart | 78 ++++++++++++++++++++++ 2 files changed, 90 insertions(+), 7 deletions(-) diff --git a/lib/src/l10n/extensions/localizations.dart b/lib/src/l10n/extensions/localizations.dart index 2bee6a96..d8726dee 100644 --- a/lib/src/l10n/extensions/localizations.dart +++ b/lib/src/l10n/extensions/localizations.dart @@ -4,18 +4,23 @@ import '../generated/quill_localizations.dart' as generated; typedef FlutterQuillLocalizations = generated.FlutterQuillLocalizations; +class MissingFlutterQuillLocalizationException extends UnimplementedError { + MissingFlutterQuillLocalizationException(); + @override + String? get message => + 'FlutterQuillLocalizations instance is required and could not found. ' + 'Ensure that you are wrapping the current widget with ' + 'FlutterQuillLocalizationsWidget or add ' + 'FlutterQuillLocalizations.delegate to the localizationsDelegates ' + 'in your App widget (e.,g WidgetsApp, MaterialApp). If you believe this is a bug, consider reporting it.'; +} + extension LocalizationsExt on BuildContext { /// Require the [FlutterQuillLocalizations] instance /// /// `loc` is short for `localizations` FlutterQuillLocalizations get loc { return FlutterQuillLocalizations.of(this) ?? - (throw UnimplementedError( - "The instance of FlutterQuillLocalizations.of(context) is null and it's" - ' required, please make sure you wrapping the current widget with ' - 'FlutterQuillLocalizationsWidget or add ' - 'FlutterQuillLocalizations.delegate to the localizationsDelegates ' - 'in your App widget, please consider reporting this as a bug', - )); + (throw MissingFlutterQuillLocalizationException()); } } diff --git a/test/widgets/editor_test.dart b/test/widgets/editor_test.dart index 2a4bd8ab..bb115284 100644 --- a/test/widgets/editor_test.dart +++ b/test/widgets/editor_test.dart @@ -3,6 +3,7 @@ import 'dart:convert' show jsonDecode; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_quill/flutter_quill.dart'; +import 'package:flutter_quill/translations.dart'; import 'package:flutter_quill_test/flutter_quill_test.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -137,5 +138,82 @@ void main() { await tester.tap(find.byIcon(Icons.copy)); expect(didCopy, isTrue); }); + + testWidgets( + 'make sure QuillEditorOpenSearchAction does not throw an exception', + (tester) async { + final editorFocusNode = FocusNode(); + await tester.pumpWidget( + MaterialApp( + home: QuillEditor.basic( + configurations: QuillEditorConfigurations(controller: controller), + focusNode: editorFocusNode, + ), + ), + ); + // Required, otherwise the action shortcuts won't be invoked. + editorFocusNode.requestFocus(); + + await tester.sendKeyDownEvent(LogicalKeyboardKey.control); + await tester.sendKeyEvent(LogicalKeyboardKey.keyF); + await tester.sendKeyUpEvent(LogicalKeyboardKey.control); + + await tester.pump(); + + final exception = tester.takeException(); + expect( + exception, + isNot( + isInstanceOf(), + ), + ); + + expect(exception, isNull); + }, + ); + + testWidgets( + 'should throw FlutterQuillLocalizationsWidget if the delegate not provided', + (tester) async { + await tester.pumpWidget( + MaterialApp( + home: Builder( + builder: (context) => Text(context.loc.font), + ), + ), + ); + + final exception = tester.takeException(); + + expect(exception, isNotNull); + expect( + exception, + isA(), + ); + }, + ); + + testWidgets( + 'should not throw FlutterQuillLocalizationsWidget if the delegate is provided', + (tester) async { + await tester.pumpWidget( + MaterialApp( + home: FlutterQuillLocalizationsWidget( + child: Builder( + builder: (context) => Text(context.loc.font), + ), + ), + ), + ); + + final exception = tester.takeException(); + + expect(exception, isNull); + expect( + exception, + isNot(isA()), + ); + }, + ); }); }