diff --git a/dev/devicelab/bin/tasks/routing_test.dart b/dev/devicelab/bin/tasks/routing_test.dart index 537373d146..d56d8ad7dd 100644 --- a/dev/devicelab/bin/tasks/routing_test.dart +++ b/dev/devicelab/bin/tasks/routing_test.dart @@ -82,6 +82,9 @@ void main() { }); int result; result = await drive.exitCode; + await flutter('install', options: [ + '--uninstall-only', + ]); if (result != 0) throw 'Failed to drive test app (exit code $result).'; result = await run.exitCode; diff --git a/dev/devicelab/manifest.yaml b/dev/devicelab/manifest.yaml index 0961e5e2ea..9c226a68b5 100644 --- a/dev/devicelab/manifest.yaml +++ b/dev/devicelab/manifest.yaml @@ -440,7 +440,6 @@ tasks: Verifies that `flutter drive --route` still works. No performance numbers. stage: devicelab required_agent_capabilities: ["linux/android"] - flaky: true linux_chrome_dev_mode: description: > diff --git a/packages/flutter_tools/lib/src/commands/drive.dart b/packages/flutter_tools/lib/src/commands/drive.dart index 2f6cc3a1e2..8ae32ebf0f 100644 --- a/packages/flutter_tools/lib/src/commands/drive.dart +++ b/packages/flutter_tools/lib/src/commands/drive.dart @@ -186,22 +186,35 @@ class DriveCommand extends RunCommandBase { ? null : _fileSystem.file(stringArg('use-application-binary')); - await driverService.start( - buildInfo, - device, - debuggingOptions, - ipv6, - applicationBinary: applicationBinary, - route: route, - userIdentifier: userIdentifier, - mainPath: targetFile, - platformArgs: { - if (traceStartup) - 'trace-startup': traceStartup, - if (web) - '--no-launch-chrome': true, + if (stringArg('use-existing-app') == null) { + await driverService.start( + buildInfo, + device, + debuggingOptions, + ipv6, + applicationBinary: applicationBinary, + route: route, + userIdentifier: userIdentifier, + mainPath: targetFile, + platformArgs: { + if (traceStartup) + 'trace-startup': traceStartup, + if (web) + '--no-launch-chrome': true, + } + ); + } else { + final Uri uri = Uri.tryParse(stringArg('use-existing-app')); + if (uri == null) { + throwToolExit('Invalid VM Service URI: ${stringArg('use-existing-app')}'); } - ); + await driverService.reuseApplication( + uri, + device, + debuggingOptions, + ipv6, + ); + } final int testResult = await driverService.startTest( testFile, diff --git a/packages/flutter_tools/lib/src/drive/drive_service.dart b/packages/flutter_tools/lib/src/drive/drive_service.dart index c646195ea8..884966cc71 100644 --- a/packages/flutter_tools/lib/src/drive/drive_service.dart +++ b/packages/flutter_tools/lib/src/drive/drive_service.dart @@ -64,6 +64,14 @@ abstract class DriverService { Map platformArgs = const {}, }); + /// If --use-existing-app is provided, configured the correct VM Service URI. + Future reuseApplication( + Uri vmServiceUri, + Device device, + DebuggingOptions debuggingOptions, + bool ipv6, + ); + /// Start the test file with the provided [arguments] and [environment], returning /// the test process exit code. Future startTest( @@ -168,10 +176,26 @@ class FlutterDriverService extends DriverService { if (result == null || !result.started) { throwToolExit('Application failed to start. Will not run test. Quitting.', exitCode: 1); } - _vmServiceUri = result.observatoryUri.toString(); + return reuseApplication( + result.observatoryUri, + device, + debuggingOptions, + ipv6, + ); + } + + @override + Future reuseApplication( + Uri vmServiceUri, + Device device, + DebuggingOptions debuggingOptions, + bool ipv6, + ) async { + _vmServiceUri = vmServiceUri.toString(); + _device = device; try { await device.dds.startDartDevelopmentService( - result.observatoryUri, + vmServiceUri, debuggingOptions.ddsPort, ipv6, debuggingOptions.disableServiceAuthCodes, @@ -228,20 +252,24 @@ class FlutterDriverService extends DriverService { ); await sharedSkSlWriter(_device, result, outputFile: writeSkslOnExit, logger: _logger); } - try { + // If the application package is available, stop and uninstall. + if (_applicationPackage != null) { if (!await _device.stopApp(_applicationPackage, userIdentifier: userIdentifier)) { _logger.printError('Failed to stop app'); } - } on Exception catch (err) { - _logger.printError('Failed to stop app due to unhandled error: $err'); - } - - try { if (!await _device.uninstallApp(_applicationPackage, userIdentifier: userIdentifier)) { - _logger.printError('Failed to uninstall app'); + _logger.printError('Failed to uninstall app'); } - } on Exception catch (err) { - _logger.printError('Failed to uninstall app due to unhandled error: $err'); + } else if (_device.supportsFlutterExit) { + // Otherwise use the VM Service URI to stop the app as a best effort approach. + final vm_service.VM vm = await _vmService.getVM(); + final vm_service.IsolateRef isolateRef = vm.isolates + .firstWhere((vm_service.IsolateRef element) { + return !element.isSystemIsolate; + }, orElse: () => null); + unawaited(_vmService.flutterExit(isolateId: isolateRef.id)); + } else { + _logger.printTrace('No application package for $_device, leaving app running'); } await _device.dispose(); } diff --git a/packages/flutter_tools/lib/src/drive/web_driver_service.dart b/packages/flutter_tools/lib/src/drive/web_driver_service.dart index 9a84c3412f..12087d093f 100644 --- a/packages/flutter_tools/lib/src/drive/web_driver_service.dart +++ b/packages/flutter_tools/lib/src/drive/web_driver_service.dart @@ -153,6 +153,11 @@ class WebDriverService extends DriverService { 'ANDROID_CHROME_ON_EMULATOR': (_browserNameToEnum(browserName) == Browser.androidChrome && androidEmulator).toString(), }; } + + @override + Future reuseApplication(Uri vmServiceUri, Device device, DebuggingOptions debuggingOptions, bool ipv6) async { + throwToolExit('--use-existing-app is not supported with flutter web driver'); + } } /// A list of supported browsers. diff --git a/packages/flutter_tools/test/general.shard/drive/drive_service_test.dart b/packages/flutter_tools/test/general.shard/drive/drive_service_test.dart index 1bf59dae19..20e6c8dba3 100644 --- a/packages/flutter_tools/test/general.shard/drive/drive_service_test.dart +++ b/packages/flutter_tools/test/general.shard/drive/drive_service_test.dart @@ -255,6 +255,47 @@ void main() { 'data': {'A': 'B'} }); }); + + testWithoutContext('Can connect to existing application and stop it during cleanup', () async { + final FakeVmServiceHost fakeVmServiceHost = FakeVmServiceHost(requests: [ + getVM, + getVM, + const FakeVmServiceRequest( + method: 'ext.flutter.exit', + args: { + 'isolateId': '1', + } + ) + ]); + final FakeProcessManager processManager = FakeProcessManager.list([]); + final DriverService driverService = setUpDriverService(processManager: processManager, vmService: fakeVmServiceHost.vmService); + final FakeDevice device = FakeDevice(LaunchResult.failed()); + + await driverService.reuseApplication( + Uri.parse('http://127.0.0.1:63426/1UasC_ihpXY=/'), + device, + DebuggingOptions.enabled(BuildInfo.debug), + false, + ); + await driverService.stop(); + }); + + testWithoutContext('Does not call flutterExit on device types that do not support it', () async { + final FakeVmServiceHost fakeVmServiceHost = FakeVmServiceHost(requests: [ + getVM, + ]); + final FakeProcessManager processManager = FakeProcessManager.list([]); + final DriverService driverService = setUpDriverService(processManager: processManager, vmService: fakeVmServiceHost.vmService); + final FakeDevice device = FakeDevice(LaunchResult.failed(), supportsFlutterExit: false); + + await driverService.reuseApplication( + Uri.parse('http://127.0.0.1:63426/1UasC_ihpXY=/'), + device, + DebuggingOptions.enabled(BuildInfo.debug), + false, + ); + await driverService.stop(); + }); } FlutterDriverService setUpDriverService({ @@ -301,7 +342,7 @@ class FakeApplicationPackageFactory extends Fake implements ApplicationPackageFa class FakeApplicationPackage extends Fake implements ApplicationPackage {} class FakeDevice extends Fake implements Device { - FakeDevice(this.result); + FakeDevice(this.result, {this.supportsFlutterExit = true}); LaunchResult result; bool didStopApp = false; @@ -312,6 +353,9 @@ class FakeDevice extends Fake implements Device { @override String get name => 'test'; + @override + final bool supportsFlutterExit; + @override final DartDevelopmentService dds = FakeDartDevelopmentService();