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 """Provides a variety of device interactions based on adb. | 5 """Provides a variety of device interactions based on adb. |
| 6 | 6 |
| 7 Eventually, this will be based on adb_wrapper. | 7 Eventually, this will be based on adb_wrapper. |
| 8 """ | 8 """ |
| 9 # pylint: disable=W0613 | 9 # pylint: disable=W0613 |
| 10 | 10 |
| 11 import signal | |
| 11 import time | 12 import time |
| 12 | 13 |
| 13 import pylib.android_commands | 14 import pylib.android_commands |
| 14 from pylib.device import adb_wrapper | 15 from pylib.device import adb_wrapper |
| 15 from pylib.device import decorators | 16 from pylib.device import decorators |
| 16 from pylib.device import device_errors | 17 from pylib.device import device_errors |
| 17 from pylib.utils import apk_helper | 18 from pylib.utils import apk_helper |
| 18 from pylib.utils import parallelizer | 19 from pylib.utils import parallelizer |
| 19 | 20 |
| 20 _DEFAULT_TIMEOUT = 30 | 21 _DEFAULT_TIMEOUT = 30 |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 291 if check_return: | 292 if check_return: |
| 292 code, output = self.old_interface.GetShellCommandStatusAndOutput( | 293 code, output = self.old_interface.GetShellCommandStatusAndOutput( |
| 293 cmd, timeout_time=timeout) | 294 cmd, timeout_time=timeout) |
| 294 if int(code) != 0: | 295 if int(code) != 0: |
| 295 raise device_errors.CommandFailedError( | 296 raise device_errors.CommandFailedError( |
| 296 cmd, 'Nonzero exit code (%d)' % code) | 297 cmd, 'Nonzero exit code (%d)' % code) |
| 297 else: | 298 else: |
| 298 output = self.old_interface.RunShellCommand(cmd, timeout_time=timeout) | 299 output = self.old_interface.RunShellCommand(cmd, timeout_time=timeout) |
| 299 return output | 300 return output |
| 300 | 301 |
| 302 @decorators.WithTimeoutAndRetriesFromInstance() | |
| 303 def KillAll(self, process_name, signum=signal.SIGKILL, root=False, | |
| 304 blocking=False, timeout=None, retries=None): | |
| 305 """Kill all processes with the given name on the device. | |
|
frankf
2014/06/19 17:54:05
A general note: if it's not too expensive these me
jbudorick
2014/06/20 00:36:44
KillAll now raises a CommandFailedError if no proc
| |
| 306 | |
| 307 Args: | |
| 308 process_name: A string containing the name of the process to kill. | |
| 309 signum: An integer containing the signal number to send to kill. Defaults | |
| 310 to 9 (SIGKILL). | |
| 311 root: A boolean indicating whether the kill should be executed with root | |
|
frankf
2014/06/19 17:54:05
I'd rename "root" to "as_root" here and above
jbudorick
2014/06/20 00:36:44
Done.
| |
| 312 priveleges. | |
| 313 blocking: A boolean indicating whether we should wait until all processes | |
| 314 with the given |process_name| are dead. | |
| 315 timeout: Same as for |IsOnline|. | |
| 316 retries: Same as for |IsOnline|. | |
| 317 """ | |
| 318 if blocking: | |
| 319 self.old_interface.KillAllBlocking(process_name, signum=signum, | |
| 320 with_su=root, timeout_sec=timeout) | |
| 321 else: | |
| 322 self.old_interface.KillAll(process_name, signum=signum, with_su=root) | |
| 323 | |
| 324 @decorators.WithTimeoutAndRetriesFromInstance() | |
| 325 def StartActivity(self, intent, blocking=False, trace_file_name=None, | |
| 326 force_stop=False, timeout=None, retries=None): | |
| 327 """Start package's activity on the device. | |
| 328 | |
| 329 Args: | |
| 330 intent: An Intent to send. | |
| 331 blocking: A boolean indicating whether we should wait for the activity to | |
| 332 finish launching. | |
| 333 trace_file_name: If present, a string that both indicates that we want to | |
| 334 profile the activity and contains the path to which the | |
| 335 trace should be saved. | |
| 336 force_stop: A boolean indicating whether we should stop the activity | |
| 337 before starting it. | |
| 338 timeout: Same as for |IsOnline|. | |
| 339 retries: Same as for |IsOnline|. | |
| 340 """ | |
| 341 self.old_interface.StartActivity( | |
|
frankf
2014/06/19 17:54:05
what happens if you fail to start an activity?
jbudorick
2014/06/20 00:36:44
It was doing the same thing as AndroidCommands.Sta
| |
| 342 intent.package, intent.activity, wait_for_completion=blocking, | |
| 343 action=intent.action, category=intent.category, data=intent.data, | |
| 344 extras=intent.extras, trace_file_name=trace_file_name, | |
| 345 force_stop=force_stop, flags=intent.flags) | |
| 346 | |
| 347 @decorators.WithTimeoutAndRetriesFromInstance() | |
| 348 def BroadcastIntent(self, intent, timeout=None, retries=None): | |
| 349 """Send a broadcast intent. | |
| 350 | |
| 351 Args: | |
| 352 intent: An Intent to broadcast. | |
| 353 timeout: Same as for |IsOnline|. | |
| 354 retries: Same as for |IsOnline|. | |
| 355 """ | |
| 356 package, old_intent = intent.action.rsplit('.', 1) | |
| 357 if intent.extras is None: | |
| 358 args = [] | |
| 359 else: | |
| 360 args = ['-e %s%s' % (k, ' "%s"' % v if v else '') | |
| 361 for k, v in intent.extras.items() if len(k) > 0] | |
| 362 self.old_interface.BroadcastIntent(package, old_intent, *args) | |
| 363 | |
| 301 def __str__(self): | 364 def __str__(self): |
| 302 """Returns the device serial.""" | 365 """Returns the device serial.""" |
| 303 return self.old_interface.GetDevice() | 366 return self.old_interface.GetDevice() |
| 304 | 367 |
| 305 @staticmethod | 368 @staticmethod |
| 306 def parallel(devices=None, async=False): | 369 def parallel(devices=None, async=False): |
| 307 """Creates a Parallelizer to operate over the provided list of devices. | 370 """Creates a Parallelizer to operate over the provided list of devices. |
| 308 | 371 |
| 309 If |devices| is either |None| or an empty list, the Parallelizer will | 372 If |devices| is either |None| or an empty list, the Parallelizer will |
| 310 operate over all attached devices. | 373 operate over all attached devices. |
| 311 | 374 |
| 312 Args: | 375 Args: |
| 313 devices: A list of either DeviceUtils instances or objects from | 376 devices: A list of either DeviceUtils instances or objects from |
| 314 from which DeviceUtils instances can be constructed. If None, | 377 from which DeviceUtils instances can be constructed. If None, |
| 315 all attached devices will be used. | 378 all attached devices will be used. |
| 316 async: If true, returns a Parallelizer that runs operations | 379 async: If true, returns a Parallelizer that runs operations |
| 317 asynchronously. | 380 asynchronously. |
| 318 Returns: | 381 Returns: |
| 319 A Parallelizer operating over |devices|. | 382 A Parallelizer operating over |devices|. |
| 320 """ | 383 """ |
| 321 if not devices or len(devices) == 0: | 384 if not devices or len(devices) == 0: |
| 322 devices = pylib.android_commands.GetAttachedDevices() | 385 devices = pylib.android_commands.GetAttachedDevices() |
| 323 parallelizer_type = (parallelizer.Parallelizer if async | 386 parallelizer_type = (parallelizer.Parallelizer if async |
| 324 else parallelizer.SyncParallelizer) | 387 else parallelizer.SyncParallelizer) |
| 325 return parallelizer_type([ | 388 return parallelizer_type([ |
| 326 d if isinstance(d, DeviceUtils) else DeviceUtils(d) | 389 d if isinstance(d, DeviceUtils) else DeviceUtils(d) |
| 327 for d in devices]) | 390 for d in devices]) |
| 328 | 391 |
| OLD | NEW |