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"""Defines a set of constants shared by test runners and other scripts."""
6
7# TODO(jbudorick): Split these constants into coherent modules.
8
9# pylint: disable=W0212
10
11import collections
12import glob
13import logging
14import os
15import subprocess
16
17import devil.android.sdk.keyevent
18from devil.android.constants import chrome
19from devil.android.sdk import version_codes
20from devil.constants import exit_codes
21
22
23keyevent = devil.android.sdk.keyevent
24
25
26DIR_SOURCE_ROOT = os.environ.get('CHECKOUT_SOURCE_ROOT',
27    os.path.abspath(os.path.join(os.path.dirname(__file__),
28                                 os.pardir, os.pardir, os.pardir, os.pardir)))
29
30PACKAGE_INFO = dict(chrome.PACKAGE_INFO)
31PACKAGE_INFO.update({
32    'legacy_browser': chrome.PackageInfo(
33        'com.google.android.browser',
34        'com.android.browser.BrowserActivity',
35        None,
36        None),
37    'chromecast_shell': chrome.PackageInfo(
38        'com.google.android.apps.mediashell',
39        'com.google.android.apps.mediashell.MediaShellActivity',
40        'castshell-command-line',
41        None),
42    'android_webview_shell': chrome.PackageInfo(
43        'org.chromium.android_webview.shell',
44        'org.chromium.android_webview.shell.AwShellActivity',
45        'android-webview-command-line',
46        None),
47    'gtest': chrome.PackageInfo(
48        'org.chromium.native_test',
49        'org.chromium.native_test.NativeUnitTestActivity',
50        'chrome-native-tests-command-line',
51        None),
52    'components_browsertests': chrome.PackageInfo(
53        'org.chromium.components_browsertests_apk',
54        ('org.chromium.components_browsertests_apk' +
55         '.ComponentsBrowserTestsActivity'),
56        'chrome-native-tests-command-line',
57        None),
58    'content_browsertests': chrome.PackageInfo(
59        'org.chromium.content_browsertests_apk',
60        'org.chromium.content_browsertests_apk.ContentBrowserTestsActivity',
61        'chrome-native-tests-command-line',
62        None),
63    'chromedriver_webview_shell': chrome.PackageInfo(
64        'org.chromium.chromedriver_webview_shell',
65        'org.chromium.chromedriver_webview_shell.Main',
66        None,
67        None),
68})
69
70
71# Ports arrangement for various test servers used in Chrome for Android.
72# Lighttpd server will attempt to use 9000 as default port, if unavailable it
73# will find a free port from 8001 - 8999.
74LIGHTTPD_DEFAULT_PORT = 9000
75LIGHTTPD_RANDOM_PORT_FIRST = 8001
76LIGHTTPD_RANDOM_PORT_LAST = 8999
77TEST_SYNC_SERVER_PORT = 9031
78TEST_SEARCH_BY_IMAGE_SERVER_PORT = 9041
79TEST_POLICY_SERVER_PORT = 9051
80
81
82TEST_EXECUTABLE_DIR = '/data/local/tmp'
83# Directories for common java libraries for SDK build.
84# These constants are defined in build/android/ant/common.xml
85SDK_BUILD_JAVALIB_DIR = 'lib.java'
86SDK_BUILD_TEST_JAVALIB_DIR = 'test.lib.java'
87SDK_BUILD_APKS_DIR = 'apks'
88
89ADB_KEYS_FILE = '/data/misc/adb/adb_keys'
90
91PERF_OUTPUT_DIR = os.path.join(DIR_SOURCE_ROOT, 'out', 'step_results')
92# The directory on the device where perf test output gets saved to.
93DEVICE_PERF_OUTPUT_DIR = (
94    '/data/data/' + PACKAGE_INFO['chrome'].package + '/files')
95
96SCREENSHOTS_DIR = os.path.join(DIR_SOURCE_ROOT, 'out_screenshots')
97
98ANDROID_SDK_VERSION = version_codes.OREO_MR1
99ANDROID_SDK_BUILD_TOOLS_VERSION = '27.0.3'
100ANDROID_SDK_ROOT = os.path.join(DIR_SOURCE_ROOT,
101                                'third_party', 'android_tools', 'sdk')
102ANDROID_SDK_TOOLS = os.path.join(ANDROID_SDK_ROOT,
103                                 'build-tools', ANDROID_SDK_BUILD_TOOLS_VERSION)
104ANDROID_NDK_ROOT = os.path.join(DIR_SOURCE_ROOT,
105                                'third_party', 'android_ndk')
106
107PROGUARD_ROOT = os.path.join(DIR_SOURCE_ROOT, 'third_party', 'proguard')
108
109BAD_DEVICES_JSON = os.path.join(DIR_SOURCE_ROOT,
110                                os.environ.get('CHROMIUM_OUT_DIR', 'out'),
111                                'bad_devices.json')
112
113UPSTREAM_FLAKINESS_SERVER = 'test-results.appspot.com'
114
115# TODO(jbudorick): Remove once unused.
116DEVICE_LOCAL_PROPERTIES_PATH = '/data/local.prop'
117
118# Configure ubsan to print stack traces in the format understood by "stack" so
119# that they will be symbolized, and disable signal handlers because they
120# interfere with the breakpad and sandbox tests.
121# This value is duplicated in
122# base/android/java/src/org/chromium/base/library_loader/LibraryLoader.java
123UBSAN_OPTIONS = (
124    'print_stacktrace=1 stack_trace_format=\'#%n pc %o %m\' '
125    'handle_segv=0 handle_sigbus=0 handle_sigfpe=0')
126
127# TODO(jbudorick): Rework this into testing/buildbot/
128PYTHON_UNIT_TEST_SUITES = {
129  'pylib_py_unittests': {
130    'path': os.path.join(DIR_SOURCE_ROOT, 'build', 'android'),
131    'test_modules': [
132      'devil.android.device_utils_test',
133      'devil.android.md5sum_test',
134      'devil.utils.cmd_helper_test',
135      'pylib.results.json_results_test',
136      'pylib.utils.proguard_test',
137    ]
138  },
139  'gyp_py_unittests': {
140    'path': os.path.join(DIR_SOURCE_ROOT, 'build', 'android', 'gyp'),
141    'test_modules': [
142      'java_cpp_enum_tests',
143      'java_google_api_keys_tests',
144      'extract_unwind_tables_tests',
145    ]
146  },
147}
148
149LOCAL_MACHINE_TESTS = ['junit', 'python']
150VALID_ENVIRONMENTS = ['local']
151VALID_TEST_TYPES = ['gtest', 'instrumentation', 'junit', 'linker', 'monkey',
152                    'perf', 'python']
153VALID_DEVICE_TYPES = ['Android', 'iOS']
154
155
156def SetBuildType(build_type):
157  """Set the BUILDTYPE environment variable.
158
159  NOTE: Using this function is deprecated, in favor of SetOutputDirectory(),
160        it is still maintained for a few scripts that typically call it
161        to implement their --release and --debug command-line options.
162
163        When writing a new script, consider supporting an --output-dir or
164        --chromium-output-dir option instead, and calling SetOutputDirectory()
165        instead.
166
167  NOTE: If CHROMIUM_OUTPUT_DIR if defined, or if SetOutputDirectory() was
168  called previously, this will be completely ignored.
169  """
170  chromium_output_dir = os.environ.get('CHROMIUM_OUTPUT_DIR')
171  if chromium_output_dir:
172    logging.warning(
173        'SetBuildType("%s") ignored since CHROMIUM_OUTPUT_DIR is already '
174        'defined as (%s)', build_type, chromium_output_dir)
175  os.environ['BUILDTYPE'] = build_type
176
177
178def SetOutputDirectory(output_directory):
179  """Set the Chromium output directory.
180
181  This must be called early by scripts that rely on GetOutDirectory() or
182  CheckOutputDirectory(). Typically by providing an --output-dir or
183  --chromium-output-dir option.
184  """
185  os.environ['CHROMIUM_OUTPUT_DIR'] = output_directory
186
187
188# The message that is printed when the Chromium output directory cannot
189# be found. Note that CHROMIUM_OUT_DIR and BUILDTYPE are not mentioned
190# intentionally to encourage the use of CHROMIUM_OUTPUT_DIR instead.
191_MISSING_OUTPUT_DIR_MESSAGE = '\
192The Chromium output directory could not be found. Please use an option such as \
193--output-directory to provide it (see --help for details). Otherwise, \
194define the CHROMIUM_OUTPUT_DIR environment variable.'
195
196
197def GetOutDirectory():
198  """Returns the Chromium build output directory.
199
200  NOTE: This is determined in the following way:
201    - From a previous call to SetOutputDirectory()
202    - Otherwise, from the CHROMIUM_OUTPUT_DIR env variable, if it is defined.
203    - Otherwise, from the current Chromium source directory, and a previous
204      call to SetBuildType() or the BUILDTYPE env variable, in combination
205      with the optional CHROMIUM_OUT_DIR env variable.
206  """
207  if 'CHROMIUM_OUTPUT_DIR' in os.environ:
208    return os.path.abspath(os.path.join(
209        DIR_SOURCE_ROOT, os.environ.get('CHROMIUM_OUTPUT_DIR')))
210
211  build_type = os.environ.get('BUILDTYPE')
212  if not build_type:
213    raise EnvironmentError(_MISSING_OUTPUT_DIR_MESSAGE)
214
215  return os.path.abspath(os.path.join(
216      DIR_SOURCE_ROOT, os.environ.get('CHROMIUM_OUT_DIR', 'out'),
217      build_type))
218
219
220def CheckOutputDirectory():
221  """Checks that the Chromium output directory is set, or can be found.
222
223  If it is not already set, this will also perform a little auto-detection:
224
225    - If the current directory contains a build.ninja file, use it as
226      the output directory.
227
228    - If CHROME_HEADLESS is defined in the environment (e.g. on a bot),
229      look if there is a single output directory under DIR_SOURCE_ROOT/out/,
230      and if so, use it as the output directory.
231
232  Raises:
233    Exception: If no output directory is detected.
234  """
235  output_dir = os.environ.get('CHROMIUM_OUTPUT_DIR')
236  if output_dir:
237    return
238
239  build_type = os.environ.get('BUILDTYPE')
240  if build_type and len(build_type) > 1:
241    return
242
243  # If CWD is an output directory, then assume it's the desired one.
244  if os.path.exists('build.ninja'):
245    output_dir = os.getcwd()
246    SetOutputDirectory(output_dir)
247    return
248
249  # When running on bots, see if the output directory is obvious.
250  # TODO(http://crbug.com/833808): Get rid of this by ensuring bots always set
251  # CHROMIUM_OUTPUT_DIR correctly.
252  if os.environ.get('CHROME_HEADLESS'):
253    dirs = glob.glob(os.path.join(DIR_SOURCE_ROOT, 'out', '*', 'build.ninja'))
254    if len(dirs) == 1:
255      SetOutputDirectory(dirs[0])
256      return
257
258    raise Exception(
259        'Chromium output directory not set, and CHROME_HEADLESS detected. ' +
260        'However, multiple out dirs exist: %r' % dirs)
261
262  raise Exception(_MISSING_OUTPUT_DIR_MESSAGE)
263
264
265# Exit codes
266ERROR_EXIT_CODE = exit_codes.ERROR
267INFRA_EXIT_CODE = exit_codes.INFRA
268WARNING_EXIT_CODE = exit_codes.WARNING
269