ios_emulators.dart 1.87 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// Copyright 2018 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:async';

import '../base/platform.dart';
import '../base/process.dart';
import '../emulator.dart';
import '../globals.dart';
11
import '../ios/mac.dart';
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
import 'ios_workflow.dart';

class IOSEmulators extends EmulatorDiscovery {
  @override
  bool get supportsPlatform => platform.isMacOS;

  @override
  bool get canListAnything => iosWorkflow.canListEmulators;

  @override
  Future<List<Emulator>> get emulators async => getEmulators();
}

class IOSEmulator extends Emulator {
  IOSEmulator(String id) : super(id, true);

  @override
  String get name => 'iOS Simulator';

  @override
  String get manufacturer => 'Apple';

  @override
35
  String get label => null;
36 37

  @override
38
  Future<void> launch() async {
39
    Future<bool> launchSimulator(List<String> additionalArgs) async {
40 41
      final List<String> args = <String>['open']
          .followedBy(additionalArgs)
42 43
          .followedBy(<String>['-a', xcode.getSimulatorPath()])
          .toList();
44 45 46 47 48 49 50

      final RunResult launchResult = await runAsync(args);
      if (launchResult.exitCode != 0) {
        printError('$launchResult');
        return false;
      }
      return true;
51
    }
52 53 54

    // First run with `-n` to force a device to boot if there isn't already one
    if (!await launchSimulator(<String>['-n']))
55 56
      return;

57 58
    // Run again to force it to Foreground (using -n doesn't force existing
    // devices to the foreground)
59
    await launchSimulator(<String>[]);
60 61 62 63 64
  }
}

/// Return the list of iOS Simulators (there can only be zero or one).
List<IOSEmulator> getEmulators() {
65
  final String simulatorPath = xcode.getSimulatorPath();
66 67 68 69
  if (simulatorPath == null) {
    return <IOSEmulator>[];
  }

70
  return <IOSEmulator>[IOSEmulator('apple_ios_simulator')];
71
}