michael@0: # Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: # Use of this source code is governed by a BSD-style license that can be michael@0: # found in the LICENSE file. michael@0: michael@0: """Parses options for the instrumentation tests.""" michael@0: michael@0: import constants michael@0: import optparse michael@0: import os michael@0: import sys michael@0: michael@0: _SDK_OUT_DIR = os.path.join(constants.CHROME_DIR, 'out') michael@0: michael@0: michael@0: def AddBuildTypeOption(option_parser): michael@0: """Decorates OptionParser with build type option.""" michael@0: default_build_type = 'Debug' michael@0: if 'BUILDTYPE' in os.environ: michael@0: default_build_type = os.environ['BUILDTYPE'] michael@0: option_parser.add_option('--debug', action='store_const', const='Debug', michael@0: dest='build_type', default=default_build_type, michael@0: help='If set, run test suites under out/Debug. ' michael@0: 'Default is env var BUILDTYPE or Debug') michael@0: option_parser.add_option('--release', action='store_const', const='Release', michael@0: dest='build_type', michael@0: help='If set, run test suites under out/Release. ' michael@0: 'Default is env var BUILDTYPE or Debug.') michael@0: michael@0: def AddInstallAPKOption(option_parser): michael@0: """Decorates OptionParser with apk option used to install the APK.""" michael@0: option_parser.add_option('--apk', michael@0: help=('The name of the apk containing the ' michael@0: ' application (with the .apk extension).')) michael@0: option_parser.add_option('--apk_package', michael@0: help=('The package name used by the apk containing ' michael@0: 'the application.')) michael@0: michael@0: def AddTestRunnerOptions(option_parser, default_timeout=60): michael@0: """Decorates OptionParser with options applicable to all tests.""" michael@0: michael@0: option_parser.add_option('-t', dest='timeout', michael@0: help='Timeout to wait for each test', michael@0: type='int', michael@0: default=default_timeout) michael@0: option_parser.add_option('-c', dest='cleanup_test_files', michael@0: help='Cleanup test files on the device after run', michael@0: action='store_true') michael@0: option_parser.add_option('-v', michael@0: '--verbose', michael@0: dest='verbose_count', michael@0: default=0, michael@0: action='count', michael@0: help='Verbose level (multiple times for more)') michael@0: profilers = ['devicestatsmonitor', 'chrometrace', 'dumpheap', 'smaps', michael@0: 'traceview'] michael@0: option_parser.add_option('--profiler', dest='profilers', action='append', michael@0: choices=profilers, michael@0: help='Profiling tool to run during test. ' michael@0: 'Pass multiple times to run multiple profilers. ' michael@0: 'Available profilers: %s' % profilers) michael@0: option_parser.add_option('--tool', michael@0: dest='tool', michael@0: help='Run the test under a tool ' michael@0: '(use --tool help to list them)') michael@0: AddBuildTypeOption(option_parser) michael@0: michael@0: michael@0: def AddInstrumentationOptions(option_parser): michael@0: """Decorates OptionParser with instrumentation tests options.""" michael@0: michael@0: AddTestRunnerOptions(option_parser) michael@0: option_parser.add_option('-w', '--wait_debugger', dest='wait_for_debugger', michael@0: action='store_true', help='Wait for debugger.') michael@0: option_parser.add_option('-I', dest='install_apk', help='Install APK.', michael@0: action='store_true') michael@0: option_parser.add_option('-f', '--test_filter', michael@0: help='Test filter (if not fully qualified, ' michael@0: 'will run all matches).') michael@0: option_parser.add_option('-A', '--annotation', dest='annotation_str', michael@0: help=('Run only tests with any of the given ' michael@0: 'annotations. ' michael@0: 'An annotation can be either a key or a ' michael@0: 'key-values pair. ' michael@0: 'A test that has no annotation is ' michael@0: 'considered "SmallTest".')) michael@0: option_parser.add_option('-j', '--java_only', action='store_true', michael@0: help='Run only the Java tests.') michael@0: option_parser.add_option('-p', '--python_only', action='store_true', michael@0: help='Run only the Python tests.') michael@0: option_parser.add_option('-n', '--run_count', type='int', michael@0: dest='number_of_runs', default=1, michael@0: help=('How many times to run each test, regardless ' michael@0: 'of the result. (Default is 1)')) michael@0: option_parser.add_option('--test-apk', dest='test_apk', michael@0: help=('The name of the apk containing the tests ' michael@0: '(without the .apk extension). For SDK ' michael@0: 'builds, the apk name without the debug ' michael@0: 'suffix(for example, ContentShellTest).')) michael@0: option_parser.add_option('--screenshot', dest='screenshot_failures', michael@0: action='store_true', michael@0: help='Capture screenshots of test failures') michael@0: option_parser.add_option('--save-perf-json', action='store_true', michael@0: help='Saves the JSON file for each UI Perf test.') michael@0: option_parser.add_option('--shard_retries', type=int, default=1, michael@0: help=('Number of times to retry each failure when ' michael@0: 'sharding.')) michael@0: option_parser.add_option('--official-build', help='Run official build tests.') michael@0: option_parser.add_option('--device', michael@0: help='Serial number of device we should use.') michael@0: option_parser.add_option('--python_test_root', michael@0: help='Root of the python-driven tests.') michael@0: michael@0: def ValidateInstrumentationOptions(option_parser, options, args): michael@0: """Validate options/arguments and populate options with defaults.""" michael@0: if len(args) > 1: michael@0: option_parser.print_help(sys.stderr) michael@0: option_parser.error('Unknown arguments: %s' % args[1:]) michael@0: if options.java_only and options.python_only: michael@0: option_parser.error('Options java_only (-j) and python_only (-p) ' michael@0: 'are mutually exclusive.') michael@0: michael@0: options.run_java_tests = True michael@0: options.run_python_tests = True michael@0: if options.java_only: michael@0: options.run_python_tests = False michael@0: elif options.python_only: michael@0: options.run_java_tests = False michael@0: michael@0: # In case of SDK Build, the jars and apks have a -debug suffix. michael@0: options.test_apk_path = os.path.join(_SDK_OUT_DIR, michael@0: options.build_type, michael@0: constants.SDK_BUILD_APKS_DIR, michael@0: '%s-debug.apk' % options.test_apk) michael@0: options.test_apk_jar_path = os.path.join(_SDK_OUT_DIR, michael@0: options.build_type, michael@0: constants.SDK_BUILD_TEST_JAVALIB_DIR, michael@0: '%s-debug.jar' % options.test_apk) michael@0: if options.annotation_str: michael@0: options.annotation = options.annotation_str.split() michael@0: elif options.test_filter: michael@0: options.annotation = [] michael@0: else: michael@0: options.annotation = ['Smoke', 'SmallTest', 'MediumTest', 'LargeTest']