Clean up flutter driver device detection. (#36434)

diff --git a/packages/flutter_tools/lib/src/commands/drive.dart b/packages/flutter_tools/lib/src/commands/drive.dart
index 76332c0..ab23856 100644
--- a/packages/flutter_tools/lib/src/commands/drive.dart
+++ b/packages/flutter_tools/lib/src/commands/drive.dart
@@ -13,6 +13,7 @@
 import '../dart/sdk.dart';
 import '../device.dart';
 import '../globals.dart';
+import '../project.dart';
 import '../resident_runner.dart';
 import '../runner/flutter_command.dart' show FlutterCommandResult;
 import 'run.dart';
@@ -94,7 +95,7 @@
     if (testFile == null)
       throwToolExit(null);
 
-    _device = await targetDeviceFinder();
+    _device = await findTargetDevice();
     if (device == null)
       throwToolExit(null);
 
@@ -187,15 +188,8 @@
   }
 }
 
-/// Finds a device to test on. May launch a simulator, if necessary.
-typedef TargetDeviceFinder = Future<Device> Function();
-TargetDeviceFinder targetDeviceFinder = findTargetDevice;
-void restoreTargetDeviceFinder() {
-  targetDeviceFinder = findTargetDevice;
-}
-
 Future<Device> findTargetDevice() async {
-  final List<Device> devices = await deviceManager.getDevices().toList();
+  final List<Device> devices = await deviceManager.findTargetDevices(FlutterProject.current());
 
   if (deviceManager.hasSpecifiedDeviceId) {
     if (devices.isEmpty) {
diff --git a/packages/flutter_tools/test/general.shard/commands/config_test.dart b/packages/flutter_tools/test/general.shard/commands/config_test.dart
index eaaa831..e7c87e8 100644
--- a/packages/flutter_tools/test/general.shard/commands/config_test.dart
+++ b/packages/flutter_tools/test/general.shard/commands/config_test.dart
@@ -12,7 +12,9 @@
 import 'package:flutter_tools/src/base/logger.dart';
 import 'package:flutter_tools/src/cache.dart';
 import 'package:flutter_tools/src/commands/config.dart';
+import 'package:flutter_tools/src/desktop.dart';
 import 'package:flutter_tools/src/version.dart';
+import 'package:flutter_tools/src/web/workflow.dart';
 import 'package:mockito/mockito.dart';
 
 import '../../src/common.dart';
@@ -24,6 +26,9 @@
   MockFlutterVersion mockFlutterVersion;
 
   setUpAll(() {
+    // TODO(jonahwilliams): remove once features are landed.
+    debugDisableDesktop = true;
+    debugDisableWeb = true;
     Cache.disableLocking();
   });
 
@@ -136,4 +141,4 @@
   String get directory => 'path/to/android/sdk';
 }
 
-class MockFlutterVersion extends Mock implements FlutterVersion {}
\ No newline at end of file
+class MockFlutterVersion extends Mock implements FlutterVersion {}
diff --git a/packages/flutter_tools/test/general.shard/commands/drive_test.dart b/packages/flutter_tools/test/general.shard/commands/drive_test.dart
index f6e39ed..aad0228 100644
--- a/packages/flutter_tools/test/general.shard/commands/drive_test.dart
+++ b/packages/flutter_tools/test/general.shard/commands/drive_test.dart
@@ -23,15 +23,10 @@
   group('drive', () {
     DriveCommand command;
     Device mockDevice;
+    Device mockUnsupportedDevice;
     MemoryFileSystem fs;
     Directory tempDir;
 
-    void withMockDevice([ Device mock ]) {
-      mockDevice = mock ?? MockDevice();
-      targetDeviceFinder = () async => mockDevice;
-      testDeviceManager.addDevice(mockDevice);
-    }
-
     setUpAll(() {
       Cache.disableLocking();
     });
@@ -47,9 +42,6 @@
       fs.file('pubspec.yaml')..createSync();
       fs.file('.packages').createSync();
       setExitFunctionForTests();
-      targetDeviceFinder = () {
-        throw 'Unexpected call to targetDeviceFinder';
-      };
       appStarter = (DriveCommand command) {
         throw 'Unexpected call to appStarter';
       };
@@ -67,12 +59,11 @@
       restoreAppStarter();
       restoreAppStopper();
       restoreTestRunner();
-      restoreTargetDeviceFinder();
       tryToDelete(tempDir);
     });
 
     testUsingContext('returns 1 when test file is not found', () async {
-      withMockDevice();
+      testDeviceManager.addDevice(MockDevice());
 
       final String testApp = fs.path.join(tempDir.path, 'test', 'e2e.dart');
       final String testFile = fs.path.join(tempDir.path, 'test_driver', 'e2e_test.dart');
@@ -94,7 +85,7 @@
     });
 
     testUsingContext('returns 1 when app fails to run', () async {
-      withMockDevice();
+      testDeviceManager.addDevice(MockDevice());
       appStarter = expectAsync1((DriveCommand command) async => null);
 
       final String testApp = fs.path.join(tempDir.path, 'test_driver', 'e2e.dart');
@@ -163,7 +154,7 @@
     });
 
     testUsingContext('returns 0 when test ends successfully', () async {
-      withMockDevice();
+      testDeviceManager.addDevice(MockDevice());
 
       final String testApp = fs.path.join(tempDir.path, 'test', 'e2e.dart');
       final String testFile = fs.path.join(tempDir.path, 'test_driver', 'e2e_test.dart');
@@ -194,7 +185,7 @@
     });
 
     testUsingContext('returns exitCode set by test runner', () async {
-      withMockDevice();
+      testDeviceManager.addDevice(MockDevice());
 
       final String testApp = fs.path.join(tempDir.path, 'test', 'e2e.dart');
       final String testFile = fs.path.join(tempDir.path, 'test_driver', 'e2e_test.dart');
@@ -231,7 +222,8 @@
     group('findTargetDevice', () {
       testUsingContext('uses specified device', () async {
         testDeviceManager.specifiedDeviceId = '123';
-        withMockDevice();
+        mockDevice = MockDevice();
+        testDeviceManager.addDevice(mockDevice);
         when(mockDevice.name).thenReturn('specified-device');
         when(mockDevice.id).thenReturn('123');
 
@@ -255,7 +247,25 @@
       testUsingContext('uses existing Android device', () async {
         mockDevice = MockAndroidDevice();
         when(mockDevice.name).thenReturn('mock-android-device');
-        withMockDevice(mockDevice);
+        testDeviceManager.addDevice(mockDevice);
+
+        final Device device = await findTargetDevice();
+        expect(device.name, 'mock-android-device');
+      }, overrides: <Type, Generator>{
+        FileSystem: () => fs,
+        Platform: platform,
+      });
+
+      testUsingContext('skips unsupported device', () async {
+        mockDevice = MockAndroidDevice();
+        mockUnsupportedDevice = MockDevice();
+        when(mockUnsupportedDevice.isSupportedForProject(any))
+            .thenReturn(false);
+        when(mockDevice.isSupportedForProject(any))
+            .thenReturn(true);
+        when(mockDevice.name).thenReturn('mock-android-device');
+        testDeviceManager.addDevice(mockDevice);
+        testDeviceManager.addDevice(mockUnsupportedDevice);
 
         final Device device = await findTargetDevice();
         expect(device.name, 'mock-android-device');
@@ -279,7 +289,7 @@
       Platform macOsPlatform() => FakePlatform(operatingSystem: 'macos');
 
       testUsingContext('uses existing simulator', () async {
-        withMockDevice();
+        testDeviceManager.addDevice(mockDevice);
         when(mockDevice.name).thenReturn('mock-simulator');
         when(mockDevice.isLocalEmulator)
             .thenAnswer((Invocation invocation) => Future<bool>.value(true));
@@ -300,7 +310,7 @@
       });
 
       Future<void> appStarterSetup() async {
-        withMockDevice();
+        testDeviceManager.addDevice(mockDevice);
 
         final MockDeviceLogReader mockDeviceLogReader = MockDeviceLogReader();
         when(mockDevice.getLogReader()).thenReturn(mockDeviceLogReader);
diff --git a/packages/flutter_tools/test/src/context.dart b/packages/flutter_tools/test/src/context.dart
index eb1ed6e..688fe51 100644
--- a/packages/flutter_tools/test/src/context.dart
+++ b/packages/flutter_tools/test/src/context.dart
@@ -188,13 +188,13 @@
   List<DeviceDiscovery> get deviceDiscoverers => <DeviceDiscovery>[];
 
   @override
-  Future<List<Device>> findTargetDevices(FlutterProject flutterProject) {
-    return getDevices().toList();
+  bool isDeviceSupportedForProject(Device device, FlutterProject flutterProject) {
+    return device.isSupportedForProject(flutterProject);
   }
 
   @override
-  bool isDeviceSupportedForProject(Device device, FlutterProject flutterProject) {
-    return device.isSupportedForProject(flutterProject);
+  Future<List<Device>> findTargetDevices(FlutterProject flutterProject) async {
+    return devices;
   }
 }