Skip to content

Commit 505ed70

Browse files
committed
test: add test for bug fix PR singerdmx#2415
1 parent d745801 commit 505ed70

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

test/common/utils/quill_test_app.dart

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_quill/flutter_quill.dart';
3+
import 'package:flutter_quill/internal.dart';
4+
import 'package:flutter_test/flutter_test.dart';
5+
6+
/// A utility for testing widgets within an application widget configured with
7+
/// the necessary localizations.
8+
///
9+
/// Simplifies test setup, enabling concise test cases such as:
10+
///
11+
/// ```dart
12+
/// testWidgets('Test description', (tester) async {
13+
/// final exampleWidget = ...;
14+
/// await tester.pumpWidget(QuillTestApp.withScaffold(exampleWidget));
15+
/// });
16+
/// ```
17+
///
18+
/// Instead of requiring the verbose setup:
19+
///
20+
/// ```dart
21+
/// testWidgets('Test description', (tester) async {
22+
/// final exampleWidget = ...;
23+
/// await tester.pumpWidget(MaterialApp(
24+
/// localizationsDelegates: FlutterQuillLocalizations.localizationsDelegates,
25+
/// supportedLocales: FlutterQuillLocalizations.supportedLocales,
26+
/// home: Scaffold(body: exampleWidget),
27+
/// ));
28+
/// });
29+
/// ```
30+
class QuillTestApp extends StatelessWidget {
31+
/// Constructs a [QuillTestApp] instance.
32+
///
33+
/// Either [home] or [scaffoldBody] must be provided.
34+
/// Throws an [ArgumentError] if both are provided.
35+
QuillTestApp({
36+
required this.home,
37+
required this.scaffoldBody,
38+
super.key,
39+
}) {
40+
if (home != null && scaffoldBody != null) {
41+
throw ArgumentError('Either the home or scaffoldBody must be null');
42+
}
43+
}
44+
45+
/// Creates a [QuillTestApp] with a [Scaffold] wrapping the given [body] widget.
46+
factory QuillTestApp.withScaffold(Widget body) =>
47+
QuillTestApp(home: null, scaffoldBody: body);
48+
49+
/// Creates a [QuillTestApp] with the specified [home] widget.
50+
factory QuillTestApp.home(Widget home) =>
51+
QuillTestApp(home: home, scaffoldBody: null);
52+
53+
/// The home widget for the application.
54+
///
55+
/// If [home] is not null, [scaffoldBody] must be null.
56+
final Widget? home;
57+
58+
/// The body widget for a [Scaffold] used as the application home.
59+
///
60+
/// If [scaffoldBody] is not null, [home] must be null.
61+
final Widget? scaffoldBody;
62+
63+
@override
64+
Widget build(BuildContext context) {
65+
return MaterialApp(
66+
localizationsDelegates: FlutterQuillLocalizations.localizationsDelegates,
67+
supportedLocales: FlutterQuillLocalizations.supportedLocales,
68+
home: home ??
69+
Scaffold(
70+
body: scaffoldBody,
71+
),
72+
);
73+
}
74+
}
75+
76+
extension LocalizationsExt on WidgetTester {
77+
/// Retrieves the localizations during a test.
78+
///
79+
/// Example usage:
80+
///
81+
/// ```dart
82+
/// testWidgets('Verifies localized text', (tester) async {
83+
/// final localizations = tester.localizationsFromElement(ImageOptionsMenu);
84+
///
85+
/// expect(find.text(localizations.successImageDownloaded), findsOneWidget);
86+
/// });
87+
/// ```
88+
FlutterQuillLocalizations localizationsFromElement(Type type) =>
89+
(element(find.byType(type)) as BuildContext).loc;
90+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:flutter_quill/flutter_quill.dart';
3+
import 'package:flutter_quill/src/editor_toolbar_shared/color.dart';
4+
import 'package:flutter_quill/src/toolbar/buttons/color/color_dialog.dart';
5+
import 'package:flutter_test/flutter_test.dart';
6+
7+
import '../../../common/utils/quill_test_app.dart';
8+
9+
void main() {
10+
group('$ColorPickerDialog', () {
11+
testWidgets(
12+
'hexController is initialized correctly after selectedColor when isToggledColor is true',
13+
(tester) async {
14+
for (final isBackground in {true, false}) {
15+
const Color exampleColor = Colors.red;
16+
final colorHex = colorToHex(exampleColor);
17+
18+
final selectionStyle = const Style().put(
19+
Attribute(
20+
isBackground ? Attribute.background.key : Attribute.color.key,
21+
AttributeScope.inline,
22+
colorHex,
23+
),
24+
);
25+
final widget = ColorPickerDialog(
26+
isBackground: isBackground,
27+
onRequestChangeColor: (context, color) {},
28+
isToggledColor: true,
29+
selectionStyle: selectionStyle,
30+
);
31+
32+
await tester.pumpWidget(QuillTestApp.withScaffold(widget));
33+
34+
expect(find.widgetWithText(TextFormField, colorHex), findsOneWidget);
35+
36+
final state = tester.state(find.byType(ColorPickerDialog))
37+
as ColorPickerDialogState;
38+
39+
final selectedColor = hexToColor(state.hexController.text);
40+
41+
expect(state.selectedColor, equals(selectedColor));
42+
expect(state.hexController.text, equals(colorToHex(selectedColor)));
43+
}
44+
});
45+
});
46+
}

0 commit comments

Comments
 (0)