From dbc18bbdb8de84a8d6a3df13803a3036f5c12525 Mon Sep 17 00:00:00 2001 From: Victoria Ashworth Date: Tue, 4 Apr 2023 17:33:26 -0500 Subject: [PATCH] Add flag to filter by device connection interface (#124034) Add flag to filter by device connection interface --- .../lib/src/commands/attach.dart | 1 + .../lib/src/commands/devices.dart | 36 ++- .../lib/src/commands/install.dart | 1 + .../flutter_tools/lib/src/commands/logs.dart | 1 + .../flutter_tools/lib/src/commands/run.dart | 1 + .../lib/src/commands/screenshot.dart | 1 + .../lib/src/runner/flutter_command.dart | 28 ++ .../lib/src/runner/target_devices.dart | 44 ++- .../commands.shard/hermetic/devices_test.dart | 269 ++++++++++++++---- .../runner/target_devices_test.dart | 170 +++++++++++ 10 files changed, 491 insertions(+), 61 deletions(-) diff --git a/packages/flutter_tools/lib/src/commands/attach.dart b/packages/flutter_tools/lib/src/commands/attach.dart index fe66eaf4f2..dece5ff1b5 100644 --- a/packages/flutter_tools/lib/src/commands/attach.dart +++ b/packages/flutter_tools/lib/src/commands/attach.dart @@ -140,6 +140,7 @@ class AttachCommand extends FlutterCommand { addDevToolsOptions(verboseHelp: verboseHelp); addServeObservatoryOptions(verboseHelp: verboseHelp); usesDeviceTimeoutOption(); + usesDeviceConnectionOption(); } final HotRunnerFactory _hotRunnerFactory; diff --git a/packages/flutter_tools/lib/src/commands/devices.dart b/packages/flutter_tools/lib/src/commands/devices.dart index 2b53bb24bf..9792a3d9f4 100644 --- a/packages/flutter_tools/lib/src/commands/devices.dart +++ b/packages/flutter_tools/lib/src/commands/devices.dart @@ -25,6 +25,7 @@ class DevicesCommand extends FlutterCommand { hide: !verboseHelp, ); usesDeviceTimeoutOption(); + usesDeviceConnectionOption(); } @override @@ -70,6 +71,7 @@ class DevicesCommand extends FlutterCommand { logger: globals.logger, deviceManager: globals.deviceManager, deviceDiscoveryTimeout: deviceDiscoveryTimeout, + deviceConnectionInterface: deviceConnectionInterface, ); await output.findAndOutputAllTargetDevices( @@ -86,18 +88,21 @@ class DevicesCommandOutput { required Logger logger, DeviceManager? deviceManager, Duration? deviceDiscoveryTimeout, + DeviceConnectionInterface? deviceConnectionInterface, }) { if (platform.isMacOS) { return DevicesCommandOutputWithExtendedWirelessDeviceDiscovery( logger: logger, deviceManager: deviceManager, deviceDiscoveryTimeout: deviceDiscoveryTimeout, + deviceConnectionInterface: deviceConnectionInterface, ); } return DevicesCommandOutput._private( logger: logger, deviceManager: deviceManager, deviceDiscoveryTimeout: deviceDiscoveryTimeout, + deviceConnectionInterface: deviceConnectionInterface, ); } @@ -105,14 +110,27 @@ class DevicesCommandOutput { required Logger logger, required DeviceManager? deviceManager, required this.deviceDiscoveryTimeout, + required this.deviceConnectionInterface, }) : _deviceManager = deviceManager, _logger = logger; final DeviceManager? _deviceManager; final Logger _logger; final Duration? deviceDiscoveryTimeout; + final DeviceConnectionInterface? deviceConnectionInterface; + + bool get _includeAttachedDevices => + deviceConnectionInterface == null || + deviceConnectionInterface == DeviceConnectionInterface.attached; + + bool get _includeWirelessDevices => + deviceConnectionInterface == null || + deviceConnectionInterface == DeviceConnectionInterface.wireless; Future> _getAttachedDevices(DeviceManager deviceManager) async { + if (!_includeAttachedDevices) { + return []; + } return deviceManager.getAllDevices( filter: DeviceDiscoveryFilter( deviceConnectionInterface: DeviceConnectionInterface.attached, @@ -121,6 +139,9 @@ class DevicesCommandOutput { } Future> _getWirelessDevices(DeviceManager deviceManager) async { + if (!_includeWirelessDevices) { + return []; + } return deviceManager.getAllDevices( filter: DeviceDiscoveryFilter( deviceConnectionInterface: DeviceConnectionInterface.wireless, @@ -207,19 +228,22 @@ class DevicesCommandOutputWithExtendedWirelessDeviceDiscovery extends DevicesCom required super.logger, super.deviceManager, super.deviceDiscoveryTimeout, + super.deviceConnectionInterface, }) : super._private(); @override Future findAndOutputAllTargetDevices({required bool machine}) async { - // When a user defines the timeout, use the super function that does not do - // longer wireless device discovery. - if (deviceDiscoveryTimeout != null) { + // When a user defines the timeout or filters to only attached devices, + // use the super function that does not do longer wireless device discovery. + if (deviceDiscoveryTimeout != null || deviceConnectionInterface == DeviceConnectionInterface.attached) { return super.findAndOutputAllTargetDevices(machine: machine); } if (machine) { final List devices = await _deviceManager?.refreshAllDevices( - filter: DeviceDiscoveryFilter(), + filter: DeviceDiscoveryFilter( + deviceConnectionInterface: deviceConnectionInterface, + ), timeout: DeviceManager.minimumWirelessDeviceDiscoveryTimeout, ) ?? []; await printDevicesAsJson(devices); @@ -249,7 +273,7 @@ class DevicesCommandOutputWithExtendedWirelessDeviceDiscovery extends DevicesCom } // Display waiting message. - if (attachedDevices.isEmpty) { + if (attachedDevices.isEmpty && _includeAttachedDevices) { _logger.printStatus(_noAttachedCheckForWireless); } else { _logger.printStatus(_checkingForWirelessDevicesMessage); @@ -265,7 +289,7 @@ class DevicesCommandOutputWithExtendedWirelessDeviceDiscovery extends DevicesCom waitingStatus.stop(); final Terminal terminal = _logger.terminal; - if (_logger.isVerbose) { + if (_logger.isVerbose && _includeAttachedDevices) { // Reprint the attach devices. if (attachedDevices.isNotEmpty) { _logger.printStatus('\n${attachedDevices.length} connected ${pluralize('device', attachedDevices.length)}:\n'); diff --git a/packages/flutter_tools/lib/src/commands/install.dart b/packages/flutter_tools/lib/src/commands/install.dart index ce6d30255d..bc3693bf21 100644 --- a/packages/flutter_tools/lib/src/commands/install.dart +++ b/packages/flutter_tools/lib/src/commands/install.dart @@ -19,6 +19,7 @@ class InstallCommand extends FlutterCommand with DeviceBasedDevelopmentArtifacts requiresPubspecYaml(); usesApplicationBinaryOption(); usesDeviceTimeoutOption(); + usesDeviceConnectionOption(); usesDeviceUserOption(); usesFlavorOption(); argParser.addFlag('uninstall-only', diff --git a/packages/flutter_tools/lib/src/commands/logs.dart b/packages/flutter_tools/lib/src/commands/logs.dart index 9abab3a2e7..519a79c9f5 100644 --- a/packages/flutter_tools/lib/src/commands/logs.dart +++ b/packages/flutter_tools/lib/src/commands/logs.dart @@ -19,6 +19,7 @@ class LogsCommand extends FlutterCommand { help: 'Clear log history before reading from logs.', ); usesDeviceTimeoutOption(); + usesDeviceConnectionOption(); } @override diff --git a/packages/flutter_tools/lib/src/commands/run.dart b/packages/flutter_tools/lib/src/commands/run.dart index 0b3e65f9ba..524add23ef 100644 --- a/packages/flutter_tools/lib/src/commands/run.dart +++ b/packages/flutter_tools/lib/src/commands/run.dart @@ -177,6 +177,7 @@ abstract class RunCommandBase extends FlutterCommand with DeviceBasedDevelopment addNullSafetyModeOptions(hide: !verboseHelp); usesDeviceUserOption(); usesDeviceTimeoutOption(); + usesDeviceConnectionOption(); addDdsOptions(verboseHelp: verboseHelp); addDevToolsOptions(verboseHelp: verboseHelp); addServeObservatoryOptions(verboseHelp: verboseHelp); diff --git a/packages/flutter_tools/lib/src/commands/screenshot.dart b/packages/flutter_tools/lib/src/commands/screenshot.dart index 3bb590aa52..21b531ecf5 100644 --- a/packages/flutter_tools/lib/src/commands/screenshot.dart +++ b/packages/flutter_tools/lib/src/commands/screenshot.dart @@ -52,6 +52,7 @@ class ScreenshotCommand extends FlutterCommand { defaultsTo: _kDeviceType, ); usesDeviceTimeoutOption(); + usesDeviceConnectionOption(); } final FileSystem fs; diff --git a/packages/flutter_tools/lib/src/runner/flutter_command.dart b/packages/flutter_tools/lib/src/runner/flutter_command.dart index ea33e6b176..b359e92fa3 100644 --- a/packages/flutter_tools/lib/src/runner/flutter_command.dart +++ b/packages/flutter_tools/lib/src/runner/flutter_command.dart @@ -112,6 +112,7 @@ class FlutterOptions { static const String kNullSafety = 'sound-null-safety'; static const String kDeviceUser = 'device-user'; static const String kDeviceTimeout = 'device-timeout'; + static const String kDeviceConnection = 'device-connection'; static const String kAnalyzeSize = 'analyze-size'; static const String kCodeSizeDirectory = 'code-size-directory'; static const String kNullAssertions = 'null-assertions'; @@ -694,6 +695,19 @@ abstract class FlutterCommand extends Command { ); } + void usesDeviceConnectionOption() { + argParser.addOption(FlutterOptions.kDeviceConnection, + defaultsTo: 'both', + help: 'Discover devices based on connection type.', + allowed: ['attached', 'wireless', 'both'], + allowedHelp: { + 'both': 'Searches for both attached and wireless devices.', + 'attached': 'Only searches for devices connected by USB or built-in (such as simulators/emulators, MacOS/Windows, Chrome)', + 'wireless': 'Only searches for devices connected wirelessly. Discovering wireless devices may take longer.' + }, + ); + } + void usesApplicationBinaryOption() { argParser.addOption( FlutterOptions.kUseApplicationBinary, @@ -722,10 +736,24 @@ abstract class FlutterCommand extends Command { return null; }(); + DeviceConnectionInterface? get deviceConnectionInterface { + if ((argResults?.options.contains(FlutterOptions.kDeviceConnection) ?? false) + && (argResults?.wasParsed(FlutterOptions.kDeviceConnection) ?? false)) { + final String? connectionType = stringArg(FlutterOptions.kDeviceConnection); + if (connectionType == 'attached') { + return DeviceConnectionInterface.attached; + } else if (connectionType == 'wireless') { + return DeviceConnectionInterface.wireless; + } + } + return null; + } + late final TargetDevices _targetDevices = TargetDevices( platform: globals.platform, deviceManager: globals.deviceManager!, logger: globals.logger, + deviceConnectionInterface: deviceConnectionInterface, ); void addBuildModeFlags({ diff --git a/packages/flutter_tools/lib/src/runner/target_devices.dart b/packages/flutter_tools/lib/src/runner/target_devices.dart index e15bee8f22..2c7a646670 100644 --- a/packages/flutter_tools/lib/src/runner/target_devices.dart +++ b/packages/flutter_tools/lib/src/runner/target_devices.dart @@ -31,31 +31,46 @@ class TargetDevices { required Platform platform, required DeviceManager deviceManager, required Logger logger, + DeviceConnectionInterface? deviceConnectionInterface, }) { if (platform.isMacOS) { return TargetDevicesWithExtendedWirelessDeviceDiscovery( deviceManager: deviceManager, logger: logger, + deviceConnectionInterface: deviceConnectionInterface, ); } return TargetDevices._private( deviceManager: deviceManager, logger: logger, + deviceConnectionInterface: deviceConnectionInterface, ); } TargetDevices._private({ required DeviceManager deviceManager, required Logger logger, + required this.deviceConnectionInterface, }) : _deviceManager = deviceManager, _logger = logger; final DeviceManager _deviceManager; final Logger _logger; + final DeviceConnectionInterface? deviceConnectionInterface; + + bool get _includeAttachedDevices => + deviceConnectionInterface == null || + deviceConnectionInterface == DeviceConnectionInterface.attached; + bool get _includeWirelessDevices => + deviceConnectionInterface == null || + deviceConnectionInterface == DeviceConnectionInterface.wireless; Future> _getAttachedDevices({ DeviceDiscoverySupportFilter? supportFilter, }) async { + if (!_includeAttachedDevices) { + return []; + } return _deviceManager.getDevices( filter: DeviceDiscoveryFilter( deviceConnectionInterface: DeviceConnectionInterface.attached, @@ -67,6 +82,9 @@ class TargetDevices { Future> _getWirelessDevices({ DeviceDiscoverySupportFilter? supportFilter, }) async { + if (!_includeWirelessDevices) { + return []; + } return _deviceManager.getDevices( filter: DeviceDiscoveryFilter( deviceConnectionInterface: DeviceConnectionInterface.wireless, @@ -85,6 +103,7 @@ class TargetDevices { supportFilter: _deviceManager.deviceSupportFilter( includeDevicesUnsupportedByProject: includeDevicesUnsupportedByProject, ), + deviceConnectionInterface: deviceConnectionInterface, ), ); } @@ -166,7 +185,11 @@ class TargetDevices { /// unsupported devices found. Future?> _handleNoDevices() async { // Get connected devices from cache, including unsupported ones. - final List unsupportedDevices = await _deviceManager.getAllDevices(); + final List unsupportedDevices = await _deviceManager.getAllDevices( + filter: DeviceDiscoveryFilter( + deviceConnectionInterface: deviceConnectionInterface, + ) + ); if (_deviceManager.hasSpecifiedDeviceId) { _logger.printStatus( @@ -344,6 +367,7 @@ class TargetDevicesWithExtendedWirelessDeviceDiscovery extends TargetDevices { TargetDevicesWithExtendedWirelessDeviceDiscovery({ required super.deviceManager, required super.logger, + super.deviceConnectionInterface, }) : super._private(); Future? _wirelessDevicesRefresh; @@ -358,7 +382,7 @@ class TargetDevicesWithExtendedWirelessDeviceDiscovery extends TargetDevices { void startExtendedWirelessDeviceDiscovery({ Duration? deviceDiscoveryTimeout, }) { - if (deviceDiscoveryTimeout == null) { + if (deviceDiscoveryTimeout == null && _includeWirelessDevices) { _wirelessDevicesRefresh ??= _deviceManager.refreshExtendedWirelessDeviceDiscoverers( timeout: DeviceManager.minimumWirelessDeviceDiscoveryTimeout, ); @@ -369,6 +393,9 @@ class TargetDevicesWithExtendedWirelessDeviceDiscovery extends TargetDevices { Future> _getRefreshedWirelessDevices({ bool includeDevicesUnsupportedByProject = false, }) async { + if (!_includeWirelessDevices) { + return []; + } startExtendedWirelessDeviceDiscovery(); return () async { await _wirelessDevicesRefresh; @@ -428,9 +455,10 @@ class TargetDevicesWithExtendedWirelessDeviceDiscovery extends TargetDevices { return null; } - // When a user defines the timeout, use the super function that does not do - // longer wireless device discovery and does not wait for devices to connect. - if (deviceDiscoveryTimeout != null) { + // When a user defines the timeout or filters to only attached devices, + // use the super function that does not do longer wireless device + // discovery and does not wait for devices to connect. + if (deviceDiscoveryTimeout != null || deviceConnectionInterface == DeviceConnectionInterface.attached) { return super.findAllTargetDevices( deviceDiscoveryTimeout: deviceDiscoveryTimeout, includeDevicesUnsupportedByProject: includeDevicesUnsupportedByProject, @@ -497,7 +525,11 @@ class TargetDevicesWithExtendedWirelessDeviceDiscovery extends TargetDevices { List attachedDevices, Future> futureWirelessDevices, ) async { - _logger.printStatus(_noAttachedCheckForWireless); + if (_includeAttachedDevices) { + _logger.printStatus(_noAttachedCheckForWireless); + } else { + _logger.printStatus(_checkingForWirelessDevicesMessage); + } final List wirelessDevices = await futureWirelessDevices; final List allDevices = attachedDevices + wirelessDevices; diff --git a/packages/flutter_tools/test/commands.shard/hermetic/devices_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/devices_test.dart index 3cefbaf833..86b8d1f74b 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/devices_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/devices_test.dart @@ -116,23 +116,62 @@ If you expected your device to be detected, please run "flutter doctor" to diagn Platform: () => platform, }); - testUsingContext('Outputs parsable JSON with --machine flag', () async { - final DevicesCommand command = DevicesCommand(); - await createTestCommandRunner(command).run(['devices', '--machine']); - expect( - json.decode(testLogger.statusText), - >[ - fakeDevices[0].json, - fakeDevices[1].json, - fakeDevices[2].json, - ], - ); - }, overrides: { - DeviceManager: () => _FakeDeviceManager(devices: deviceList), - ProcessManager: () => FakeProcessManager.any(), - Cache: () => cache, - Artifacts: () => Artifacts.test(), - Platform: () => platform, + group('with --machine flag', () { + testUsingContext('Outputs parsable JSON', () async { + final DevicesCommand command = DevicesCommand(); + await createTestCommandRunner(command).run(['devices', '--machine']); + expect( + json.decode(testLogger.statusText), + >[ + fakeDevices[0].json, + fakeDevices[1].json, + fakeDevices[2].json, + ], + ); + }, overrides: { + DeviceManager: () => _FakeDeviceManager(devices: deviceList), + ProcessManager: () => FakeProcessManager.any(), + Cache: () => cache, + Artifacts: () => Artifacts.test(), + Platform: () => platform, + }); + + group('when deviceConnectionInterface', () { + testUsingContext('filtered to attached', () async { + final DevicesCommand command = DevicesCommand(); + await createTestCommandRunner(command).run(['devices', '--machine', '--device-connection', 'attached']); + expect( + json.decode(testLogger.statusText), + >[ + fakeDevices[0].json, + fakeDevices[1].json, + ], + ); + }, overrides: { + DeviceManager: () => _FakeDeviceManager(devices: deviceList), + ProcessManager: () => FakeProcessManager.any(), + Cache: () => cache, + Artifacts: () => Artifacts.test(), + Platform: () => platform, + }); + + testUsingContext('filtered to wireless', () async { + final DevicesCommand command = DevicesCommand(); + await createTestCommandRunner(command).run(['devices', '--machine', '--device-connection', 'wireless']); + expect( + json.decode(testLogger.statusText), + >[ + fakeDevices[2].json, + ], + ); + }, overrides: { + DeviceManager: () => _FakeDeviceManager(devices: deviceList), + ProcessManager: () => FakeProcessManager.any(), + Cache: () => cache, + Artifacts: () => Artifacts.test(), + Platform: () => platform, + }); + }); }); testUsingContext('available devices and diagnostics', () async { @@ -155,6 +194,41 @@ wireless android (mobile) • wireless-android • android-arm • Test SDK (1.2 ProcessManager: () => FakeProcessManager.any(), Platform: () => platform, }); + + group('when deviceConnectionInterface', () { + testUsingContext('filtered to attached', () async { + final DevicesCommand command = DevicesCommand(); + await createTestCommandRunner(command).run(['devices', '--device-connection', 'attached']); + expect(testLogger.statusText, ''' +2 connected devices: + +ephemeral (mobile) • ephemeral • android-arm • Test SDK (1.2.3) (emulator) +webby (mobile) • webby • web-javascript • Web SDK (1.2.4) (emulator) + +• Cannot connect to device ABC +'''); + }, overrides: { + DeviceManager: () => _FakeDeviceManager(devices: deviceList), + ProcessManager: () => FakeProcessManager.any(), + Platform: () => platform, + }); + + testUsingContext('filtered to wireless', () async { + final DevicesCommand command = DevicesCommand(); + await createTestCommandRunner(command).run(['devices', '--device-connection', 'wireless']); + expect(testLogger.statusText, ''' +1 wirelessly connected device: + +wireless android (mobile) • wireless-android • android-arm • Test SDK (1.2.3) (emulator) + +• Cannot connect to device ABC +'''); + }, overrides: { + DeviceManager: () => _FakeDeviceManager(devices: deviceList), + ProcessManager: () => FakeProcessManager.any(), + Platform: () => platform, + }); + }); }); group('when includes only attached devices', () { @@ -225,12 +299,13 @@ wireless android (mobile) • wireless-android • android-arm • Test SDK (1.2 Platform: () => platform, }); - testUsingContext('no error when no connected devices', () async { - final DevicesCommand command = DevicesCommand(); - await createTestCommandRunner(command).run(['devices']); - expect( - testLogger.statusText, - equals(''' + group('when no connected devices', () { + testUsingContext('no error', () async { + final DevicesCommand command = DevicesCommand(); + await createTestCommandRunner(command).run(['devices']); + expect( + testLogger.statusText, + equals(''' No devices found yet. Checking for wireless devices... No devices detected. @@ -239,14 +314,51 @@ Run "flutter emulators" to list and start any available device emulators. If you expected your device to be detected, please run "flutter doctor" to diagnose potential issues. You may also try increasing the time to wait for connected devices with the --device-timeout flag. Visit https://flutter.dev/setup/ for troubleshooting tips. '''), - ); - }, overrides: { - AndroidSdk: () => null, - DeviceManager: () => NoDevicesManager(), - ProcessManager: () => FakeProcessManager.any(), - Cache: () => cache, - Artifacts: () => Artifacts.test(), - Platform: () => platform, + ); + }, overrides: { + AndroidSdk: () => null, + DeviceManager: () => NoDevicesManager(), + ProcessManager: () => FakeProcessManager.any(), + Cache: () => cache, + Artifacts: () => Artifacts.test(), + Platform: () => platform, + }); + + group('when deviceConnectionInterface', () { + testUsingContext('filtered to attached', () async { + final DevicesCommand command = DevicesCommand(); + await createTestCommandRunner(command).run(['devices', '--device-connection', 'attached']); + expect(testLogger.statusText, ''' +No devices detected. + +Run "flutter emulators" to list and start any available device emulators. + +If you expected your device to be detected, please run "flutter doctor" to diagnose potential issues. You may also try increasing the time to wait for connected devices with the --device-timeout flag. Visit https://flutter.dev/setup/ for troubleshooting tips. +'''); + }, overrides: { + DeviceManager: () => NoDevicesManager(), + ProcessManager: () => FakeProcessManager.any(), + Platform: () => platform, + }); + + testUsingContext('filtered to wireless', () async { + final DevicesCommand command = DevicesCommand(); + await createTestCommandRunner(command).run(['devices', '--device-connection', 'wireless']); + expect(testLogger.statusText, ''' +Checking for wireless devices... + +No devices detected. + +Run "flutter emulators" to list and start any available device emulators. + +If you expected your device to be detected, please run "flutter doctor" to diagnose potential issues. You may also try increasing the time to wait for connected devices with the --device-timeout flag. Visit https://flutter.dev/setup/ for troubleshooting tips. +'''); + }, overrides: { + DeviceManager: () => NoDevicesManager(), + ProcessManager: () => FakeProcessManager.any(), + Platform: () => platform, + }); + }); }); group('when includes both attached and wireless devices', () { @@ -273,24 +385,64 @@ If you expected your device to be detected, please run "flutter doctor" to diagn Platform: () => platform, }); - testUsingContext('Outputs parsable JSON with --machine flag', () async { - final DevicesCommand command = DevicesCommand(); - await createTestCommandRunner(command).run(['devices', '--machine']); - expect( - json.decode(testLogger.statusText), - >[ - fakeDevices[0].json, - fakeDevices[1].json, - fakeDevices[2].json, - fakeDevices[3].json, - ], - ); - }, overrides: { - DeviceManager: () => _FakeDeviceManager(devices: deviceList), - ProcessManager: () => FakeProcessManager.any(), - Cache: () => cache, - Artifacts: () => Artifacts.test(), - Platform: () => platform, + group('with --machine flag', () { + testUsingContext('Outputs parsable JSON', () async { + final DevicesCommand command = DevicesCommand(); + await createTestCommandRunner(command).run(['devices', '--machine']); + expect( + json.decode(testLogger.statusText), + >[ + fakeDevices[0].json, + fakeDevices[1].json, + fakeDevices[2].json, + fakeDevices[3].json, + ], + ); + }, overrides: { + DeviceManager: () => _FakeDeviceManager(devices: deviceList), + ProcessManager: () => FakeProcessManager.any(), + Cache: () => cache, + Artifacts: () => Artifacts.test(), + Platform: () => platform, + }); + + group('when deviceConnectionInterface', () { + testUsingContext('filtered to attached', () async { + final DevicesCommand command = DevicesCommand(); + await createTestCommandRunner(command).run(['devices', '--machine', '--device-connection', 'attached']); + expect( + json.decode(testLogger.statusText), + >[ + fakeDevices[0].json, + fakeDevices[1].json, + ], + ); + }, overrides: { + DeviceManager: () => _FakeDeviceManager(devices: deviceList), + ProcessManager: () => FakeProcessManager.any(), + Cache: () => cache, + Artifacts: () => Artifacts.test(), + Platform: () => platform, + }); + + testUsingContext('filtered to wireless', () async { + final DevicesCommand command = DevicesCommand(); + await createTestCommandRunner(command).run(['devices', '--machine', '--device-connection', 'wireless']); + expect( + json.decode(testLogger.statusText), + >[ + fakeDevices[2].json, + fakeDevices[3].json, + ], + ); + }, overrides: { + DeviceManager: () => _FakeDeviceManager(devices: deviceList), + ProcessManager: () => FakeProcessManager.any(), + Cache: () => cache, + Artifacts: () => Artifacts.test(), + Platform: () => platform, + }); + }); }); testUsingContext('available devices and diagnostics', () async { @@ -401,6 +553,25 @@ wireless ios (mobile) • wireless-ios • ios • iOS 16 (simul Platform: () => platform, Logger: () => fakeLogger, }); + + testUsingContext('when deviceConnectionInterface filtered to wireless', () async { + final DevicesCommand command = DevicesCommand(); + await createTestCommandRunner(command).run(['devices', '--device-connection', 'wireless']); + expect(testLogger.statusText, ''' +Checking for wireless devices... + +2 wirelessly connected devices: + +wireless android (mobile) • wireless-android • android-arm • Test SDK (1.2.3) (emulator) +wireless ios (mobile) • wireless-ios • ios • iOS 16 (simulator) + +• Cannot connect to device ABC +'''); + }, overrides: { + DeviceManager: () => _FakeDeviceManager(devices: deviceList), + ProcessManager: () => FakeProcessManager.any(), + Platform: () => platform, + }); }); }); diff --git a/packages/flutter_tools/test/general.shard/runner/target_devices_test.dart b/packages/flutter_tools/test/general.shard/runner/target_devices_test.dart index e454281339..38101b464a 100644 --- a/packages/flutter_tools/test/general.shard/runner/target_devices_test.dart +++ b/packages/flutter_tools/test/general.shard/runner/target_devices_test.dart @@ -217,6 +217,48 @@ If you would like your app to run on android, consider running `flutter create . expect(deviceManager.androidDiscoverer.discoverDevicesCalled, 0); expect(deviceManager.androidDiscoverer.numberOfTimesPolled, 1); }); + + group('when deviceConnectionInterface does not match', () { + testUsingContext('filter of wireless', () async { + deviceManager.androidDiscoverer.deviceList = [attachedAndroidDevice1]; + + final TargetDevices targetDevices = TargetDevices( + platform: platform, + deviceManager: deviceManager, + logger: logger, + deviceConnectionInterface: DeviceConnectionInterface.wireless, + ); + final List? devices = await targetDevices.findAllTargetDevices(); + + expect(logger.statusText, equals(''' +No supported devices connected. +''')); + expect(devices, isNull); + expect(deviceManager.androidDiscoverer.devicesCalled, 2); + expect(deviceManager.androidDiscoverer.discoverDevicesCalled, 0); + expect(deviceManager.androidDiscoverer.numberOfTimesPolled, 1); + }); + + testUsingContext('filter of attached', () async { + deviceManager.androidDiscoverer.deviceList = [wirelessAndroidDevice1]; + + final TargetDevices targetDevices = TargetDevices( + platform: platform, + deviceManager: deviceManager, + logger: logger, + deviceConnectionInterface: DeviceConnectionInterface.attached, + ); + final List? devices = await targetDevices.findAllTargetDevices(); + + expect(logger.statusText, equals(''' +No supported devices connected. +''')); + expect(devices, isNull); + expect(deviceManager.androidDiscoverer.devicesCalled, 2); + expect(deviceManager.androidDiscoverer.discoverDevicesCalled, 0); + expect(deviceManager.androidDiscoverer.numberOfTimesPolled, 1); + }); + }); }); group('with hasSpecifiedDeviceId', () { @@ -272,6 +314,58 @@ target-device (mobile) • xxx • android • Android 10 (unsupported) expect(deviceManager.androidDiscoverer.discoverDevicesCalled, 0); expect(deviceManager.androidDiscoverer.numberOfTimesPolled, 1); }); + + group('when deviceConnectionInterface does not match', () { + testUsingContext('filter of wireless', () async { + final FakeDevice device1 = FakeDevice(deviceName: 'not-a-match'); + final FakeDevice device2 = FakeDevice.wireless(deviceName: 'not-a-match-2'); + deviceManager.androidDiscoverer.deviceList = [exactMatchAndroidDevice, device1, device2]; + + final TargetDevices targetDevices = TargetDevices( + platform: platform, + deviceManager: deviceManager, + logger: logger, + deviceConnectionInterface: DeviceConnectionInterface.wireless, + ); + final List? devices = await targetDevices.findAllTargetDevices(); + + expect(logger.statusText, equals(''' +No supported devices found with name or id matching 'target-device'. + +The following devices were found: +not-a-match-2 (mobile) • xxx • android • Android 10 +''')); + expect(devices, isNull); + expect(deviceManager.androidDiscoverer.devicesCalled, 3); + expect(deviceManager.androidDiscoverer.discoverDevicesCalled, 0); + expect(deviceManager.androidDiscoverer.numberOfTimesPolled, 1); + }); + + testUsingContext('filter of attached', () async { + final FakeDevice device1 = FakeDevice(deviceName: 'not-a-match'); + final FakeDevice device2 = FakeDevice.wireless(deviceName: 'not-a-match-2'); + deviceManager.androidDiscoverer.deviceList = [exactMatchWirelessAndroidDevice, device1, device2]; + + final TargetDevices targetDevices = TargetDevices( + platform: platform, + deviceManager: deviceManager, + logger: logger, + deviceConnectionInterface: DeviceConnectionInterface.attached, + ); + final List? devices = await targetDevices.findAllTargetDevices(); + + expect(logger.statusText, equals(''' +No supported devices found with name or id matching 'target-device'. + +The following devices were found: +not-a-match (mobile) • xxx • android • Android 10 +''')); + expect(devices, isNull); + expect(deviceManager.androidDiscoverer.devicesCalled, 3); + expect(deviceManager.androidDiscoverer.discoverDevicesCalled, 0); + expect(deviceManager.androidDiscoverer.numberOfTimesPolled, 1); + }); + }); }); group('with hasSpecifiedAllDevices', () { @@ -944,6 +1038,28 @@ Unable to locate a development device; please run 'flutter doctor' for informati expect(deviceManager.iosDiscoverer.numberOfTimesPolled, 1); }); + testUsingContext('ensure no refresh when deviceConnectionInterface is attached', () async { + final BufferLogger logger = BufferLogger.test(); + final TestDeviceManager deviceManager = TestDeviceManager( + logger: logger, + platform: platform, + ); + deviceManager.iosDiscoverer.deviceList = [attachedIOSDevice1]; + + final TargetDevicesWithExtendedWirelessDeviceDiscovery targetDevices = TargetDevicesWithExtendedWirelessDeviceDiscovery( + deviceManager: deviceManager, + logger: logger, + deviceConnectionInterface: DeviceConnectionInterface.attached, + ); + final List? devices = await targetDevices.findAllTargetDevices(); + + expect(logger.statusText, equals('')); + expect(devices, [attachedIOSDevice1]); + expect(deviceManager.iosDiscoverer.devicesCalled, 1); + expect(deviceManager.iosDiscoverer.discoverDevicesCalled, 0); + expect(deviceManager.iosDiscoverer.numberOfTimesPolled, 1); + }); + testUsingContext('ensure unsupported for projects are included when includeDevicesUnsupportedByProject is true', () async { final BufferLogger logger = BufferLogger.test(); final TestDeviceManager deviceManager = TestDeviceManager( @@ -1055,6 +1171,30 @@ No supported devices connected. expect(deviceManager.iosDiscoverer.discoverDevicesCalled, 1); expect(deviceManager.iosDiscoverer.numberOfTimesPolled, 2); }); + + group('when deviceConnectionInterface does not match', () { + testUsingContext('filter of wireless', () async { + deviceManager.iosDiscoverer.deviceList = [attachedIOSDevice1]; + deviceManager.iosDiscoverer.refreshDeviceList = [attachedIOSDevice1]; + + final TestTargetDevicesWithExtendedWirelessDeviceDiscovery targetDevices = TestTargetDevicesWithExtendedWirelessDeviceDiscovery( + deviceManager: deviceManager, + logger: logger, + deviceConnectionInterface: DeviceConnectionInterface.wireless, + ); + final List? devices = await targetDevices.findAllTargetDevices(); + + expect(logger.statusText, equals(''' +Checking for wireless devices... + +No supported devices connected. +''')); + expect(devices, isNull); + expect(deviceManager.iosDiscoverer.devicesCalled, 2); + expect(deviceManager.iosDiscoverer.discoverDevicesCalled, 1); + expect(deviceManager.iosDiscoverer.numberOfTimesPolled, 1); + }); + }); }); group('with hasSpecifiedDeviceId', () { @@ -1121,6 +1261,35 @@ target-device (mobile) • xxx • ios • iOS 16 (unsupported) expect(deviceManager.iosDiscoverer.numberOfTimesPolled, 2); expect(deviceManager.iosDiscoverer.xcdevice.waitedForDeviceToConnect, isFalse); }); + + group('when deviceConnectionInterface does not match', () { + testUsingContext('filter of wireless', () async { + final FakeIOSDevice device1 = FakeIOSDevice.notConnectedWireless(deviceName: 'not-a-match'); + final FakeIOSDevice device1Connected = FakeIOSDevice.connectedWireless(deviceName: 'not-a-match'); + deviceManager.iosDiscoverer.deviceList = [exactMatchAttachedIOSDevice, device1]; + deviceManager.iosDiscoverer.refreshDeviceList = [exactMatchAttachedIOSDevice, device1Connected]; + + final TestTargetDevicesWithExtendedWirelessDeviceDiscovery targetDevices = TestTargetDevicesWithExtendedWirelessDeviceDiscovery( + deviceManager: deviceManager, + logger: logger, + deviceConnectionInterface: DeviceConnectionInterface.wireless, + ); + final List? devices = await targetDevices.findAllTargetDevices(); + + expect(logger.statusText, equals(''' +Checking for wireless devices... + +No supported devices found with name or id matching 'target-device'. + +The following devices were found: +not-a-match (mobile) • xxx • ios • iOS 16 +''')); + expect(devices, isNull); + expect(deviceManager.iosDiscoverer.devicesCalled, 3); + expect(deviceManager.iosDiscoverer.discoverDevicesCalled, 1); + expect(deviceManager.iosDiscoverer.numberOfTimesPolled, 2); + }); + }); }); group('with hasSpecifiedAllDevices', () { @@ -2223,6 +2392,7 @@ class TestTargetDevicesWithExtendedWirelessDeviceDiscovery extends TargetDevices TestTargetDevicesWithExtendedWirelessDeviceDiscovery({ required super.deviceManager, required super.logger, + super.deviceConnectionInterface, }) : _deviceSelection = TestTargetDeviceSelection(logger); final TestTargetDeviceSelection _deviceSelection;