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