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 |
11 import datetime | 11 import datetime |
12 import logging | 12 import logging |
13 import os | 13 import os |
14 import random | |
14 import re | 15 import re |
15 import shlex | 16 import shlex |
16 import subprocess | 17 import subprocess |
17 import sys | 18 import sys |
18 import tempfile | 19 import tempfile |
19 import time | 20 import time |
20 | 21 |
21 import pexpect | 22 import pexpect |
22 import io_stats_parser | 23 import io_stats_parser |
23 | 24 |
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
305 uninstall_command = 'uninstall %s' % package | 306 uninstall_command = 'uninstall %s' % package |
306 | 307 |
307 logging.info('>>> $' + uninstall_command) | 308 logging.info('>>> $' + uninstall_command) |
308 return self._adb.SendCommand(uninstall_command, timeout_time=60) | 309 return self._adb.SendCommand(uninstall_command, timeout_time=60) |
309 | 310 |
310 def Install(self, package_file_path, reinstall=False): | 311 def Install(self, package_file_path, reinstall=False): |
311 """Installs the specified package to the device. | 312 """Installs the specified package to the device. |
312 | 313 |
313 Args: | 314 Args: |
314 package_file_path: Path to .apk file to install. | 315 package_file_path: Path to .apk file to install. |
315 reinstall: Whether to reinstall over existing package | 316 reinstall: Reinstall an existing apk, keeping the data. |
316 | 317 |
317 Returns: | 318 Returns: |
318 A status string returned by adb install | 319 A status string returned by adb install |
319 """ | 320 """ |
320 assert os.path.isfile(package_file_path) | 321 assert os.path.isfile(package_file_path) |
321 | 322 |
323 install_cmd = ['install'] | |
324 | |
322 if reinstall: | 325 if reinstall: |
323 install_cmd = 'install -r %s' | 326 install_cmd.append('-r') |
324 else: | |
325 install_cmd = 'install %s' | |
326 | 327 |
327 return self._adb.SendCommand(install_cmd % package_file_path, | 328 install_cmd.append(package_file_path) |
328 timeout_time=2*60, retry_count=0) | 329 install_cmd = ' '.join(install_cmd) |
Isaac (away)
2012/08/23 02:30:07
nit: please use fresh variable here
| |
329 | 330 |
330 def ManagedInstall(self, apk_path, keep_data, package_name=None, | 331 logging.info('>>> $' + install_cmd) |
332 return self._adb.SendCommand(install_cmd, timeout_time=2*60, retry_count=0) | |
333 | |
334 def ManagedInstall(self, apk_path, keep_data=False, package_name=None, | |
331 reboots_on_failure=2): | 335 reboots_on_failure=2): |
332 """Installs specified package and reboots device on timeouts. | 336 """Installs specified package and reboots device on timeouts. |
333 | 337 |
334 Args: | 338 Args: |
335 apk_path: Path to .apk file to install. | 339 apk_path: Path to .apk file to install. |
336 keep_data: Whether to keep data if package already exists | 340 keep_data: Reinstalls instead of uninstalling first, preserving the |
337 package_name: Package name (only needed if keep_data=False) | 341 application data. |
342 package_name: Package name (only needed if keep_data=False). | |
338 reboots_on_failure: number of time to reboot if package manager is frozen. | 343 reboots_on_failure: number of time to reboot if package manager is frozen. |
339 | 344 |
340 Returns: | 345 Returns: |
341 A status string returned by adb install | 346 A status string returned by adb install |
342 """ | 347 """ |
343 reboots_left = reboots_on_failure | 348 reboots_left = reboots_on_failure |
344 while True: | 349 while True: |
345 try: | 350 try: |
346 if not keep_data: | 351 if not keep_data: |
352 assert package_name | |
347 self.Uninstall(package_name) | 353 self.Uninstall(package_name) |
348 install_status = self.Install(apk_path, keep_data) | 354 install_status = self.Install(apk_path, reinstall=keep_data) |
349 if 'Success' in install_status: | 355 if 'Success' in install_status: |
350 return install_status | 356 return install_status |
351 except errors.WaitForResponseTimedOutError: | 357 except errors.WaitForResponseTimedOutError: |
352 print '@@@STEP_WARNINGS@@@' | 358 print '@@@STEP_WARNINGS@@@' |
353 logging.info('Timeout on installing %s' % apk_path) | 359 logging.info('Timeout on installing %s' % apk_path) |
354 | 360 |
355 if reboots_left <= 0: | 361 if reboots_left <= 0: |
356 raise Exception('Install failure') | 362 raise Exception('Install failure') |
357 | 363 |
358 # Force a hard reboot on last attempt | 364 # Force a hard reboot on last attempt |
(...skipping 621 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
980 if len(process_results) <= 8: | 986 if len(process_results) <= 8: |
981 continue | 987 continue |
982 # Column 0 is the executable name | 988 # Column 0 is the executable name |
983 # Column 1 is the pid | 989 # Column 1 is the pid |
984 # Column 8 is the Inode in use | 990 # Column 8 is the Inode in use |
985 if process_results[8] == socket_name: | 991 if process_results[8] == socket_name: |
986 pids.append((int(process_results[1]), process_results[0])) | 992 pids.append((int(process_results[1]), process_results[0])) |
987 break | 993 break |
988 logging.info('PidsUsingDevicePort: %s', pids) | 994 logging.info('PidsUsingDevicePort: %s', pids) |
989 return pids | 995 return pids |
996 | |
997 def RunMonkey(self, package_name, category=None, throttle=100, seed=None, | |
998 event_count=10000, verbosity=1, extra_args=''): | |
999 """Runs monkey test for a given package. | |
1000 | |
1001 Args: | |
1002 package_name: Allowed package. | |
1003 category: A list of allowed categories. | |
1004 throttle: Delay between events (ms). | |
1005 seed: Seed value for pseduo-random generator. Same seed value | |
1006 generates the same sequence of events. Seed is randomized by | |
1007 default. | |
1008 event_count: Number of events to generate. | |
1009 verbosity: Verbosity level [0-3]. | |
1010 extra_args: A string of other args to pass to the command verbatim. | |
1011 | |
1012 Returns: | |
1013 Output of the test run. | |
1014 """ | |
1015 category = category or [] | |
1016 seed = seed or random.randint(1, 100) | |
1017 | |
1018 cmd = ['monkey', | |
1019 '-p %s' % package_name, | |
1020 ' '.join(['-c %s' % c for c in category]), | |
1021 '--throttle %d' % throttle, | |
1022 '-s %d' % seed, | |
1023 '-v ' * verbosity, | |
1024 extra_args, | |
1025 '%s' % event_count] | |
1026 return self.RunShellCommand(' '.join(cmd), | |
1027 timeout_time=event_count*throttle*1.5) | |
OLD | NEW |