OLD | NEW |
(Empty) | |
| 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 |
| 6 import collections |
| 7 import logging |
| 8 import optparse |
| 9 import os |
| 10 import sys |
| 11 |
| 12 BUILD_ANDROID_DIR = os.path.join(os.path.dirname(__file__), |
| 13 os.pardir, |
| 14 os.pardir, |
| 15 'build', |
| 16 'android') |
| 17 sys.path.append(BUILD_ANDROID_DIR) |
| 18 from pylib import constants |
| 19 from pylib import flag_changer |
| 20 from pylib.device import device_errors |
| 21 from pylib.device import device_utils |
| 22 from pylib.device import intent |
| 23 |
| 24 # Browser Constants |
| 25 DEFAULT_BROWSER = 'chrome' |
| 26 |
| 27 # Action Constants |
| 28 ACTION_PACKAGE = 'org.chromium.base' |
| 29 ACTION_TRIM = { |
| 30 'moderate' : ACTION_PACKAGE + '.ACTION_TRIM_MEMORY_MODERATE', |
| 31 'critical' : ACTION_PACKAGE + '.ACTION_TRIM_MEMORY_RUNNING_CRITICAL', |
| 32 'complete' : ACTION_PACKAGE + '.ACTION_TRIM_MEMORY' |
| 33 } |
| 34 ACTION_LOW = ACTION_PACKAGE + '.ACTION_LOW_MEMORY' |
| 35 |
| 36 # Command Line Constants |
| 37 ENABLE_TEST_INTENTS_FLAG = '--enable-test-intents' |
| 38 |
| 39 def main(argv): |
| 40 option_parser = optparse.OptionParser() |
| 41 option_parser.add_option('-l', |
| 42 '--low', |
| 43 help='Simulate Activity#onLowMemory()', |
| 44 action='store_true') |
| 45 option_parser.add_option('-t', |
| 46 '--trim', |
| 47 help=('Simulate Activity#onTrimMemory(...) with ' + |
| 48 ', '.join(ACTION_TRIM.keys())), |
| 49 type='string') |
| 50 option_parser.add_option('-b', |
| 51 '--browser', |
| 52 default=DEFAULT_BROWSER, |
| 53 help=('Which browser to use. One of ' + |
| 54 ', '.join(constants.PACKAGE_INFO.keys()) + |
| 55 ' [default: %default]'), |
| 56 type='string') |
| 57 |
| 58 (options, args) = option_parser.parse_args(argv) |
| 59 |
| 60 if len(args) > 1: |
| 61 print 'Unknown argument: ', args[1:] |
| 62 option_parser.print_help() |
| 63 sys.exit(1) |
| 64 |
| 65 if options.low and options.trim: |
| 66 option_parser.error('options --low and --trim are mutually exclusive') |
| 67 |
| 68 if not options.low and not options.trim: |
| 69 option_parser.print_help() |
| 70 sys.exit(1) |
| 71 |
| 72 action = None |
| 73 if options.low: |
| 74 action = ACTION_LOW |
| 75 elif options.trim in ACTION_TRIM.keys(): |
| 76 action = ACTION_TRIM[options.trim] |
| 77 |
| 78 if action is None: |
| 79 option_parser.print_help() |
| 80 sys.exit(1) |
| 81 |
| 82 if not options.browser in constants.PACKAGE_INFO.keys(): |
| 83 option_parser.error('Unknown browser option ' + options.browser) |
| 84 |
| 85 package_info = constants.PACKAGE_INFO[options.browser] |
| 86 |
| 87 package = package_info.package |
| 88 activity = package_info.activity |
| 89 |
| 90 devices = device_utils.DeviceUtils.HealthyDevices() |
| 91 if not devices: |
| 92 raise device_errors.NoDevicesError() |
| 93 elif len(devices) > 1: |
| 94 logging.warning('Multiple devices attached. Using %s.', str(devices[0])) |
| 95 device = devices[0] |
| 96 |
| 97 try: |
| 98 device.EnableRoot() |
| 99 except device_errors.CommandFailedError as e: |
| 100 # Try to change the flags and start the activity anyway. |
| 101 # TODO(jbudorick) Handle this exception appropriately after interface |
| 102 # conversions are finished. |
| 103 logging.error(str(e)) |
| 104 flags = flag_changer.FlagChanger(device, package_info.cmdline_file) |
| 105 if ENABLE_TEST_INTENTS_FLAG not in flags.Get(): |
| 106 flags.AddFlags([ENABLE_TEST_INTENTS_FLAG]) |
| 107 |
| 108 device.StartActivity(intent.Intent(package=package, activity=activity, |
| 109 action=action)) |
| 110 |
| 111 if __name__ == '__main__': |
| 112 sys.exit(main(sys.argv)) |
OLD | NEW |