Skip to content

Commit

Permalink
test: add test for bug fix PR #2415
Browse files Browse the repository at this point in the history
  • Loading branch information
EchoEllet committed Dec 19, 2024
1 parent d745801 commit 505ed70
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
90 changes: 90 additions & 0 deletions test/common/utils/quill_test_app.dart
Original file line number Diff line number Diff line change
@@ -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;
}
46 changes: 46 additions & 0 deletions test/toolbar/buttons/color/color_dialog_test.dart
Original file line number Diff line number Diff line change
@@ -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)));
}
});
});
}

0 comments on commit 505ed70

Please sign in to comment.