1# Copyright 2014 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 os
6import logging
7import re
8import subprocess
9
10from telemetry.core import platform
11from telemetry.internal.platform import device
12
13
14IOSSIM_BUILD_DIRECTORIES = [
15    'Debug-iphonesimulator',
16    'Profile-iphonesimulator',
17    'Release-iphonesimulator'
18]
19
20class IOSDevice(device.Device):
21  def __init__(self):
22    super(IOSDevice, self).__init__(name='ios', guid='ios')
23
24  @classmethod
25  def GetAllConnectedDevices(cls, blacklist):
26    return []
27
28
29def _IsIosDeviceAttached():
30  devices = subprocess.check_output('system_profiler SPUSBDataType', shell=True)
31  for line in devices.split('\n'):
32    if line and re.match(r'\s*(iPod|iPhone|iPad):', line):
33      return True
34  return False
35
36def _IsIosSimulatorAvailable(chrome_root):
37  """Determines whether an iOS simulator is present in the local checkout.
38
39  Assumes the iOS simulator (iossim) and Chromium have already been built.
40
41  Returns:
42    True if at least one simulator is found, otherwise False.
43  """
44  for build_dir in IOSSIM_BUILD_DIRECTORIES:
45    iossim_path = os.path.join(
46        chrome_root, 'out', build_dir, 'iossim')
47    chromium_path = os.path.join(
48        chrome_root, 'out', build_dir, 'Chromium.app')
49
50    # If the iOS simulator and Chromium app are present, return True
51    if os.path.exists(iossim_path) and os.path.exists(chromium_path):
52      return True
53
54  return False
55
56def FindAllAvailableDevices(options):
57  """Returns a list of available devices.
58  """
59  # TODO(baxley): Add support for all platforms possible. Probably Linux,
60  # probably not Windows.
61  if platform.GetHostPlatform().GetOSName() != 'mac':
62    return []
63
64  if options.chrome_root is None:
65    logging.warning('--chrome-root is not specified, skip iOS simulator tests.')
66    return []
67
68  if (not _IsIosDeviceAttached() and not
69      _IsIosSimulatorAvailable(options.chrome_root)):
70    return []
71
72  return [IOSDevice()]
73