Unverified Commit d85ea513 authored by Jenn Magder's avatar Jenn Magder Committed by GitHub

Move iMobileDevice to globals (#49724)

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