From 66502138af7da925c9ef83003e37c24d38768164 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Fri, 16 Jun 2017 14:33:49 -0700 Subject: [PATCH] Extract all libimobiledevice invocations to IMobileDevice class (#10793) Moves all remaining calls to tools that are part of the libimobiledevice suite of tools to the IMobileDevice class. This allows for better tracking of this dependency, and easier mocking in tests. --- .../flutter_tools/lib/src/ios/devices.dart | 12 +-- packages/flutter_tools/lib/src/ios/mac.dart | 8 ++ .../flutter_tools/test/ios/devices_test.dart | 77 ------------------- packages/flutter_tools/test/ios/mac_test.dart | 56 ++++++++++++++ 4 files changed, 67 insertions(+), 86 deletions(-) diff --git a/packages/flutter_tools/lib/src/ios/devices.dart b/packages/flutter_tools/lib/src/ios/devices.dart index 106174ce09..bb3eca7261 100644 --- a/packages/flutter_tools/lib/src/ios/devices.dart +++ b/packages/flutter_tools/lib/src/ios/devices.dart @@ -43,8 +43,6 @@ class IOSDevice extends Device { IOSDevice(String id, { this.name }) : super(id) { _installerPath = _checkForCommand('ideviceinstaller'); _iproxyPath = _checkForCommand('iproxy'); - _loggerPath = _checkForCommand('idevicesyslog'); - _screenshotPath = _checkForCommand('idevicescreenshot'); _pusherPath = _checkForCommand( 'ios-deploy', 'To copy files to iOS devices, please install ios-deploy. To install, run:\n' @@ -55,8 +53,6 @@ class IOSDevice extends Device { String _installerPath; String _iproxyPath; - String _loggerPath; - String _screenshotPath; String _pusherPath; @override @@ -335,12 +331,10 @@ class IOSDevice extends Device { } @override - bool get supportsScreenshot => _screenshotPath != null && _screenshotPath.isNotEmpty; + bool get supportsScreenshot => iMobileDevice.isInstalled; @override - Future takeScreenshot(File outputFile) { - return runCheckedAsync([_screenshotPath, outputFile.path]); - } + Future takeScreenshot(File outputFile) => iMobileDevice.takeScreenshot(outputFile); } class _IOSDeviceLogReader extends DeviceLogReader { @@ -372,7 +366,7 @@ class _IOSDeviceLogReader extends DeviceLogReader { String get name => device.name; void _start() { - runCommand([device._loggerPath]).then((Process process) { + iMobileDevice.startLogger().then((Process process) { _process = process; _process.stdout.transform(UTF8.decoder).transform(const LineSplitter()).listen(_onLine); _process.stderr.transform(UTF8.decoder).transform(const LineSplitter()).listen(_onLine); diff --git a/packages/flutter_tools/lib/src/ios/mac.dart b/packages/flutter_tools/lib/src/ios/mac.dart index a646650620..7d1bfa6022 100644 --- a/packages/flutter_tools/lib/src/ios/mac.dart +++ b/packages/flutter_tools/lib/src/ios/mac.dart @@ -84,6 +84,14 @@ class IMobileDevice { String getInfoForDevice(String deviceID, String key) { return runSync(['ideviceinfo', '-k', key, '-u', deviceID]).trim(); } + + /// Starts `idevicesyslog` and returns the running process. + Future startLogger() => runCommand(['idevicesyslog']); + + /// Captures a screenshot to the specified outputfile. + Future takeScreenshot(File outputFile) { + return runCheckedAsync(['idevicescreenshot', outputFile.path]); + } } class Xcode { diff --git a/packages/flutter_tools/test/ios/devices_test.dart b/packages/flutter_tools/test/ios/devices_test.dart index fda5250f7a..4e2726874b 100644 --- a/packages/flutter_tools/test/ios/devices_test.dart +++ b/packages/flutter_tools/test/ios/devices_test.dart @@ -2,9 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'dart:async'; -import 'dart:io' show ProcessResult; - import 'package:file/file.dart'; import 'package:flutter_tools/src/base/file_system.dart'; import 'package:flutter_tools/src/ios/devices.dart'; @@ -68,78 +65,4 @@ void main() { }); }); - group('screenshot', () { - MockProcessManager mockProcessManager; - MockFile mockOutputFile; - IOSDevice iosDeviceUnderTest; - - setUp(() { - mockProcessManager = new MockProcessManager(); - mockOutputFile = new MockFile(); - }); - - testUsingContext('error if idevicescreenshot is not installed', () async { - when(mockOutputFile.path).thenReturn(fs.path.join('some', 'test', 'path', 'image.png')); - // Let everything else return exit code 0 so process.dart doesn't crash. - // The matcher order is important. - when( - mockProcessManager.run(any, environment: null, workingDirectory: null) - ).thenReturn( - new Future.value(new ProcessResult(2, 0, '', '')) - ); - // Let `which idevicescreenshot` fail with exit code 1. - when( - mockProcessManager.runSync( - ['which', 'idevicescreenshot'], environment: null, workingDirectory: null) - ).thenReturn( - new ProcessResult(1, 1, '', '') - ); - - iosDeviceUnderTest = new IOSDevice('1234'); - await iosDeviceUnderTest.takeScreenshot(mockOutputFile); - verify(mockProcessManager.runSync( - ['which', 'idevicescreenshot'], environment: null, workingDirectory: null)); - verifyNever(mockProcessManager.run( - ['idevicescreenshot', fs.path.join('some', 'test', 'path', 'image.png')], - environment: null, - workingDirectory: null - )); - expect(testLogger.errorText, contains('brew install ideviceinstaller')); - }, - overrides: { - ProcessManager: () => mockProcessManager, - Platform: () => osx, - }); - - testUsingContext('idevicescreenshot captures and returns screenshot', () async { - when(mockOutputFile.path).thenReturn(fs.path.join('some', 'test', 'path', 'image.png')); - // Let everything else return exit code 0. - // The matcher order is important. - when( - mockProcessManager.run(any, environment: null, workingDirectory: null) - ).thenReturn( - new Future.value(new ProcessResult(4, 0, '', '')) - ); - // Let there be idevicescreenshot in the PATH. - when( - mockProcessManager.runSync( - ['which', 'idevicescreenshot'], environment: null, workingDirectory: null) - ).thenReturn( - new ProcessResult(3, 0, fs.path.join('some', 'path', 'to', 'iscreenshot'), '') - ); - - iosDeviceUnderTest = new IOSDevice('1234'); - await iosDeviceUnderTest.takeScreenshot(mockOutputFile); - verify(mockProcessManager.runSync( - ['which', 'idevicescreenshot'], environment: null, workingDirectory: null)); - verify(mockProcessManager.run( - [ - fs.path.join('some', 'path', 'to', 'iscreenshot'), - fs.path.join('some', 'test', 'path', 'image.png') - ], - environment: null, - workingDirectory: null - )); - }, overrides: {ProcessManager: () => mockProcessManager}); - }); } diff --git a/packages/flutter_tools/test/ios/mac_test.dart b/packages/flutter_tools/test/ios/mac_test.dart index e267b05ad8..69d8dc2947 100644 --- a/packages/flutter_tools/test/ios/mac_test.dart +++ b/packages/flutter_tools/test/ios/mac_test.dart @@ -2,13 +2,69 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:async'; + +import 'package:file/file.dart'; import 'package:flutter_tools/src/application_package.dart'; +import 'package:flutter_tools/src/base/file_system.dart'; +import 'package:flutter_tools/src/base/io.dart' show ProcessResult; import 'package:flutter_tools/src/ios/mac.dart'; +import 'package:mockito/mockito.dart'; +import 'package:platform/platform.dart'; +import 'package:process/process.dart'; import 'package:test/test.dart'; import '../src/context.dart'; +class MockProcessManager extends Mock implements ProcessManager {} +class MockFile extends Mock implements File {} + void main() { + final FakePlatform osx = new FakePlatform.fromPlatform(const LocalPlatform()); + osx.operatingSystem = 'macos'; + + group('IMobileDevice', () { + group('screenshot', () { + final String outputPath = fs.path.join('some', 'test', 'path', 'image.png'); + MockProcessManager mockProcessManager; + MockFile mockOutputFile; + + setUp(() { + mockProcessManager = new MockProcessManager(); + mockOutputFile = new MockFile(); + }); + + testUsingContext('error if idevicescreenshot is not installed', () async { + when(mockOutputFile.path).thenReturn(outputPath); + + // Let `idevicescreenshot` fail with exit code 1. + when(mockProcessManager.run(['idevicescreenshot', outputPath], + environment: null, + workingDirectory: null + )).thenReturn(new ProcessResult(4, 1, '', '')); + + expect(() async => await iMobileDevice.takeScreenshot(mockOutputFile), throwsA(anything)); + }, overrides: { + ProcessManager: () => mockProcessManager, + Platform: () => osx, + }); + + testUsingContext('idevicescreenshot captures and returns screenshot', () async { + when(mockOutputFile.path).thenReturn(outputPath); + when(mockProcessManager.run(any, environment: null, workingDirectory: null)) + .thenReturn(new Future.value(new ProcessResult(4, 0, '', ''))); + + await iMobileDevice.takeScreenshot(mockOutputFile); + verify(mockProcessManager.run(['idevicescreenshot', outputPath], + environment: null, + workingDirectory: null + )); + }, overrides: { + ProcessManager: () => mockProcessManager, + }); + }); + }); + group('Diagnose Xcode build failure', () { BuildableIOSApp app;