From 4e5e47e76a480995effffcffadc4a3d61a30bd41 Mon Sep 17 00:00:00 2001 From: Ricardo Amador <32242716+ricardoamador@users.noreply.github.com> Date: Fri, 29 Sep 2023 18:58:52 -0700 Subject: [PATCH] Add device ready check (#135526) *Replace this paragraph with a description of what this PR is changing or adding, and why. Consider including before/after screenshots.* *List which issues are fixed by this PR. You must list at least one issue.* Fixes https://github.com/flutter/flutter/issues/121420 *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* --- dev/devicelab/lib/framework/devices.dart | 53 +++++++++++++++++++++++- dev/devicelab/lib/tasks/perf_tests.dart | 14 ++----- 2 files changed, 54 insertions(+), 13 deletions(-) diff --git a/dev/devicelab/lib/framework/devices.dart b/dev/devicelab/lib/framework/devices.dart index 2720dd96898..7ce3782664a 100644 --- a/dev/devicelab/lib/framework/devices.dart +++ b/dev/devicelab/lib/framework/devices.dart @@ -8,6 +8,7 @@ import 'dart:io'; import 'dart:math' as math; import 'package:path/path.dart' as path; +import 'package:retry/retry.dart'; import 'utils.dart'; @@ -193,6 +194,20 @@ abstract class Device { /// Stop a process. Future stop(String packageName); + /// Wait for the device to become ready. + Future awaitDevice(); + + Future uninstallApp() async { + await flutter('install', options: [ + '--uninstall-only', + '-d', + deviceId]); + + await Future.delayed(const Duration(seconds: 2)); + + await awaitDevice(); + } + @override String toString() { return 'device: $deviceId'; @@ -848,6 +863,23 @@ class AndroidDevice extends Device { Future reboot() { return adb(['reboot']); } + + @override + Future awaitDevice() async { + print('Waiting for device.'); + final String waitOut = await adb(['wait-for-device']); + print(waitOut); + const RetryOptions retryOptions = RetryOptions(delayFactor: Duration(seconds: 1), maxAttempts: 10, maxDelay: Duration(minutes: 1)); + await retryOptions.retry(() async { + final String adbShellOut = await adb(['shell', 'getprop sys.boot_completed']); + if (adbShellOut != '1') { + print('Device not ready.'); + print(adbShellOut); + throw const DeviceException('Phone not ready.'); + } + }, retryIf: (Exception e) => e is DeviceException); + print('Done waiting for device.'); + } } class IosDeviceDiscovery implements DeviceDiscovery { @@ -1081,6 +1113,9 @@ class IosDevice extends Device { Future reboot() { return Process.run('idevicediagnostics', ['restart', '-u', deviceId]); } + + @override + Future awaitDevice() async {} } class LinuxDevice extends Device { @@ -1133,6 +1168,9 @@ class LinuxDevice extends Device { @override Future wakeUp() async { } + + @override + Future awaitDevice() async {} } class MacosDevice extends Device { @@ -1185,6 +1223,9 @@ class MacosDevice extends Device { @override Future wakeUp() async { } + + @override + Future awaitDevice() async {} } class WindowsDevice extends Device { @@ -1237,6 +1278,9 @@ class WindowsDevice extends Device { @override Future wakeUp() async { } + + @override + Future awaitDevice() async {} } /// Fuchsia device. @@ -1291,6 +1335,9 @@ class FuchsiaDevice extends Device { Future reboot() async { // Unsupported. } + + @override + Future awaitDevice() async {} } /// Path to the `adb` executable. @@ -1366,6 +1413,9 @@ class FakeDevice extends Device { Future reboot() async { // Unsupported. } + + @override + Future awaitDevice() async {} } class FakeDeviceDiscovery implements DeviceDiscovery { @@ -1428,6 +1478,5 @@ class FakeDeviceDiscovery implements DeviceDiscovery { } @override - Future performPreflightTasks() async { - } + Future performPreflightTasks() async { } } diff --git a/dev/devicelab/lib/tasks/perf_tests.dart b/dev/devicelab/lib/tasks/perf_tests.dart index 1dc73382bfa..e439e947af3 100644 --- a/dev/devicelab/lib/tasks/perf_tests.dart +++ b/dev/devicelab/lib/tasks/perf_tests.dart @@ -958,11 +958,7 @@ class StartupTest { } } - await flutter('install', options: [ - '--uninstall-only', - '-d', - device.deviceId, - ]); + await device.uninstallApp(); } final Map averageResults = _average(results, iterations); @@ -1084,11 +1080,7 @@ class DevtoolsStartupTest { await process.exitCode; } - await flutter('install', options: [ - '--uninstall-only', - '-d', - device.deviceId, - ]); + await device.uninstallApp(); if (sawLine) { return TaskResult.success(null, benchmarkScoreKeys: []); @@ -1850,7 +1842,7 @@ class MemoryTest { } await adb.cancel(); - await flutter('install', options: ['--uninstall-only', '-d', device!.deviceId]); + await device!.uninstallApp(); final ListStatistics startMemoryStatistics = ListStatistics(_startMemory); final ListStatistics endMemoryStatistics = ListStatistics(_endMemory);