flutter_flutter/packages/flutter/test/services/text_editing_delta_test.dart
Kate Lovett 9d96df2364
Modernize framework lints (#179089)
WIP

Commits separated as follows:
- Update lints in analysis_options files
- Run `dart fix --apply`
- Clean up leftover analysis issues 
- Run `dart format .` in the right places.

Local analysis and testing passes. Checking CI now.

Part of https://github.com/flutter/flutter/issues/178827
- Adoption of flutter_lints in examples/api coming in a separate change
(cc @loic-sharma)

## Pre-launch Checklist

- [ ] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [ ] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [ ] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [ ] I signed the [CLA].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [ ] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [ ] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

**Note**: The Flutter team is currently trialing the use of [Gemini Code
Assist for
GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code).
Comments from the `gemini-code-assist` bot should not be taken as
authoritative feedback from the Flutter team. If you find its comments
useful you can update your code accordingly, but if you are unsure or
disagree with the feedback, please feel free to wait for a Flutter team
member's review for guidance on which automated comments should be
addressed.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
2025-11-26 01:10:39 +00:00

396 lines
14 KiB
Dart

// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:convert' show jsonDecode;
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('TextEditingDeltaInsertion', () {
test('Verify creation of insertion delta when inserting at a collapsed selection.', () {
const jsonInsertionDelta =
'{'
'"oldText": "",'
' "deltaText": "let there be text",'
' "deltaStart": 0,'
' "deltaEnd": 0,'
' "selectionBase": 17,'
' "selectionExtent": 17,'
' "selectionAffinity" : "TextAffinity.downstream" ,'
' "selectionIsDirectional": false,'
' "composingBase": -1,'
' "composingExtent": -1}';
final delta =
TextEditingDelta.fromJSON(jsonDecode(jsonInsertionDelta) as Map<String, dynamic>)
as TextEditingDeltaInsertion;
const TextRange expectedComposing = TextRange.empty;
const expectedInsertionOffset = 0;
const expectedSelection = TextSelection.collapsed(offset: 17);
expect(delta.oldText, '');
expect(delta.textInserted, 'let there be text');
expect(delta.insertionOffset, expectedInsertionOffset);
expect(delta.selection, expectedSelection);
expect(delta.composing, expectedComposing);
});
test('Verify creation of insertion delta when inserting at end of composing region.', () {
const jsonInsertionDelta =
'{'
'"oldText": "hello worl",'
' "deltaText": "world",'
' "deltaStart": 6,'
' "deltaEnd": 10,'
' "selectionBase": 11,'
' "selectionExtent": 11,'
' "selectionAffinity" : "TextAffinity.downstream",'
' "selectionIsDirectional": false,'
' "composingBase": 6,'
' "composingExtent": 11}';
final delta =
TextEditingDelta.fromJSON(jsonDecode(jsonInsertionDelta) as Map<String, dynamic>)
as TextEditingDeltaInsertion;
const expectedComposing = TextRange(start: 6, end: 11);
const expectedInsertionOffset = 10;
const expectedSelection = TextSelection.collapsed(offset: 11);
expect(delta.oldText, 'hello worl');
expect(delta.textInserted, 'd');
expect(delta.insertionOffset, expectedInsertionOffset);
expect(delta.selection, expectedSelection);
expect(delta.composing, expectedComposing);
});
test('Verify invalid TextEditingDeltaInsertion fails to apply', () {
const delta = TextEditingDeltaInsertion(
oldText: 'hello worl',
textInserted: 'd',
insertionOffset: 11,
selection: TextSelection.collapsed(offset: 11),
composing: TextRange.empty,
);
expect(() {
delta.apply(TextEditingValue.empty);
}, throwsAssertionError);
});
test('Verify TextEditingDeltaInsertion debugFillProperties', () {
final builder = DiagnosticPropertiesBuilder();
const insertionDelta = TextEditingDeltaInsertion(
oldText: 'hello worl',
textInserted: 'd',
insertionOffset: 10,
selection: TextSelection.collapsed(offset: 11),
composing: TextRange.empty,
);
insertionDelta.debugFillProperties(builder);
final List<String> description = builder.properties
.where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info))
.map((DiagnosticsNode node) => node.toString())
.toList();
expect(description, <String>[
'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', () {
test('Verify creation of deletion delta when deleting.', () {
const jsonDeletionDelta =
'{'
'"oldText": "let there be text.",'
' "deltaText": "",'
' "deltaStart": 1,'
' "deltaEnd": 2,'
' "selectionBase": 1,'
' "selectionExtent": 1,'
' "selectionAffinity" : "TextAffinity.downstream" ,'
' "selectionIsDirectional": false,'
' "composingBase": -1,'
' "composingExtent": -1}';
final delta =
TextEditingDelta.fromJSON(jsonDecode(jsonDeletionDelta) as Map<String, dynamic>)
as TextEditingDeltaDeletion;
const TextRange expectedComposing = TextRange.empty;
const expectedDeletedRange = TextRange(start: 1, end: 2);
const expectedSelection = TextSelection.collapsed(offset: 1);
expect(delta.oldText, 'let there be text.');
expect(delta.textDeleted, 'e');
expect(delta.deletedRange, expectedDeletedRange);
expect(delta.selection, expectedSelection);
expect(delta.composing, expectedComposing);
});
test('Verify creation of deletion delta when deleting at end of composing region.', () {
const jsonDeletionDelta =
'{'
'"oldText": "hello world",'
' "deltaText": "worl",'
' "deltaStart": 6,'
' "deltaEnd": 11,'
' "selectionBase": 10,'
' "selectionExtent": 10,'
' "selectionAffinity" : "TextAffinity.downstream",'
' "selectionIsDirectional": false,'
' "composingBase": 6,'
' "composingExtent": 10}';
final delta =
TextEditingDelta.fromJSON(jsonDecode(jsonDeletionDelta) as Map<String, dynamic>)
as TextEditingDeltaDeletion;
const expectedComposing = TextRange(start: 6, end: 10);
const expectedDeletedRange = TextRange(start: 10, end: 11);
const expectedSelection = TextSelection.collapsed(offset: 10);
expect(delta.oldText, 'hello world');
expect(delta.textDeleted, 'd');
expect(delta.deletedRange, expectedDeletedRange);
expect(delta.selection, expectedSelection);
expect(delta.composing, expectedComposing);
});
test('Verify invalid TextEditingDeltaDeletion fails to apply', () {
const delta = TextEditingDeltaDeletion(
oldText: 'hello world',
deletedRange: TextRange(start: 5, end: 12),
selection: TextSelection.collapsed(offset: 5),
composing: TextRange.empty,
);
expect(() {
delta.apply(TextEditingValue.empty);
}, throwsAssertionError);
});
test('Verify TextEditingDeltaDeletion debugFillProperties', () {
final builder = DiagnosticPropertiesBuilder();
const deletionDelta = TextEditingDeltaDeletion(
oldText: 'hello world',
deletedRange: TextRange(start: 6, end: 10),
selection: TextSelection.collapsed(offset: 6),
composing: TextRange.empty,
);
deletionDelta.debugFillProperties(builder);
final List<String> description = builder.properties
.where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info))
.map((DiagnosticsNode node) => node.toString())
.toList();
expect(description, <String>[
'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', () {
test('Verify creation of replacement delta when replacing with longer.', () {
const jsonReplacementDelta =
'{'
'"oldText": "hello worfi",'
' "deltaText": "working",'
' "deltaStart": 6,'
' "deltaEnd": 11,'
' "selectionBase": 13,'
' "selectionExtent": 13,'
' "selectionAffinity" : "TextAffinity.downstream",'
' "selectionIsDirectional": false,'
' "composingBase": 6,'
' "composingExtent": 13}';
final delta =
TextEditingDelta.fromJSON(jsonDecode(jsonReplacementDelta) as Map<String, dynamic>)
as TextEditingDeltaReplacement;
const expectedComposing = TextRange(start: 6, end: 13);
const expectedReplacedRange = TextRange(start: 6, end: 11);
const expectedSelection = TextSelection.collapsed(offset: 13);
expect(delta.oldText, 'hello worfi');
expect(delta.textReplaced, 'worfi');
expect(delta.replacementText, 'working');
expect(delta.replacedRange, expectedReplacedRange);
expect(delta.selection, expectedSelection);
expect(delta.composing, expectedComposing);
});
test('Verify creation of replacement delta when replacing with shorter.', () {
const jsonReplacementDelta =
'{'
'"oldText": "hello world",'
' "deltaText": "h",'
' "deltaStart": 6,'
' "deltaEnd": 11,'
' "selectionBase": 7,'
' "selectionExtent": 7,'
' "selectionAffinity" : "TextAffinity.downstream",'
' "selectionIsDirectional": false,'
' "composingBase": 6,'
' "composingExtent": 7}';
final delta =
TextEditingDelta.fromJSON(jsonDecode(jsonReplacementDelta) as Map<String, dynamic>)
as TextEditingDeltaReplacement;
const expectedComposing = TextRange(start: 6, end: 7);
const expectedReplacedRange = TextRange(start: 6, end: 11);
const expectedSelection = TextSelection.collapsed(offset: 7);
expect(delta.oldText, 'hello world');
expect(delta.textReplaced, 'world');
expect(delta.replacementText, 'h');
expect(delta.replacedRange, expectedReplacedRange);
expect(delta.selection, expectedSelection);
expect(delta.composing, expectedComposing);
});
test('Verify creation of replacement delta when replacing with same.', () {
const jsonReplacementDelta =
'{'
'"oldText": "hello world",'
' "deltaText": "words",'
' "deltaStart": 6,'
' "deltaEnd": 11,'
' "selectionBase": 11,'
' "selectionExtent": 11,'
' "selectionAffinity" : "TextAffinity.downstream",'
' "selectionIsDirectional": false,'
' "composingBase": 6,'
' "composingExtent": 11}';
final delta =
TextEditingDelta.fromJSON(jsonDecode(jsonReplacementDelta) as Map<String, dynamic>)
as TextEditingDeltaReplacement;
const expectedComposing = TextRange(start: 6, end: 11);
const expectedReplacedRange = TextRange(start: 6, end: 11);
const expectedSelection = TextSelection.collapsed(offset: 11);
expect(delta.oldText, 'hello world');
expect(delta.textReplaced, 'world');
expect(delta.replacementText, 'words');
expect(delta.replacedRange, expectedReplacedRange);
expect(delta.selection, expectedSelection);
expect(delta.composing, expectedComposing);
});
test('Verify invalid TextEditingDeltaReplacement fails to apply', () {
const delta = TextEditingDeltaReplacement(
oldText: 'hello worl',
replacementText: 'world',
replacedRange: TextRange(start: 5, end: 11),
selection: TextSelection.collapsed(offset: 11),
composing: TextRange.empty,
);
expect(() {
delta.apply(TextEditingValue.empty);
}, throwsAssertionError);
});
test('Verify TextEditingDeltaReplacement debugFillProperties', () {
final builder = DiagnosticPropertiesBuilder();
const 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<String> description = builder.properties
.where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info))
.map((DiagnosticsNode node) => node.toString())
.toList();
expect(description, <String>[
'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', () {
test('Verify non text update delta created.', () {
const jsonNonTextUpdateDelta =
'{'
'"oldText": "hello world",'
' "deltaText": "",'
' "deltaStart": -1,'
' "deltaEnd": -1,'
' "selectionBase": 10,'
' "selectionExtent": 10,'
' "selectionAffinity" : "TextAffinity.downstream",'
' "selectionIsDirectional": false,'
' "composingBase": 6,'
' "composingExtent": 11}';
final delta =
TextEditingDelta.fromJSON(jsonDecode(jsonNonTextUpdateDelta) as Map<String, dynamic>)
as TextEditingDeltaNonTextUpdate;
const expectedComposing = TextRange(start: 6, end: 11);
const expectedSelection = TextSelection.collapsed(offset: 10);
expect(delta.oldText, 'hello world');
expect(delta.selection, expectedSelection);
expect(delta.composing, expectedComposing);
});
test('Verify invalid TextEditingDeltaNonTextUpdate fails to apply', () {
const delta = TextEditingDeltaNonTextUpdate(
oldText: 'hello world',
selection: TextSelection.collapsed(offset: 12),
composing: TextRange.empty,
);
expect(() {
delta.apply(TextEditingValue.empty);
}, throwsAssertionError);
});
test('Verify TextEditingDeltaNonTextUpdate debugFillProperties', () {
final builder = DiagnosticPropertiesBuilder();
const nonTextUpdateDelta = TextEditingDeltaNonTextUpdate(
oldText: 'hello world',
selection: TextSelection.collapsed(offset: 7),
composing: TextRange(start: 6, end: 7),
);
nonTextUpdateDelta.debugFillProperties(builder);
final List<String> description = builder.properties
.where((DiagnosticsNode node) => !node.isFiltered(DiagnosticLevel.info))
.map((DiagnosticsNode node) => node.toString())
.toList();
expect(description, <String>[
'oldText: hello world',
'selection: TextSelection.collapsed(offset: 7, affinity: TextAffinity.downstream, isDirectional: false)',
'composing: TextRange(start: 6, end: 7)',
]);
});
});
}