Home | History | Annotate | Download | only in android
      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 optparse
      8 import os
      9 import sys
     10 
     11 BUILD_ANDROID_DIR = os.path.join(os.path.dirname(__file__),
     12                                  os.pardir,
     13                                  os.pardir,
     14                                  'build',
     15                                  'android')
     16 sys.path.append(BUILD_ANDROID_DIR)
     17 from pylib import android_commands
     18 from pylib import constants
     19 from pylib import flag_changer
     20 
     21 # Browser Constants
     22 DEFAULT_BROWSER = 'chrome'
     23 
     24 # Action Constants
     25 ACTION_PACKAGE = 'org.chromium.base'
     26 ACTION_TRIM = {
     27     'moderate' : ACTION_PACKAGE + '.ACTION_TRIM_MEMORY_MODERATE',
     28     'critical' : ACTION_PACKAGE + '.ACTION_TRIM_MEMORY_RUNNING_CRITICAL',
     29     'complete' : ACTION_PACKAGE + '.ACTION_TRIM_MEMORY'
     30 }
     31 ACTION_LOW = ACTION_PACKAGE + '.ACTION_LOW_MEMORY'
     32 
     33 # Command Line Constants
     34 ENABLE_TEST_INTENTS_FLAG = '--enable-test-intents'
     35 
     36 def main(argv):
     37   option_parser = optparse.OptionParser()
     38   option_parser.add_option('-l',
     39                            '--low',
     40                            help='Simulate Activity#onLowMemory()',
     41                            action='store_true')
     42   option_parser.add_option('-t',
     43                            '--trim',
     44                            help=('Simulate Activity#onTrimMemory(...) with ' +
     45                                  ', '.join(ACTION_TRIM.keys())),
     46                            type='string')
     47   option_parser.add_option('-b',
     48                            '--browser',
     49                            default=DEFAULT_BROWSER,
     50                            help=('Which browser to use. One of ' +
     51                                  ', '.join(constants.PACKAGE_INFO.keys()) +
     52                                  ' [default: %default]'),
     53                            type='string')
     54 
     55   (options, args) = option_parser.parse_args(argv)
     56 
     57   if len(args) > 1:
     58     print 'Unknown argument: ', args[1:]
     59     option_parser.print_help()
     60     sys.exit(1)
     61 
     62   if options.low and options.trim:
     63     option_parser.error('options --low and --trim are mutually exclusive')
     64 
     65   if not options.low and not options.trim:
     66     option_parser.print_help()
     67     sys.exit(1)
     68 
     69   action = None
     70   if options.low:
     71     action = ACTION_LOW
     72   elif options.trim in ACTION_TRIM.keys():
     73     action = ACTION_TRIM[options.trim]
     74 
     75   if action is None:
     76     option_parser.print_help()
     77     sys.exit(1)
     78 
     79   if not options.browser in constants.PACKAGE_INFO.keys():
     80     option_parser.error('Unknown browser option ' + options.browser)
     81 
     82   package_info = constants.PACKAGE_INFO[options.browser]
     83 
     84   package = package_info.package
     85   activity = package_info.activity
     86 
     87   adb = android_commands.AndroidCommands(device=None)
     88 
     89   adb.EnableAdbRoot()
     90   flags = flag_changer.FlagChanger(adb, package_info.cmdline_file)
     91   if ENABLE_TEST_INTENTS_FLAG not in flags.Get():
     92     flags.AddFlags([ENABLE_TEST_INTENTS_FLAG])
     93 
     94   adb.StartActivity(package, activity, action=action)
     95 
     96 if __name__ == '__main__':
     97   sys.exit(main(sys.argv))
     98