1# Copyright (C) 2023 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""java_test macro for building and running Java tests with Bazel."""
16
17load("@rules_java//java:defs.bzl", "java_binary")
18load(
19    "//build/bazel/rules/tradefed:tradefed.bzl",
20    "FILTER_GENERATOR_SUFFIX",
21    "LANGUAGE_JAVA",
22    "TEST_DEP_SUFFIX",
23    "java_test_filter_generator",
24    "tradefed_test_suite",
25)
26
27HOST_TEST_TEMPLATE = "//build/make/core:java_host_unit_test_config_template.xml"
28
29def java_test(
30        name = "",
31        srcs = [],
32        deps = [],
33        tags = [],
34        visibility = None,
35        target_compatible_with = [],
36        **kwargs):
37    """java_test macro for building and running Java tests with Bazel.
38
39    Args:
40      name: The name of this target.
41      srcs: The list of source files that are processed to create the target.
42      deps: The list of other libraries to be linked in to the target.
43      tags: Tags for the test binary target and test suite target.
44      visibility: Bazel visibility declarations for this target.
45      target_compatible_with: A list of constraint_values that must be present
46        in the target platform for this target to be considered compatible.
47      **kwargs: map, additional args to pass to android_binary.
48    """
49    test_dep_name = name + TEST_DEP_SUFFIX
50    java_binary_name = name + "_jb"
51
52    # tradefed_test_suite uses the _deploy.jar from this java_binary to execute tests.
53    java_binary(
54        name = java_binary_name,
55        srcs = srcs,
56        deps = deps,
57        create_executable = False,
58        tags = tags + ["manual"],
59        visibility = visibility,
60        target_compatible_with = target_compatible_with,
61        **kwargs
62    )
63
64    native.filegroup(
65        name = test_dep_name,
66        srcs = [java_binary_name + "_deploy.jar"],
67    )
68
69    test_filter_generator_name = name + FILTER_GENERATOR_SUFFIX
70    java_test_filter_generator(
71        name = test_filter_generator_name,
72        srcs = srcs,
73        module_name = name,
74    )
75
76    tradefed_test_suite(
77        name = name,
78        test_dep = test_dep_name,
79        test_config = None,
80        template_test_config = None,
81        template_configs = None,
82        template_install_base = None,
83        test_filter_generator = test_filter_generator_name,
84        tags = tags,
85        test_language = LANGUAGE_JAVA,
86        visibility = visibility,
87        deviceless_test_config = HOST_TEST_TEMPLATE,
88    )
89