| 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 |
| (...skipping 539 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 550 | 550 |
| 551 Args: | 551 Args: |
| 552 intent: An Intent to broadcast. | 552 intent: An Intent to broadcast. |
| 553 timeout: timeout in seconds | 553 timeout: timeout in seconds |
| 554 retries: number of retries | 554 retries: number of retries |
| 555 | 555 |
| 556 Raises: | 556 Raises: |
| 557 CommandTimeoutError on timeout. | 557 CommandTimeoutError on timeout. |
| 558 DeviceUnreachableError on missing device. | 558 DeviceUnreachableError on missing device. |
| 559 """ | 559 """ |
| 560 package, old_intent = intent.action.rsplit('.', 1) | 560 cmd = ['am', 'broadcast', '-a', intent.action] |
| 561 if intent.extras is None: | 561 if intent.extras: |
| 562 args = [] | 562 for key, value in intent.extras.iteritems(): |
| 563 else: | 563 if key: |
| 564 args = ['-e %s%s' % (k, ' "%s"' % v if v else '') | 564 cmd.extend(['-e', key]) |
| 565 for k, v in intent.extras.items() if len(k) > 0] | 565 if value: |
| 566 self.old_interface.BroadcastIntent(package, old_intent, *args) | 566 cmd.append(str(value)) |
| 567 self.RunShellCommand(cmd, check_return=True) |
| 567 | 568 |
| 568 @decorators.WithTimeoutAndRetriesFromInstance() | 569 @decorators.WithTimeoutAndRetriesFromInstance() |
| 569 def GoHome(self, timeout=None, retries=None): | 570 def GoHome(self, timeout=None, retries=None): |
| 570 """Return to the home screen. | 571 """Return to the home screen. |
| 571 | 572 |
| 572 Args: | 573 Args: |
| 573 timeout: timeout in seconds | 574 timeout: timeout in seconds |
| 574 retries: number of retries | 575 retries: number of retries |
| 575 | 576 |
| 576 Raises: | 577 Raises: |
| 577 CommandTimeoutError on timeout. | 578 CommandTimeoutError on timeout. |
| 578 DeviceUnreachableError on missing device. | 579 DeviceUnreachableError on missing device. |
| 579 """ | 580 """ |
| 580 self.old_interface.GoHome() | 581 self.old_interface.GoHome() |
| 581 | 582 |
| 582 @decorators.WithTimeoutAndRetriesFromInstance() | 583 @decorators.WithTimeoutAndRetriesFromInstance() |
| 583 def ForceStop(self, package, timeout=None, retries=None): | 584 def ForceStop(self, package, timeout=None, retries=None): |
| 584 """Close the application. | 585 """Close the application. |
| 585 | 586 |
| 586 Args: | 587 Args: |
| 587 package: A string containing the name of the package to stop. | 588 package: A string containing the name of the package to stop. |
| 588 timeout: timeout in seconds | 589 timeout: timeout in seconds |
| 589 retries: number of retries | 590 retries: number of retries |
| 590 | 591 |
| 591 Raises: | 592 Raises: |
| 592 CommandTimeoutError on timeout. | 593 CommandTimeoutError on timeout. |
| 593 DeviceUnreachableError on missing device. | 594 DeviceUnreachableError on missing device. |
| 594 """ | 595 """ |
| 595 self.old_interface.CloseApplication(package) | 596 self.RunShellCommand(['am', 'force-stop', package], check_return=True) |
| 596 | 597 |
| 597 @decorators.WithTimeoutAndRetriesFromInstance() | 598 @decorators.WithTimeoutAndRetriesFromInstance() |
| 598 def ClearApplicationState(self, package, timeout=None, retries=None): | 599 def ClearApplicationState(self, package, timeout=None, retries=None): |
| 599 """Clear all state for the given package. | 600 """Clear all state for the given package. |
| 600 | 601 |
| 601 Args: | 602 Args: |
| 602 package: A string containing the name of the package to stop. | 603 package: A string containing the name of the package to stop. |
| 603 timeout: timeout in seconds | 604 timeout: timeout in seconds |
| 604 retries: number of retries | 605 retries: number of retries |
| 605 | 606 |
| 606 Raises: | 607 Raises: |
| 607 CommandTimeoutError on timeout. | 608 CommandTimeoutError on timeout. |
| 608 DeviceUnreachableError on missing device. | 609 DeviceUnreachableError on missing device. |
| 609 """ | 610 """ |
| 610 self.old_interface.ClearApplicationState(package) | 611 # Check that the package exists before clearing it. Necessary because |
| 612 # calling pm clear on a package that doesn't exist may never return. |
| 613 if self.GetApplicationPath(package): |
| 614 self.RunShellCommand(['pm', 'clear', package], check_return=True) |
| 611 | 615 |
| 612 @decorators.WithTimeoutAndRetriesFromInstance() | 616 @decorators.WithTimeoutAndRetriesFromInstance() |
| 613 def SendKeyEvent(self, keycode, timeout=None, retries=None): | 617 def SendKeyEvent(self, keycode, timeout=None, retries=None): |
| 614 """Sends a keycode to the device. | 618 """Sends a keycode to the device. |
| 615 | 619 |
| 616 See: http://developer.android.com/reference/android/view/KeyEvent.html | 620 See: http://developer.android.com/reference/android/view/KeyEvent.html |
| 617 | 621 |
| 618 Args: | 622 Args: |
| 619 keycode: A integer keycode to send to the device. | 623 keycode: A integer keycode to send to the device. |
| 620 timeout: timeout in seconds | 624 timeout: timeout in seconds |
| 621 retries: number of retries | 625 retries: number of retries |
| 622 | 626 |
| 623 Raises: | 627 Raises: |
| 624 CommandTimeoutError on timeout. | 628 CommandTimeoutError on timeout. |
| 625 DeviceUnreachableError on missing device. | 629 DeviceUnreachableError on missing device. |
| 626 """ | 630 """ |
| 627 self.old_interface.SendKeyEvent(keycode) | 631 self.RunShellCommand(['input', 'keyevent', format(keycode, 'd')], |
| 632 check_return=True) |
| 628 | 633 |
| 629 PUSH_CHANGED_FILES_DEFAULT_TIMEOUT = 10 * _DEFAULT_TIMEOUT | 634 PUSH_CHANGED_FILES_DEFAULT_TIMEOUT = 10 * _DEFAULT_TIMEOUT |
| 630 PUSH_CHANGED_FILES_DEFAULT_RETRIES = _DEFAULT_RETRIES | 635 PUSH_CHANGED_FILES_DEFAULT_RETRIES = _DEFAULT_RETRIES |
| 631 | 636 |
| 632 @decorators.WithTimeoutAndRetriesDefaults( | 637 @decorators.WithTimeoutAndRetriesDefaults( |
| 633 PUSH_CHANGED_FILES_DEFAULT_TIMEOUT, | 638 PUSH_CHANGED_FILES_DEFAULT_TIMEOUT, |
| 634 PUSH_CHANGED_FILES_DEFAULT_RETRIES) | 639 PUSH_CHANGED_FILES_DEFAULT_RETRIES) |
| 635 def PushChangedFiles(self, host_device_tuples, timeout=None, | 640 def PushChangedFiles(self, host_device_tuples, timeout=None, |
| 636 retries=None): | 641 retries=None): |
| 637 """Push files to the device, skipping files that don't need updating. | 642 """Push files to the device, skipping files that don't need updating. |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 931 def Ls(self, device_path, timeout=None, retries=None): | 936 def Ls(self, device_path, timeout=None, retries=None): |
| 932 """Lists the contents of a directory on the device. | 937 """Lists the contents of a directory on the device. |
| 933 | 938 |
| 934 Args: | 939 Args: |
| 935 device_path: A string containing the path of the directory on the device | 940 device_path: A string containing the path of the directory on the device |
| 936 to list. | 941 to list. |
| 937 timeout: timeout in seconds | 942 timeout: timeout in seconds |
| 938 retries: number of retries | 943 retries: number of retries |
| 939 | 944 |
| 940 Returns: | 945 Returns: |
| 941 The contents of the directory specified by |device_path|. | 946 A list of pairs (filename, stat) for each file found in the directory, |
| 947 where the stat object has the properties: st_mode, st_size, and st_time. |
| 942 | 948 |
| 943 Raises: | 949 Raises: |
| 950 AdbCommandFailedError if |device_path| does not specify a valid and |
| 951 accessible directory in the device. |
| 944 CommandTimeoutError on timeout. | 952 CommandTimeoutError on timeout. |
| 945 DeviceUnreachableError on missing device. | 953 DeviceUnreachableError on missing device. |
| 946 """ | 954 """ |
| 947 return self.old_interface.ListPathContents(device_path) | 955 return self.adb.Ls(device_path) |
| 956 |
| 957 @decorators.WithTimeoutAndRetriesFromInstance() |
| 958 def Stat(self, device_path, timeout=None, retries=None): |
| 959 """Get the stat attributes of a file or directory on the device. |
| 960 |
| 961 Args: |
| 962 device_path: A string containing the path of from which to get attributes |
| 963 on the device. |
| 964 timeout: timeout in seconds |
| 965 retries: number of retries |
| 966 |
| 967 Returns: |
| 968 A stat object with the properties: st_mode, st_size, and st_time |
| 969 |
| 970 Raises: |
| 971 CommandFailedError if device_path cannot be found on the device. |
| 972 CommandTimeoutError on timeout. |
| 973 DeviceUnreachableError on missing device. |
| 974 """ |
| 975 dirname, target = device_path.rsplit('/', 1) |
| 976 for filename, stat in self.adb.Ls(dirname): |
| 977 if filename == target: |
| 978 return stat |
| 979 raise device_errors.CommandFailedError( |
| 980 'Cannot find file or directory: %r' % device_path, str(self)) |
| 948 | 981 |
| 949 @decorators.WithTimeoutAndRetriesFromInstance() | 982 @decorators.WithTimeoutAndRetriesFromInstance() |
| 950 def SetJavaAsserts(self, enabled, timeout=None, retries=None): | 983 def SetJavaAsserts(self, enabled, timeout=None, retries=None): |
| 951 """Enables or disables Java asserts. | 984 """Enables or disables Java asserts. |
| 952 | 985 |
| 953 Args: | 986 Args: |
| 954 enabled: A boolean indicating whether Java asserts should be enabled | 987 enabled: A boolean indicating whether Java asserts should be enabled |
| 955 or disabled. | 988 or disabled. |
| 956 timeout: timeout in seconds | 989 timeout: timeout in seconds |
| 957 retries: number of retries | 990 retries: number of retries |
| (...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1161 Returns: | 1194 Returns: |
| 1162 A Parallelizer operating over |devices|. | 1195 A Parallelizer operating over |devices|. |
| 1163 """ | 1196 """ |
| 1164 if not devices or len(devices) == 0: | 1197 if not devices or len(devices) == 0: |
| 1165 devices = pylib.android_commands.GetAttachedDevices() | 1198 devices = pylib.android_commands.GetAttachedDevices() |
| 1166 parallelizer_type = (parallelizer.Parallelizer if async | 1199 parallelizer_type = (parallelizer.Parallelizer if async |
| 1167 else parallelizer.SyncParallelizer) | 1200 else parallelizer.SyncParallelizer) |
| 1168 return parallelizer_type([ | 1201 return parallelizer_type([ |
| 1169 d if isinstance(d, DeviceUtils) else DeviceUtils(d) | 1202 d if isinstance(d, DeviceUtils) else DeviceUtils(d) |
| 1170 for d in devices]) | 1203 for d in devices]) |
| OLD | NEW |