| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """ | 5 """ |
| 6 Provides a variety of device interactions based on adb. | 6 Provides a variety of device interactions based on adb. |
| 7 | 7 |
| 8 Eventually, this will be based on adb_wrapper. | 8 Eventually, this will be based on adb_wrapper. |
| 9 """ | 9 """ |
| 10 # pylint: disable=W0613 |
| 10 | 11 |
| 11 import multiprocessing | 12 import multiprocessing |
| 12 import os | 13 import os |
| 13 import sys | 14 import sys |
| 15 |
| 14 import pylib.android_commands | 16 import pylib.android_commands |
| 15 from pylib.device import adb_wrapper | 17 from pylib.device import adb_wrapper |
| 18 from pylib.device import decorators |
| 16 | 19 |
| 17 CHROME_SRC_DIR = os.path.abspath( | 20 CHROME_SRC_DIR = os.path.abspath( |
| 18 os.path.join(os.path.dirname(__file__), '..', '..', '..', '..')) | 21 os.path.join(os.path.dirname(__file__), '..', '..', '..', '..')) |
| 19 sys.path.append(os.path.join( | 22 sys.path.append(os.path.join( |
| 20 CHROME_SRC_DIR, 'third_party', 'android_testrunner')) | 23 CHROME_SRC_DIR, 'third_party', 'android_testrunner')) |
| 21 import errors | 24 import errors |
| 22 | 25 |
| 23 def GetAVDs(): | 26 _DEFAULT_TIMEOUT = 30 |
| 24 return pylib.android_commands.GetAVDs() | 27 _DEFAULT_RETRIES = 3 |
| 25 | 28 |
| 26 | 29 |
| 27 # multiprocessing map_async requires a top-level function for pickle library. | 30 # multiprocessing map_async requires a top-level function for pickle library. |
| 28 def RebootDeviceSafe(device): | 31 def RebootDeviceSafe(device): |
| 29 """Reboot a device, wait for it to start, and squelch timeout exceptions.""" | 32 """Reboot a device, wait for it to start, and squelch timeout exceptions.""" |
| 30 try: | 33 try: |
| 31 DeviceUtils(device).old_interface.Reboot(True) | 34 DeviceUtils(device).old_interface.Reboot(True) |
| 32 except errors.DeviceUnresponsiveError as e: | 35 except errors.DeviceUnresponsiveError as e: |
| 33 return e | 36 return e |
| 34 | 37 |
| 35 | 38 |
| 36 def RebootDevices(): | 39 def RebootDevices(): |
| 37 """Reboot all attached and online devices.""" | 40 """Reboot all attached and online devices.""" |
| 38 devices = pylib.android_commands.GetAttachedDevices() | 41 devices = pylib.android_commands.GetAttachedDevices() |
| 39 print 'Rebooting: %s' % devices | 42 print 'Rebooting: %s' % devices |
| 40 if devices: | 43 if devices: |
| 41 pool = multiprocessing.Pool(len(devices)) | 44 pool = multiprocessing.Pool(len(devices)) |
| 42 results = pool.map_async(RebootDeviceSafe, devices).get(99999) | 45 results = pool.map_async(RebootDeviceSafe, devices).get(99999) |
| 43 | 46 |
| 44 for device, result in zip(devices, results): | 47 for device, result in zip(devices, results): |
| 45 if result: | 48 if result: |
| 46 print '%s failed to startup.' % device | 49 print '%s failed to startup.' % device |
| 47 | 50 |
| 48 if any(results): | 51 if any(results): |
| 49 print 'RebootDevices() Warning: %s' % results | 52 print 'RebootDevices() Warning: %s' % results |
| 50 else: | 53 else: |
| 51 print 'Reboots complete.' | 54 print 'Reboots complete.' |
| 52 | 55 |
| 53 | 56 |
| 57 @decorators.WithTimeoutAndRetriesDefaults( |
| 58 _DEFAULT_TIMEOUT, _DEFAULT_RETRIES) |
| 59 def GetAVDs(timeout=None, retries=None): |
| 60 """ Returns a list of Android Virtual Devices. |
| 61 |
| 62 Args: |
| 63 timeout: The length of time to wait before giving up on either killing or |
| 64 restarting the server. |
| 65 retries: The number of times to retry either killing or restarting the |
| 66 server. |
| 67 Returns: |
| 68 A list containing the configured AVDs. |
| 69 """ |
| 70 return pylib.android_commands.GetAVDs() |
| 71 |
| 72 |
| 73 @decorators.WithTimeoutAndRetriesDefaults( |
| 74 _DEFAULT_TIMEOUT, _DEFAULT_RETRIES) |
| 75 def RestartServer(timeout=None, retries=None): |
| 76 """ Restarts the adb server. |
| 77 |
| 78 Args: |
| 79 timeout: The length of time to wait before giving up on either killing or |
| 80 restarting the server. |
| 81 retries: The number of times to retry either killing or restarting the |
| 82 server. |
| 83 Raises: |
| 84 CommandFailedError if we fail to kill or restart the server. |
| 85 """ |
| 86 pylib.android_commands.AndroidCommands().RestartAdbServer() |
| 87 |
| 88 |
| 54 class DeviceUtils(object): | 89 class DeviceUtils(object): |
| 55 | 90 |
| 56 def __init__(self, device): | 91 def __init__(self, device): |
| 57 self.old_interface = None | 92 self.old_interface = None |
| 58 if isinstance(device, basestring): | 93 if isinstance(device, basestring): |
| 59 self.old_interface = pylib.android_commands.AndroidCommands(device) | 94 self.old_interface = pylib.android_commands.AndroidCommands(device) |
| 60 elif isinstance(device, adb_wrapper.AdbWrapper): | 95 elif isinstance(device, adb_wrapper.AdbWrapper): |
| 61 self.old_interface = pylib.android_commands.AndroidCommands(str(device)) | 96 self.old_interface = pylib.android_commands.AndroidCommands(str(device)) |
| 62 elif isinstance(device, pylib.android_commands.AndroidCommands): | 97 elif isinstance(device, pylib.android_commands.AndroidCommands): |
| 63 self.old_interface = device | 98 self.old_interface = device |
| 64 elif not device: | 99 elif not device: |
| 65 self.old_interface = pylib.android_commands.AndroidCommands() | 100 self.old_interface = pylib.android_commands.AndroidCommands() |
| 66 | 101 |
| OLD | NEW |