OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 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 an interface to communicate with the device via the adb command. | 5 """Provides an interface to communicate with the device via the adb command. |
6 | 6 |
7 Assumes adb binary is currently on system path. | 7 Assumes adb binary is currently on system path. |
8 """ | 8 """ |
9 # pylint: disable-all | 9 # pylint: disable-all |
10 | 10 |
(...skipping 739 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
750 """ | 750 """ |
751 pids = self.ExtractPid(process) | 751 pids = self.ExtractPid(process) |
752 if pids: | 752 if pids: |
753 cmd = 'kill -%d %s' % (signum, ' '.join(pids)) | 753 cmd = 'kill -%d %s' % (signum, ' '.join(pids)) |
754 if with_su: | 754 if with_su: |
755 self.RunShellCommandWithSU(cmd) | 755 self.RunShellCommandWithSU(cmd) |
756 else: | 756 else: |
757 self.RunShellCommand(cmd) | 757 self.RunShellCommand(cmd) |
758 return len(pids) | 758 return len(pids) |
759 | 759 |
760 def KillAllBlocking(self, process, timeout_sec): | 760 def KillAllBlocking(self, process, timeout_sec, signum=9, with_su=False): |
761 """Blocking version of killall, connected via adb. | 761 """Blocking version of killall, connected via adb. |
762 | 762 |
763 This waits until no process matching the corresponding name appears in ps' | 763 This waits until no process matching the corresponding name appears in ps' |
764 output anymore. | 764 output anymore. |
765 | 765 |
766 Args: | 766 Args: |
767 process: name of the process to kill off | 767 process: name of the process to kill off |
768 timeout_sec: the timeout in seconds | 768 timeout_sec: the timeout in seconds |
769 | 769 signum: same as |KillAll| |
| 770 with_su: same as |KillAll| |
770 Returns: | 771 Returns: |
771 the number of processes killed | 772 the number of processes killed |
772 """ | 773 """ |
773 processes_killed = self.KillAll(process) | 774 processes_killed = self.KillAll(process, signum=signum, with_su=with_su) |
774 if processes_killed: | 775 if processes_killed: |
775 elapsed = 0 | 776 elapsed = 0 |
776 wait_period = 0.1 | 777 wait_period = 0.1 |
777 # Note that this doesn't take into account the time spent in ExtractPid(). | 778 # Note that this doesn't take into account the time spent in ExtractPid(). |
778 while self.ExtractPid(process) and elapsed < timeout_sec: | 779 while self.ExtractPid(process) and elapsed < timeout_sec: |
779 time.sleep(wait_period) | 780 time.sleep(wait_period) |
780 elapsed += wait_period | 781 elapsed += wait_period |
781 if elapsed >= timeout_sec: | 782 if elapsed >= timeout_sec: |
782 return 0 | 783 return processes_killed - self.ExtractPid(process) |
783 return processes_killed | 784 return processes_killed |
784 | 785 |
785 @staticmethod | 786 @staticmethod |
786 def _GetActivityCommand(package, activity, wait_for_completion, action, | 787 def _GetActivityCommand(package, activity, wait_for_completion, action, |
787 category, data, extras, trace_file_name, force_stop, | 788 category, data, extras, trace_file_name, force_stop, |
788 flags): | 789 flags): |
789 """Creates command to start |package|'s activity on the device. | 790 """Creates command to start |package|'s activity on the device. |
790 | 791 |
791 Args - as for StartActivity | 792 Args - as for StartActivity |
792 | 793 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
835 activity: Name of activity (e.g. '.Main' or | 836 activity: Name of activity (e.g. '.Main' or |
836 'com.google.android.apps.chrome.Main'). | 837 'com.google.android.apps.chrome.Main'). |
837 wait_for_completion: wait for the activity to finish launching (-W flag). | 838 wait_for_completion: wait for the activity to finish launching (-W flag). |
838 action: string (e.g. "android.intent.action.MAIN"). Default is VIEW. | 839 action: string (e.g. "android.intent.action.MAIN"). Default is VIEW. |
839 category: string (e.g. "android.intent.category.HOME") | 840 category: string (e.g. "android.intent.category.HOME") |
840 data: Data string to pass to activity (e.g. 'http://www.example.com/'). | 841 data: Data string to pass to activity (e.g. 'http://www.example.com/'). |
841 extras: Dict of extras to pass to activity. Values are significant. | 842 extras: Dict of extras to pass to activity. Values are significant. |
842 trace_file_name: If used, turns on and saves the trace to this file name. | 843 trace_file_name: If used, turns on and saves the trace to this file name. |
843 force_stop: force stop the target app before starting the activity (-S | 844 force_stop: force stop the target app before starting the activity (-S |
844 flag). | 845 flag). |
| 846 Returns: |
| 847 The output of the underlying command as a list of lines. |
845 """ | 848 """ |
846 cmd = self._GetActivityCommand(package, activity, wait_for_completion, | 849 cmd = self._GetActivityCommand(package, activity, wait_for_completion, |
847 action, category, data, extras, | 850 action, category, data, extras, |
848 trace_file_name, force_stop, flags) | 851 trace_file_name, force_stop, flags) |
849 self.RunShellCommand(cmd) | 852 return self.RunShellCommand(cmd) |
850 | 853 |
851 def StartActivityTimed(self, package, activity, wait_for_completion=False, | 854 def StartActivityTimed(self, package, activity, wait_for_completion=False, |
852 action='android.intent.action.VIEW', | 855 action='android.intent.action.VIEW', |
853 category=None, data=None, | 856 category=None, data=None, |
854 extras=None, trace_file_name=None, | 857 extras=None, trace_file_name=None, |
855 force_stop=False, flags=None): | 858 force_stop=False, flags=None): |
856 """Starts |package|'s activity on the device, returning the start time | 859 """Starts |package|'s activity on the device, returning the start time |
857 | 860 |
858 Args - as for StartActivity | 861 Args - as for StartActivity |
859 | 862 |
860 Returns: | 863 Returns: |
861 a timestamp string for the time at which the activity started | 864 A tuple containing: |
| 865 - the output of the underlying command as a list of lines, and |
| 866 - a timestamp string for the time at which the activity started |
862 """ | 867 """ |
863 cmd = self._GetActivityCommand(package, activity, wait_for_completion, | 868 cmd = self._GetActivityCommand(package, activity, wait_for_completion, |
864 action, category, data, extras, | 869 action, category, data, extras, |
865 trace_file_name, force_stop, flags) | 870 trace_file_name, force_stop, flags) |
866 self.StartMonitoringLogcat() | 871 self.StartMonitoringLogcat() |
867 self.RunShellCommand('log starting activity; ' + cmd) | 872 out = self.RunShellCommand('log starting activity; ' + cmd) |
868 activity_started_re = re.compile('.*starting activity.*') | 873 activity_started_re = re.compile('.*starting activity.*') |
869 m = self.WaitForLogMatch(activity_started_re, None) | 874 m = self.WaitForLogMatch(activity_started_re, None) |
870 assert m | 875 assert m |
871 start_line = m.group(0) | 876 start_line = m.group(0) |
872 return GetLogTimestamp(start_line, self.GetDeviceYear()) | 877 return (out, GetLogTimestamp(start_line, self.GetDeviceYear())) |
873 | 878 |
874 def StartCrashUploadService(self, package): | 879 def StartCrashUploadService(self, package): |
875 # TODO(frankf): We really need a python wrapper around Intent | 880 # TODO(frankf): We really need a python wrapper around Intent |
876 # to be shared with StartActivity/BroadcastIntent. | 881 # to be shared with StartActivity/BroadcastIntent. |
877 cmd = ( | 882 cmd = ( |
878 'am startservice -a %s.crash.ACTION_FIND_ALL -n ' | 883 'am startservice -a %s.crash.ACTION_FIND_ALL -n ' |
879 '%s/%s.crash.MinidumpUploadService' % | 884 '%s/%s.crash.MinidumpUploadService' % |
880 (constants.PACKAGE_INFO['chrome'].package, | 885 (constants.PACKAGE_INFO['chrome'].package, |
881 package, | 886 package, |
882 constants.PACKAGE_INFO['chrome'].package)) | 887 constants.PACKAGE_INFO['chrome'].package)) |
(...skipping 1078 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1961 """ | 1966 """ |
1962 def __init__(self, output): | 1967 def __init__(self, output): |
1963 self._output = output | 1968 self._output = output |
1964 | 1969 |
1965 def write(self, data): | 1970 def write(self, data): |
1966 data = data.replace('\r\r\n', '\n') | 1971 data = data.replace('\r\r\n', '\n') |
1967 self._output.write(data) | 1972 self._output.write(data) |
1968 | 1973 |
1969 def flush(self): | 1974 def flush(self): |
1970 self._output.flush() | 1975 self._output.flush() |
OLD | NEW |