diff --git a/packages/flutter/lib/src/services/text_editing_delta.dart b/packages/flutter/lib/src/services/text_editing_delta.dart index f4415bb335..090de35e1b 100644 --- a/packages/flutter/lib/src/services/text_editing_delta.dart +++ b/packages/flutter/lib/src/services/text_editing_delta.dart @@ -54,7 +54,7 @@ bool _debugTextRangeIsValid(TextRange range, String text) { /// * [TextInputConfiguration], to opt-in your [DeltaTextInputClient] to receive /// [TextEditingDelta]'s you must set [TextInputConfiguration.enableDeltaModel] /// to true. -abstract class TextEditingDelta { +abstract class TextEditingDelta with Diagnosticable { /// Creates a delta for a given change to the editing state. /// /// {@template flutter.services.TextEditingDelta} @@ -288,6 +288,16 @@ class TextEditingDeltaInsertion extends TextEditingDelta { assert(_debugTextRangeIsValid(composing, newText), 'Applying TextEditingDeltaInsertion failed, the composing range: $composing is not within the bounds of $newText of length: ${newText.length}'); return value.copyWith(text: newText, selection: selection, composing: composing); } + + @override + void debugFillProperties(DiagnosticPropertiesBuilder properties) { + super.debugFillProperties(properties); + properties.add(DiagnosticsProperty('oldText', oldText)); + properties.add(DiagnosticsProperty('textInserted', textInserted)); + properties.add(DiagnosticsProperty('insertionOffset', insertionOffset)); + properties.add(DiagnosticsProperty('selection', selection)); + properties.add(DiagnosticsProperty('composing', composing)); + } } /// A structure representing the deletion of a single/or contiguous sequence of @@ -324,6 +334,16 @@ class TextEditingDeltaDeletion extends TextEditingDelta { assert(_debugTextRangeIsValid(composing, newText), 'Applying TextEditingDeltaDeletion failed, the composing range: $composing is not within the bounds of $newText of length: ${newText.length}'); return value.copyWith(text: newText, selection: selection, composing: composing); } + + @override + void debugFillProperties(DiagnosticPropertiesBuilder properties) { + super.debugFillProperties(properties); + properties.add(DiagnosticsProperty('oldText', oldText)); + properties.add(DiagnosticsProperty('textDeleted', textDeleted)); + properties.add(DiagnosticsProperty('deletedRange', deletedRange)); + properties.add(DiagnosticsProperty('selection', selection)); + properties.add(DiagnosticsProperty('composing', composing)); + } } /// A structure representing a replacement of a range of characters with a @@ -370,6 +390,17 @@ class TextEditingDeltaReplacement extends TextEditingDelta { assert(_debugTextRangeIsValid(composing, newText), 'Applying TextEditingDeltaReplacement failed, the composing range: $composing is not within the bounds of $newText of length: ${newText.length}'); return value.copyWith(text: newText, selection: selection, composing: composing); } + + @override + void debugFillProperties(DiagnosticPropertiesBuilder properties) { + super.debugFillProperties(properties); + properties.add(DiagnosticsProperty('oldText', oldText)); + properties.add(DiagnosticsProperty('textReplaced', textReplaced)); + properties.add(DiagnosticsProperty('replacementText', replacementText)); + properties.add(DiagnosticsProperty('replacedRange', replacedRange)); + properties.add(DiagnosticsProperty('selection', selection)); + properties.add(DiagnosticsProperty('composing', composing)); + } } /// A structure representing changes to the selection and/or composing regions @@ -402,4 +433,12 @@ class TextEditingDeltaNonTextUpdate extends TextEditingDelta { assert(_debugTextRangeIsValid(composing, oldText), 'Applying TextEditingDeltaNonTextUpdate failed, the composing region: $composing is not within the bounds of $oldText of length: ${oldText.length}'); return TextEditingValue(text: oldText, selection: selection, composing: composing); } + + @override + void debugFillProperties(DiagnosticPropertiesBuilder properties) { + super.debugFillProperties(properties); + properties.add(DiagnosticsProperty('oldText', oldText)); + properties.add(DiagnosticsProperty('selection', selection)); + properties.add(DiagnosticsProperty('composing', composing)); + } } diff --git a/packages/flutter/test/services/text_editing_delta_test.dart b/packages/flutter/test/services/text_editing_delta_test.dart index 39f8056bbd..0877590311 100644 --- a/packages/flutter/test/services/text_editing_delta_test.dart +++ b/packages/flutter/test/services/text_editing_delta_test.dart @@ -4,6 +4,7 @@ import 'dart:convert' show jsonDecode; +import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -70,6 +71,34 @@ void main() { expect(() { delta.apply(TextEditingValue.empty); }, throwsAssertionError); }); + + test('Verify TextEditingDeltaInsertion debugFillProperties', () { + final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder(); + const TextEditingDeltaInsertion insertionDelta = TextEditingDeltaInsertion( + oldText: 'hello worl', + textInserted: 'd', + insertionOffset: 10, + selection: TextSelection.collapsed(offset: 11), + composing: TextRange.empty, + ); + + insertionDelta.debugFillProperties(builder); + + final List description = builder.properties + .where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info)) + .map((DiagnosticsNode node) => node.toString()).toList(); + + expect( + description, + [ + 'oldText: hello worl', + 'textInserted: d', + 'insertionOffset: 10', + 'selection: TextSelection.collapsed(offset: 11, affinity: TextAffinity.downstream, isDirectional: false)', + 'composing: TextRange(start: -1, end: -1)', + ], + ); + }); }); group('TextEditingDeltaDeletion', () { @@ -134,6 +163,33 @@ void main() { expect(() { delta.apply(TextEditingValue.empty); }, throwsAssertionError); }); + + test('Verify TextEditingDeltaDeletion debugFillProperties', () { + final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder(); + const TextEditingDeltaDeletion deletionDelta = TextEditingDeltaDeletion( + oldText: 'hello world', + deletedRange: TextRange(start: 6, end: 10), + selection: TextSelection.collapsed(offset: 6), + composing: TextRange.empty, + ); + + deletionDelta.debugFillProperties(builder); + + final List description = builder.properties + .where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info)) + .map((DiagnosticsNode node) => node.toString()).toList(); + + expect( + description, + [ + 'oldText: hello world', + 'textDeleted: worl', + 'deletedRange: TextRange(start: 6, end: 10)', + 'selection: TextSelection.collapsed(offset: 6, affinity: TextAffinity.downstream, isDirectional: false)', + 'composing: TextRange(start: -1, end: -1)', + ], + ); + }); }); group('TextEditingDeltaReplacement', () { @@ -227,6 +283,35 @@ void main() { expect(() { delta.apply(TextEditingValue.empty); }, throwsAssertionError); }); + + test('Verify TextEditingDeltaReplacement debugFillProperties', () { + final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder(); + const TextEditingDeltaReplacement replacementDelta = TextEditingDeltaReplacement( + oldText: 'hello world', + replacementText: 'h', + replacedRange: TextRange(start: 6, end: 11), + selection: TextSelection.collapsed(offset: 7), + composing: TextRange(start: 6, end: 7), + ); + + replacementDelta.debugFillProperties(builder); + + final List description = builder.properties + .where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info)) + .map((DiagnosticsNode node) => node.toString()).toList(); + + expect( + description, + [ + 'oldText: hello world', + 'textReplaced: world', + 'replacementText: h', + 'replacedRange: TextRange(start: 6, end: 11)', + 'selection: TextSelection.collapsed(offset: 7, affinity: TextAffinity.downstream, isDirectional: false)', + 'composing: TextRange(start: 6, end: 7)', + ], + ); + }); }); group('TextEditingDeltaNonTextUpdate', () { @@ -262,5 +347,29 @@ void main() { expect(() { delta.apply(TextEditingValue.empty); }, throwsAssertionError); }); + + test('Verify TextEditingDeltaNonTextUpdate debugFillProperties', () { + final DiagnosticPropertiesBuilder builder = DiagnosticPropertiesBuilder(); + const TextEditingDeltaNonTextUpdate nonTextUpdateDelta = TextEditingDeltaNonTextUpdate( + oldText: 'hello world', + selection: TextSelection.collapsed(offset: 7), + composing: TextRange(start: 6, end: 7), + ); + + nonTextUpdateDelta.debugFillProperties(builder); + + final List description = builder.properties + .where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info)) + .map((DiagnosticsNode node) => node.toString()).toList(); + + expect( + description, + [ + 'oldText: hello world', + 'selection: TextSelection.collapsed(offset: 7, affinity: TextAffinity.downstream, isDirectional: false)', + 'composing: TextRange(start: 6, end: 7)', + ], + ); + }); }); }