Chromium Code Reviews| 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 512 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 523 elapsed = 0 | 523 elapsed = 0 |
| 524 wait_period = 0.1 | 524 wait_period = 0.1 |
| 525 # Note that this doesn't take into account the time spent in ExtractPid(). | 525 # Note that this doesn't take into account the time spent in ExtractPid(). |
| 526 while self.ExtractPid(process) and elapsed < timeout_sec: | 526 while self.ExtractPid(process) and elapsed < timeout_sec: |
| 527 time.sleep(wait_period) | 527 time.sleep(wait_period) |
| 528 elapsed += wait_period | 528 elapsed += wait_period |
| 529 if elapsed >= timeout_sec: | 529 if elapsed >= timeout_sec: |
| 530 return 0 | 530 return 0 |
| 531 return processes_killed | 531 return processes_killed |
| 532 | 532 |
| 533 def StartActivity(self, package, activity, wait_for_completion=False, | 533 def _ActivityCmd(self, package, activity, wait_for_completion, action, |
|
Sami
2013/01/29 15:07:59
Bikeshed: _GetActivityCommand?
aberent
2013/01/29 17:02:18
Done.
| |
| 534 action='android.intent.action.VIEW', | 534 category, data, extras, trace_file_name, force_stop): |
| 535 category=None, data=None, | 535 """Creates command to start |package|'s activity on the device. |
| 536 extras=None, trace_file_name=None, | |
| 537 force_stop=False): | |
| 538 """Starts |package|'s activity on the device. | |
| 539 | 536 |
| 540 Args: | 537 Args: |
| 541 package: Name of package to start (e.g. 'com.google.android.apps.chrome'). | 538 package: Name of package to start (e.g. 'com.google.android.apps.chrome'). |
| 542 activity: Name of activity (e.g. '.Main' or | 539 activity: Name of activity (e.g. '.Main' or |
| 543 'com.google.android.apps.chrome.Main'). | 540 'com.google.android.apps.chrome.Main'). |
| 544 wait_for_completion: wait for the activity to finish launching (-W flag). | 541 wait_for_completion: wait for the activity to finish launching (-W flag). |
| 545 action: string (e.g. "android.intent.action.MAIN"). Default is VIEW. | 542 action: string (e.g. "android.intent.action.MAIN"). Default is VIEW. |
| 546 category: string (e.g. "android.intent.category.HOME") | 543 category: string (e.g. "android.intent.category.HOME") |
| 547 data: Data string to pass to activity (e.g. 'http://www.example.com/'). | 544 data: Data string to pass to activity (e.g. 'http://www.example.com/'). |
| 548 extras: Dict of extras to pass to activity. Values are significant. | 545 extras: Dict of extras to pass to activity. Values are significant. |
| 549 trace_file_name: If used, turns on and saves the trace to this file name. | 546 trace_file_name: If used, turns on and saves the trace to this file name. |
| 550 force_stop: force stop the target app before starting the activity (-S | 547 force_stop: force stop the target app before starting the activity (-S |
| 551 flag). | 548 flag). |
| 552 """ | 549 """ |
|
Sami
2013/01/29 15:07:59
Could also document the return value here.
aberent
2013/01/29 17:02:18
Done.
| |
| 553 cmd = 'am start -a %s' % action | 550 cmd = 'am start -a %s' % action |
| 554 if force_stop: | 551 if force_stop: |
| 555 cmd += ' -S' | 552 cmd += ' -S' |
| 556 if wait_for_completion: | 553 if wait_for_completion: |
| 557 cmd += ' -W' | 554 cmd += ' -W' |
| 558 if category: | 555 if category: |
| 559 cmd += ' -c %s' % category | 556 cmd += ' -c %s' % category |
| 560 if package and activity: | 557 if package and activity: |
| 561 cmd += ' -n %s/%s' % (package, activity) | 558 cmd += ' -n %s/%s' % (package, activity) |
| 562 if data: | 559 if data: |
| 563 cmd += ' -d "%s"' % data | 560 cmd += ' -d "%s"' % data |
| 564 if extras: | 561 if extras: |
| 565 for key in extras: | 562 for key in extras: |
| 566 value = extras[key] | 563 value = extras[key] |
| 567 if isinstance(value, str): | 564 if isinstance(value, str): |
| 568 cmd += ' --es' | 565 cmd += ' --es' |
| 569 elif isinstance(value, bool): | 566 elif isinstance(value, bool): |
| 570 cmd += ' --ez' | 567 cmd += ' --ez' |
| 571 elif isinstance(value, int): | 568 elif isinstance(value, int): |
| 572 cmd += ' --ei' | 569 cmd += ' --ei' |
| 573 else: | 570 else: |
| 574 raise NotImplementedError( | 571 raise NotImplementedError( |
| 575 'Need to teach StartActivity how to pass %s extras' % type(value)) | 572 'Need to teach StartActivity how to pass %s extras' % type(value)) |
| 576 cmd += ' %s %s' % (key, value) | 573 cmd += ' %s %s' % (key, value) |
| 577 if trace_file_name: | 574 if trace_file_name: |
| 578 cmd += ' --start-profiler ' + trace_file_name | 575 cmd += ' --start-profiler ' + trace_file_name |
| 576 return cmd | |
| 577 | |
| 578 def StartActivity(self, package, activity, wait_for_completion=False, | |
| 579 action='android.intent.action.VIEW', | |
| 580 category=None, data=None, | |
| 581 extras=None, trace_file_name=None, | |
| 582 force_stop=False): | |
| 583 """Starts |package|'s activity on the device. | |
|
Sami
2013/01/29 15:07:59
Rather than having three copies of more or less th
aberent
2013/01/29 17:02:18
Done.
| |
| 584 | |
| 585 Args: | |
| 586 package: Name of package to start (e.g. 'com.google.android.apps.chrome'). | |
| 587 activity: Name of activity (e.g. '.Main' or | |
| 588 'com.google.android.apps.chrome.Main'). | |
| 589 wait_for_completion: wait for the activity to finish launching (-W flag). | |
| 590 action: string (e.g. "android.intent.action.MAIN"). Default is VIEW. | |
| 591 category: string (e.g. "android.intent.category.HOME") | |
| 592 data: Data string to pass to activity (e.g. 'http://www.example.com/'). | |
| 593 extras: Dict of extras to pass to activity. Values are significant. | |
| 594 trace_file_name: If used, turns on and saves the trace to this file name. | |
| 595 force_stop: force stop the target app before starting the activity (-S | |
| 596 flag). | |
| 597 """ | |
| 598 cmd = self._ActivityCmd(package, activity, wait_for_completion, action, | |
| 599 category, data, extras, trace_file_name, force_stop) | |
| 579 self.RunShellCommand(cmd) | 600 self.RunShellCommand(cmd) |
| 580 | 601 |
| 602 def StartActivityTimed(self, package, activity, wait_for_completion=False, | |
| 603 action='android.intent.action.VIEW', | |
| 604 category=None, data=None, | |
| 605 extras=None, trace_file_name=None, | |
| 606 force_stop=False): | |
| 607 """Starts |package|'s activity on the device, returning the start time | |
| 608 | |
| 609 Args: | |
| 610 package: Name of package to start (e.g. 'com.google.android.apps.chrome'). | |
| 611 activity: Name of activity (e.g. '.Main' or | |
| 612 'com.google.android.apps.chrome.Main'). | |
| 613 wait_for_completion: wait for the activity to finish launching (-W flag). | |
| 614 action: string (e.g. "android.intent.action.MAIN"). Default is VIEW. | |
| 615 category: string (e.g. "android.intent.category.HOME") | |
| 616 data: Data string to pass to activity (e.g. 'http://www.example.com/'). | |
| 617 extras: Dict of extras to pass to activity. Values are significant. | |
| 618 trace_file_name: If used, turns on and saves the trace to this file name. | |
| 619 force_stop: force stop the target app before starting the activity (-S | |
| 620 flag). | |
| 621 """ | |
|
Sami
2013/01/29 15:07:59
Return value description missing here too.
aberent
2013/01/29 17:02:18
Done.
| |
| 622 cmd = self._ActivityCmd(package, activity, wait_for_completion, action, | |
| 623 category, data, extras, trace_file_name, force_stop) | |
| 624 self.StartMonitoringLogcat() | |
| 625 self.RunShellCommand('log starting activity; ' + cmd) | |
|
Sami
2013/01/29 15:07:59
This works nicely, but I was wondering if you cons
aberent
2013/01/29 17:02:18
That is what it used to do, but we don't seem to g
| |
| 626 activity_started_re = re.compile('.*starting activity.*') | |
| 627 m = self.WaitForLogMatch(activity_started_re, None) | |
| 628 assert m | |
| 629 start_line = m.group(0) | |
| 630 return GetLogTimestamp(start_line, self.GetDeviceYear()) | |
| 631 | |
| 581 def GoHome(self): | 632 def GoHome(self): |
| 582 """Tell the device to return to the home screen. Blocks until completion.""" | 633 """Tell the device to return to the home screen. Blocks until completion.""" |
| 583 self.RunShellCommand('am start -W ' | 634 self.RunShellCommand('am start -W ' |
| 584 '-a android.intent.action.MAIN -c android.intent.category.HOME') | 635 '-a android.intent.action.MAIN -c android.intent.category.HOME') |
| 585 | 636 |
| 586 def CloseApplication(self, package): | 637 def CloseApplication(self, package): |
| 587 """Attempt to close down the application, using increasing violence. | 638 """Attempt to close down the application, using increasing violence. |
| 588 | 639 |
| 589 Args: | 640 Args: |
| 590 package: Name of the process to kill off, e.g. | 641 package: Name of the process to kill off, e.g. |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 851 if error_match: | 902 if error_match: |
| 852 return None | 903 return None |
| 853 success_match = success_re.search(line) | 904 success_match = success_re.search(line) |
| 854 if success_match: | 905 if success_match: |
| 855 return success_match | 906 return success_match |
| 856 logging.info('<<< Skipped Logcat Line:' + str(line)) | 907 logging.info('<<< Skipped Logcat Line:' + str(line)) |
| 857 except pexpect.TIMEOUT: | 908 except pexpect.TIMEOUT: |
| 858 raise pexpect.TIMEOUT( | 909 raise pexpect.TIMEOUT( |
| 859 'Timeout (%ds) exceeded waiting for pattern "%s" (tip: use -vv ' | 910 'Timeout (%ds) exceeded waiting for pattern "%s" (tip: use -vv ' |
| 860 'to debug)' % | 911 'to debug)' % |
| 861 (self._logcat.timeout, success_re.pattern)) | 912 (timeout, success_re.pattern)) |
| 862 except pexpect.EOF: | 913 except pexpect.EOF: |
| 863 # It seems that sometimes logcat can end unexpectedly. This seems | 914 # It seems that sometimes logcat can end unexpectedly. This seems |
| 864 # to happen during Chrome startup after a reboot followed by a cache | 915 # to happen during Chrome startup after a reboot followed by a cache |
| 865 # clean. I don't understand why this happens, but this code deals with | 916 # clean. I don't understand why this happens, but this code deals with |
| 866 # getting EOF in logcat. | 917 # getting EOF in logcat. |
| 867 logging.critical('Found EOF in adb logcat. Restarting...') | 918 logging.critical('Found EOF in adb logcat. Restarting...') |
| 868 # Rerun spawn with original arguments. Note that self._logcat.args[0] is | 919 # Rerun spawn with original arguments. Note that self._logcat.args[0] is |
| 869 # the path of adb, so we don't want it in the arguments. | 920 # the path of adb, so we don't want it in the arguments. |
| 870 self._logcat = pexpect.spawn('adb', | 921 self._logcat = pexpect.spawn('adb', |
| 871 self._logcat.args[1:], | 922 self._logcat.args[1:], |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1148 """ | 1199 """ |
| 1149 def __init__(self, output): | 1200 def __init__(self, output): |
| 1150 self._output = output | 1201 self._output = output |
| 1151 | 1202 |
| 1152 def write(self, data): | 1203 def write(self, data): |
| 1153 data = data.replace('\r\r\n', '\n') | 1204 data = data.replace('\r\r\n', '\n') |
| 1154 self._output.write(data) | 1205 self._output.write(data) |
| 1155 | 1206 |
| 1156 def flush(self): | 1207 def flush(self): |
| 1157 self._output.flush() | 1208 self._output.flush() |
| OLD | NEW |