|
| 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 | +} |
0 commit comments