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.
12 BUILD_ANDROID_DIR
= os
.path
.join(os
.path
.dirname(__file__
),
17 sys
.path
.append(BUILD_ANDROID_DIR
)
18 from pylib
import android_commands
19 from pylib
import constants
20 from pylib
import flag_changer
21 from pylib
.device
import device_errors
22 from pylib
.device
import device_utils
23 from pylib
.device
import intent
26 DEFAULT_BROWSER
= 'chrome'
29 ACTION_PACKAGE
= 'org.chromium.base'
31 'moderate' : ACTION_PACKAGE
+ '.ACTION_TRIM_MEMORY_MODERATE',
32 'critical' : ACTION_PACKAGE
+ '.ACTION_TRIM_MEMORY_RUNNING_CRITICAL',
33 'complete' : ACTION_PACKAGE
+ '.ACTION_TRIM_MEMORY'
35 ACTION_LOW
= ACTION_PACKAGE
+ '.ACTION_LOW_MEMORY'
37 # Command Line Constants
38 ENABLE_TEST_INTENTS_FLAG
= '--enable-test-intents'
41 option_parser
= optparse
.OptionParser()
42 option_parser
.add_option('-l',
44 help='Simulate Activity#onLowMemory()',
46 option_parser
.add_option('-t',
48 help=('Simulate Activity#onTrimMemory(...) with ' +
49 ', '.join(ACTION_TRIM
.keys())),
51 option_parser
.add_option('-b',
53 default
=DEFAULT_BROWSER
,
54 help=('Which browser to use. One of ' +
55 ', '.join(constants
.PACKAGE_INFO
.keys()) +
56 ' [default: %default]'),
59 (options
, args
) = option_parser
.parse_args(argv
)
62 print 'Unknown argument: ', args
[1:]
63 option_parser
.print_help()
66 if options
.low
and options
.trim
:
67 option_parser
.error('options --low and --trim are mutually exclusive')
69 if not options
.low
and not options
.trim
:
70 option_parser
.print_help()
76 elif options
.trim
in ACTION_TRIM
.keys():
77 action
= ACTION_TRIM
[options
.trim
]
80 option_parser
.print_help()
83 if not options
.browser
in constants
.PACKAGE_INFO
.keys():
84 option_parser
.error('Unknown browser option ' + options
.browser
)
86 package_info
= constants
.PACKAGE_INFO
[options
.browser
]
88 package
= package_info
.package
89 activity
= package_info
.activity
91 devices
= android_commands
.GetAttachedDevices()
93 raise device_errors
.NoDevicesError()
94 elif len(devices
) > 1:
95 logging
.warning('Multiple devices attached. Using %s.' % devices
[0])
96 device
= device_utils
.DeviceUtils(devices
[0])
100 except device_errors
.CommandFailedError
as e
:
101 # Try to change the flags and start the activity anyway.
102 # TODO(jbudorick) Handle this exception appropriately after interface
103 # conversions are finished.
104 logging
.error(str(e
))
105 flags
= flag_changer
.FlagChanger(device
, package_info
.cmdline_file
)
106 if ENABLE_TEST_INTENTS_FLAG
not in flags
.Get():
107 flags
.AddFlags([ENABLE_TEST_INTENTS_FLAG
])
109 device
.StartActivity(intent
.Intent(package
=package
, activity
=activity
,
112 if __name__
== '__main__':
113 sys
.exit(main(sys
.argv
))