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
This commit is contained in:
committed by
GitHub
parent
16441f4bf7
commit
a6ea64457a
@@ -2432,9 +2432,9 @@ class EditableTextState extends State<EditableText> 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(
|
||||
|
||||
@@ -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<EditableTextState>(
|
||||
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<EditableTextState>(
|
||||
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<EditableTextState>(
|
||||
find.byType(EditableText),
|
||||
);
|
||||
|
||||
// Selecting all.
|
||||
controller.selection = TextSelection(
|
||||
baseOffset: 0,
|
||||
extentOffset: controller.text.length,
|
||||
);
|
||||
expect(state.cutEnabled, isTrue);
|
||||
|
||||
final List<ContextMenuButtonItem>? 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<EditableTextState>(
|
||||
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<EditableTextState>(
|
||||
find.byType(EditableText),
|
||||
);
|
||||
|
||||
// Selecting all.
|
||||
controller.selection = TextSelection(
|
||||
baseOffset: 0,
|
||||
extentOffset: controller.text.length,
|
||||
);
|
||||
expect(state.copyEnabled, isTrue);
|
||||
|
||||
final List<ContextMenuButtonItem>? 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<EditableTextState>(
|
||||
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<EditableTextState>(
|
||||
find.byType(EditableText),
|
||||
);
|
||||
|
||||
// Moving caret to the end.
|
||||
controller.selection = TextSelection.collapsed(offset: controller.text.length);
|
||||
expect(state.pasteEnabled, isTrue);
|
||||
|
||||
final List<ContextMenuButtonItem>? 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<EditableTextState>(
|
||||
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<EditableTextState>(
|
||||
find.byType(EditableText),
|
||||
);
|
||||
|
||||
final List<ContextMenuButtonItem>? 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');
|
||||
|
||||
Reference in New Issue
Block a user