@@ -15,18 +15,6 @@ export 'package:flutter/services.dart' show TextEditingValue, TextInputAction;
1515///
1616/// Typical app tests will not need to use this class directly.
1717///
18- /// The [TestWidgetsFlutterBinding] class registers a [TestTextInput] instance
19- /// ([TestWidgetsFlutterBinding.testTextInput] ) as a stub keyboard
20- /// implementation if its [TestWidgetsFlutterBinding.registerTestTextInput]
21- /// property returns true when a test starts, and unregisters it when the test
22- /// ends (unless it ends with a failure).
23- ///
24- /// See [register] , [unregister] , and [isRegistered] for details.
25- ///
26- /// The [updateEditingValue] , [enterText] , and [receiveAction] methods can be
27- /// used even when the [TestTextInput] is not registered. All other methods
28- /// will assert if [isRegistered] is false.
29- ///
3018/// See also:
3119///
3220/// * [WidgetTester.enterText] , which uses this class to simulate keyboard input.
@@ -49,43 +37,37 @@ class TestTextInput {
4937 /// The messenger which sends the bytes for this channel, not null.
5038 BinaryMessenger get _binaryMessenger => ServicesBinding .instance! .defaultBinaryMessenger;
5139
52- /// Log for method calls.
53- ///
54- /// For all registered channels, handled calls are added to the list. Can
55- /// be cleaned using `log.clear()` .
56- final List <MethodCall > log = < MethodCall > [];
57-
58- /// Resets any internal state of this object.
40+ /// Resets any internal state of this object and calls [register] .
5941 ///
6042 /// This method is invoked by the testing framework between tests. It should
6143 /// not ordinarily be called by tests directly.
62- void reset () {
44+ void resetAndRegister () {
6345 log.clear ();
6446 editingState = null ;
6547 setClientArgs = null ;
6648 _client = 0 ;
6749 _isVisible = false ;
50+ register ();
6851 }
69-
7052 /// Installs this object as a mock handler for [SystemChannels.textInput] .
71- ///
72- /// Called by the binding at the top of a test when
73- /// [TestWidgetsFlutterBinding.registerTestTextInput] is true.
7453 void register () => SystemChannels .textInput.setMockMethodCallHandler (_handleTextInputCall);
7554
7655 /// Removes this object as a mock handler for [SystemChannels.textInput] .
7756 ///
7857 /// After calling this method, the channel will exchange messages with the
79- /// Flutter engine instead of the stub.
80- ///
81- /// Called by the binding at the end of a (successful) test when
82- /// [TestWidgetsFlutterBinding.registerTestTextInput] is true.
58+ /// Flutter engine. Use this with [FlutterDriver] tests that need to display
59+ /// on-screen keyboard provided by the operating system.
8360 void unregister () => SystemChannels .textInput.setMockMethodCallHandler (null );
8461
62+ /// Log for method calls.
63+ ///
64+ /// For all registered channels, handled calls are added to the list. Can
65+ /// be cleaned using `log.clear()` .
66+ final List <MethodCall > log = < MethodCall > [];
67+
8568 /// Whether this [TestTextInput] is registered with [SystemChannels.textInput] .
8669 ///
87- /// The binding uses the [register] and [unregister] methods to control this
88- /// value when [TestWidgetsFlutterBinding.registerTestTextInput] is true.
70+ /// Use [register] and [unregister] methods to control this value.
8971 bool get isRegistered => SystemChannels .textInput.checkMockMethodCallHandler (_handleTextInputCall);
9072
9173 /// Whether there are any active clients listening to text input.
@@ -96,13 +78,11 @@ class TestTextInput {
9678
9779 int _client = 0 ;
9880
99- /// The last set of arguments supplied to the `TextInput.setClient` and
100- /// `TextInput.updateConfig` methods of this stub implementation.
81+ /// Arguments supplied to the TextInput.setClient method call.
10182 Map <String , dynamic >? setClientArgs;
10283
10384 /// The last set of arguments that [TextInputConnection.setEditingState] sent
104- /// to this stub implementation (i.e. the arguments set to
105- /// `TextInput.setEditingState` ).
85+ /// to the embedder.
10686 ///
10787 /// This is a map representation of a [TextEditingValue] object. For example,
10888 /// it will have a `text` entry whose value matches the most recent
@@ -138,27 +118,15 @@ class TestTextInput {
138118 }
139119
140120 /// Whether the onscreen keyboard is visible to the user.
141- ///
142- /// Specifically, this reflects the last call to `TextInput.show` or
143- /// `TextInput.hide` received by the stub implementation.
144121 bool get isVisible {
145122 assert (isRegistered);
146123 return _isVisible;
147124 }
148125 bool _isVisible = false ;
149126
150- /// Simulates the user hiding the onscreen keyboard.
151- ///
152- /// This does nothing but set the internal flag.
153- void hide () {
154- assert (isRegistered);
155- _isVisible = false ;
156- }
157-
158127 /// Simulates the user changing the [TextEditingValue] to the given value.
159- ///
160- /// This can be called even if the [TestTextInput] has not been [register] ed.
161128 void updateEditingValue (TextEditingValue value) {
129+ assert (isRegistered);
162130 // Not using the `expect` function because in the case of a FlutterDriver
163131 // test this code does not run in a package:test test zone.
164132 if (_client == 0 )
@@ -178,7 +146,6 @@ class TestTextInput {
178146 /// Simulates the user closing the text input connection.
179147 ///
180148 /// For example:
181- ///
182149 /// - User pressed the home button and sent the application to background.
183150 /// - User closed the virtual keyboard.
184151 void closeConnection () {
@@ -200,9 +167,8 @@ class TestTextInput {
200167 }
201168
202169 /// Simulates the user typing the given text.
203- ///
204- /// This can be called even if the [TestTextInput] has not been [register] ed.
205170 void enterText (String text) {
171+ assert (isRegistered);
206172 updateEditingValue (TextEditingValue (
207173 text: text,
208174 ));
@@ -211,9 +177,8 @@ class TestTextInput {
211177 /// Simulates the user pressing one of the [TextInputAction] buttons.
212178 /// Does not check that the [TextInputAction] performed is an acceptable one
213179 /// based on the `inputAction` [setClientArgs] .
214- ///
215- /// This can be called even if the [TestTextInput] has not been [register] ed.
216180 Future <void > receiveAction (TextInputAction action) async {
181+ assert (isRegistered);
217182 return TestAsyncUtils .guard (() {
218183 // Not using the `expect` function because in the case of a FlutterDriver
219184 // test this code does not run in a package:test test zone.
@@ -251,4 +216,10 @@ class TestTextInput {
251216 return completer.future;
252217 });
253218 }
219+
220+ /// Simulates the user hiding the onscreen keyboard.
221+ void hide () {
222+ assert (isRegistered);
223+ _isVisible = false ;
224+ }
254225}
0 commit comments