Skip to content

Commit 6784135

Browse files
authored
SelectionOverlay and TextSelectionOverlay should dispatch creation and disposal events. (#138804)
1 parent b619b70 commit 6784135

2 files changed

Lines changed: 108 additions & 1 deletion

File tree

‎packages/flutter/lib/src/widgets/text_selection.dart‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,15 @@ class TextSelectionOverlay {
332332
required TextMagnifierConfiguration magnifierConfiguration,
333333
}) : _handlesVisible = handlesVisible,
334334
_value = value {
335+
// TODO(polina-c): stop duplicating code across disposables
336+
// https://github.com/flutter/flutter/issues/137435
337+
if (kFlutterMemoryAllocationsEnabled) {
338+
MemoryAllocations.instance.dispatchObjectCreated(
339+
library: 'package:flutter/widgets.dart',
340+
className: '$TextSelectionOverlay',
341+
object: this,
342+
);
343+
}
335344
renderObject.selectionStartInViewport.addListener(_updateTextSelectionOverlayVisibilities);
336345
renderObject.selectionEndInViewport.addListener(_updateTextSelectionOverlayVisibilities);
337346
_updateTextSelectionOverlayVisibilities();
@@ -585,6 +594,11 @@ class TextSelectionOverlay {
585594

586595
/// {@macro flutter.widgets.SelectionOverlay.dispose}
587596
void dispose() {
597+
// TODO(polina-c): stop duplicating code across disposables
598+
// https://github.com/flutter/flutter/issues/137435
599+
if (kFlutterMemoryAllocationsEnabled) {
600+
MemoryAllocations.instance.dispatchObjectDisposed(object: this);
601+
}
588602
_selectionOverlay.dispose();
589603
renderObject.selectionStartInViewport.removeListener(_updateTextSelectionOverlayVisibilities);
590604
renderObject.selectionEndInViewport.removeListener(_updateTextSelectionOverlayVisibilities);
@@ -956,7 +970,17 @@ class SelectionOverlay {
956970
_lineHeightAtEnd = lineHeightAtEnd,
957971
_selectionEndpoints = selectionEndpoints,
958972
_toolbarLocation = toolbarLocation,
959-
assert(debugCheckHasOverlay(context));
973+
assert(debugCheckHasOverlay(context)) {
974+
// TODO(polina-c): stop duplicating code across disposables
975+
// https://github.com/flutter/flutter/issues/137435
976+
if (kFlutterMemoryAllocationsEnabled) {
977+
MemoryAllocations.instance.dispatchObjectCreated(
978+
library: 'package:flutter/widgets.dart',
979+
className: '$SelectionOverlay',
980+
object: this,
981+
);
982+
}
983+
}
960984

961985
/// {@macro flutter.widgets.SelectionOverlay.context}
962986
final BuildContext context;
@@ -1506,6 +1530,11 @@ class SelectionOverlay {
15061530
/// Disposes this object and release resources.
15071531
/// {@endtemplate}
15081532
void dispose() {
1533+
// TODO(polina-c): stop duplicating code across disposables
1534+
// https://github.com/flutter/flutter/issues/137435
1535+
if (kFlutterMemoryAllocationsEnabled) {
1536+
MemoryAllocations.instance.dispatchObjectDisposed(object: this);
1537+
}
15091538
hide();
15101539
_magnifierInfo.dispose();
15111540
}

‎packages/flutter/test/widgets/text_selection_test.dart‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,19 @@ void main() {
12421242
);
12431243
}
12441244

1245+
testWidgetsWithLeakTracking('dispatches memory events', (WidgetTester tester) async {
1246+
await expectLater(
1247+
await memoryEvents(
1248+
() async {
1249+
final SelectionOverlay overlay = await pumpApp(tester);
1250+
overlay.dispose();
1251+
},
1252+
SelectionOverlay,
1253+
),
1254+
areCreateAndDispose,
1255+
);
1256+
});
1257+
12451258
testWidgetsWithLeakTracking('can show and hide handles', (WidgetTester tester) async {
12461259
final TextSelectionControlsSpy spy = TextSelectionControlsSpy();
12471260
final SelectionOverlay selectionOverlay = await pumpApp(
@@ -1703,6 +1716,71 @@ void main() {
17031716
expect(controller.selection.extentOffset, controller.text.length);
17041717
expect(scrollController.position.pixels, scrollController.position.maxScrollExtent);
17051718
});
1719+
1720+
group('TextSelectionOverlay', () {
1721+
Future<TextSelectionOverlay> pumpApp(WidgetTester tester) async {
1722+
final UniqueKey column = UniqueKey();
1723+
final LayerLink startHandleLayerLink = LayerLink();
1724+
final LayerLink endHandleLayerLink = LayerLink();
1725+
final LayerLink toolbarLayerLink = LayerLink();
1726+
1727+
final UniqueKey editableText = UniqueKey();
1728+
final TextEditingController controller = TextEditingController();
1729+
addTearDown(controller.dispose);
1730+
final FocusNode focusNode = FocusNode();
1731+
addTearDown(focusNode.dispose);
1732+
1733+
await tester.pumpWidget(MaterialApp(
1734+
home: Column(
1735+
key: column,
1736+
children: <Widget>[
1737+
FakeEditableText(
1738+
key: editableText,
1739+
controller: controller,
1740+
focusNode: focusNode,
1741+
),
1742+
CompositedTransformTarget(
1743+
link: startHandleLayerLink,
1744+
child: const Text('start handle'),
1745+
),
1746+
CompositedTransformTarget(
1747+
link: endHandleLayerLink,
1748+
child: const Text('end handle'),
1749+
),
1750+
CompositedTransformTarget(
1751+
link: toolbarLayerLink,
1752+
child: const Text('toolbar'),
1753+
),
1754+
],
1755+
),
1756+
));
1757+
1758+
return TextSelectionOverlay(
1759+
value: TextEditingValue.empty,
1760+
renderObject: tester.state<EditableTextState>(find.byKey(editableText)).renderEditable,
1761+
context: tester.element(find.byKey(column)),
1762+
onSelectionHandleTapped: () {},
1763+
startHandleLayerLink: startHandleLayerLink,
1764+
endHandleLayerLink: endHandleLayerLink,
1765+
selectionDelegate: FakeTextSelectionDelegate(),
1766+
toolbarLayerLink: toolbarLayerLink,
1767+
magnifierConfiguration: TextMagnifierConfiguration.disabled,
1768+
);
1769+
}
1770+
1771+
testWidgetsWithLeakTracking('dispatches memory events', (WidgetTester tester) async {
1772+
await expectLater(
1773+
await memoryEvents(
1774+
() async {
1775+
final TextSelectionOverlay overlay = await pumpApp(tester);
1776+
overlay.dispose();
1777+
},
1778+
TextSelectionOverlay,
1779+
),
1780+
areCreateAndDispose,
1781+
);
1782+
});
1783+
});
17061784
}
17071785

17081786
class FakeTextSelectionGestureDetectorBuilderDelegate implements TextSelectionGestureDetectorBuilderDelegate {

0 commit comments

Comments
 (0)