| 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 | 9 |
| 10 import collections | 10 import collections |
| (...skipping 617 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 628 wait_period = 0.1 | 628 wait_period = 0.1 |
| 629 # Note that this doesn't take into account the time spent in ExtractPid(). | 629 # Note that this doesn't take into account the time spent in ExtractPid(). |
| 630 while self.ExtractPid(process) and elapsed < timeout_sec: | 630 while self.ExtractPid(process) and elapsed < timeout_sec: |
| 631 time.sleep(wait_period) | 631 time.sleep(wait_period) |
| 632 elapsed += wait_period | 632 elapsed += wait_period |
| 633 if elapsed >= timeout_sec: | 633 if elapsed >= timeout_sec: |
| 634 return 0 | 634 return 0 |
| 635 return processes_killed | 635 return processes_killed |
| 636 | 636 |
| 637 def _GetActivityCommand(self, package, activity, wait_for_completion, action, | 637 def _GetActivityCommand(self, package, activity, wait_for_completion, action, |
| 638 category, data, extras, trace_file_name, force_stop): | 638 category, data, extras, trace_file_name, force_stop, |
| 639 flags): |
| 639 """Creates command to start |package|'s activity on the device. | 640 """Creates command to start |package|'s activity on the device. |
| 640 | 641 |
| 641 Args - as for StartActivity | 642 Args - as for StartActivity |
| 642 | 643 |
| 643 Returns: | 644 Returns: |
| 644 the command to run on the target to start the activity | 645 the command to run on the target to start the activity |
| 645 """ | 646 """ |
| 646 cmd = 'am start -a %s' % action | 647 cmd = 'am start -a %s' % action |
| 647 if force_stop: | 648 if force_stop: |
| 648 cmd += ' -S' | 649 cmd += ' -S' |
| (...skipping 13 matching lines...) Expand all Loading... |
| 662 elif isinstance(value, bool): | 663 elif isinstance(value, bool): |
| 663 cmd += ' --ez' | 664 cmd += ' --ez' |
| 664 elif isinstance(value, int): | 665 elif isinstance(value, int): |
| 665 cmd += ' --ei' | 666 cmd += ' --ei' |
| 666 else: | 667 else: |
| 667 raise NotImplementedError( | 668 raise NotImplementedError( |
| 668 'Need to teach StartActivity how to pass %s extras' % type(value)) | 669 'Need to teach StartActivity how to pass %s extras' % type(value)) |
| 669 cmd += ' %s %s' % (key, value) | 670 cmd += ' %s %s' % (key, value) |
| 670 if trace_file_name: | 671 if trace_file_name: |
| 671 cmd += ' --start-profiler ' + trace_file_name | 672 cmd += ' --start-profiler ' + trace_file_name |
| 673 if flags: |
| 674 cmd += ' -f %s' % flags |
| 672 return cmd | 675 return cmd |
| 673 | 676 |
| 674 def StartActivity(self, package, activity, wait_for_completion=False, | 677 def StartActivity(self, package, activity, wait_for_completion=False, |
| 675 action='android.intent.action.VIEW', | 678 action='android.intent.action.VIEW', |
| 676 category=None, data=None, | 679 category=None, data=None, |
| 677 extras=None, trace_file_name=None, | 680 extras=None, trace_file_name=None, |
| 678 force_stop=False): | 681 force_stop=False, flags=None): |
| 679 """Starts |package|'s activity on the device. | 682 """Starts |package|'s activity on the device. |
| 680 | 683 |
| 681 Args: | 684 Args: |
| 682 package: Name of package to start (e.g. 'com.google.android.apps.chrome'). | 685 package: Name of package to start (e.g. 'com.google.android.apps.chrome'). |
| 683 activity: Name of activity (e.g. '.Main' or | 686 activity: Name of activity (e.g. '.Main' or |
| 684 'com.google.android.apps.chrome.Main'). | 687 'com.google.android.apps.chrome.Main'). |
| 685 wait_for_completion: wait for the activity to finish launching (-W flag). | 688 wait_for_completion: wait for the activity to finish launching (-W flag). |
| 686 action: string (e.g. "android.intent.action.MAIN"). Default is VIEW. | 689 action: string (e.g. "android.intent.action.MAIN"). Default is VIEW. |
| 687 category: string (e.g. "android.intent.category.HOME") | 690 category: string (e.g. "android.intent.category.HOME") |
| 688 data: Data string to pass to activity (e.g. 'http://www.example.com/'). | 691 data: Data string to pass to activity (e.g. 'http://www.example.com/'). |
| 689 extras: Dict of extras to pass to activity. Values are significant. | 692 extras: Dict of extras to pass to activity. Values are significant. |
| 690 trace_file_name: If used, turns on and saves the trace to this file name. | 693 trace_file_name: If used, turns on and saves the trace to this file name. |
| 691 force_stop: force stop the target app before starting the activity (-S | 694 force_stop: force stop the target app before starting the activity (-S |
| 692 flag). | 695 flag). |
| 693 """ | 696 """ |
| 694 cmd = self._GetActivityCommand(package, activity, wait_for_completion, | 697 cmd = self._GetActivityCommand(package, activity, wait_for_completion, |
| 695 action, category, data, extras, | 698 action, category, data, extras, |
| 696 trace_file_name, force_stop) | 699 trace_file_name, force_stop, flags) |
| 697 self.RunShellCommand(cmd) | 700 self.RunShellCommand(cmd) |
| 698 | 701 |
| 699 def StartActivityTimed(self, package, activity, wait_for_completion=False, | 702 def StartActivityTimed(self, package, activity, wait_for_completion=False, |
| 700 action='android.intent.action.VIEW', | 703 action='android.intent.action.VIEW', |
| 701 category=None, data=None, | 704 category=None, data=None, |
| 702 extras=None, trace_file_name=None, | 705 extras=None, trace_file_name=None, |
| 703 force_stop=False): | 706 force_stop=False, flags=None): |
| 704 """Starts |package|'s activity on the device, returning the start time | 707 """Starts |package|'s activity on the device, returning the start time |
| 705 | 708 |
| 706 Args - as for StartActivity | 709 Args - as for StartActivity |
| 707 | 710 |
| 708 Returns: | 711 Returns: |
| 709 a timestamp string for the time at which the activity started | 712 a timestamp string for the time at which the activity started |
| 710 """ | 713 """ |
| 711 cmd = self._GetActivityCommand(package, activity, wait_for_completion, | 714 cmd = self._GetActivityCommand(package, activity, wait_for_completion, |
| 712 action, category, data, extras, | 715 action, category, data, extras, |
| 713 trace_file_name, force_stop) | 716 trace_file_name, force_stop, flags) |
| 714 self.StartMonitoringLogcat() | 717 self.StartMonitoringLogcat() |
| 715 self.RunShellCommand('log starting activity; ' + cmd) | 718 self.RunShellCommand('log starting activity; ' + cmd) |
| 716 activity_started_re = re.compile('.*starting activity.*') | 719 activity_started_re = re.compile('.*starting activity.*') |
| 717 m = self.WaitForLogMatch(activity_started_re, None) | 720 m = self.WaitForLogMatch(activity_started_re, None) |
| 718 assert m | 721 assert m |
| 719 start_line = m.group(0) | 722 start_line = m.group(0) |
| 720 return GetLogTimestamp(start_line, self.GetDeviceYear()) | 723 return GetLogTimestamp(start_line, self.GetDeviceYear()) |
| 721 | 724 |
| 722 def GoHome(self): | 725 def GoHome(self): |
| 723 """Tell the device to return to the home screen. Blocks until completion.""" | 726 """Tell the device to return to the home screen. Blocks until completion.""" |
| (...skipping 818 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1542 """ | 1545 """ |
| 1543 def __init__(self, output): | 1546 def __init__(self, output): |
| 1544 self._output = output | 1547 self._output = output |
| 1545 | 1548 |
| 1546 def write(self, data): | 1549 def write(self, data): |
| 1547 data = data.replace('\r\r\n', '\n') | 1550 data = data.replace('\r\r\n', '\n') |
| 1548 self._output.write(data) | 1551 self._output.write(data) |
| 1549 | 1552 |
| 1550 def flush(self): | 1553 def flush(self): |
| 1551 self._output.flush() | 1554 self._output.flush() |
| OLD | NEW |