Unverified Commit cc26a1aa authored by Victoria Ashworth's avatar Victoria Ashworth Committed by GitHub

Update device filtering and introduce isConnected and connectionInterface (#121359)

Update device filtering and introduce isConnected and connectionInterface
parent c590086b
......@@ -883,7 +883,7 @@ class DeviceDomain extends Domain {
Future<List<Map<String, Object?>>> getDevices([ Map<String, Object?>? args ]) async {
return <Map<String, Object?>>[
for (final PollingDeviceDiscovery discoverer in _discoverers)
for (final Device device in await discoverer.devices)
for (final Device device in await discoverer.devices())
await _deviceToMap(device),
];
}
......@@ -1069,7 +1069,7 @@ class DeviceDomain extends Domain {
/// Return the device matching the deviceId field in the args.
Future<Device?> _getDevice(String? deviceId) async {
for (final PollingDeviceDiscovery discoverer in _discoverers) {
final List<Device> devices = await discoverer.devices;
final List<Device> devices = await discoverer.devices();
Device? device;
for (final Device localDevice in devices) {
if (localDevice.id == deviceId) {
......
......@@ -62,7 +62,7 @@ class DevicesCommand extends FlutterCommand {
exitCode: 1);
}
final List<Device> devices = await globals.deviceManager?.refreshAllConnectedDevices(timeout: deviceDiscoveryTimeout) ?? <Device>[];
final List<Device> devices = await globals.deviceManager?.refreshAllDevices(timeout: deviceDiscoveryTimeout) ?? <Device>[];
if (boolArgDeprecated('machine')) {
await printDevicesAsJson(devices);
......
......@@ -209,7 +209,9 @@ class DriveCommand extends RunCommandBase {
String? get applicationBinaryPath => stringArgDeprecated(FlutterOptions.kUseApplicationBinary);
Future<Device?> get targetedDevice async {
return findTargetDevice(includeUnsupportedDevices: applicationBinaryPath == null);
return findTargetDevice(
includeDevicesUnsupportedByProject: applicationBinaryPath == null,
);
}
// Network devices need `publish-port` to be enabled because it requires mDNS.
......
......@@ -36,7 +36,7 @@ class LogsCommand extends FlutterCommand {
@override
Future<FlutterCommandResult> verifyThenRunCommand(String? commandPath) async {
device = await findTargetDevice(includeUnsupportedDevices: true);
device = await findTargetDevice(includeDevicesUnsupportedByProject: true);
if (device == null) {
throwToolExit(null);
}
......
This diff is collapsed.
......@@ -688,7 +688,7 @@ class DeviceValidator extends DoctorValidator {
@override
Future<ValidationResult> validate() async {
final List<Device> devices = await _deviceManager.getAllConnectedDevices();
final List<Device> devices = await _deviceManager.getAllDevices();
List<ValidationMessage> installedMessages = <ValidationMessage>[];
if (devices.isNotEmpty) {
installedMessages = (await Device.descriptions(devices))
......
......@@ -57,11 +57,14 @@ class ProxiedDevices extends DeviceDiscovery {
List<Device>? _devices;
@override
Future<List<Device>> get devices async =>
_devices ?? await discoverDevices();
Future<List<Device>> devices({DeviceDiscoveryFilter? filter}) async =>
_devices ?? await discoverDevices(filter: filter);
@override
Future<List<Device>> discoverDevices({Duration? timeout}) async {
Future<List<Device>> discoverDevices({
Duration? timeout,
DeviceDiscoveryFilter? filter
}) async {
final List<Map<String, Object?>> discoveredDevices = _cast<List<dynamic>>(await connection.sendRequest('device.discoverDevices')).cast<Map<String, Object?>>();
final List<ProxiedDevice> devices = <ProxiedDevice>[
for (final Map<String, Object?> device in discoveredDevices)
......
......@@ -1491,7 +1491,7 @@ abstract class FlutterCommand extends Command<void> {
/// If no device can be found that meets specified criteria,
/// then print an error message and return null.
Future<List<Device>?> findAllTargetDevices({
bool includeUnsupportedDevices = false,
bool includeDevicesUnsupportedByProject = false,
}) async {
if (!globals.doctor!.canLaunchAnything) {
globals.printError(userMessages.flutterNoDevelopmentDevice);
......@@ -1499,14 +1499,14 @@ abstract class FlutterCommand extends Command<void> {
}
final DeviceManager deviceManager = globals.deviceManager!;
List<Device> devices = await deviceManager.findTargetDevices(
includeUnsupportedDevices ? null : FlutterProject.current(),
includeDevicesUnsupportedByProject: includeDevicesUnsupportedByProject,
timeout: deviceDiscoveryTimeout,
);
if (devices.isEmpty) {
if (deviceManager.hasSpecifiedDeviceId) {
globals.logger.printStatus(userMessages.flutterNoMatchingDevice(deviceManager.specifiedDeviceId!));
final List<Device> allDevices = await deviceManager.getAllConnectedDevices();
final List<Device> allDevices = await deviceManager.getAllDevices();
if (allDevices.isNotEmpty) {
globals.logger.printStatus('');
globals.logger.printStatus('The following devices were found:');
......@@ -1543,7 +1543,7 @@ abstract class FlutterCommand extends Command<void> {
} else {
// Show an error message asking the user to specify `-d all` if they
// want to run on multiple devices.
final List<Device> allDevices = await deviceManager.getAllConnectedDevices();
final List<Device> allDevices = await deviceManager.getAllDevices();
globals.logger.printStatus(userMessages.flutterSpecifyDeviceWithAllOption);
globals.logger.printStatus('');
await Device.printDevices(allDevices, globals.logger);
......@@ -1607,18 +1607,20 @@ abstract class FlutterCommand extends Command<void> {
/// If a device cannot be found that meets specified criteria,
/// then print an error message and return null.
///
/// If [includeUnsupportedDevices] is true, the tool does not filter
/// If [includeDevicesUnsupportedByProject] is true, the tool does not filter
/// the list by the current project support list.
Future<Device?> findTargetDevice({
bool includeUnsupportedDevices = false,
bool includeDevicesUnsupportedByProject = false,
}) async {
List<Device>? deviceList = await findAllTargetDevices(includeUnsupportedDevices: includeUnsupportedDevices);
List<Device>? deviceList = await findAllTargetDevices(
includeDevicesUnsupportedByProject: includeDevicesUnsupportedByProject,
);
if (deviceList == null) {
return null;
}
if (deviceList.length > 1) {
globals.printStatus(userMessages.flutterSpecifyDevice);
deviceList = await globals.deviceManager!.getAllConnectedDevices();
deviceList = await globals.deviceManager!.getAllDevices();
globals.printStatus('');
await Device.printDevices(deviceList, globals.logger);
return null;
......
......@@ -1240,6 +1240,9 @@ class FakeAndroidDevice extends Fake implements AndroidDevice {
@override
bool isSupported() => true;
@override
bool get isConnected => true;
@override
bool get supportsHotRestart => true;
......@@ -1340,6 +1343,9 @@ class FakeIOSDevice extends Fake implements IOSDevice {
@override
bool isSupportedForProject(FlutterProject project) => true;
@override
bool get isConnected => true;
}
class FakeMDnsClient extends Fake implements MDnsClient {
......
......@@ -50,7 +50,7 @@ void main() {
testUsingContext("get devices' platform types", () async {
final List<String> platformTypes = Device.devicesPlatformTypes(
await globals.deviceManager!.getAllConnectedDevices(),
await globals.deviceManager!.getAllDevices(),
);
expect(platformTypes, <String>['android', 'web']);
}, overrides: <Type, Generator>{
......@@ -134,12 +134,14 @@ class _FakeDeviceManager extends DeviceManager {
_FakeDeviceManager() : super(logger: testLogger);
@override
Future<List<Device>> getAllConnectedDevices() =>
Future<List<Device>> getAllDevices({DeviceDiscoveryFilter? filter}) =>
Future<List<Device>>.value(fakeDevices.map((FakeDeviceJsonData d) => d.dev).toList());
@override
Future<List<Device>> refreshAllConnectedDevices({Duration? timeout}) =>
getAllConnectedDevices();
Future<List<Device>> refreshAllDevices({
Duration? timeout,
DeviceDiscoveryFilter? filter,
}) => getAllDevices(filter: filter);
@override
Future<List<String>> getDeviceDiagnostics() => Future<List<String>>.value(
......@@ -154,11 +156,16 @@ class NoDevicesManager extends DeviceManager {
NoDevicesManager() : super(logger: testLogger);
@override
Future<List<Device>> getAllConnectedDevices() async => <Device>[];
Future<List<Device>> getAllDevices({
DeviceDiscoveryFilter? filter,
}) async => <Device>[];
@override
Future<List<Device>> refreshAllConnectedDevices({Duration? timeout}) =>
getAllConnectedDevices();
Future<List<Device>> refreshAllDevices({
Duration? timeout,
DeviceDiscoveryFilter? filter,
}) =>
getAllDevices();
@override
List<DeviceDiscovery> get deviceDiscoverers => <DeviceDiscovery>[];
......
......@@ -1192,7 +1192,9 @@ class FakeDeviceManager extends Fake implements DeviceManager {
List<Device> devices = <Device>[];
@override
Future<List<Device>> getAllConnectedDevices() async => devices;
Future<List<Device>> getAllDevices({
DeviceDiscoveryFilter? filter,
}) async => devices;
@override
Future<List<String>> getDeviceDiagnostics() async => diagnostics;
......
......@@ -587,10 +587,16 @@ class FakeDeviceManager extends Fake implements DeviceManager {
String? specifiedDeviceId;
@override
Future<List<Device>> getDevices() async => devices;
Future<List<Device>> getDevices({
DeviceDiscoveryFilter? filter,
}) async => devices;
@override
Future<List<Device>> findTargetDevices(FlutterProject? flutterProject, {Duration? timeout, bool promptUserToChooseDevice = true}) async => devices;
Future<List<Device>> findTargetDevices({
bool includeDevicesUnsupportedByProject = false,
Duration? timeout,
bool promptUserToChooseDevice = true,
}) async => devices;
}
/// A [FlutterDriverFactory] that creates a [NeverEndingDriverService].
......
......@@ -98,7 +98,7 @@ void main() {
final ProxiedDevices proxiedDevices = ProxiedDevices(clientDaemonConnection, logger: bufferLogger);
final List<Device> devices = await proxiedDevices.devices;
final List<Device> devices = await proxiedDevices.devices();
expect(devices, hasLength(1));
final Device device = devices[0];
final bool supportsRuntimeMode = await device.supportsRuntimeMode(BuildMode.release);
......@@ -121,7 +121,7 @@ void main() {
final FakeDeviceLogReader fakeLogReader = FakeDeviceLogReader();
fakeDevice.logReader = fakeLogReader;
final List<Device> devices = await proxiedDevices.devices;
final List<Device> devices = await proxiedDevices.devices();
expect(devices, hasLength(1));
final Device device = devices[0];
final DeviceLogReader logReader = await device.getLogReader();
......@@ -153,7 +153,7 @@ void main() {
dummyApplicationBinary.writeAsStringSync('dummy content');
prebuiltApplicationPackage.applicationPackage = dummyApplicationBinary;
final List<Device> devices = await proxiedDevices.devices;
final List<Device> devices = await proxiedDevices.devices();
expect(devices, hasLength(1));
final Device device = devices[0];
......@@ -200,7 +200,7 @@ void main() {
final ProxiedDevices proxiedDevices = ProxiedDevices(clientDaemonConnection, logger: bufferLogger);
final List<Device> devices = await proxiedDevices.devices;
final List<Device> devices = await proxiedDevices.devices();
expect(devices, hasLength(1));
final Device device = devices[0];
......
......@@ -1054,6 +1054,9 @@ class FakeDevice extends Fake implements Device {
@override
bool get supportsFastStart => false;
@override
bool get isConnected => true;
bool supported = true;
@override
......
......@@ -920,7 +920,9 @@ class _FakeDeviceManager extends DeviceManager {
final List<Device> _connectedDevices;
@override
Future<List<Device>> getAllConnectedDevices() async => _connectedDevices;
Future<List<Device>> getAllDevices({
DeviceDiscoveryFilter? filter,
}) async => _connectedDevices;
@override
List<DeviceDiscovery> get deviceDiscoverers => <DeviceDiscovery>[];
......
......@@ -88,7 +88,10 @@ class FakeDeviceManager extends Fake implements DeviceManager {
String? specifiedDeviceId;
@override
Future<List<Device>> refreshAllConnectedDevices({Duration? timeout}) async {
Future<List<Device>> refreshAllDevices({
Duration? timeout,
DeviceDiscoveryFilter? filter,
}) async {
return devices;
}
}
......@@ -166,7 +166,7 @@ void main() {
directory: dir,
logger: BufferLogger.test()
)
).devices, <Device>[]);
).devices(), <Device>[]);
});
testWithoutContext('CustomDevice: no devices listed if custom devices feature flag disabled', () async {
......@@ -184,7 +184,7 @@ void main() {
directory: dir,
logger: BufferLogger.test()
)
).devices, <Device>[]);
).devices(), <Device>[]);
});
testWithoutContext('CustomDevices.devices', () async {
......@@ -208,7 +208,7 @@ void main() {
directory: dir,
logger: BufferLogger.test()
)
).devices,
).devices(),
hasLength(1)
);
});
......
......@@ -67,7 +67,7 @@ void main() {
logger: BufferLogger.test(),
processManager: FakeProcessManager.any(),
operatingSystemUtils: FakeOperatingSystemUtils(),
).devices, <Device>[]);
).devices(), <Device>[]);
});
testWithoutContext('LinuxDevice: no devices listed if Linux feature flag disabled', () async {
......@@ -78,7 +78,7 @@ void main() {
logger: BufferLogger.test(),
processManager: FakeProcessManager.any(),
operatingSystemUtils: FakeOperatingSystemUtils(),
).devices, <Device>[]);
).devices(), <Device>[]);
});
testWithoutContext('LinuxDevice: devices', () async {
......@@ -89,7 +89,7 @@ void main() {
logger: BufferLogger.test(),
processManager: FakeProcessManager.any(),
operatingSystemUtils: FakeOperatingSystemUtils(),
).devices, hasLength(1));
).devices(), hasLength(1));
});
testWithoutContext('LinuxDevice has well known id "linux"', () async {
......
......@@ -93,7 +93,7 @@ void main() {
featureFlags: TestFeatureFlags(isMacOSEnabled: true),
platform: linux,
),
).devices, isEmpty);
).devices(), isEmpty);
});
testWithoutContext('No devices listed if platform is supported and feature is disabled', () async {
......@@ -109,7 +109,7 @@ void main() {
),
);
expect(await macOSDevices.devices, isEmpty);
expect(await macOSDevices.devices(), isEmpty);
});
testWithoutContext('devices listed if platform is supported and feature is enabled', () async {
......@@ -125,7 +125,7 @@ void main() {
),
);
expect(await macOSDevices.devices, hasLength(1));
expect(await macOSDevices.devices(), hasLength(1));
});
testWithoutContext('has a well known device id macos', () async {
......
......@@ -50,7 +50,7 @@ void main() {
);
expect(discoverer.supportsPlatform, isTrue);
final List<Device> devices = await discoverer.devices;
final List<Device> devices = await discoverer.devices();
expect(devices, isEmpty);
});
......@@ -66,7 +66,7 @@ void main() {
);
expect(discoverer.supportsPlatform, isTrue);
final List<Device> devices = await discoverer.devices;
final List<Device> devices = await discoverer.devices();
expect(devices, isEmpty);
});
......@@ -82,7 +82,7 @@ void main() {
);
expect(discoverer.supportsPlatform, isTrue);
final List<Device> devices = await discoverer.devices;
final List<Device> devices = await discoverer.devices();
expect(devices, isEmpty);
});
......@@ -98,7 +98,7 @@ void main() {
);
expect(discoverer.supportsPlatform, isTrue);
List<Device> devices = await discoverer.devices;
List<Device> devices = await discoverer.devices();
expect(devices, hasLength(1));
final Device device = devices.single;
......
......@@ -47,7 +47,7 @@ void main() {
testWithoutContext('no device', () async {
final FlutterTesterDevices discoverer = setUpFlutterTesterDevices();
final List<Device> devices = await discoverer.devices;
final List<Device> devices = await discoverer.devices();
expect(devices, isEmpty);
});
......@@ -55,7 +55,7 @@ void main() {
FlutterTesterDevices.showFlutterTesterDevice = true;
final FlutterTesterDevices discoverer = setUpFlutterTesterDevices();
final List<Device> devices = await discoverer.devices;
final List<Device> devices = await discoverer.devices();
expect(devices, hasLength(1));
final Device device = devices.single;
......
......@@ -48,7 +48,7 @@ void main() {
logger: BufferLogger.test(),
processManager: FakeProcessManager.any(),
fileSystem: MemoryFileSystem.test(),
).devices, <Device>[]);
).devices(), <Device>[]);
});
testWithoutContext('WindowsDevices lists a devices if the workflow is supported', () async {
......@@ -61,7 +61,7 @@ void main() {
logger: BufferLogger.test(),
processManager: FakeProcessManager.any(),
fileSystem: MemoryFileSystem.test(),
).devices, hasLength(1));
).devices(), hasLength(1));
});
testWithoutContext('isSupportedForProject is true with editable host app', () async {
......
......@@ -207,21 +207,31 @@ class FakeDeviceManager implements DeviceManager {
}
@override
Future<List<Device>> getAllConnectedDevices() async => devices;
Future<List<Device>> getAllDevices({
DeviceDiscoveryFilter? filter,
}) async => devices;
@override
Future<List<Device>> refreshAllConnectedDevices({ Duration? timeout }) async => devices;
Future<List<Device>> refreshAllDevices({
Duration? timeout,
DeviceDiscoveryFilter? filter,
}) async => devices;
@override
Future<List<Device>> getDevicesById(String deviceId) async {
Future<List<Device>> getDevicesById(
String deviceId, {
DeviceDiscoveryFilter? filter,
}) async {
return devices.where((Device device) => device.id == deviceId).toList();
}
@override
Future<List<Device>> getDevices() {
Future<List<Device>> getDevices({
DeviceDiscoveryFilter? filter,
}) {
return hasSpecifiedDeviceId
? getDevicesById(specifiedDeviceId!)
: getAllConnectedDevices();
? getDevicesById(specifiedDeviceId!, filter: filter)
: getAllDevices(filter: filter);
}
void addDevice(Device device) => devices.add(device);
......@@ -236,16 +246,27 @@ class FakeDeviceManager implements DeviceManager {
List<DeviceDiscovery> get deviceDiscoverers => <DeviceDiscovery>[];
@override
bool isDeviceSupportedForProject(Device device, FlutterProject? flutterProject) {
return device.isSupportedForProject(flutterProject!);
Future<List<Device>> findTargetDevices({
bool includeDevicesUnsupportedByProject = false,
Duration? timeout,
bool promptUserToChooseDevice = true,
}) async {
return devices;
}
@override
Future<List<Device>> findTargetDevices(FlutterProject? flutterProject, { Duration? timeout, bool promptUserToChooseDevice = true }) async {
return devices;
DeviceDiscoverySupportFilter deviceSupportFilter({
bool includeDevicesUnsupportedByProject = false,
FlutterProject? flutterProject,
}) {
return TestDeviceDiscoverySupportFilter();
}
}
class TestDeviceDiscoverySupportFilter extends Fake implements DeviceDiscoverySupportFilter {
TestDeviceDiscoverySupportFilter();
}
class FakeAndroidLicenseValidator extends Fake implements AndroidLicenseValidator {
@override
Future<LicensesAccepted> get licensesAccepted async => LicensesAccepted.all;
......
......@@ -62,6 +62,8 @@ class FakeDevice extends Device {
bool ephemeral = true,
bool isSupported = true,
bool isSupportedForProject = true,
this.isConnected = true,
this.connectionInterface = DeviceConnectionInterface.attached,
PlatformType type = PlatformType.web,
LaunchResult? launchResult,
}) : _isSupported = isSupported,
......@@ -118,6 +120,12 @@ class FakeDevice extends Device {
@override
bool isSupported() => _isSupported;
@override
bool isConnected;
@override
DeviceConnectionInterface connectionInterface;
@override
Future<bool> isLocalEmulator = Future<bool>.value(true);
......@@ -174,7 +182,10 @@ class FakePollingDeviceDiscovery extends PollingDeviceDiscovery {
bool discoverDevicesCalled = false;
@override
Future<List<Device>> discoverDevices({Duration? timeout}) {
Future<List<Device>> discoverDevices({
Duration? timeout,
DeviceDiscoveryFilter? filter,
}) {
discoverDevicesCalled = true;
return super.discoverDevices(timeout: timeout);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment