From 6830edd0be259cc61adb8e27b65b3fa6b9aa6c63 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Fri, 19 Jul 2019 14:48:17 -0700 Subject: [PATCH] Clean up flutter driver device detection. (#36434) --- .../flutter_tools/lib/src/commands/drive.dart | 12 ++--- .../general.shard/commands/config_test.dart | 7 ++- .../general.shard/commands/drive_test.dart | 46 +++++++++++-------- packages/flutter_tools/test/src/context.dart | 8 ++-- 4 files changed, 41 insertions(+), 32 deletions(-) diff --git a/packages/flutter_tools/lib/src/commands/drive.dart b/packages/flutter_tools/lib/src/commands/drive.dart index 76332c06291..ab23856a114 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/package_map.dart'; 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 @@ class DriveCommand extends RunCommandBase { if (testFile == null) throwToolExit(null); - _device = await targetDeviceFinder(); + _device = await findTargetDevice(); if (device == null) throwToolExit(null); @@ -187,15 +188,8 @@ class DriveCommand extends RunCommandBase { } } -/// Finds a device to test on. May launch a simulator, if necessary. -typedef TargetDeviceFinder = Future Function(); -TargetDeviceFinder targetDeviceFinder = findTargetDevice; -void restoreTargetDeviceFinder() { - targetDeviceFinder = findTargetDevice; -} - Future findTargetDevice() async { - final List devices = await deviceManager.getDevices().toList(); + final List 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 eaaa8319ad4..e7c87e8cf1c 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/context.dart'; 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 @@ void main() { MockFlutterVersion mockFlutterVersion; setUpAll(() { + // TODO(jonahwilliams): remove once features are landed. + debugDisableDesktop = true; + debugDisableWeb = true; Cache.disableLocking(); }); @@ -136,4 +141,4 @@ class MockAndroidSdk extends Mock implements AndroidSdk { 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 f6e39edd91b..aad02288faa 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 @@ void main() { 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 @@ void main() { 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 @@ void main() { 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 @@ void main() { }); 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 @@ void main() { }); 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 @@ void main() { }); 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 @@ void main() { 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 @@ void main() { 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: { + 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 @@ void main() { 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.value(true)); @@ -300,7 +310,7 @@ void main() { }); Future 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 eb1ed6ebdff..688fe515e0e 100644 --- a/packages/flutter_tools/test/src/context.dart +++ b/packages/flutter_tools/test/src/context.dart @@ -188,13 +188,13 @@ class FakeDeviceManager implements DeviceManager { List get deviceDiscoverers => []; @override - Future> 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> findTargetDevices(FlutterProject flutterProject) async { + return devices; } }