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