Move simulator screenshot logic to use simctl (#8216)

* Move simulator screenshot logic to use simctl

* Add simulator screenshot tests
diff --git a/packages/flutter_tools/lib/src/ios/simulators.dart b/packages/flutter_tools/lib/src/ios/simulators.dart
index 69143fd..ebf23f3 100644
--- a/packages/flutter_tools/lib/src/ios/simulators.dart
+++ b/packages/flutter_tools/lib/src/ios/simulators.dart
@@ -249,6 +249,10 @@
       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 @@
       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();
   }
 }