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.sdk import version_codes
19from devil.constants import exit_codes
20
21
22keyevent = devil.android.sdk.keyevent
23
24
25DIR_SOURCE_ROOT = os.environ.get('CHECKOUT_SOURCE_ROOT',
26    os.path.abspath(os.path.join(os.path.dirname(__file__),
27                                 os.pardir, os.pardir, os.pardir, os.pardir)))
28
29PackageInfo = collections.namedtuple('PackageInfo',
30    ['package', 'activity', 'cmdline_file', 'devtools_socket'])
31
32PACKAGE_INFO = {
33    'chrome_document': PackageInfo(
34        'com.google.android.apps.chrome.document',
35        'com.google.android.apps.chrome.document.ChromeLauncherActivity',
36        '/data/local/chrome-command-line',
37        'chrome_devtools_remote'),
38    'chrome': PackageInfo(
39        'com.google.android.apps.chrome',
40        'com.google.android.apps.chrome.Main',
41        '/data/local/chrome-command-line',
42        'chrome_devtools_remote'),
43    'chrome_beta': PackageInfo(
44        'com.chrome.beta',
45        'com.google.android.apps.chrome.Main',
46        '/data/local/chrome-command-line',
47        'chrome_devtools_remote'),
48    'chrome_stable': PackageInfo(
49        'com.android.chrome',
50        'com.google.android.apps.chrome.Main',
51        '/data/local/chrome-command-line',
52        'chrome_devtools_remote'),
53    'chrome_dev': PackageInfo(
54        'com.chrome.dev',
55        'com.google.android.apps.chrome.Main',
56        '/data/local/chrome-command-line',
57        'chrome_devtools_remote'),
58    'chrome_canary': PackageInfo(
59        'com.chrome.canary',
60        'com.google.android.apps.chrome.Main',
61        '/data/local/chrome-command-line',
62        'chrome_devtools_remote'),
63    'chrome_work': PackageInfo(
64        'com.chrome.work',
65        'com.google.android.apps.chrome.Main',
66        '/data/local/chrome-command-line',
67        'chrome_devtools_remote'),
68    'chromium': PackageInfo(
69        'org.chromium.chrome',
70        'com.google.android.apps.chrome.Main',
71        '/data/local/chrome-command-line',
72        'chrome_devtools_remote'),
73    'legacy_browser': PackageInfo(
74        'com.google.android.browser',
75        'com.android.browser.BrowserActivity',
76        None,
77        None),
78    'chromecast_shell': PackageInfo(
79        'com.google.android.apps.mediashell',
80        'com.google.android.apps.mediashell.MediaShellActivity',
81        '/data/local/tmp/castshell-command-line',
82        None),
83    'content_shell': PackageInfo(
84        'org.chromium.content_shell_apk',
85        'org.chromium.content_shell_apk.ContentShellActivity',
86        '/data/local/tmp/content-shell-command-line',
87        None),
88    'android_webview_shell': PackageInfo(
89        'org.chromium.android_webview.shell',
90        'org.chromium.android_webview.shell.AwShellActivity',
91        '/data/local/tmp/android-webview-command-line',
92        None),
93    'gtest': PackageInfo(
94        'org.chromium.native_test',
95        'org.chromium.native_test.NativeUnitTestActivity',
96        '/data/local/tmp/chrome-native-tests-command-line',
97        None),
98    'components_browsertests': PackageInfo(
99        'org.chromium.components_browsertests_apk',
100        ('org.chromium.components_browsertests_apk' +
101         '.ComponentsBrowserTestsActivity'),
102        '/data/local/tmp/chrome-native-tests-command-line',
103        None),
104    'content_browsertests': PackageInfo(
105        'org.chromium.content_browsertests_apk',
106        'org.chromium.content_browsertests_apk.ContentBrowserTestsActivity',
107        '/data/local/tmp/chrome-native-tests-command-line',
108        None),
109    'chromedriver_webview_shell': PackageInfo(
110        'org.chromium.chromedriver_webview_shell',
111        'org.chromium.chromedriver_webview_shell.Main',
112        None,
113        None),
114}
115
116
117# Ports arrangement for various test servers used in Chrome for Android.
118# Lighttpd server will attempt to use 9000 as default port, if unavailable it
119# will find a free port from 8001 - 8999.
120LIGHTTPD_DEFAULT_PORT = 9000
121LIGHTTPD_RANDOM_PORT_FIRST = 8001
122LIGHTTPD_RANDOM_PORT_LAST = 8999
123TEST_SYNC_SERVER_PORT = 9031
124TEST_SEARCH_BY_IMAGE_SERVER_PORT = 9041
125TEST_POLICY_SERVER_PORT = 9051
126
127
128TEST_EXECUTABLE_DIR = '/data/local/tmp'
129# Directories for common java libraries for SDK build.
130# These constants are defined in build/android/ant/common.xml
131SDK_BUILD_JAVALIB_DIR = 'lib.java'
132SDK_BUILD_TEST_JAVALIB_DIR = 'test.lib.java'
133SDK_BUILD_APKS_DIR = 'apks'
134
135ADB_KEYS_FILE = '/data/misc/adb/adb_keys'
136
137PERF_OUTPUT_DIR = os.path.join(DIR_SOURCE_ROOT, 'out', 'step_results')
138# The directory on the device where perf test output gets saved to.
139DEVICE_PERF_OUTPUT_DIR = (
140    '/data/data/' + PACKAGE_INFO['chrome'].package + '/files')
141
142SCREENSHOTS_DIR = os.path.join(DIR_SOURCE_ROOT, 'out_screenshots')
143
144ANDROID_SDK_VERSION = version_codes.MARSHMALLOW
145ANDROID_SDK_BUILD_TOOLS_VERSION = '23.0.1'
146ANDROID_SDK_ROOT = os.path.join(DIR_SOURCE_ROOT,
147                                'third_party', 'android_tools', 'sdk')
148ANDROID_SDK_TOOLS = os.path.join(ANDROID_SDK_ROOT,
149                                 'build-tools', ANDROID_SDK_BUILD_TOOLS_VERSION)
150ANDROID_NDK_ROOT = os.path.join(DIR_SOURCE_ROOT,
151                                'third_party', 'android_tools', 'ndk')
152
153PROGUARD_SCRIPT_PATH = os.path.join(
154    ANDROID_SDK_ROOT, 'tools', 'proguard', 'bin', 'proguard.sh')
155
156PROGUARD_ROOT = os.path.join(DIR_SOURCE_ROOT, 'third_party', 'proguard')
157
158BAD_DEVICES_JSON = os.path.join(DIR_SOURCE_ROOT,
159                                os.environ.get('CHROMIUM_OUT_DIR', 'out'),
160                                'bad_devices.json')
161
162UPSTREAM_FLAKINESS_SERVER = 'test-results.appspot.com'
163
164# TODO(jbudorick): Remove once unused.
165DEVICE_LOCAL_PROPERTIES_PATH = '/data/local.prop'
166
167# TODO(jbudorick): Rework this into testing/buildbot/
168PYTHON_UNIT_TEST_SUITES = {
169  'pylib_py_unittests': {
170    'path': os.path.join(DIR_SOURCE_ROOT, 'build', 'android'),
171    'test_modules': [
172      'devil.android.device_utils_test',
173      'devil.android.md5sum_test',
174      'devil.utils.cmd_helper_test',
175      'pylib.results.json_results_test',
176      'pylib.utils.proguard_test',
177    ]
178  },
179  'gyp_py_unittests': {
180    'path': os.path.join(DIR_SOURCE_ROOT, 'build', 'android', 'gyp'),
181    'test_modules': [
182      'java_cpp_enum_tests',
183      'java_google_api_keys_tests',
184    ]
185  },
186}
187
188LOCAL_MACHINE_TESTS = ['junit', 'python']
189VALID_ENVIRONMENTS = ['local', 'remote_device']
190VALID_TEST_TYPES = ['gtest', 'instrumentation', 'junit', 'linker', 'monkey',
191                    'perf', 'python', 'uirobot']
192VALID_DEVICE_TYPES = ['Android', 'iOS']
193
194
195def GetBuildType():
196  try:
197    return os.environ['BUILDTYPE']
198  except KeyError:
199    raise EnvironmentError(
200        'The BUILDTYPE environment variable has not been set')
201
202
203def SetBuildType(build_type):
204  os.environ['BUILDTYPE'] = build_type
205
206
207def SetBuildDirectory(build_directory):
208  os.environ['CHROMIUM_OUT_DIR'] = build_directory
209
210
211def SetOutputDirectory(output_directory):
212  os.environ['CHROMIUM_OUTPUT_DIR'] = output_directory
213
214
215def GetOutDirectory(build_type=None):
216  """Returns the out directory where the output binaries are built.
217
218  Args:
219    build_type: Build type, generally 'Debug' or 'Release'. Defaults to the
220      globally set build type environment variable BUILDTYPE.
221  """
222  if 'CHROMIUM_OUTPUT_DIR' in os.environ:
223    return os.path.abspath(os.path.join(
224        DIR_SOURCE_ROOT, os.environ.get('CHROMIUM_OUTPUT_DIR')))
225
226  return os.path.abspath(os.path.join(
227      DIR_SOURCE_ROOT, os.environ.get('CHROMIUM_OUT_DIR', 'out'),
228      GetBuildType() if build_type is None else build_type))
229
230
231def CheckOutputDirectory():
232  """Checks that CHROMIUM_OUT_DIR or CHROMIUM_OUTPUT_DIR is set.
233
234  If neither are set, but the current working directory is a build directory,
235  then CHROMIUM_OUTPUT_DIR is set to the current working directory.
236
237  Raises:
238    Exception: If no output directory is detected.
239  """
240  output_dir = os.environ.get('CHROMIUM_OUTPUT_DIR')
241  out_dir = os.environ.get('CHROMIUM_OUT_DIR')
242  if not output_dir and not out_dir:
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    elif os.environ.get('CHROME_HEADLESS'):
248      # When running on bots, see if the output directory is obvious.
249      dirs = glob.glob(os.path.join(DIR_SOURCE_ROOT, 'out', '*', 'build.ninja'))
250      if len(dirs) == 1:
251        SetOutputDirectory(dirs[0])
252      else:
253        raise Exception('Neither CHROMIUM_OUTPUT_DIR nor CHROMIUM_OUT_DIR '
254                        'has been set. CHROME_HEADLESS detected, but multiple '
255                        'out dirs exist: %r' % dirs)
256    else:
257      raise Exception('Neither CHROMIUM_OUTPUT_DIR nor CHROMIUM_OUT_DIR '
258                      'has been set')
259
260
261# TODO(jbudorick): Convert existing callers to AdbWrapper.GetAdbPath() and
262# remove this.
263def GetAdbPath():
264  from devil.android.sdk import adb_wrapper
265  return adb_wrapper.AdbWrapper.GetAdbPath()
266
267
268# Exit codes
269ERROR_EXIT_CODE = exit_codes.ERROR
270INFRA_EXIT_CODE = exit_codes.INFRA
271WARNING_EXIT_CODE = exit_codes.WARNING
272