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 706 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
717 action, category, data, extras, | 717 action, category, data, extras, |
718 trace_file_name, force_stop, flags) | 718 trace_file_name, force_stop, flags) |
719 self.StartMonitoringLogcat() | 719 self.StartMonitoringLogcat() |
720 self.RunShellCommand('log starting activity; ' + cmd) | 720 self.RunShellCommand('log starting activity; ' + cmd) |
721 activity_started_re = re.compile('.*starting activity.*') | 721 activity_started_re = re.compile('.*starting activity.*') |
722 m = self.WaitForLogMatch(activity_started_re, None) | 722 m = self.WaitForLogMatch(activity_started_re, None) |
723 assert m | 723 assert m |
724 start_line = m.group(0) | 724 start_line = m.group(0) |
725 return GetLogTimestamp(start_line, self.GetDeviceYear()) | 725 return GetLogTimestamp(start_line, self.GetDeviceYear()) |
726 | 726 |
| 727 def BroadcastIntent(self, package, intent, *args): |
| 728 """Send a broadcast intent. |
| 729 |
| 730 Args: |
| 731 package: Name of package containing the intent. |
| 732 intent: Name of the intent. |
| 733 args: Optional extra arguments for the intent. |
| 734 """ |
| 735 cmd = 'am broadcast -a %s.%s %s' % (package, intent, ' '.join(args)) |
| 736 self.RunShellCommand(cmd) |
| 737 |
727 def GoHome(self): | 738 def GoHome(self): |
728 """Tell the device to return to the home screen. Blocks until completion.""" | 739 """Tell the device to return to the home screen. Blocks until completion.""" |
729 self.RunShellCommand('am start -W ' | 740 self.RunShellCommand('am start -W ' |
730 '-a android.intent.action.MAIN -c android.intent.category.HOME') | 741 '-a android.intent.action.MAIN -c android.intent.category.HOME') |
731 | 742 |
732 def CloseApplication(self, package): | 743 def CloseApplication(self, package): |
733 """Attempt to close down the application, using increasing violence. | 744 """Attempt to close down the application, using increasing violence. |
734 | 745 |
735 Args: | 746 Args: |
736 package: Name of the process to kill off, e.g. | 747 package: Name of the process to kill off, e.g. |
(...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1137 args += shlex.split(self._adb._target_arg) | 1148 args += shlex.split(self._adb._target_arg) |
1138 args += ['logcat', '-v', 'threadtime'] | 1149 args += ['logcat', '-v', 'threadtime'] |
1139 if filters: | 1150 if filters: |
1140 args.extend(filters) | 1151 args.extend(filters) |
1141 else: | 1152 else: |
1142 args.append('*:v') | 1153 args.append('*:v') |
1143 | 1154 |
1144 if logfile: | 1155 if logfile: |
1145 logfile = NewLineNormalizer(logfile) | 1156 logfile = NewLineNormalizer(logfile) |
1146 | 1157 |
1147 # Spawn logcat and syncronize with it. | 1158 # Spawn logcat and synchronize with it. |
1148 for _ in range(4): | 1159 for _ in range(4): |
1149 self._logcat = pexpect.spawn(constants.ADB_PATH, args, timeout=10, | 1160 self._logcat = pexpect.spawn(constants.ADB_PATH, args, timeout=10, |
1150 logfile=logfile) | 1161 logfile=logfile) |
1151 self.RunShellCommand('log startup_sync') | 1162 if not clear or self.SyncLogCat(): |
1152 if self._logcat.expect(['startup_sync', pexpect.EOF, | |
1153 pexpect.TIMEOUT]) == 0: | |
1154 break | 1163 break |
1155 self._logcat.close(force=True) | 1164 self._logcat.close(force=True) |
1156 else: | 1165 else: |
1157 logging.critical('Error reading from logcat: ' + str(self._logcat.match)) | 1166 logging.critical('Error reading from logcat: ' + str(self._logcat.match)) |
1158 sys.exit(1) | 1167 sys.exit(1) |
1159 | 1168 |
| 1169 def SyncLogCat(self): |
| 1170 """Synchronize with logcat. |
| 1171 |
| 1172 Synchronize with the monitored logcat so that WaitForLogMatch will only |
| 1173 consider new message that are received after this point in time. |
| 1174 |
| 1175 Returns: |
| 1176 True if the synchronization succeeded. |
| 1177 """ |
| 1178 assert self._logcat |
| 1179 tag = 'logcat_sync_%s' % time.time() |
| 1180 self.RunShellCommand('log ' + tag) |
| 1181 return self._logcat.expect([tag, pexpect.EOF, pexpect.TIMEOUT]) == 0 |
| 1182 |
1160 def GetMonitoredLogCat(self): | 1183 def GetMonitoredLogCat(self): |
1161 """Returns an "adb logcat" command as created by pexpected.spawn.""" | 1184 """Returns an "adb logcat" command as created by pexpected.spawn.""" |
1162 if not self._logcat: | 1185 if not self._logcat: |
1163 self.StartMonitoringLogcat(clear=False) | 1186 self.StartMonitoringLogcat(clear=False) |
1164 return self._logcat | 1187 return self._logcat |
1165 | 1188 |
1166 def WaitForLogMatch(self, success_re, error_re, clear=False, timeout=10): | 1189 def WaitForLogMatch(self, success_re, error_re, clear=False, timeout=10): |
1167 """Blocks until a matching line is logged or a timeout occurs. | 1190 """Blocks until a matching line is logged or a timeout occurs. |
1168 | 1191 |
1169 Args: | 1192 Args: |
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1503 """Saves a screenshot image to |host_file| on the host. | 1526 """Saves a screenshot image to |host_file| on the host. |
1504 | 1527 |
1505 Args: | 1528 Args: |
1506 host_file: Absolute path to the image file to store on the host. | 1529 host_file: Absolute path to the image file to store on the host. |
1507 """ | 1530 """ |
1508 host_dir = os.path.dirname(host_file) | 1531 host_dir = os.path.dirname(host_file) |
1509 if not os.path.exists(host_dir): | 1532 if not os.path.exists(host_dir): |
1510 os.makedirs(host_dir) | 1533 os.makedirs(host_dir) |
1511 device_file = '%s/screenshot.png' % self.GetExternalStorage() | 1534 device_file = '%s/screenshot.png' % self.GetExternalStorage() |
1512 self.RunShellCommand('/system/bin/screencap -p %s' % device_file) | 1535 self.RunShellCommand('/system/bin/screencap -p %s' % device_file) |
| 1536 self.PullFileFromDevice(device_file, host_file) |
| 1537 |
| 1538 def PullFileFromDevice(self, device_file, host_file): |
| 1539 """Download |device_file| on the device from to |host_file| on the host. |
| 1540 |
| 1541 Args: |
| 1542 device_file: Absolute path to the file to retrieve from the device. |
| 1543 host_file: Absolute path to the file to store on the host. |
| 1544 """ |
1513 assert self._adb.Pull(device_file, host_file) | 1545 assert self._adb.Pull(device_file, host_file) |
1514 assert os.path.exists(host_file) | 1546 assert os.path.exists(host_file) |
1515 | 1547 |
1516 def SetUtilWrapper(self, util_wrapper): | 1548 def SetUtilWrapper(self, util_wrapper): |
1517 """Sets a wrapper prefix to be used when running a locally-built | 1549 """Sets a wrapper prefix to be used when running a locally-built |
1518 binary on the device (ex.: md5sum_bin). | 1550 binary on the device (ex.: md5sum_bin). |
1519 """ | 1551 """ |
1520 self._util_wrapper = util_wrapper | 1552 self._util_wrapper = util_wrapper |
1521 | 1553 |
1522 def RunInstrumentationTest(self, test, test_package, instr_args, timeout): | 1554 def RunInstrumentationTest(self, test, test_package, instr_args, timeout): |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1578 """ | 1610 """ |
1579 def __init__(self, output): | 1611 def __init__(self, output): |
1580 self._output = output | 1612 self._output = output |
1581 | 1613 |
1582 def write(self, data): | 1614 def write(self, data): |
1583 data = data.replace('\r\r\n', '\n') | 1615 data = data.replace('\r\r\n', '\n') |
1584 self._output.write(data) | 1616 self._output.write(data) |
1585 | 1617 |
1586 def flush(self): | 1618 def flush(self): |
1587 self._output.flush() | 1619 self._output.flush() |
OLD | NEW |