-
Notifications
You must be signed in to change notification settings - Fork 845
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
}); | ||
}); | ||
} |