Fix to Linux_pixel_7pro integration_ui_keyboard_resize test flakiness (#162308)

## `integration_ui_keyboard_resize` flakiness reduction

This PR addresses flakiness in the `integration_ui_keyboard_resize` test
(issue #161300) and slightly improves its performance.

**Issue summary:**

The `integration_ui_keyboard_resize` test occasionally fails because the
device keyboard does not reliably open at the start of the test. This PR
mitigates one cause of this flakiness, though other potential underlying
issues may still exist. See #161300 for a detailed investigation.

**Changes and rationale:**

1.  **Improved keyboard detection logic (flakiness reduction):**
* The core change moves `await driver.tap(defaultTextField);` *inside*
the loop that checks for layout changes. This ensures the test
repeatedly attempts to open the keyboard *while* waiting for the
expected layout shift, significantly improving reliability.
    *   **Local testing results (MacBook Pro M1 + Pixel 8 Pro):**
* **Original implementation:** Consistent failures within the first 15
iterations.
* **This PR:** Ran consistently for at least 300 iterations (out of
2000) before any failure, with some runs exceeding 900 iterations.

2.  **`enableTextEntryEmulation` configuration improvement:**
* The `keyboard_resize.dart` app is *exclusively* used for testing and
requires `enableTextEntryEmulation: false` for proper device keyboard
interaction.
* This PR sets `enableTextEntryEmulation: false` directly within the
app's `enableFlutterDriverExtension` setup. This simplifies the test
setup, avoids cross-thread communication, and prevents the app from
appearing broken when run independently (as manual text input wouldn't
work otherwise).

3.  **UI enhancements for manual testing:**
* Added `SafeArea` and `InputDecoration` to `keyboard_resize.dart`. This
ensures the `TextField` is fully visible and usable during manual
testing (it was previously obscured by the Android status bar). This
improves the developer experience when debugging the app directly.

4.  **Optimized polling for speed:**
* Reduced the polling interval and increased the number of polling
iterations (maintaining the overall timeout). This change resulted in a
~2-second speed improvement per test iteration during local testing.

**Further flakiness improvement discussion and ideas**
See the discussion at https://github.com/flutter/flutter/issues/161300

**Testing and reproduction:**

To reproduce the original flakiness and verify the fix (reproducibility
may vary based on setup; see
https://github.com/flutter/flutter/issues/161300#issuecomment-2618952501):

1. **Disable automatic reboots:** Comment out the `await
checkForRebootRequired();` line in
9e273d5e6e/dev/devicelab/lib/framework/framework.dart (L240-L243)
2. **Disable automatic retries:** Set `static const int retryNumber =
0;` in
9e273d5e6e/dev/devicelab/lib/framework/cocoon.dart (L56-L57)
3. **Install `hyperfine`:** This is a command-line benchmarking tool
(e.g., `brew install hyperfine` on macOS).
4. **Connect a device/emulator:** Connect a physical Android device or
start an emulator.
5. **Run repeated tests:** Use `hyperfine` to run the test multiple
times and capture the output:
    ```bash
hyperfine -r 100 'dart bin/test_runner.dart test -t
integration_ui_keyboard_resize > log.txt'
    ```
This commit is contained in:
Harri Kirik 2025-03-05 10:36:07 +02:00 committed by GitHub
parent d42a974c51
commit e4c726ab89
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 19 deletions

View File

@ -9,6 +9,7 @@ import 'keys.dart' as keys;
void main() {
enableFlutterDriverExtension(
enableTextEntryEmulation: false,
handler: (String? message) async {
// TODO(cbernaschina): remove when test flakiness is resolved
return 'keyboard_resize';
@ -46,21 +47,24 @@ class _MyHomePageState extends State<MyHomePage> {
key: const Key(keys.kDefaultTextField),
controller: _controller,
focusNode: FocusNode(),
decoration: const InputDecoration(border: OutlineInputBorder()),
);
return Scaffold(
body: Stack(
fit: StackFit.expand,
alignment: Alignment.bottomCenter,
children: <Widget>[
LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return Center(
child: Text('${constraints.biggest.height}', key: const Key(keys.kHeightText)),
);
},
),
textField,
],
body: SafeArea(
child: Stack(
fit: StackFit.expand,
alignment: Alignment.bottomCenter,
children: <Widget>[
LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
return Center(
child: Text('${constraints.biggest.height}', key: const Key(keys.kHeightText)),
);
},
),
textField,
],
),
),
floatingActionButton: FloatingActionButton(
key: const Key(keys.kUnfocusButton),

View File

@ -19,7 +19,6 @@ void main() {
});
test('Ensure keyboard dismissal resizes the view to original size', () async {
await driver.setTextEntryEmulation(enabled: false);
final SerializableFinder heightText = find.byValueKey(keys.kHeightText);
await driver.waitFor(heightText);
@ -29,11 +28,22 @@ void main() {
// Focus the text field to show the keyboard.
final SerializableFinder defaultTextField = find.byValueKey(keys.kDefaultTextField);
await driver.waitFor(defaultTextField);
await driver.tap(defaultTextField);
// The only practical way to detect the software keyboard opening or closing
// is to use polling and wait for the layout to change.
// We pick a short polling interval to speed up the test for most devices.
// In local tests, Pixel 8 Pro API 36 usually only one poll iteration is needed,
// older device like Galaxy Tab S3 API 28 takes 2-3 iterations.
const Duration pollDelay300Ms = Duration(milliseconds: 300);
bool heightTextDidShrink = false;
for (int i = 0; i < 6; ++i) {
await Future<void>.delayed(const Duration(seconds: 1));
// TODO(harri35): Reconsider this polling duration when the root cause is found
// in https://github.com/flutter/flutter/issues/163606.
// Sometimes it can take up to 21.3 seconds for the keyboard to open,
// so we allow ample time here (200 * pollDelay300Ms = 60 sec)
for (int i = 0; i < 200; ++i) {
await driver.tap(defaultTextField);
await Future<void>.delayed(pollDelay300Ms);
// Measure the height with keyboard displayed.
final String heightWithKeyboardShown = await driver.getText(heightText);
if (double.parse(heightWithKeyboardShown) < double.parse(startHeight)) {
@ -49,8 +59,8 @@ void main() {
await driver.tap(unfocusButton);
bool heightTextDidExpand = false;
for (int i = 0; i < 3; ++i) {
await Future<void>.delayed(const Duration(seconds: 1));
for (int i = 0; i < 10; ++i) {
await Future<void>.delayed(pollDelay300Ms);
// Measure the final height.
final String endHeight = await driver.getText(heightText);
if (endHeight == startHeight) {