From a6ea64457a4457f54ee8669afdeb9de01b2963e6 Mon Sep 17 00:00:00 2001 From: Konstantin Sokolovskyi Date: Tue, 7 Feb 2023 14:45:50 +0100 Subject: [PATCH] Fix cut button creation in 'buttonItemsForToolbarOptions' (#119822) * Fix cut button creation in buttonItemsForToolbarOptions * Remove extra spaces in tests * Replace fails with expects, Add periods in the comments --- .../lib/src/widgets/editable_text.dart | 4 +- .../test/widgets/editable_text_test.dart | 284 ++++++++++++++++++ 2 files changed, 286 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index 4ce4f42314..daa895008d 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -2432,9 +2432,9 @@ class EditableTextState extends State with AutomaticKeepAliveClien if (toolbarOptions.cut && cutEnabled) ContextMenuButtonItem( onPressed: () { - selectAll(SelectionChangedCause.toolbar); + cutSelection(SelectionChangedCause.toolbar); }, - type: ContextMenuButtonType.selectAll, + type: ContextMenuButtonType.cut, ), if (toolbarOptions.copy && copyEnabled) ContextMenuButtonItem( diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index 8ecfd5c578..9e7fe4f109 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart @@ -2157,6 +2157,290 @@ void main() { expect(state.textEditingValue.selection.isCollapsed, isTrue); }); + group('buttonItemsForToolbarOptions', () { + testWidgets('returns null when toolbarOptions are empty', (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: EditableText( + controller: TextEditingController(text: 'TEXT'), + toolbarOptions: ToolbarOptions.empty, + focusNode: focusNode, + style: textStyle, + cursorColor: cursorColor, + backgroundCursorColor: Colors.grey, + ), + ), + ); + + final EditableTextState state = tester.state( + find.byType(EditableText), + ); + + expect(state.buttonItemsForToolbarOptions(), isNull); + }); + + testWidgets('returns empty array when only cut is selected in toolbarOptions but cut is not enabled', (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: EditableText( + controller: TextEditingController(text: 'TEXT'), + toolbarOptions: const ToolbarOptions(cut: true), + readOnly: true, + focusNode: focusNode, + style: textStyle, + cursorColor: cursorColor, + backgroundCursorColor: Colors.grey, + ), + ), + ); + + final EditableTextState state = tester.state( + find.byType(EditableText), + ); + + expect(state.cutEnabled, isFalse); + expect(state.buttonItemsForToolbarOptions(), isEmpty); + }); + + testWidgets('returns only cut button when only cut is selected in toolbarOptions and cut is enabled', (WidgetTester tester) async { + const String text = 'TEXT'; + final TextEditingController controller = TextEditingController(text: text); + + await tester.pumpWidget( + MaterialApp( + home: EditableText( + controller: controller, + toolbarOptions: const ToolbarOptions(cut: true), + focusNode: focusNode, + style: textStyle, + cursorColor: cursorColor, + backgroundCursorColor: Colors.grey, + ), + ), + ); + + final EditableTextState state = tester.state( + find.byType(EditableText), + ); + + // Selecting all. + controller.selection = TextSelection( + baseOffset: 0, + extentOffset: controller.text.length, + ); + expect(state.cutEnabled, isTrue); + + final List? items = state.buttonItemsForToolbarOptions(); + + expect(items, isNotNull); + expect(items, hasLength(1)); + + final ContextMenuButtonItem cutButton = items!.first; + expect(cutButton.type, ContextMenuButtonType.cut); + + cutButton.onPressed(); + await tester.pump(); + + expect(controller.text, isEmpty); + final ClipboardData? data = await Clipboard.getData('text/plain'); + expect(data, isNotNull); + expect(data!.text, equals(text)); + }); + + testWidgets('returns empty array when only copy is selected in toolbarOptions but copy is not enabled', (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: EditableText( + controller: TextEditingController(text: 'TEXT'), + toolbarOptions: const ToolbarOptions(copy: true), + obscureText: true, + focusNode: focusNode, + style: textStyle, + cursorColor: cursorColor, + backgroundCursorColor: Colors.grey, + ), + ), + ); + + final EditableTextState state = tester.state( + find.byType(EditableText), + ); + + expect(state.copyEnabled, isFalse); + expect(state.buttonItemsForToolbarOptions(), isEmpty); + }); + + testWidgets('returns only copy button when only copy is selected in toolbarOptions and copy is enabled', (WidgetTester tester) async { + const String text = 'TEXT'; + final TextEditingController controller = TextEditingController(text: text); + + await tester.pumpWidget( + MaterialApp( + home: EditableText( + controller: controller, + toolbarOptions: const ToolbarOptions(copy: true), + focusNode: focusNode, + style: textStyle, + cursorColor: cursorColor, + backgroundCursorColor: Colors.grey, + ), + ), + ); + + final EditableTextState state = tester.state( + find.byType(EditableText), + ); + + // Selecting all. + controller.selection = TextSelection( + baseOffset: 0, + extentOffset: controller.text.length, + ); + expect(state.copyEnabled, isTrue); + + final List? items = state.buttonItemsForToolbarOptions(); + + expect(items, isNotNull); + expect(items, hasLength(1)); + + final ContextMenuButtonItem copyButton = items!.first; + expect(copyButton.type, ContextMenuButtonType.copy); + + copyButton.onPressed(); + await tester.pump(); + + expect(controller.text, equals(text)); + final ClipboardData? data = await Clipboard.getData('text/plain'); + expect(data, isNotNull); + expect(data!.text, equals(text)); + }); + + testWidgets('returns empty array when only paste is selected in toolbarOptions but paste is not enabled', (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: EditableText( + controller: TextEditingController(text: 'TEXT'), + toolbarOptions: const ToolbarOptions(paste: true), + readOnly: true, + focusNode: focusNode, + style: textStyle, + cursorColor: cursorColor, + backgroundCursorColor: Colors.grey, + ), + ), + ); + + final EditableTextState state = tester.state( + find.byType(EditableText), + ); + + expect(state.pasteEnabled, isFalse); + expect(state.buttonItemsForToolbarOptions(), isEmpty); + }); + + testWidgets('returns only paste button when only paste is selected in toolbarOptions and paste is enabled', (WidgetTester tester) async { + const String text = 'TEXT'; + final TextEditingController controller = TextEditingController(text: text); + + await tester.pumpWidget( + MaterialApp( + home: EditableText( + controller: controller, + toolbarOptions: const ToolbarOptions(paste: true), + focusNode: focusNode, + style: textStyle, + cursorColor: cursorColor, + backgroundCursorColor: Colors.grey, + ), + ), + ); + + final EditableTextState state = tester.state( + find.byType(EditableText), + ); + + // Moving caret to the end. + controller.selection = TextSelection.collapsed(offset: controller.text.length); + expect(state.pasteEnabled, isTrue); + + final List? items = state.buttonItemsForToolbarOptions(); + + expect(items, isNotNull); + expect(items, hasLength(1)); + + final ContextMenuButtonItem pasteButton = items!.first; + expect(pasteButton.type, ContextMenuButtonType.paste); + + // Setting data which will be pasted into the clipboard. + await Clipboard.setData(const ClipboardData(text: text)); + + pasteButton.onPressed(); + await tester.pump(); + + expect(controller.text, equals(text + text)); + }); + + testWidgets('returns empty array when only selectAll is selected in toolbarOptions but selectAll is not enabled', (WidgetTester tester) async { + await tester.pumpWidget( + MaterialApp( + home: EditableText( + controller: TextEditingController(text: 'TEXT'), + toolbarOptions: const ToolbarOptions(selectAll: true), + readOnly: true, + obscureText: true, + focusNode: focusNode, + style: textStyle, + cursorColor: cursorColor, + backgroundCursorColor: Colors.grey, + ), + ), + ); + + final EditableTextState state = tester.state( + find.byType(EditableText), + ); + + expect(state.selectAllEnabled, isFalse); + expect(state.buttonItemsForToolbarOptions(), isEmpty); + }); + + testWidgets('returns only selectAll button when only selectAll is selected in toolbarOptions and selectAll is enabled', (WidgetTester tester) async { + const String text = 'TEXT'; + final TextEditingController controller = TextEditingController(text: text); + + await tester.pumpWidget( + MaterialApp( + home: EditableText( + controller: controller, + toolbarOptions: const ToolbarOptions(selectAll: true), + focusNode: focusNode, + style: textStyle, + cursorColor: cursorColor, + backgroundCursorColor: Colors.grey, + ), + ), + ); + + final EditableTextState state = tester.state( + find.byType(EditableText), + ); + + final List? items = state.buttonItemsForToolbarOptions(); + + expect(items, isNotNull); + expect(items, hasLength(1)); + + final ContextMenuButtonItem selectAllButton = items!.first; + expect(selectAllButton.type, ContextMenuButtonType.selectAll); + + selectAllButton.onPressed(); + await tester.pump(); + + expect(controller.text, equals(text)); + expect(state.textEditingValue.selection.textInside(text), equals(text)); + }); + }); + testWidgets('Handles the read-only flag correctly', (WidgetTester tester) async { final TextEditingController controller = TextEditingController(text: 'Lorem ipsum dolor sit amet');