enterText to move the caret to the end (#79506)
This commit is contained in:
@@ -166,10 +166,14 @@ class TestTextInput {
|
||||
}
|
||||
|
||||
/// Simulates the user typing the given text.
|
||||
///
|
||||
/// Calling this method replaces the content of the connected input field with
|
||||
/// `text`, and places the caret at the end of the text.
|
||||
void enterText(String text) {
|
||||
assert(isRegistered);
|
||||
updateEditingValue(TextEditingValue(
|
||||
text: text,
|
||||
selection: TextSelection.collapsed(offset: text.length),
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
@@ -1040,13 +1040,16 @@ class WidgetTester extends WidgetController implements HitTestDispatcher, Ticker
|
||||
});
|
||||
}
|
||||
|
||||
/// Give the text input widget specified by [finder] the focus and
|
||||
/// enter [text] as if it been provided by the onscreen keyboard.
|
||||
/// Give the text input widget specified by [finder] the focus and replace its
|
||||
/// content with [text], as if it had been provided by the onscreen keyboard.
|
||||
///
|
||||
/// The widget specified by [finder] must be an [EditableText] or have
|
||||
/// an [EditableText] descendant. For example `find.byType(TextField)`
|
||||
/// or `find.byType(TextFormField)`, or `find.byType(EditableText)`.
|
||||
///
|
||||
/// When the returned future completes, the text input widget's text will be
|
||||
/// exactly `text`, and the caret will be placed at the end of `text`.
|
||||
///
|
||||
/// To just give [finder] the focus without entering any text,
|
||||
/// see [showKeyboard].
|
||||
Future<void> enterText(Finder finder, String text) async {
|
||||
|
||||
@@ -7,6 +7,24 @@ import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('enterText works', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
const MaterialApp(
|
||||
home: Material(
|
||||
child: TextField(),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
final EditableTextState state = tester.state(find.byType(EditableText));
|
||||
expect(state.textEditingValue.text, '');
|
||||
|
||||
await tester.enterText(find.byType(EditableText), 'let there be text');
|
||||
expect(state.textEditingValue.text, 'let there be text');
|
||||
expect(state.textEditingValue.selection.isCollapsed, isTrue);
|
||||
expect(state.textEditingValue.selection.baseOffset, 17);
|
||||
});
|
||||
|
||||
testWidgets('receiveAction() forwards exception when exception occurs during action processing', (WidgetTester tester) async {
|
||||
// Setup a widget that can receive focus so that we can open the keyboard.
|
||||
const Widget widget = MaterialApp(
|
||||
|
||||
Reference in New Issue
Block a user