[Android] Generate scripts at build time to run android tests.

Making changes so that at build time scripts will be generated that can be used to easily run tests.

BUG=490781
CQ_INCLUDE_TRYBOTS=tryserver.blink:linux_blink_rel

Review URL: https://codereview.chromium.org/1120883002

Cr-Original-Commit-Position: refs/heads/master@{#332518}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: 56d80d7a4c1ff50bf21112486cc9b2308c7e7bf0
This commit is contained in:
mikecase 2015-06-02 17:57:26 -07:00 коммит произвёл Commit bot
Родитель 65db813b72
Коммит d587fae3e6
5 изменённых файлов: 325 добавлений и 1 удалений

Просмотреть файл

@ -0,0 +1,92 @@
#!/usr/bin/env python
#
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Creates a script to run an android test using build/android/test_runner.py.
"""
import argparse
import os
import sys
from util import build_utils
SCRIPT_TEMPLATE = """\
#!/usr/bin/env python
#
# This file was generated by build/android/gyp/create_test_runner_script.py
import logging
import os
import sys
def main():
script_directory = os.path.dirname(__file__)
def ResolvePath(path):
\"\"\"Returns an absolute filepath given a path relative to this script.
\"\"\"
return os.path.abspath(os.path.join(script_directory, path))
test_runner_path = ResolvePath('{test_runner_path}')
test_runner_args = {test_runner_args}
test_runner_path_args = {test_runner_path_args}
for arg, path in test_runner_path_args.iteritems():
test_runner_args.extend([arg, ResolvePath(path)])
test_runner_cmd = ' '.join(
[test_runner_path] + test_runner_args + sys.argv[1:])
logging.critical(test_runner_cmd)
os.system(test_runner_cmd)
if __name__ == '__main__':
sys.exit(main())
"""
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--script-output-path',
help='Output path for executable script.')
parser.add_argument('--depfile',
help='Path to the depfile. This must be specified as '
"the action's first output.")
# We need to intercept any test runner path arguments and make all
# of the paths relative to the output script directory.
group = parser.add_argument_group('Test runner path arguments.')
group.add_argument('--output-directory')
group.add_argument('--isolate-file-path')
args, test_runner_args = parser.parse_known_args()
def RelativizePathToScript(path):
"""Returns the path relative to the output script directory."""
return os.path.relpath(path, os.path.dirname(args.script_output_path))
test_runner_path = os.path.join(
os.path.dirname(__file__), os.path.pardir, 'test_runner.py')
test_runner_path = RelativizePathToScript(test_runner_path)
test_runner_path_args = {}
if args.output_directory:
test_runner_path_args['--output-directory'] = RelativizePathToScript(
args.output_directory)
if args.isolate_file_path:
test_runner_path_args['--isolate-file-path'] = RelativizePathToScript(
args.isolate_file_path)
with open(args.script_output_path, 'w') as script:
script.write(SCRIPT_TEMPLATE.format(
test_runner_path=str(test_runner_path),
test_runner_args=str(test_runner_args),
test_runner_path_args=str(test_runner_path_args)))
os.chmod(args.script_output_path, 0750)
if args.depfile:
build_utils.WriteDepfile(
args.depfile,
build_utils.GetPythonDependencies())
if __name__ == '__main__':
sys.exit(main())

72
android/test_runner.gypi Normal file
Просмотреть файл

@ -0,0 +1,72 @@
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Generates a script in the output bin directory which runs the test
# target using the test runner script in build/android/pylib/test_runner.py.
#
# To use this, include this file in a gtest or instrumentation test target.
# {
# 'target_name': 'gtest',
# 'type': 'none',
# 'variables': {
# 'test_type': 'gtest', # string
# 'test_suite_name': 'gtest_suite' # string
# 'isolate_file': 'path/to/gtest.isolate' # string
# },
# 'includes': ['path/to/this/gypi/file'],
# }
#
# {
# 'target_name': 'instrumentation_apk',
# 'type': 'none',
# 'variables': {
# 'test_type': 'instrumentation', # string
# 'apk_name': 'TestApk' # string
# 'isolate_file': 'path/to/instrumentation_test.isolate' # string
# },
# 'includes': ['path/to/this/gypi/file'],
# }
#
{
'variables': {
'variables': {
'isolate_file%': '',
},
'test_runner_args': ['--output-directory', '<(PRODUCT_DIR)'],
'conditions': [
['test_type == "gtest"', {
'test_runner_args': ['--suite', '<(test_suite_name)'],
'script_name': 'run_<(test_suite_name)',
}],
['test_type == "instrumentation"', {
'test_runner_args': ['--test-apk', '<(apk_name)'],
'script_name': 'run_<(_target_name)',
}],
['isolate_file != ""', {
'test_runner_args': ['--isolate-file-path', '<(isolate_file)']
}],
],
},
'actions': [
{
'action_name': 'create_test_runner_script_<(script_name)',
'message': 'Creating test runner script <(script_name)',
'variables': {
'script_output_path': '<(PRODUCT_DIR)/bin/<(script_name)',
},
'inputs': [
'<(DEPTH)/build/android/gyp/create_test_runner_script.py',
],
'outputs': [
'<(script_output_path)'
],
'action': [
'python', '<(DEPTH)/build/android/gyp/create_test_runner_script.py',
'--script-output-path=<(script_output_path)',
'<(test_type)', '<@(test_runner_args)',
],
},
],
}

Просмотреть файл

@ -29,6 +29,7 @@
['OS == "android"', {
'variables': {
# These are used to configure java_apk.gypi included below.
'test_type': 'gtest',
'apk_name': '<(test_suite_name)',
'intermediate_dir': '<(PRODUCT_DIR)/<(test_suite_name)_apk',
'final_apk_path': '<(intermediate_dir)/<(test_suite_name)-debug.apk',
@ -37,7 +38,7 @@
# TODO(yfriedman, cjhopman): Support managed installs for gtests.
'gyp_managed_install': 0,
},
'includes': [ 'java_apk.gypi' ],
'includes': [ 'java_apk.gypi', 'android/test_runner.gypi' ],
}], # 'OS == "android"
], # conditions
}

Просмотреть файл

@ -1415,3 +1415,57 @@ template("generate_split_manifest") {
]
}
}
# Generates a script in the output bin directory which runs the test
# target using the test runner script in build/android/test_runner.py.
template("test_runner_script") {
testonly = true
_test_name = invoker.test_name
_test_type = invoker.test_type
action(target_name) {
script = "//build/android/gyp/create_test_runner_script.py"
depfile = "$target_gen_dir/$target_name.d"
test_runner_args = [
_test_type,
"--output-directory",
rebase_path(root_build_dir, root_build_dir),
]
if (_test_type == "gtest") {
assert(defined(invoker.test_suite))
test_runner_args += [
"--suite",
invoker.test_suite,
]
} else if (_test_type == "instrumentation") {
assert(defined(invoker.test_apk))
test_runner_args += [
"--test-apk",
invoker.test_apk,
]
} else {
assert(false, "Invalid test type: $_test_type.")
}
if (defined(invoker.isolate_file)) {
test_runner_args += [
"--isolate-file-path",
rebase_path(invoker.isolate_file, root_build_dir),
]
}
generated_script = "$root_build_dir/bin/run_${_test_name}"
outputs = [
depfile,
generated_script,
]
args = [
"--depfile",
rebase_path(depfile, root_build_dir),
"--script-output-path",
rebase_path(generated_script, root_build_dir),
]
args += test_runner_args
}
}

Просмотреть файл

@ -1644,6 +1644,111 @@ template("android_apk") {
}
}
# Declare an Android instrumentation test apk
#
# This target creates an Android instrumentation test apk.
#
# Variables
# android_manifest: Path to AndroidManifest.xml.
# data_deps: List of dependencies needed at runtime. These will be built but
# won't change the generated .apk in any way (in fact they may be built
# after the .apk is).
# deps: List of dependencies. All Android java resources and libraries in the
# "transitive closure" of these dependencies will be included in the apk.
# Note: this "transitive closure" actually only includes such targets if
# they are depended on through android_library or android_resources targets
# (and so not through builtin targets like 'action', 'group', etc).
# java_files: List of .java files to include in the apk.
# srcjar_deps: List of srcjar dependencies. The .java files in the srcjars
# will be added to java_files and be included in this apk.
# apk_name: Name for final apk.
# final_apk_path: Path to final built apk. Default is
# $root_out_dir/apks/$apk_name.apk. Setting this will override apk_name.
# native_libs: List paths of native libraries to include in this apk. If these
# libraries depend on other shared_library targets, those dependencies will
# also be included in the apk.
# apk_under_test: The apk being tested.
# isolate_file: Isolate file containing the list of test data dependencies.
#
# DEPRECATED_java_in_dir: Directory containing java files. All .java files in
# this directory will be included in the library. This is only supported to
# ease the gyp->gn conversion and will be removed in the future.
#
# Example
# instrumentation_test_apk("foo_test_apk") {
# android_manifest = "AndroidManifest.xml"
# apk_name = "FooTest"
# apk_under_test = "Foo"
# java_files = [
# "android/org/chromium/foo/FooTestCase.java",
# "android/org/chromium/foo/FooExampleTest.java",
# ]
# deps = [
# ":foo_test_support_java"
# ]
# }
template("instrumentation_test_apk") {
set_sources_assignment_filter([])
testonly = true
_template_name = target_name
if (defined(invoker.apk_name)) {
test_runner_data_dep = [ ":${_template_name}__test_runner_script" ]
test_runner_script("${_template_name}__test_runner_script") {
test_name = invoker.target_name
test_type = "instrumentation"
test_apk = invoker.apk_name
if (defined(invoker.isolate_file)) {
isolate_file = invoker.isolate_file
}
}
}
android_apk(target_name) {
if (defined(invoker.android_manifest)) {
android_manifest = invoker.android_manifest
}
data_deps = [
"//testing/android/driver:driver_apk",
"//tools/android/forwarder2",
"//tools/android/md5sum",
]
if (defined(test_runner_data_dep)) {
data_deps += test_runner_data_dep
}
if (defined(invoker.data_deps)) {
data_deps += invoker.data_deps
}
deps = [
"//testing/android/broker:broker_java",
]
if (defined(invoker.deps)) {
deps += invoker.deps
}
if (defined(invoker.java_files)) {
java_files = invoker.java_files
}
if (defined(invoker.srcjar_deps)) {
srcjar_deps = invoker.srcjar_deps
}
if (defined(invoker.apk_name)) {
apk_name = invoker.apk_name
}
if (defined(invoker.final_apk_path)) {
final_apk_path = invoker.final_apk_path
}
if (defined(invoker.native_libs)) {
native_libs = invoker.native_libs
}
if (defined(invoker.apk_under_test)) {
apk_under_test = invoker.apk_under_test
}
if (defined(invoker.DEPRECATED_java_in_dir)) {
DEPRECATED_java_in_dir = invoker.DEPRECATED_java_in_dir
}
}
}
# Declare an Android gtest apk
#
# This target creates an Android apk for running gtest-based unittests.