diff --git a/packages/flutter_tools/lib/src/globals.dart b/packages/flutter_tools/lib/src/globals.dart index 7a27db520d..83fd491bda 100644 --- a/packages/flutter_tools/lib/src/globals.dart +++ b/packages/flutter_tools/lib/src/globals.dart @@ -14,6 +14,7 @@ import 'base/io.dart'; import 'base/logger.dart'; import 'base/terminal.dart'; import 'cache.dart'; +import 'ios/mac.dart'; import 'macos/xcode.dart'; import 'version.dart'; @@ -44,6 +45,7 @@ Platform get platform => context.get() ?? _kLocalPlatform; Xcode get xcode => context.get(); FlutterVersion get flutterVersion => context.get(); +IMobileDevice get iMobileDevice => context.get(); /// Display an error level message to the user. Commands should use this if they /// fail in some way. diff --git a/packages/flutter_tools/lib/src/ios/devices.dart b/packages/flutter_tools/lib/src/ios/devices.dart index c9b4341289..30e368b21f 100644 --- a/packages/flutter_tools/lib/src/ios/devices.dart +++ b/packages/flutter_tools/lib/src/ios/devices.dart @@ -174,20 +174,20 @@ class IOSDevice extends Device { if (!globals.platform.isMacOS) { throw UnsupportedError('Control of iOS devices or simulators only supported on Mac OS.'); } - if (!iMobileDevice.isInstalled) { + if (!globals.iMobileDevice.isInstalled) { return []; } final List devices = []; - for (String id in (await iMobileDevice.getAvailableDeviceIDs()).split('\n')) { + for (String id in (await globals.iMobileDevice.getAvailableDeviceIDs()).split('\n')) { id = id.trim(); if (id.isEmpty) { continue; } try { - final String deviceName = await iMobileDevice.getInfoForDevice(id, 'DeviceName'); - final String sdkVersion = await iMobileDevice.getInfoForDevice(id, 'ProductVersion'); + final String deviceName = await globals.iMobileDevice.getInfoForDevice(id, 'DeviceName'); + final String sdkVersion = await globals.iMobileDevice.getInfoForDevice(id, 'ProductVersion'); devices.add(IOSDevice(id, name: deviceName, sdkVersion: sdkVersion)); } on IOSDeviceNotFoundError catch (error) { // Unable to find device with given udid. Possibly a network device. @@ -283,7 +283,7 @@ class IOSDevice extends Device { String cpuArchitecture; try { - cpuArchitecture = await iMobileDevice.getInfoForDevice(id, 'CPUArchitecture'); + cpuArchitecture = await globals.iMobileDevice.getInfoForDevice(id, 'CPUArchitecture'); } on IOSDeviceNotFoundError catch (e) { globals.printError(e.message); return LaunchResult.failed(); @@ -460,11 +460,11 @@ class IOSDevice extends Device { void clearLogs() { } @override - bool get supportsScreenshot => iMobileDevice.isInstalled; + bool get supportsScreenshot => globals.iMobileDevice.isInstalled; @override Future takeScreenshot(File outputFile) async { - await iMobileDevice.takeScreenshot(outputFile); + await globals.iMobileDevice.takeScreenshot(outputFile); } @override @@ -610,7 +610,7 @@ class IOSDeviceLogReader extends DeviceLogReader { if (device.majorSdkVersion >= _minimumUniversalLoggingSdkVersion) { return; } - iMobileDevice.startLogger(device.id).then((Process process) { + globals.iMobileDevice.startLogger(device.id).then((Process process) { process.stdout.transform(utf8.decoder).transform(const LineSplitter()).listen(_newSyslogLineHandler()); process.stderr.transform(utf8.decoder).transform(const LineSplitter()).listen(_newSyslogLineHandler()); process.exitCode.whenComplete(() { diff --git a/packages/flutter_tools/lib/src/ios/mac.dart b/packages/flutter_tools/lib/src/ios/mac.dart index 977a1e50e2..4119f63406 100644 --- a/packages/flutter_tools/lib/src/ios/mac.dart +++ b/packages/flutter_tools/lib/src/ios/mac.dart @@ -9,7 +9,6 @@ import 'package:meta/meta.dart'; import '../application_package.dart'; import '../artifacts.dart'; import '../base/common.dart'; -import '../base/context.dart'; import '../base/file_system.dart'; import '../base/io.dart'; import '../base/logger.dart'; @@ -26,8 +25,6 @@ import '../reporting/reporting.dart'; import 'code_signing.dart'; import 'xcodeproj.dart'; -IMobileDevice get iMobileDevice => context.get(); - /// Specialized exception for expected situations where the ideviceinfo /// tool responds with exit code 255 / 'No device found' message class IOSDeviceNotFoundError implements Exception { diff --git a/packages/flutter_tools/test/general.shard/ios/devices_test.dart b/packages/flutter_tools/test/general.shard/ios/devices_test.dart index 74fb188a59..cc6aa43afb 100644 --- a/packages/flutter_tools/test/general.shard/ios/devices_test.dart +++ b/packages/flutter_tools/test/general.shard/ios/devices_test.dart @@ -770,8 +770,8 @@ void main() { }); testUsingContext('returns no devices if none are attached', () async { - when(iMobileDevice.isInstalled).thenReturn(true); - when(iMobileDevice.getAvailableDeviceIDs()) + when(globals.iMobileDevice.isInstalled).thenReturn(true); + when(globals.iMobileDevice.getAvailableDeviceIDs()) .thenAnswer((Invocation invocation) => Future.value('')); final List devices = await IOSDevice.getAttachedDevices(); expect(devices, isEmpty); @@ -783,8 +783,8 @@ void main() { final List unsupportedPlatforms = [linuxPlatform, windowsPlatform]; for (final Platform platform in unsupportedPlatforms) { testUsingContext('throws Unsupported Operation exception on ${platform.operatingSystem}', () async { - when(iMobileDevice.isInstalled).thenReturn(false); - when(iMobileDevice.getAvailableDeviceIDs()) + when(globals.iMobileDevice.isInstalled).thenReturn(false); + when(globals.iMobileDevice.getAvailableDeviceIDs()) .thenAnswer((Invocation invocation) => Future.value('')); expect( () async { await IOSDevice.getAttachedDevices(); }, @@ -797,19 +797,19 @@ void main() { } testUsingContext('returns attached devices', () async { - when(iMobileDevice.isInstalled).thenReturn(true); - when(iMobileDevice.getAvailableDeviceIDs()) + when(globals.iMobileDevice.isInstalled).thenReturn(true); + when(globals.iMobileDevice.getAvailableDeviceIDs()) .thenAnswer((Invocation invocation) => Future.value(''' 98206e7a4afd4aedaff06e687594e089dede3c44 f577a7903cc54959be2e34bc4f7f80b7009efcf4 ''')); - when(iMobileDevice.getInfoForDevice('98206e7a4afd4aedaff06e687594e089dede3c44', 'DeviceName')) + when(globals.iMobileDevice.getInfoForDevice('98206e7a4afd4aedaff06e687594e089dede3c44', 'DeviceName')) .thenAnswer((_) => Future.value('La tele me regarde')); - when(iMobileDevice.getInfoForDevice('98206e7a4afd4aedaff06e687594e089dede3c44', 'ProductVersion')) + when(globals.iMobileDevice.getInfoForDevice('98206e7a4afd4aedaff06e687594e089dede3c44', 'ProductVersion')) .thenAnswer((_) => Future.value('10.3.2')); - when(iMobileDevice.getInfoForDevice('f577a7903cc54959be2e34bc4f7f80b7009efcf4', 'DeviceName')) + when(globals.iMobileDevice.getInfoForDevice('f577a7903cc54959be2e34bc4f7f80b7009efcf4', 'DeviceName')) .thenAnswer((_) => Future.value('Puits sans fond')); - when(iMobileDevice.getInfoForDevice('f577a7903cc54959be2e34bc4f7f80b7009efcf4', 'ProductVersion')) + when(globals.iMobileDevice.getInfoForDevice('f577a7903cc54959be2e34bc4f7f80b7009efcf4', 'ProductVersion')) .thenAnswer((_) => Future.value('11.0')); final List devices = await IOSDevice.getAttachedDevices(); expect(devices, hasLength(2)); @@ -823,15 +823,15 @@ f577a7903cc54959be2e34bc4f7f80b7009efcf4 }); testUsingContext('returns attached devices and ignores devices that cannot be found by ideviceinfo', () async { - when(iMobileDevice.isInstalled).thenReturn(true); - when(iMobileDevice.getAvailableDeviceIDs()) + when(globals.iMobileDevice.isInstalled).thenReturn(true); + when(globals.iMobileDevice.getAvailableDeviceIDs()) .thenAnswer((Invocation invocation) => Future.value(''' 98206e7a4afd4aedaff06e687594e089dede3c44 f577a7903cc54959be2e34bc4f7f80b7009efcf4 ''')); - when(iMobileDevice.getInfoForDevice('98206e7a4afd4aedaff06e687594e089dede3c44', 'DeviceName')) + when(globals.iMobileDevice.getInfoForDevice('98206e7a4afd4aedaff06e687594e089dede3c44', 'DeviceName')) .thenAnswer((_) => Future.value('La tele me regarde')); - when(iMobileDevice.getInfoForDevice('f577a7903cc54959be2e34bc4f7f80b7009efcf4', 'DeviceName')) + when(globals.iMobileDevice.getInfoForDevice('f577a7903cc54959be2e34bc4f7f80b7009efcf4', 'DeviceName')) .thenThrow(const IOSDeviceNotFoundError('Device not found')); final List devices = await IOSDevice.getAttachedDevices(); expect(devices, hasLength(1)); diff --git a/packages/flutter_tools/test/general.shard/ios/mac_test.dart b/packages/flutter_tools/test/general.shard/ios/mac_test.dart index 0f2c9d7dfc..7cea841e3f 100644 --- a/packages/flutter_tools/test/general.shard/ios/mac_test.dart +++ b/packages/flutter_tools/test/general.shard/ios/mac_test.dart @@ -60,7 +60,7 @@ void main() { when(mockProcessManager.runSync( [ideviceIdPath, '-h'], environment: anyNamed('environment'), )).thenReturn(ProcessResult(123, 1, '', '')); - expect(await iMobileDevice.isWorking, false); + expect(await globals.iMobileDevice.isWorking, false); }, overrides: { ProcessManager: () => mockProcessManager, Artifacts: () => mockArtifacts, @@ -71,7 +71,7 @@ void main() { [ideviceIdPath, '-l'], environment: {'DYLD_LIBRARY_PATH': libimobiledevicePath}, )).thenThrow(ProcessException(ideviceIdPath, ['-l'])); - expect(() async => await iMobileDevice.getAvailableDeviceIDs(), throwsToolExit()); + expect(() async => await globals.iMobileDevice.getAvailableDeviceIDs(), throwsToolExit()); }, overrides: { ProcessManager: () => mockProcessManager, Cache: () => mockCache, @@ -83,7 +83,7 @@ void main() { [ideviceIdPath, '-l'], environment: {'DYLD_LIBRARY_PATH': libimobiledevicePath}, )).thenAnswer((_) => Future.value(ProcessResult(1, 1, '', 'Sad today'))); - expect(() async => await iMobileDevice.getAvailableDeviceIDs(), throwsToolExit()); + expect(() async => await globals.iMobileDevice.getAvailableDeviceIDs(), throwsToolExit()); }, overrides: { ProcessManager: () => mockProcessManager, Cache: () => mockCache, @@ -95,7 +95,7 @@ void main() { [ideviceIdPath, '-l'], environment: {'DYLD_LIBRARY_PATH': libimobiledevicePath}, )).thenAnswer((_) => Future.value(ProcessResult(1, 0, 'foo', ''))); - expect(await iMobileDevice.getAvailableDeviceIDs(), 'foo'); + expect(await globals.iMobileDevice.getAvailableDeviceIDs(), 'foo'); }, overrides: { ProcessManager: () => mockProcessManager, Cache: () => mockCache, @@ -108,7 +108,7 @@ void main() { [ideviceInfoPath, '-u', 'foo', '-k', 'bar'], environment: {'DYLD_LIBRARY_PATH': libimobiledevicePath}, )).thenAnswer((_) => Future.value(ProcessResult(1, 255, 'No device found with udid foo, is it plugged in?', ''))); - expect(() async => await iMobileDevice.getInfoForDevice('foo', 'bar'), throwsA(isA())); + expect(() async => await globals.iMobileDevice.getInfoForDevice('foo', 'bar'), throwsA(isA())); }, overrides: { ProcessManager: () => mockProcessManager, Cache: () => mockCache, @@ -129,7 +129,7 @@ void main() { ); return Future.value(result); }); - expect(() async => await iMobileDevice.getInfoForDevice('foo', 'bar'), throwsA(isA())); + expect(() async => await globals.iMobileDevice.getInfoForDevice('foo', 'bar'), throwsA(isA())); }, overrides: { ProcessManager: () => mockProcessManager, Cache: () => mockCache, @@ -150,7 +150,7 @@ void main() { ); return Future.value(result); }); - expect(() async => await iMobileDevice.getInfoForDevice('foo', 'bar'), throwsToolExit()); + expect(() async => await globals.iMobileDevice.getInfoForDevice('foo', 'bar'), throwsToolExit()); }, overrides: { ProcessManager: () => mockProcessManager, Cache: () => mockCache, @@ -171,7 +171,7 @@ void main() { ); return Future.value(result); }); - expect(() async => await iMobileDevice.getInfoForDevice('foo', 'bar'), throwsA(isA())); + expect(() async => await globals.iMobileDevice.getInfoForDevice('foo', 'bar'), throwsA(isA())); }, overrides: { ProcessManager: () => mockProcessManager, Cache: () => mockCache, @@ -198,7 +198,7 @@ void main() { workingDirectory: null, )).thenAnswer((_) => Future.value(ProcessResult(4, 1, '', ''))); - expect(() async => await iMobileDevice.takeScreenshot(mockOutputFile), throwsA(anything)); + expect(() async => await globals.iMobileDevice.takeScreenshot(mockOutputFile), throwsA(anything)); }, overrides: { ProcessManager: () => mockProcessManager, Platform: () => osx, @@ -210,7 +210,7 @@ void main() { when(mockProcessManager.run(any, environment: anyNamed('environment'), workingDirectory: null)).thenAnswer( (Invocation invocation) => Future.value(ProcessResult(4, 0, '', ''))); - await iMobileDevice.takeScreenshot(mockOutputFile); + await globals.iMobileDevice.takeScreenshot(mockOutputFile); verify(mockProcessManager.run([idevicescreenshotPath, outputPath], environment: {'DYLD_LIBRARY_PATH': libimobiledevicePath}, workingDirectory: null,