[email protected] | 9aa79e36 | 2013-10-07 20:49:04 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2013 The Chromium Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
Raul Tambre | 57e09d6 | 2019-09-22 17:18:52 | [diff] [blame] | 6 | from __future__ import print_function |
| 7 | |
[email protected] | 9aa79e36 | 2013-10-07 20:49:04 | [diff] [blame] | 8 | import collections |
[email protected] | 3f67ea1 | 2014-05-17 15:52:50 | [diff] [blame] | 9 | import logging |
[email protected] | 9aa79e36 | 2013-10-07 20:49:04 | [diff] [blame] | 10 | import optparse |
| 11 | import os |
| 12 | import sys |
| 13 | |
jbudorick | 636bdd2 | 2016-01-14 18:14:34 | [diff] [blame] | 14 | _SRC_PATH = os.path.abspath(os.path.join( |
| 15 | os.path.dirname(__file__), '..', '..')) |
| 16 | |
bashi | a1533f1 | 2016-08-05 01:20:20 | [diff] [blame] | 17 | sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'catapult', 'devil')) |
jbudorick | 636bdd2 | 2016-01-14 18:14:34 | [diff] [blame] | 18 | from devil.android import device_errors |
| 19 | from devil.android import device_utils |
zhenw | 5e903500 | 2016-02-11 19:47:47 | [diff] [blame] | 20 | from devil.android import flag_changer |
jbudorick | 636bdd2 | 2016-01-14 18:14:34 | [diff] [blame] | 21 | from devil.android.sdk import intent |
| 22 | |
| 23 | sys.path.append(os.path.join(_SRC_PATH, 'build', 'android')) |
| 24 | import devil_chromium |
[email protected] | 9aa79e36 | 2013-10-07 20:49:04 | [diff] [blame] | 25 | from pylib import constants |
[email protected] | 9aa79e36 | 2013-10-07 20:49:04 | [diff] [blame] | 26 | |
| 27 | # Browser Constants |
| 28 | DEFAULT_BROWSER = 'chrome' |
| 29 | |
| 30 | # Action Constants |
| 31 | ACTION_PACKAGE = 'org.chromium.base' |
| 32 | ACTION_TRIM = { |
| 33 | 'moderate' : ACTION_PACKAGE + '.ACTION_TRIM_MEMORY_MODERATE', |
| 34 | 'critical' : ACTION_PACKAGE + '.ACTION_TRIM_MEMORY_RUNNING_CRITICAL', |
| 35 | 'complete' : ACTION_PACKAGE + '.ACTION_TRIM_MEMORY' |
| 36 | } |
| 37 | ACTION_LOW = ACTION_PACKAGE + '.ACTION_LOW_MEMORY' |
| 38 | |
| 39 | # Command Line Constants |
| 40 | ENABLE_TEST_INTENTS_FLAG = '--enable-test-intents' |
| 41 | |
| 42 | def main(argv): |
| 43 | option_parser = optparse.OptionParser() |
| 44 | option_parser.add_option('-l', |
| 45 | '--low', |
| 46 | help='Simulate Activity#onLowMemory()', |
| 47 | action='store_true') |
| 48 | option_parser.add_option('-t', |
| 49 | '--trim', |
| 50 | help=('Simulate Activity#onTrimMemory(...) with ' + |
| 51 | ', '.join(ACTION_TRIM.keys())), |
| 52 | type='string') |
| 53 | option_parser.add_option('-b', |
| 54 | '--browser', |
| 55 | default=DEFAULT_BROWSER, |
| 56 | help=('Which browser to use. One of ' + |
| 57 | ', '.join(constants.PACKAGE_INFO.keys()) + |
| 58 | ' [default: %default]'), |
| 59 | type='string') |
| 60 | |
| 61 | (options, args) = option_parser.parse_args(argv) |
| 62 | |
| 63 | if len(args) > 1: |
Raul Tambre | 57e09d6 | 2019-09-22 17:18:52 | [diff] [blame] | 64 | print('Unknown argument: ', args[1:]) |
[email protected] | 9aa79e36 | 2013-10-07 20:49:04 | [diff] [blame] | 65 | option_parser.print_help() |
| 66 | sys.exit(1) |
| 67 | |
| 68 | if options.low and options.trim: |
| 69 | option_parser.error('options --low and --trim are mutually exclusive') |
| 70 | |
| 71 | if not options.low and not options.trim: |
| 72 | option_parser.print_help() |
| 73 | sys.exit(1) |
| 74 | |
| 75 | action = None |
| 76 | if options.low: |
| 77 | action = ACTION_LOW |
| 78 | elif options.trim in ACTION_TRIM.keys(): |
| 79 | action = ACTION_TRIM[options.trim] |
| 80 | |
| 81 | if action is None: |
| 82 | option_parser.print_help() |
| 83 | sys.exit(1) |
| 84 | |
| 85 | if not options.browser in constants.PACKAGE_INFO.keys(): |
| 86 | option_parser.error('Unknown browser option ' + options.browser) |
| 87 | |
jbudorick | 636bdd2 | 2016-01-14 18:14:34 | [diff] [blame] | 88 | devil_chromium.Initialize() |
| 89 | |
[email protected] | 9aa79e36 | 2013-10-07 20:49:04 | [diff] [blame] | 90 | package_info = constants.PACKAGE_INFO[options.browser] |
| 91 | |
| 92 | package = package_info.package |
| 93 | activity = package_info.activity |
| 94 | |
jbudorick | ac496302b | 2015-05-14 22:52:11 | [diff] [blame] | 95 | devices = device_utils.DeviceUtils.HealthyDevices() |
[email protected] | 221d56e1 | 2014-07-29 08:14:55 | [diff] [blame] | 96 | if not devices: |
| 97 | raise device_errors.NoDevicesError() |
| 98 | elif len(devices) > 1: |
jbudorick | ac496302b | 2015-05-14 22:52:11 | [diff] [blame] | 99 | logging.warning('Multiple devices attached. Using %s.', str(devices[0])) |
| 100 | device = devices[0] |
[email protected] | 9aa79e36 | 2013-10-07 20:49:04 | [diff] [blame] | 101 | |
[email protected] | 3f67ea1 | 2014-05-17 15:52:50 | [diff] [blame] | 102 | try: |
| 103 | device.EnableRoot() |
| 104 | except device_errors.CommandFailedError as e: |
| 105 | # Try to change the flags and start the activity anyway. |
| 106 | # TODO(jbudorick) Handle this exception appropriately after interface |
| 107 | # conversions are finished. |
| 108 | logging.error(str(e)) |
[email protected] | f1ed1c9 | 2014-04-29 09:58:02 | [diff] [blame] | 109 | flags = flag_changer.FlagChanger(device, package_info.cmdline_file) |
mnaganov | 376a773 | 2015-10-24 00:33:21 | [diff] [blame] | 110 | flags.AddFlags([ENABLE_TEST_INTENTS_FLAG]) |
[email protected] | 9aa79e36 | 2013-10-07 20:49:04 | [diff] [blame] | 111 | |
[email protected] | 402e54cd | 2014-06-25 05:09:06 | [diff] [blame] | 112 | device.StartActivity(intent.Intent(package=package, activity=activity, |
| 113 | action=action)) |
[email protected] | 9aa79e36 | 2013-10-07 20:49:04 | [diff] [blame] | 114 | |
| 115 | if __name__ == '__main__': |
| 116 | sys.exit(main(sys.argv)) |