1// Copyright 2018 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5import 'dart:async'; 6 7import '../base/platform.dart'; 8import '../base/process.dart'; 9import '../device.dart'; 10import '../emulator.dart'; 11import '../globals.dart'; 12import '../macos/xcode.dart'; 13import 'ios_workflow.dart'; 14import 'simulators.dart'; 15 16class IOSEmulators extends EmulatorDiscovery { 17 @override 18 bool get supportsPlatform => platform.isMacOS; 19 20 @override 21 bool get canListAnything => iosWorkflow.canListEmulators; 22 23 @override 24 Future<List<Emulator>> get emulators async => getEmulators(); 25} 26 27class IOSEmulator extends Emulator { 28 IOSEmulator(String id) : super(id, true); 29 30 @override 31 String get name => 'iOS Simulator'; 32 33 @override 34 String get manufacturer => 'Apple'; 35 36 @override 37 Category get category => Category.mobile; 38 39 @override 40 PlatformType get platformType => PlatformType.ios; 41 42 @override 43 Future<void> launch() async { 44 Future<bool> launchSimulator(List<String> additionalArgs) async { 45 final List<String> args = <String>['open'] 46 .followedBy(additionalArgs) 47 .followedBy(<String>['-a', xcode.getSimulatorPath()]) 48 .toList(); 49 50 final RunResult launchResult = await runAsync(args); 51 if (launchResult.exitCode != 0) { 52 printError('$launchResult'); 53 return false; 54 } 55 return true; 56 } 57 58 // First run with `-n` to force a device to boot if there isn't already one 59 if (!await launchSimulator(<String>['-n'])) 60 return; 61 62 // Run again to force it to Foreground (using -n doesn't force existing 63 // devices to the foreground) 64 await launchSimulator(<String>[]); 65 } 66} 67 68/// Return the list of iOS Simulators (there can only be zero or one). 69List<IOSEmulator> getEmulators() { 70 final String simulatorPath = xcode.getSimulatorPath(); 71 if (simulatorPath == null) { 72 return <IOSEmulator>[]; 73 } 74 75 return <IOSEmulator>[IOSEmulator(iosSimulatorId)]; 76} 77