1# Copyright (c) 2012 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Parses options for the instrumentation tests."""
6
7import constants
8import optparse
9import os
10import sys
11
12_SDK_OUT_DIR = os.path.join(constants.CHROME_DIR, 'out')
13
14
15def AddBuildTypeOption(option_parser):
16  """Decorates OptionParser with build type option."""
17  default_build_type = 'Debug'
18  if 'BUILDTYPE' in os.environ:
19    default_build_type = os.environ['BUILDTYPE']
20  option_parser.add_option('--debug', action='store_const', const='Debug',
21                           dest='build_type', default=default_build_type,
22                           help='If set, run test suites under out/Debug. '
23                                'Default is env var BUILDTYPE or Debug')
24  option_parser.add_option('--release', action='store_const', const='Release',
25                           dest='build_type',
26                           help='If set, run test suites under out/Release. '
27                                'Default is env var BUILDTYPE or Debug.')
28
29def AddInstallAPKOption(option_parser):
30  """Decorates OptionParser with apk option used to install the APK."""
31  option_parser.add_option('--apk',
32                           help=('The name of the apk containing the '
33                                 ' application (with the .apk extension).'))
34  option_parser.add_option('--apk_package',
35                           help=('The package name used by the apk containing '
36                                 'the application.'))
37
38def AddTestRunnerOptions(option_parser, default_timeout=60):
39  """Decorates OptionParser with options applicable to all tests."""
40
41  option_parser.add_option('-t', dest='timeout',
42                           help='Timeout to wait for each test',
43                           type='int',
44                           default=default_timeout)
45  option_parser.add_option('-c', dest='cleanup_test_files',
46                           help='Cleanup test files on the device after run',
47                           action='store_true')
48  option_parser.add_option('-v',
49                           '--verbose',
50                           dest='verbose_count',
51                           default=0,
52                           action='count',
53                           help='Verbose level (multiple times for more)')
54  profilers = ['devicestatsmonitor', 'chrometrace', 'dumpheap', 'smaps',
55               'traceview']
56  option_parser.add_option('--profiler', dest='profilers', action='append',
57                           choices=profilers,
58                           help='Profiling tool to run during test. '
59                           'Pass multiple times to run multiple profilers. '
60                           'Available profilers: %s' % profilers)
61  option_parser.add_option('--tool',
62                           dest='tool',
63                           help='Run the test under a tool '
64                           '(use --tool help to list them)')
65  AddBuildTypeOption(option_parser)
66
67
68def AddInstrumentationOptions(option_parser):
69  """Decorates OptionParser with instrumentation tests options."""
70
71  AddTestRunnerOptions(option_parser)
72  option_parser.add_option('-w', '--wait_debugger', dest='wait_for_debugger',
73                           action='store_true', help='Wait for debugger.')
74  option_parser.add_option('-I', dest='install_apk', help='Install APK.',
75                           action='store_true')
76  option_parser.add_option('-f', '--test_filter',
77                           help='Test filter (if not fully qualified, '
78                           'will run all matches).')
79  option_parser.add_option('-A', '--annotation', dest='annotation_str',
80                           help=('Run only tests with any of the given '
81                                 'annotations. '
82                                 'An annotation can be either a key or a '
83                                 'key-values pair. '
84                                 'A test that has no annotation is '
85                                 'considered "SmallTest".'))
86  option_parser.add_option('-j', '--java_only', action='store_true',
87                           help='Run only the Java tests.')
88  option_parser.add_option('-p', '--python_only', action='store_true',
89                           help='Run only the Python tests.')
90  option_parser.add_option('-n', '--run_count', type='int',
91                           dest='number_of_runs', default=1,
92                           help=('How many times to run each test, regardless '
93                                 'of the result. (Default is 1)'))
94  option_parser.add_option('--test-apk', dest='test_apk',
95                           help=('The name of the apk containing the tests '
96                                 '(without the .apk extension). For SDK '
97                                 'builds, the apk name without the debug '
98                                 'suffix(for example, ContentShellTest).'))
99  option_parser.add_option('--screenshot', dest='screenshot_failures',
100                           action='store_true',
101                           help='Capture screenshots of test failures')
102  option_parser.add_option('--save-perf-json', action='store_true',
103                           help='Saves the JSON file for each UI Perf test.')
104  option_parser.add_option('--shard_retries', type=int, default=1,
105                           help=('Number of times to retry each failure when '
106                                 'sharding.'))
107  option_parser.add_option('--official-build', help='Run official build tests.')
108  option_parser.add_option('--device',
109                           help='Serial number of device we should use.')
110  option_parser.add_option('--python_test_root',
111                           help='Root of the python-driven tests.')
112
113def ValidateInstrumentationOptions(option_parser, options, args):
114  """Validate options/arguments and populate options with defaults."""
115  if len(args) > 1:
116    option_parser.print_help(sys.stderr)
117    option_parser.error('Unknown arguments: %s' % args[1:])
118  if options.java_only and options.python_only:
119    option_parser.error('Options java_only (-j) and python_only (-p) '
120                        'are mutually exclusive.')
121
122  options.run_java_tests = True
123  options.run_python_tests = True
124  if options.java_only:
125    options.run_python_tests = False
126  elif options.python_only:
127    options.run_java_tests = False
128
129  # In case of SDK Build, the jars and apks have a -debug suffix.
130  options.test_apk_path = os.path.join(_SDK_OUT_DIR,
131                                       options.build_type,
132                                       constants.SDK_BUILD_APKS_DIR,
133                                       '%s-debug.apk' % options.test_apk)
134  options.test_apk_jar_path = os.path.join(_SDK_OUT_DIR,
135                                           options.build_type,
136                                           constants.SDK_BUILD_TEST_JAVALIB_DIR,
137                                           '%s-debug.jar' % options.test_apk)
138  if options.annotation_str:
139    options.annotation = options.annotation_str.split()
140  elif options.test_filter:
141    options.annotation = []
142  else:
143    options.annotation = ['Smoke', 'SmallTest', 'MediumTest', 'LargeTest']
144