diff --git a/dev/bots/check_code_samples.dart b/dev/bots/check_code_samples.dart index 0ea3724fcc..8a01459fd7 100644 --- a/dev/bots/check_code_samples.dart +++ b/dev/bots/check_code_samples.dart @@ -395,7 +395,6 @@ final Set _knownMissingTests = { 'examples/api/test/widgets/interactive_viewer/interactive_viewer.transformation_controller.0_test.dart', 'examples/api/test/widgets/interactive_viewer/interactive_viewer.0_test.dart', 'examples/api/test/widgets/notification_listener/notification.0_test.dart', - 'examples/api/test/widgets/editable_text/text_editing_controller.0_test.dart', 'examples/api/test/widgets/editable_text/editable_text.on_changed.0_test.dart', 'examples/api/test/widgets/overscroll_indicator/glowing_overscroll_indicator.1_test.dart', 'examples/api/test/widgets/overscroll_indicator/glowing_overscroll_indicator.0_test.dart', diff --git a/examples/api/test/widgets/editable_text/text_editing_controller.0_test.dart b/examples/api/test/widgets/editable_text/text_editing_controller.0_test.dart new file mode 100644 index 0000000000..11737ed024 --- /dev/null +++ b/examples/api/test/widgets/editable_text/text_editing_controller.0_test.dart @@ -0,0 +1,67 @@ +// 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 'package:flutter/material.dart'; +import 'package:flutter_api_samples/widgets/editable_text/text_editing_controller.0.dart' + as example; +import 'package:flutter_test/flutter_test.dart'; + +void main() { + testWidgets('Forces text to be lower case', (WidgetTester tester) async { + await tester.pumpWidget( + const example.TextEditingControllerExampleApp(), + ); + + const String input = 'Almost Everything Is a WIDGET! 💙'; + + await tester.enterText(find.byType(TextFormField), input); + await tester.pump(); + + final TextFormField textField = tester.widget(find.byType(TextFormField)); + final TextEditingController controller = textField.controller!; + + expect(find.text(input.toLowerCase()), findsOneWidget); + expect(controller.text, input.toLowerCase()); + }); + + testWidgets('Keeps the caret at the end of the input', (WidgetTester tester) async { + await tester.pumpWidget( + const example.TextEditingControllerExampleApp(), + ); + + const String input = 'flutter'; + + await tester.enterText(find.byType(TextFormField), input); + await tester.pump(); + + expect(find.text(input), findsOneWidget); + + final TextFormField textField = tester.widget(find.byType(TextFormField)); + final TextEditingController controller = textField.controller!; + + // Verify that the caret positioned at the end of the input. + expect( + controller.selection, + const TextSelection.collapsed(offset: input.length), + ); + + final RenderBox box = tester.renderObject(find.byType(TextFormField)); + + // Calculate the center-left point of the field. + final Offset centerLeftPoint = box.localToGlobal( + Offset(0, box.size.height / 2), + ); + + // Tap on the center-left point of the field to try to change the caret + // position. + await tester.tapAt(centerLeftPoint); + await tester.pump(); + + // Verify that the caret position remains unchanged. + expect( + controller.selection, + const TextSelection.collapsed(offset: input.length), + ); + }); +}