Skip to content

Commit 6515806

Browse files
authored
Revert "Fix a legacy TODO (flutter#77454)" (flutter#78584)
This reverts commit 5e4e530.
1 parent efbde44 commit 6515806

File tree

5 files changed

+60
-105
lines changed

5 files changed

+60
-105
lines changed

dev/integration_tests/web_e2e_tests/test_driver/text_editing_integration.dart

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
// found in the LICENSE file.
44

55
// @dart = 2.9
6-
76
import 'dart:html';
87
import 'dart:js_util' as js_util;
9-
108
import 'package:flutter/gestures.dart';
119
import 'package:flutter/services.dart';
1210
import 'package:flutter_test/flutter_test.dart';
@@ -23,6 +21,9 @@ void main() {
2321
app.main();
2422
await tester.pumpAndSettle();
2523

24+
// TODO(nurhan): https://github.com/flutter/flutter/issues/51885
25+
SystemChannels.textInput.setMockMethodCallHandler(null);
26+
2627
// Focus on a TextFormField.
2728
final Finder finder = find.byKey(const Key('input'));
2829
expect(finder, findsOneWidget);
@@ -48,6 +49,9 @@ void main() {
4849
app.main();
4950
await tester.pumpAndSettle();
5051

52+
// TODO(nurhan): https://github.com/flutter/flutter/issues/51885
53+
SystemChannels.textInput.setMockMethodCallHandler(null);
54+
5155
// Focus on a TextFormField.
5256
final Finder finder = find.byKey(const Key('empty-input'));
5357
expect(finder, findsOneWidget);
@@ -73,6 +77,9 @@ void main() {
7377
app.main();
7478
await tester.pumpAndSettle();
7579

80+
// TODO(nurhan): https://github.com/flutter/flutter/issues/51885
81+
SystemChannels.textInput.setMockMethodCallHandler(null);
82+
7683
// This text will show no-enter initially. It will have 'enter-pressed'
7784
// after `onFieldSubmitted` of TextField is triggered.
7885
final Finder textFinder = find.byKey(const Key('text'));
@@ -106,6 +113,9 @@ void main() {
106113
app.main();
107114
await tester.pumpAndSettle();
108115

116+
// TODO(nurhan): https://github.com/flutter/flutter/issues/51885
117+
SystemChannels.textInput.setMockMethodCallHandler(null);
118+
109119
// Focus on a TextFormField.
110120
final Finder finder = find.byKey(const Key('input'));
111121
expect(finder, findsOneWidget);
@@ -138,6 +148,9 @@ void main() {
138148
app.main();
139149
await tester.pumpAndSettle();
140150

151+
// TODO(nurhan): https://github.com/flutter/flutter/issues/51885
152+
SystemChannels.textInput.setMockMethodCallHandler(null);
153+
141154
// Focus on a TextFormField.
142155
final Finder finder = find.byKey(const Key('input'));
143156
expect(finder, findsOneWidget);
@@ -185,6 +198,9 @@ void main() {
185198
app.main();
186199
await tester.pumpAndSettle();
187200

201+
// TODO(nurhan): https://github.com/flutter/flutter/issues/51885
202+
SystemChannels.textInput.setMockMethodCallHandler(null);
203+
188204
// Select something from the selectable text.
189205
final Finder finder = find.byKey(const Key('selectable'));
190206
expect(finder, findsOneWidget);

packages/flutter_test/lib/src/binding.dart

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,9 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
195195

196196
/// Called by the test framework at the beginning of a widget test to
197197
/// prepare the binding for the next test.
198-
///
199-
/// If [registerTestTextInput] returns true when this method is called,
200-
/// the [testTextInput] is configured to simulate the keyboard.
201198
void reset() {
202199
_restorationManager = null;
203200
resetGestureBinding();
204-
testTextInput.reset();
205-
if (registerTestTextInput)
206-
_testTextInput.register();
207201
}
208202

209203
@override
@@ -243,28 +237,14 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
243237
@protected
244238
bool get overrideHttpClient => true;
245239

246-
/// Determines whether the binding automatically registers [testTextInput] as
247-
/// a fake keyboard implementation.
240+
/// Determines whether the binding automatically registers [testTextInput].
248241
///
249242
/// Unit tests make use of this to mock out text input communication for
250243
/// widgets. An integration test would set this to false, to test real IME
251244
/// or keyboard input.
252245
///
253246
/// [TestTextInput.isRegistered] reports whether the text input mock is
254247
/// registered or not.
255-
///
256-
/// Some of the properties and methods on [testTextInput] are only valid if
257-
/// [registerTestTextInput] returns true when a test starts. If those
258-
/// members are accessed when using a binding that sets this flag to false,
259-
/// they will throw.
260-
///
261-
/// If this property returns true when a test ends, the [testTextInput] is
262-
/// unregistered.
263-
///
264-
/// This property should not change the value it returns during the lifetime
265-
/// of the binding. Changing the value of this property risks very confusing
266-
/// behavior as the [TestTextInput] may be inconsistently registered or
267-
/// unregistered.
268248
@protected
269249
bool get registerTestTextInput => true;
270250

@@ -339,6 +319,9 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
339319
binding.setupHttpOverrides();
340320
}
341321
_testTextInput = TestTextInput(onCleared: _resetFocusedEditable);
322+
if (registerTestTextInput) {
323+
_testTextInput.register();
324+
}
342325
}
343326

344327
@override
@@ -816,8 +799,6 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
816799
// alone so that we don't cause more spurious errors.
817800
runApp(Container(key: UniqueKey(), child: _postTestMessage)); // Unmount any remaining widgets.
818801
await pump();
819-
if (registerTestTextInput)
820-
_testTextInput.unregister();
821802
invariantTester();
822803
_verifyAutoUpdateGoldensUnset(autoUpdateGoldensBeforeTest && !isBrowser);
823804
_verifyReportTestExceptionUnset(reportTestExceptionBeforeTest);

packages/flutter_test/lib/src/test_text_input.dart

Lines changed: 23 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

packages/flutter_test/lib/src/widget_tester.dart

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ void testWidgets(
149149
() async {
150150
binding.reset();
151151
debugResetSemanticsIdCounter();
152+
tester.resetTestTextInput();
152153
Object? memento;
153154
try {
154155
memento = await variant.setUp(value);
@@ -1001,14 +1002,19 @@ class WidgetTester extends WidgetController implements HitTestDispatcher, Ticker
10011002
///
10021003
/// Typical app tests will not need to use this value. To add text to widgets
10031004
/// like [TextField] or [TextFormField], call [enterText].
1004-
///
1005-
/// Some of the properties and methods on this value are only valid if the
1006-
/// binding's [TestWidgetsFlutterBinding.registerTestTextInput] flag is set to
1007-
/// true as a test is starting (meaning that the keyboard is to be simulated
1008-
/// by the test framework). If those members are accessed when using a binding
1009-
/// that sets this flag to false, they will throw.
10101005
TestTextInput get testTextInput => binding.testTextInput;
10111006

1007+
/// Ensures that [testTextInput] is registered and [TestTextInput.log] is
1008+
/// reset.
1009+
///
1010+
/// This is called by the testing framework before test runs, so that if a
1011+
/// previous test has set its own handler on [SystemChannels.textInput], the
1012+
/// [testTextInput] regains control and the log is fresh for the new test.
1013+
/// It should not typically need to be called by tests.
1014+
void resetTestTextInput() {
1015+
testTextInput.resetAndRegister();
1016+
}
1017+
10121018
/// Give the text input widget specified by [finder] the focus, as if the
10131019
/// onscreen keyboard had appeared.
10141020
///

packages/flutter_test/test/bindings_test.dart

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import 'package:flutter_test/flutter_test.dart';
1111
import 'package:test_api/test_api.dart' as test_package;
1212

1313
void main() {
14-
final AutomatedTestWidgetsFlutterBinding binding = AutomatedTestWidgetsFlutterBinding();
15-
1614
group(TestViewConfiguration, () {
1715
test('is initialized with top-level window if one is not provided', () {
1816
// The code below will throw without the default.
@@ -22,32 +20,15 @@ void main() {
2220

2321
group(AutomatedTestWidgetsFlutterBinding, () {
2422
test('allows setting defaultTestTimeout to 5 minutes', () {
23+
final AutomatedTestWidgetsFlutterBinding binding = AutomatedTestWidgetsFlutterBinding();
2524
binding.defaultTestTimeout = const test_package.Timeout(Duration(minutes: 5));
2625
expect(binding.defaultTestTimeout.duration, const Duration(minutes: 5));
2726
});
2827
});
2928

30-
// The next three tests must run in order -- first using `test`, then `testWidgets`, then `test` again.
31-
32-
int order = 0;
33-
3429
test('Initializes httpOverrides and testTextInput', () async {
35-
assert(order == 0);
36-
expect(binding.testTextInput, isNotNull);
37-
expect(binding.testTextInput.isRegistered, isFalse);
30+
final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized() as TestWidgetsFlutterBinding;
31+
expect(binding.testTextInput.isRegistered, true);
3832
expect(HttpOverrides.current, isNotNull);
39-
order += 1;
40-
});
41-
42-
testWidgets('Registers testTextInput', (WidgetTester tester) async {
43-
assert(order == 1);
44-
expect(tester.testTextInput.isRegistered, isTrue);
45-
order += 1;
46-
});
47-
48-
test('Unregisters testTextInput', () async {
49-
assert(order == 2);
50-
expect(binding.testTextInput.isRegistered, isFalse);
51-
order += 1;
5233
});
5334
}

0 commit comments

Comments
 (0)