droger | d8eae74 | 2017-02-28 13:05:29 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # |
| 3 | # Copyright 2017 The Chromium Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | """Helper script to launch Chrome on device and WebPageReplay on host.""" |
| 8 | |
| 9 | import logging |
| 10 | import optparse |
| 11 | import os |
| 12 | import sys |
| 13 | import time |
| 14 | |
| 15 | _SRC_PATH = os.path.abspath(os.path.join( |
| 16 | os.path.dirname(__file__), os.pardir, os.pardir, os.pardir)) |
| 17 | |
| 18 | sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'catapult', 'devil')) |
| 19 | from devil.android import device_utils |
perezju | bdc24416 | 2017-03-08 11:45:04 | [diff] [blame^] | 20 | from devil.android import flag_changer |
droger | d8eae74 | 2017-02-28 13:05:29 | [diff] [blame] | 21 | from devil.android.constants import chrome |
| 22 | from devil.android.perf import cache_control |
| 23 | from devil.android.sdk import intent |
| 24 | |
| 25 | sys.path.append(os.path.join(_SRC_PATH, 'build', 'android')) |
| 26 | import devil_chromium |
| 27 | |
| 28 | import chrome_setup |
| 29 | import device_setup |
| 30 | |
| 31 | |
| 32 | def RunChrome(device, cold, chrome_args, package_info): |
| 33 | """Runs Chrome on the device. |
| 34 | |
| 35 | Args: |
| 36 | device: (DeviceUtils) device to run the tests on. |
| 37 | cold: (bool) Whether caches should be dropped. |
| 38 | chrome_args: ([str]) List of arguments to pass to Chrome. |
| 39 | package_info: (PackageInfo) Chrome package info. |
| 40 | """ |
| 41 | if not device.HasRoot(): |
| 42 | device.EnableRoot() |
| 43 | |
| 44 | cmdline_file = package_info.cmdline_file |
| 45 | package = package_info.package |
perezju | bdc24416 | 2017-03-08 11:45:04 | [diff] [blame^] | 46 | with flag_changer.CustomCommandLineFlags(device, cmdline_file, chrome_args): |
droger | d8eae74 | 2017-02-28 13:05:29 | [diff] [blame] | 47 | device.ForceStop(package) |
| 48 | |
| 49 | if cold: |
| 50 | chrome_setup.ResetChromeLocalState(device, package) |
| 51 | cache_control.CacheControl(device).DropRamCaches() |
| 52 | |
| 53 | start_intent = intent.Intent(package=package, data='about:blank', |
| 54 | activity=package_info.activity) |
| 55 | try: |
| 56 | device.StartActivity(start_intent, blocking=True) |
| 57 | print ( |
| 58 | '\n\n' |
| 59 | ' +---------------------------------------------+\n' |
| 60 | ' | Chrome launched, press Ctrl-C to interrupt. |\n' |
| 61 | ' +---------------------------------------------+') |
| 62 | while True: |
| 63 | time.sleep(1) |
| 64 | except KeyboardInterrupt: |
| 65 | pass |
| 66 | finally: |
| 67 | device.ForceStop(package) |
| 68 | |
| 69 | |
| 70 | def _CreateOptionParser(): |
| 71 | description = 'Launches Chrome on a device, connected to a WebPageReplay ' \ |
| 72 | 'instance running on the host. The WPR archive must be ' \ |
| 73 | 'passed as parameter.' |
| 74 | parser = optparse.OptionParser(description=description, |
| 75 | usage='Usage: %prog [options] wpr_archive') |
| 76 | |
| 77 | # Device-related options. |
| 78 | d = optparse.OptionGroup(parser, 'Device options') |
| 79 | d.add_option('--device', help='Device ID') |
| 80 | d.add_option('--cold', help='Purge all caches before running Chrome.', |
| 81 | default=False, action='store_true') |
| 82 | d.add_option('--chrome_package_name', |
| 83 | help='Chrome package name (e.g. "chrome" or "chromium") ' |
| 84 | '[default: %default].', default='chrome') |
| 85 | parser.add_option_group(d) |
| 86 | |
| 87 | # WebPageReplay-related options. |
| 88 | w = optparse.OptionGroup(parser, 'WebPageReplay options') |
| 89 | w.add_option('--record', |
| 90 | help='Enable this to record a new WPR archive.', |
| 91 | action='store_true', default=False) |
| 92 | w.add_option('--wpr_log', help='WPR log path.') |
| 93 | w.add_option('--network_condition', help='Network condition for emulation.') |
| 94 | parser.add_option_group(w) |
| 95 | |
| 96 | return parser |
| 97 | |
| 98 | |
| 99 | def main(): |
| 100 | parser = _CreateOptionParser() |
| 101 | options, args = parser.parse_args() |
| 102 | if len(args) != 1: |
| 103 | parser.error("Incorrect number of arguments.") |
| 104 | devil_chromium.Initialize() |
| 105 | devices = device_utils.DeviceUtils.HealthyDevices() |
| 106 | device = devices[0] |
| 107 | if len(devices) != 1 and options.device is None: |
| 108 | logging.error('Several devices attached, must specify one with --device.') |
| 109 | sys.exit(0) |
| 110 | if options.device is not None: |
| 111 | matching_devices = [d for d in devices if str(d) == options.device] |
| 112 | if not matching_devices: |
| 113 | logging.error('Device not found.') |
| 114 | sys.exit(0) |
| 115 | device = matching_devices[0] |
| 116 | |
| 117 | with device_setup.RemoteWprHost(device, args[0], options.record, |
| 118 | options.network_condition, |
| 119 | out_log_path=options.wpr_log) as wpr_attr: |
| 120 | RunChrome(device, options.cold, |
| 121 | chrome_setup.CHROME_ARGS + wpr_attr.chrome_args, |
| 122 | chrome.PACKAGE_INFO[options.chrome_package_name]) |
| 123 | |
| 124 | |
| 125 | if __name__ == '__main__': |
| 126 | main() |