Add test for text_editing_controller.0.dart API example. (#148872)

This PR contributes to https://github.com/flutter/flutter/issues/130459

### Description
- Adds test for `examples/api/lib/widgets/editable_text/text_editing_controller.0.dart`
This commit is contained in:
Kostia Sokolovskyi 2024-05-24 19:50:00 +02:00 committed by GitHub
parent 6093a9cd2b
commit 6a20a5b19c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 67 additions and 1 deletions

View File

@ -395,7 +395,6 @@ final Set<String> _knownMissingTests = <String>{
'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',

View File

@ -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),
);
});
}