diff --git a/packages/flutter_tools/lib/src/ios/devices.dart b/packages/flutter_tools/lib/src/ios/devices.dart index f15a9db9f9..cbe7a2aca0 100644 --- a/packages/flutter_tools/lib/src/ios/devices.dart +++ b/packages/flutter_tools/lib/src/ios/devices.dart @@ -44,6 +44,7 @@ class IOSDevice extends Device { _iproxyPath = _checkForCommand('iproxy'); _debuggerPath = _checkForCommand('idevicedebug'); _loggerPath = _checkForCommand('idevicesyslog'); + _screenshotPath = _checkForCommand('idevicescreenshot'); _pusherPath = _checkForCommand( 'ios-deploy', 'To copy files to iOS devices, please install ios-deploy. ' @@ -70,6 +71,9 @@ class IOSDevice extends Device { String _loggerPath; String get loggerPath => _loggerPath; + String _screenshotPath; + String get screenshotPath => _screenshotPath; + String _pusherPath; String get pusherPath => _pusherPath; @@ -118,23 +122,21 @@ class IOSDevice extends Device { return runSync([informerPath, '-k', infoKey, '-u', deviceID]).trim(); } - static final Map _commandMap = {}; static String _checkForCommand( String command, [ String macInstructions = _ideviceinstallerInstructions ]) { - return _commandMap.putIfAbsent(command, () { - try { - command = runCheckedSync(['which', command]).trim(); - } catch (e) { - if (p.platform.isMacOS) { - printError('$command not found. $macInstructions'); - } else { - printError('Cannot control iOS devices or simulators. $command is not available on your platform.'); - } + try { + command = runCheckedSync(['which', command]).trim(); + } catch (e) { + if (p.platform.isMacOS) { + printError('$command not found. $macInstructions'); + } else { + printError('Cannot control iOS devices or simulators. $command is not available on your platform.'); } - return command; - }); + return null; + } + return command; } @override @@ -364,14 +366,12 @@ class IOSDevice extends Device { } @override - bool get supportsScreenshot => false; + bool get supportsScreenshot => screenshotPath != null && screenshotPath.isNotEmpty; @override Future takeScreenshot(File outputFile) { - // We could use idevicescreenshot here (installed along with the brew - // ideviceinstaller tools). It however requires a developer disk image on - // the device. - return new Future.error('Taking screenshots is not supported on iOS devices. Consider using a simulator instead.'); + runCheckedSync([screenshotPath, outputFile.path]); + return new Future.value(); } } diff --git a/packages/flutter_tools/test/src/ios/devices_test.dart b/packages/flutter_tools/test/src/ios/devices_test.dart new file mode 100644 index 0000000000..f92e9f9887 --- /dev/null +++ b/packages/flutter_tools/test/src/ios/devices_test.dart @@ -0,0 +1,97 @@ +// Copyright 2017 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +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'; + +import 'package:mockito/mockito.dart'; +import 'package:process/process.dart'; +import 'package:test/test.dart'; + +import '../context.dart'; +class MockProcessManager extends Mock implements ProcessManager {} +class MockFile extends Mock implements File {} + +void main() { + group('test screenshot', () { + MockProcessManager mockProcessManager; + MockFile mockOutputFile; + IOSDevice iosDeviceUnderTest; + + setUp(() { + mockProcessManager = new MockProcessManager(); + mockOutputFile = new MockFile(); + }); + + testUsingContext( + 'screenshot without ideviceinstaller error', + () 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.runSync(any, environment: null, workingDirectory: null) + ).thenReturn( + new ProcessResult(2, 0, '', null) + ); + // Let `which idevicescreenshot` fail with exit code 1. + when( + mockProcessManager.runSync( + ['which', 'idevicescreenshot'], environment: null, workingDirectory: null) + ).thenReturn( + new ProcessResult(1, 1, '', null) + ); + + iosDeviceUnderTest = new IOSDevice('1234'); + iosDeviceUnderTest.takeScreenshot(mockOutputFile); + verify(mockProcessManager.runSync( + ['which', 'idevicescreenshot'], environment: null, workingDirectory: null)); + verifyNever(mockProcessManager.runSync( + ['idevicescreenshot', fs.path.join('some', 'test', 'path', 'image.png')], + environment: null, + workingDirectory: null + )); + expect(testLogger.errorText, contains('brew install ideviceinstaller')); + }, + overrides: {ProcessManager: () => mockProcessManager} + ); + + testUsingContext( + 'screenshot with ideviceinstaller gets command', + () 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.runSync(any, environment: null, workingDirectory: null) + ).thenReturn( + new ProcessResult(4, 0, '', null) + ); + // 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'), null) + ); + + iosDeviceUnderTest = new IOSDevice('1234'); + iosDeviceUnderTest.takeScreenshot(mockOutputFile); + verify(mockProcessManager.runSync( + ['which', 'idevicescreenshot'], environment: null, workingDirectory: null)); + verify(mockProcessManager.runSync( + [ + 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/src/ios/simulators_test.dart b/packages/flutter_tools/test/src/ios/simulators_test.dart index 37202e0a8e..0b26ec4f48 100644 --- a/packages/flutter_tools/test/src/ios/simulators_test.dart +++ b/packages/flutter_tools/test/src/ios/simulators_test.dart @@ -1,6 +1,7 @@ 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/mac.dart'; import 'package:flutter_tools/src/ios/simulators.dart'; import 'package:mockito/mockito.dart'; @@ -122,7 +123,7 @@ void main() { when(mockXcode.xcodeMinorVersion).thenReturn(1); expect(deviceUnderTest.supportsScreenshot, false); }, - overrides: { Xcode: () => mockXcode } + overrides: {Xcode: () => mockXcode} ); testUsingContext( @@ -132,7 +133,7 @@ void main() { when(mockXcode.xcodeMinorVersion).thenReturn(2); expect(deviceUnderTest.supportsScreenshot, true); MockFile mockFile = new MockFile(); - when(mockFile.path).thenReturn('/some/path/to/screenshot.png'); + when(mockFile.path).thenReturn(fs.path.join('some', 'path', 'to', 'screenshot.png')); deviceUnderTest.takeScreenshot(mockFile); verify(mockProcessManager.runSync( [ @@ -141,7 +142,7 @@ void main() { 'io', 'booted', 'screenshot', - '/some/path/to/screenshot.png' + fs.path.join('some', 'path', 'to', 'screenshot.png'), ], environment: null, workingDirectory: null