From 505ed7018fac8ea60eb2a340d4ceffb0324ba295 Mon Sep 17 00:00:00 2001 From: Ellet Date: Thu, 19 Dec 2024 15:33:44 +0300 Subject: [PATCH] test: add test for bug fix PR #2415 --- test/common/utils/quill_test_app.dart | 90 +++++++++++++++++++ .../buttons/color/color_dialog_test.dart | 46 ++++++++++ 2 files changed, 136 insertions(+) create mode 100644 test/common/utils/quill_test_app.dart create mode 100644 test/toolbar/buttons/color/color_dialog_test.dart diff --git a/test/common/utils/quill_test_app.dart b/test/common/utils/quill_test_app.dart new file mode 100644 index 000000000..0f03ff235 --- /dev/null +++ b/test/common/utils/quill_test_app.dart @@ -0,0 +1,90 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_quill/flutter_quill.dart'; +import 'package:flutter_quill/internal.dart'; +import 'package:flutter_test/flutter_test.dart'; + +/// A utility for testing widgets within an application widget configured with +/// the necessary localizations. +/// +/// Simplifies test setup, enabling concise test cases such as: +/// +/// ```dart +/// testWidgets('Test description', (tester) async { +/// final exampleWidget = ...; +/// await tester.pumpWidget(QuillTestApp.withScaffold(exampleWidget)); +/// }); +/// ``` +/// +/// Instead of requiring the verbose setup: +/// +/// ```dart +/// testWidgets('Test description', (tester) async { +/// final exampleWidget = ...; +/// await tester.pumpWidget(MaterialApp( +/// localizationsDelegates: FlutterQuillLocalizations.localizationsDelegates, +/// supportedLocales: FlutterQuillLocalizations.supportedLocales, +/// home: Scaffold(body: exampleWidget), +/// )); +/// }); +/// ``` +class QuillTestApp extends StatelessWidget { + /// Constructs a [QuillTestApp] instance. + /// + /// Either [home] or [scaffoldBody] must be provided. + /// Throws an [ArgumentError] if both are provided. + QuillTestApp({ + required this.home, + required this.scaffoldBody, + super.key, + }) { + if (home != null && scaffoldBody != null) { + throw ArgumentError('Either the home or scaffoldBody must be null'); + } + } + + /// Creates a [QuillTestApp] with a [Scaffold] wrapping the given [body] widget. + factory QuillTestApp.withScaffold(Widget body) => + QuillTestApp(home: null, scaffoldBody: body); + + /// Creates a [QuillTestApp] with the specified [home] widget. + factory QuillTestApp.home(Widget home) => + QuillTestApp(home: home, scaffoldBody: null); + + /// The home widget for the application. + /// + /// If [home] is not null, [scaffoldBody] must be null. + final Widget? home; + + /// The body widget for a [Scaffold] used as the application home. + /// + /// If [scaffoldBody] is not null, [home] must be null. + final Widget? scaffoldBody; + + @override + Widget build(BuildContext context) { + return MaterialApp( + localizationsDelegates: FlutterQuillLocalizations.localizationsDelegates, + supportedLocales: FlutterQuillLocalizations.supportedLocales, + home: home ?? + Scaffold( + body: scaffoldBody, + ), + ); + } +} + +extension LocalizationsExt on WidgetTester { + /// Retrieves the localizations during a test. + /// + /// Example usage: + /// + /// ```dart + /// testWidgets('Verifies localized text', (tester) async { + /// final localizations = tester.localizationsFromElement(ImageOptionsMenu); + /// + /// expect(find.text(localizations.successImageDownloaded), findsOneWidget); + /// }); + /// ``` + FlutterQuillLocalizations localizationsFromElement(Type type) => + (element(find.byType(type)) as BuildContext).loc; +} diff --git a/test/toolbar/buttons/color/color_dialog_test.dart b/test/toolbar/buttons/color/color_dialog_test.dart new file mode 100644 index 000000000..95109c7da --- /dev/null +++ b/test/toolbar/buttons/color/color_dialog_test.dart @@ -0,0 +1,46 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_quill/flutter_quill.dart'; +import 'package:flutter_quill/src/editor_toolbar_shared/color.dart'; +import 'package:flutter_quill/src/toolbar/buttons/color/color_dialog.dart'; +import 'package:flutter_test/flutter_test.dart'; + +import '../../../common/utils/quill_test_app.dart'; + +void main() { + group('$ColorPickerDialog', () { + testWidgets( + 'hexController is initialized correctly after selectedColor when isToggledColor is true', + (tester) async { + for (final isBackground in {true, false}) { + const Color exampleColor = Colors.red; + final colorHex = colorToHex(exampleColor); + + final selectionStyle = const Style().put( + Attribute( + isBackground ? Attribute.background.key : Attribute.color.key, + AttributeScope.inline, + colorHex, + ), + ); + final widget = ColorPickerDialog( + isBackground: isBackground, + onRequestChangeColor: (context, color) {}, + isToggledColor: true, + selectionStyle: selectionStyle, + ); + + await tester.pumpWidget(QuillTestApp.withScaffold(widget)); + + expect(find.widgetWithText(TextFormField, colorHex), findsOneWidget); + + final state = tester.state(find.byType(ColorPickerDialog)) + as ColorPickerDialogState; + + final selectedColor = hexToColor(state.hexController.text); + + expect(state.selectedColor, equals(selectedColor)); + expect(state.hexController.text, equals(colorToHex(selectedColor))); + } + }); + }); +}