Skip to content

Commit 6352118

Browse files
Add a 'quirks mode' when pressing ENTER via IME (Resolves #31) (#36)
1 parent 8be83a2 commit 6352118

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

lib/src/keyboard.dart

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,16 @@ extension KeyboardInput on WidgetTester {
8888

8989
/// Simulates the user pressing ENTER in a widget attached to the IME.
9090
///
91-
/// Instead of key events, this method generates a "\n" insertion followed by a TextInputAction.newline.
91+
/// Instead of key events, this method generates a `\n` insertion followed by a `TextInputAction.newline`.
92+
///
93+
/// {@template newline_quirks_mode}
94+
/// WARNING: On Android Web, and seemingly only Android Web, we've observed that the standard behavior is
95+
/// to send a newline `\n`, but not a `TextInputAction.newline`. Because this behavior is an outlier, we believe
96+
/// it's likely some kind of bug. Rather than always implement this behavior for Android Web, this method
97+
/// has a [useQuirksMode] parameter. When [useQuirksMode] is `true`, no `TextInputAction.newline` is dispatched
98+
/// for Android Web, but when its `false`, a `\n` AND a `TextInputAction.newline` are both dispatched, regardless
99+
/// of platform.
100+
/// {@endtemplate}
92101
///
93102
/// {@template ime_client_getter}
94103
/// The given [finder] must find a [StatefulWidget] whose [State] implements
@@ -102,6 +111,7 @@ extension KeyboardInput on WidgetTester {
102111
GetDeltaTextInputClient? getter,
103112
bool settle = true,
104113
int extraPumps = 0,
114+
bool useQuirksMode = true,
105115
}) async {
106116
if (!testTextInput.hasAnyClients) {
107117
// There isn't any IME connections.
@@ -110,8 +120,15 @@ extension KeyboardInput on WidgetTester {
110120

111121
await ime.typeText('\n', finder: finder, getter: getter);
112122
await pump();
113-
await testTextInput.receiveAction(TextInputAction.newline);
114-
await pump();
123+
124+
// On any platform, except Android Web, we want to dispatch `TextInputAction.newline`
125+
// in addition to the newline `\n`. However, on Android Web, when quirks mode is
126+
// activated, we don't want to send a `TextInputAction.newline` because we've observed
127+
// that in the real world, Android Web doesn't dispatch a `TextInputAction.newline`.
128+
if (_keyEventPlatform != "android" || !kIsWeb || !useQuirksMode) {
129+
await testTextInput.receiveAction(TextInputAction.newline);
130+
await pump();
131+
}
115132

116133
await _maybeSettleOrExtraPumps(settle: settle, extraPumps: extraPumps);
117134
}
@@ -122,12 +139,17 @@ extension KeyboardInput on WidgetTester {
122139
/// then this method simulates pressing the newline action button on a software keyboard, which inserts "/n"
123140
/// into the text, and also sends a NEWLINE action to the IME client.
124141
///
142+
/// Pressing ENTER through the IME has some quirks:
143+
///
144+
/// {@macro newline_quirks_mode}
145+
///
125146
/// {@macro ime_client_getter}
126147
Future<void> pressEnterAdaptive({
127148
Finder? finder,
128149
GetDeltaTextInputClient? getter,
129150
bool settle = true,
130151
int extraPumps = 0,
152+
bool useQuirksMode = true,
131153
}) async {
132154
final handled = await sendKeyEvent(LogicalKeyboardKey.enter, platform: _keyEventPlatform);
133155
if (handled) {
@@ -137,7 +159,13 @@ extension KeyboardInput on WidgetTester {
137159
return;
138160
}
139161

140-
await pressEnterWithIme(finder: finder, getter: getter, settle: settle, extraPumps: extraPumps);
162+
await pressEnterWithIme(
163+
finder: finder,
164+
getter: getter,
165+
settle: settle,
166+
extraPumps: extraPumps,
167+
useQuirksMode: useQuirksMode,
168+
);
141169
}
142170

143171
/// Simulates pressing the SPACE key.

0 commit comments

Comments
 (0)