Unverified Commit 6830edd0 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

Clean up flutter driver device detection. (#36434)

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