From 1ab3cb9ee3e138612a7a51212f52b6301709ad79 Mon Sep 17 00:00:00 2001 From: Callum Moffat Date: Wed, 8 Mar 2023 14:42:04 -0500 Subject: [PATCH] Clear _scribbleCacheKey when connection closes (#122145) Clear _scribbleCacheKey when connection closes --- .../lib/src/widgets/editable_text.dart | 1 + .../test/widgets/editable_text_test.dart | 81 +++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/packages/flutter/lib/src/widgets/editable_text.dart b/packages/flutter/lib/src/widgets/editable_text.dart index ebe4680709..32366462fd 100644 --- a/packages/flutter/lib/src/widgets/editable_text.dart +++ b/packages/flutter/lib/src/widgets/editable_text.dart @@ -3165,6 +3165,7 @@ class EditableTextState extends State with AutomaticKeepAliveClien _textInputConnection!.close(); _textInputConnection = null; _lastKnownRemoteTextEditingValue = null; + _scribbleCacheKey = null; removeTextPlaceholder(); } } diff --git a/packages/flutter/test/widgets/editable_text_test.dart b/packages/flutter/test/widgets/editable_text_test.dart index bc7e732edf..1bf00f6fa0 100644 --- a/packages/flutter/test/widgets/editable_text_test.dart +++ b/packages/flutter/test/widgets/editable_text_test.dart @@ -852,6 +852,87 @@ void main() { expect(focusNode.hasFocus, isFalse); }); + testWidgets('selection rects re-sent when refocused', (WidgetTester tester) async { + final List> log = >[]; + SystemChannels.textInput.setMockMethodCallHandler((MethodCall methodCall) async { + if (methodCall.method == 'TextInput.setSelectionRects') { + final List args = methodCall.arguments as List; + final List selectionRects = []; + for (final dynamic rect in args) { + selectionRects.add(SelectionRect( + position: (rect as List)[4] as int, + bounds: Rect.fromLTWH(rect[0] as double, rect[1] as double, rect[2] as double, rect[3] as double), + )); + } + log.add(selectionRects); + } + }); + + final TextEditingController controller = TextEditingController(); + final ScrollController scrollController = ScrollController(); + controller.text = 'Text1'; + + Future pumpEditableText({ double? width, double? height, TextAlign textAlign = TextAlign.start }) async { + await tester.pumpWidget( + MediaQuery( + data: const MediaQueryData(), + child: Directionality( + textDirection: TextDirection.ltr, + child: Center( + child: SizedBox( + width: width, + height: height, + child: EditableText( + controller: controller, + textAlign: textAlign, + scrollController: scrollController, + maxLines: null, + focusNode: focusNode, + cursorWidth: 0, + style: Typography.material2018().black.titleMedium!, + cursorColor: Colors.blue, + backgroundCursorColor: Colors.grey, + ), + ), + ), + ), + ), + ); + } + + const List expectedRects = [ + SelectionRect(position: 0, bounds: Rect.fromLTRB(0.0, 0.0, 14.0, 14.0)), + SelectionRect(position: 1, bounds: Rect.fromLTRB(14.0, 0.0, 28.0, 14.0)), + SelectionRect(position: 2, bounds: Rect.fromLTRB(28.0, 0.0, 42.0, 14.0)), + SelectionRect(position: 3, bounds: Rect.fromLTRB(42.0, 0.0, 56.0, 14.0)), + SelectionRect(position: 4, bounds: Rect.fromLTRB(56.0, 0.0, 70.0, 14.0)) + ]; + + await pumpEditableText(); + expect(log, isEmpty); + + await tester.showKeyboard(find.byType(EditableText)); + // First update. + expect(log.single, expectedRects); + log.clear(); + + await tester.pumpAndSettle(); + expect(log, isEmpty); + + focusNode.unfocus(); + await tester.pumpAndSettle(); + expect(log, isEmpty); + + focusNode.requestFocus(); + //await tester.showKeyboard(find.byType(EditableText)); + await tester.pumpAndSettle(); + // Should re-receive the same rects. + expect(log.single, expectedRects); + log.clear(); + + // On web, we should rely on the browser's implementation of Scribble, so we will not send selection rects. + }, skip: kIsWeb, variant: const TargetPlatformVariant({ TargetPlatform.iOS })); // [intended] + testWidgets('EditableText does not derive selection color from DefaultSelectionStyle', (WidgetTester tester) async { // Regression test for https://github.com/flutter/flutter/issues/103341. const TextEditingValue value = TextEditingValue(