Move simulator screenshot logic to use simctl (#8216)
* Move simulator screenshot logic to use simctl * Add simulator screenshot tests
This commit is contained in:
@@ -76,6 +76,12 @@ class Xcode {
|
||||
String _xcodeVersionText;
|
||||
String get xcodeVersionText => _xcodeVersionText;
|
||||
|
||||
int _xcodeMajorVersion;
|
||||
int get xcodeMajorVersion => _xcodeMajorVersion;
|
||||
|
||||
int _xcodeMinorVersion;
|
||||
int get xcodeMinorVersion => _xcodeMinorVersion;
|
||||
|
||||
final RegExp xcodeVersionRegex = new RegExp(r'Xcode ([0-9.]+)');
|
||||
|
||||
bool get xcodeVersionSatisfactory {
|
||||
@@ -85,10 +91,10 @@ class Xcode {
|
||||
String version = xcodeVersionRegex.firstMatch(xcodeVersionText).group(1);
|
||||
List<String> components = version.split('.');
|
||||
|
||||
int major = int.parse(components[0]);
|
||||
int minor = components.length == 1 ? 0 : int.parse(components[1]);
|
||||
_xcodeMajorVersion = int.parse(components[0]);
|
||||
_xcodeMinorVersion = components.length == 1 ? 0 : int.parse(components[1]);
|
||||
|
||||
return _xcodeVersionCheckValid(major, minor);
|
||||
return _xcodeVersionCheckValid(_xcodeMajorVersion, _xcodeMinorVersion);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -249,6 +249,10 @@ class SimControl {
|
||||
args.addAll(launchArgs);
|
||||
runCheckedSync(args);
|
||||
}
|
||||
|
||||
void takeScreenshot(String outputPath) {
|
||||
runCheckedSync(<String>[_xcrunPath, 'simctl', 'io', 'booted', 'screenshot', outputPath]);
|
||||
}
|
||||
}
|
||||
|
||||
/// Enumerates all data sections of `xcrun simctl list --json` command.
|
||||
@@ -582,44 +586,18 @@ class IOSSimulator extends Device {
|
||||
logFile.writeAsBytesSync(<int>[]);
|
||||
}
|
||||
|
||||
@override
|
||||
bool get supportsScreenshot => true;
|
||||
bool get _xcodeVersionSupportsScreenshot {
|
||||
return Xcode.instance.xcodeMajorVersion > 8 ||
|
||||
(Xcode.instance.xcodeMajorVersion == 8 && Xcode.instance.xcodeMinorVersion >= 2);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Null> takeScreenshot(File outputFile) async {
|
||||
Directory desktopDir = fs.directory(fs.path.join(homeDirPath, 'Desktop'));
|
||||
bool get supportsScreenshot => _xcodeVersionSupportsScreenshot;
|
||||
|
||||
// 'Simulator Screen Shot Mar 25, 2016, 2.59.43 PM.png'
|
||||
|
||||
Set<File> getScreenshots() {
|
||||
return new Set<File>.from(desktopDir.listSync().where((FileSystemEntity entity) {
|
||||
String name = fs.path.basename(entity.path);
|
||||
return entity is File && name.startsWith('Simulator') && name.endsWith('.png');
|
||||
}));
|
||||
}
|
||||
|
||||
Set<File> existingScreenshots = getScreenshots();
|
||||
|
||||
runSync(<String>[
|
||||
'osascript',
|
||||
'-e',
|
||||
'activate application "Simulator"\n'
|
||||
'tell application "System Events" to keystroke "s" using command down'
|
||||
]);
|
||||
|
||||
// There is some latency here from the applescript call.
|
||||
await new Future<Null>.delayed(new Duration(seconds: 1));
|
||||
|
||||
Set<File> shots = getScreenshots().difference(existingScreenshots);
|
||||
|
||||
if (shots.isEmpty) {
|
||||
printError('Unable to locate the screenshot file.');
|
||||
return false;
|
||||
}
|
||||
|
||||
File shot = shots.first;
|
||||
outputFile.writeAsBytesSync(shot.readAsBytesSync());
|
||||
shot.delete();
|
||||
@override
|
||||
Future<Null> takeScreenshot(File outputFile) {
|
||||
SimControl.instance.takeScreenshot(outputFile.path);
|
||||
return new Future<Null>.value();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user