From eb4388e3efb8c330d1666d5fcd6f305f64af710b Mon Sep 17 00:00:00 2001 From: kfdykme Date: Sun, 11 Aug 2024 17:38:16 +0800 Subject: [PATCH] feature: enanble custom TextSelectionControls - add test --- ...ditor_text_selection_controllers_test.dart | 89 +++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 packages/fleather/test/widgets/editor_text_selection_controllers_test.dart diff --git a/packages/fleather/test/widgets/editor_text_selection_controllers_test.dart b/packages/fleather/test/widgets/editor_text_selection_controllers_test.dart new file mode 100644 index 00000000..c7d85d91 --- /dev/null +++ b/packages/fleather/test/widgets/editor_text_selection_controllers_test.dart @@ -0,0 +1,89 @@ +import 'dart:math' as math; + +import 'package:fleather/fleather.dart'; +import 'package:fleather/src/widgets/editor_input_client_mixin.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/rendering.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter_test/flutter_test.dart'; +import '../testing.dart'; + +class MyTextSelectionHandle extends StatefulWidget { + final Size size; + const MyTextSelectionHandle( + {super.key, required this.size}); + + @override + State createState() { + return MyTextSelectionHandleState(); + } +} + +class MyTextSelectionHandleState + extends State { + @override + Widget build(BuildContext context) { + return Container( + width: widget.size.width, + height: widget.size.height, + color: Colors.red, + ); + } +} + +class MyTextSelectionControllers + extends MaterialTextSelectionControls { + final Size size; + MyTextSelectionControllers(this.size); + + @override + Widget buildHandle(BuildContext context, + TextSelectionHandleType type, double textHeight, + [VoidCallback? onTap]) { + final Widget handle = MyTextSelectionHandle( + size: size, + ); + + return switch (type) { + TextSelectionHandleType.left => Transform.rotate( + angle: math.pi / 2.0, + child: handle), // points up-right + TextSelectionHandleType.right => + handle, // points up-left + TextSelectionHandleType.collapsed => Transform.rotate( + angle: math.pi / 4.0, child: handle), // points up + }; + } +} + +void main() { + group('CustomTextSelectionControllers', () { + + testWidgets('set customTextSelectionControllers', + (tester) async { + final document = ParchmentDocument.fromJson([ + {'insert': 'some text\n'} + ]); + FleatherController controller = + FleatherController(document: document); + FocusNode focusNode = FocusNode(); + final Size testSize = Size(230, 5); + final editor = MaterialApp( + home: FleatherEditor( + controller: controller, + focusNode: focusNode, + textSelectionControls: + MyTextSelectionControllers(testSize)), + ); + await tester.pumpWidget(editor); + await tester.tap(find.byType(RawEditor).first); + await tester.pumpAndSettle(); + expect(focusNode.hasFocus, isTrue); + tester.binding.scheduleWarmUpFrame(); + final handleState = + tester.state(find.byType(MyTextSelectionHandle)) + as MyTextSelectionHandleState; + expect(handleState.context.size, testSize); + }); + }); +}