blob: be3ecf15832bf4abbb053b930bef03a00bffcb57 [file] [log] [blame]
[email protected]9aa79e362013-10-07 20:49:041#!/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 Tambre57e09d62019-09-22 17:18:526from __future__ import print_function
7
[email protected]9aa79e362013-10-07 20:49:048import collections
[email protected]3f67ea12014-05-17 15:52:509import logging
[email protected]9aa79e362013-10-07 20:49:0410import optparse
11import os
12import sys
13
jbudorick636bdd22016-01-14 18:14:3414_SRC_PATH = os.path.abspath(os.path.join(
15 os.path.dirname(__file__), '..', '..'))
16
bashia1533f12016-08-05 01:20:2017sys.path.append(os.path.join(_SRC_PATH, 'third_party', 'catapult', 'devil'))
jbudorick636bdd22016-01-14 18:14:3418from devil.android import device_errors
19from devil.android import device_utils
zhenw5e9035002016-02-11 19:47:4720from devil.android import flag_changer
jbudorick636bdd22016-01-14 18:14:3421from devil.android.sdk import intent
22
23sys.path.append(os.path.join(_SRC_PATH, 'build', 'android'))
24import devil_chromium
[email protected]9aa79e362013-10-07 20:49:0425from pylib import constants
[email protected]9aa79e362013-10-07 20:49:0426
27# Browser Constants
28DEFAULT_BROWSER = 'chrome'
29
30# Action Constants
31ACTION_PACKAGE = 'org.chromium.base'
32ACTION_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}
37ACTION_LOW = ACTION_PACKAGE + '.ACTION_LOW_MEMORY'
38
39# Command Line Constants
40ENABLE_TEST_INTENTS_FLAG = '--enable-test-intents'
41
42def 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 Tambre57e09d62019-09-22 17:18:5264 print('Unknown argument: ', args[1:])
[email protected]9aa79e362013-10-07 20:49:0465 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
jbudorick636bdd22016-01-14 18:14:3488 devil_chromium.Initialize()
89
[email protected]9aa79e362013-10-07 20:49:0490 package_info = constants.PACKAGE_INFO[options.browser]
91
92 package = package_info.package
93 activity = package_info.activity
94
jbudorickac496302b2015-05-14 22:52:1195 devices = device_utils.DeviceUtils.HealthyDevices()
[email protected]221d56e12014-07-29 08:14:5596 if not devices:
97 raise device_errors.NoDevicesError()
98 elif len(devices) > 1:
jbudorickac496302b2015-05-14 22:52:1199 logging.warning('Multiple devices attached. Using %s.', str(devices[0]))
100 device = devices[0]
[email protected]9aa79e362013-10-07 20:49:04101
[email protected]3f67ea12014-05-17 15:52:50102 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]f1ed1c92014-04-29 09:58:02109 flags = flag_changer.FlagChanger(device, package_info.cmdline_file)
mnaganov376a7732015-10-24 00:33:21110 flags.AddFlags([ENABLE_TEST_INTENTS_FLAG])
[email protected]9aa79e362013-10-07 20:49:04111
[email protected]402e54cd2014-06-25 05:09:06112 device.StartActivity(intent.Intent(package=package, activity=activity,
113 action=action))
[email protected]9aa79e362013-10-07 20:49:04114
115if __name__ == '__main__':
116 sys.exit(main(sys.argv))