Chromium Code Reviews| 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 # pylint: disable=W0613 |
| 11 | 11 |
| 12 import multiprocessing | 12 import multiprocessing |
| 13 import os | 13 import os |
| 14 import sys | 14 import sys |
| 15 | 15 |
| 16 import pylib.android_commands | 16 import pylib.android_commands |
| 17 from pylib.device import adb_wrapper | 17 from pylib.device import adb_wrapper |
| 18 from pylib.device import decorators | 18 from pylib.device import decorators |
| 19 from pylib.device import device_errors | |
| 19 | 20 |
| 20 CHROME_SRC_DIR = os.path.abspath( | 21 CHROME_SRC_DIR = os.path.abspath( |
| 21 os.path.join(os.path.dirname(__file__), '..', '..', '..', '..')) | 22 os.path.join(os.path.dirname(__file__), '..', '..', '..', '..')) |
| 22 sys.path.append(os.path.join( | 23 sys.path.append(os.path.join( |
| 23 CHROME_SRC_DIR, 'third_party', 'android_testrunner')) | 24 CHROME_SRC_DIR, 'third_party', 'android_testrunner')) |
| 24 import errors | 25 import errors |
| 25 | 26 |
| 26 _DEFAULT_TIMEOUT = 30 | 27 _DEFAULT_TIMEOUT = 30 |
| 27 _DEFAULT_RETRIES = 3 | 28 _DEFAULT_RETRIES = 3 |
| 28 | 29 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 71 """ Restarts the adb server. | 72 """ Restarts the adb server. |
| 72 | 73 |
| 73 Raises: | 74 Raises: |
| 74 CommandFailedError if we fail to kill or restart the server. | 75 CommandFailedError if we fail to kill or restart the server. |
| 75 """ | 76 """ |
| 76 pylib.android_commands.AndroidCommands().RestartAdbServer() | 77 pylib.android_commands.AndroidCommands().RestartAdbServer() |
| 77 | 78 |
| 78 | 79 |
| 79 class DeviceUtils(object): | 80 class DeviceUtils(object): |
| 80 | 81 |
| 81 def __init__(self, device): | 82 def __init__(self, device, default_timeout=_DEFAULT_TIMEOUT, |
| 83 default_retries=_DEFAULT_RETRIES): | |
| 84 """ DeviceUtils constructor. | |
| 85 | |
| 86 Args: | |
| 87 device: Either a device serial, an existing AdbWrapper instance, an | |
| 88 an existing AndroidCommands instance, or nothing. | |
| 89 default_timeout: An integer containing the default number of seconds to | |
| 90 wait for an operation to complete if no explicit value | |
| 91 is provided. | |
| 92 default_retries: An integer containing the default number or times an | |
| 93 operation should be retried on failure if no explicit | |
| 94 value is provided. | |
| 95 """ | |
| 82 self.old_interface = None | 96 self.old_interface = None |
| 83 if isinstance(device, basestring): | 97 if isinstance(device, basestring): |
| 84 self.old_interface = pylib.android_commands.AndroidCommands(device) | 98 self.old_interface = pylib.android_commands.AndroidCommands(device) |
| 85 elif isinstance(device, adb_wrapper.AdbWrapper): | 99 elif isinstance(device, adb_wrapper.AdbWrapper): |
| 86 self.old_interface = pylib.android_commands.AndroidCommands(str(device)) | 100 self.old_interface = pylib.android_commands.AndroidCommands(str(device)) |
| 87 elif isinstance(device, pylib.android_commands.AndroidCommands): | 101 elif isinstance(device, pylib.android_commands.AndroidCommands): |
| 88 self.old_interface = device | 102 self.old_interface = device |
| 89 elif not device: | 103 elif not device: |
| 90 self.old_interface = pylib.android_commands.AndroidCommands() | 104 self.old_interface = pylib.android_commands.AndroidCommands() |
| 105 else: | |
| 106 raise device_errors.DeviceUnreachableError('Unexpected device type?') | |
|
craigdh
2014/05/14 20:33:08
This error message is confusing (is my physical de
jbudorick
2014/05/14 21:02:01
Done.
| |
| 107 self._default_timeout = default_timeout | |
| 108 self._default_retries = default_retries | |
| 91 | 109 |
| 110 @decorators.WithTimeoutAndRetriesFromInstance( | |
| 111 '_default_timeout', '_default_retries') | |
|
craigdh
2014/05/14 20:33:08
I think it would be fine to default to these value
jbudorick
2014/05/14 21:02:01
Holding for now, but I'm interested to hear what t
| |
| 112 def IsOnline(self, timeout=None, retries=None): | |
| 113 """ Checks whether the device is online. | |
| 114 | |
| 115 Args: | |
| 116 timeout: An integer containing the number of seconds to wait for the | |
| 117 operation to complete. | |
| 118 retries: An integer containing the number of times the operation should | |
| 119 be retried if it fails. | |
| 120 Returns: | |
| 121 True if the device is online, False otherwise. | |
| 122 """ | |
| 123 return self.old_interface.IsOnline() | |
| 124 | |
| 125 @decorators.WithTimeoutAndRetriesFromInstance( | |
| 126 '_default_timeout', '_default_retries') | |
| 127 def HasRoot(self, timeout=None, retries=None): | |
| 128 """ Checks whether or not adbd has root priveleges. | |
| 129 | |
| 130 Args: | |
| 131 timeout: An integer containing the number of seconds to wait for the | |
| 132 operation to complete. | |
| 133 retries: An integer containing the number of times the operation should | |
| 134 be retried if it fails. | |
| 135 Returns: | |
| 136 True if adbd has root priveleges, False otherwise. | |
| 137 """ | |
| 138 return self.old_interface.IsRootEnabled() | |
| 139 | |
| 140 @decorators.WithTimeoutAndRetriesFromInstance( | |
| 141 '_default_timeout', '_default_retries') | |
| 142 def EnableRoot(self, timeout=None, retries=None): | |
| 143 """ Restarts adbd with root priveleges. | |
| 144 | |
| 145 Args: | |
| 146 timeout: An integer containing the number of seconds to wait for the | |
| 147 operation to complete. | |
| 148 retries: An integer containing the number of times the operation should | |
| 149 be retried if it fails. | |
| 150 Raises: | |
| 151 CommandFailedError if root could not be enabled. | |
| 152 """ | |
| 153 if not self.old_interface.EnableAdbRoot(): | |
| 154 raise device_errors.CommandFailedError( | |
| 155 'adb root', 'Could not enable root.') | |
| 156 | |
| OLD | NEW |