summaryrefslogtreecommitdiff
path: root/javatests/com/google/android/libraries/mobiledatadownload/test_defs.bzl
blob: 964756d15d8a24cfe688d4e9deb8611b076aa53a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Common methods to generate test suites."""

load("//devtools/deps/check:deps_check.bzl", "check_dependencies")
load(
    "@build_bazel_rules_android//android:rules.bzl",
    "android_application_test",
    "android_local_test",
    "infer_test_class_from_srcs",
)
load("//devtools/build_cleaner/skylark:build_defs.bzl", "register_extension_info")

_MANIFEST_VALUES = {
    "minSdkVersion": "16",
    "targetSdkVersion": "29",
}

def mdd_local_test(name, deps, manifest_values = _MANIFEST_VALUES, **kwargs):
    """Generate 2 flavors for android_local_test, with default behavior and with all flags on.

    The all on testing only works as expected if the test includes flagrule/FlagRule as a rule in
    the test.

    Args:
      name: the test name
      deps: a list of deps
      manifest_values: manifest values to use.
      **kwargs: Any other param that is supported by <internal>.
    """

    android_local_test(
        name = name,
        deps = deps,
        manifest_values = manifest_values,
        **kwargs
    )

# See <internal>
register_extension_info(
    extension = mdd_local_test,
    label_regex_for_dep = "(all_on_)?{extension_name}",
)

# Run mdd_android_test on an assortment of emulators.
# The last item in the list will be used as the default.
_EMULATOR_IMAGES = [
    # Automotive
    "//tools/android/emulated_devices/automotive:auto_29_x86",

    # Android Phone
    "//tools/android/emulated_devices/generic_phone:google_21_x86_gms_stable",
    "//tools/android/emulated_devices/generic_phone:google_22_x86_gms_stable",
    "//tools/android/emulated_devices/generic_phone:google_23_x86_gms_stable",
    "//tools/android/emulated_devices/generic_phone:google_24_x86_gms_stable",
    "//tools/android/emulated_devices/generic_phone:google_25_x86_gms_stable",
    "//tools/android/emulated_devices/generic_phone:google_26_x86_gms_stable",
    "//tools/android/emulated_devices/generic_phone:google_27_x86_gms_stable",
    "//tools/android/emulated_devices/generic_phone:google_28_x86_gms_stable",
]

# Logcat filter flags.
_COMMON_LOGCAT_ARGS = [
    "--test_logcat_filter='MDD:V,MobileDataDownload:V'",
]

# This ensures that the `android_application_test` will merge the resources in its dependencies into the
# test binary, just like an `android_binary` rule does.
# This is a workaround for b/111061456.
_EMPTY_LOCAL_RESOURCE_FILES = []

# Wrapper around android_application_test to generate targets for multiple emulator images.
def mdd_android_test(name, target_devices = _EMULATOR_IMAGES, **kwargs):
    """Generate an android_application_test for MDD.

    Args:
      name: the test name
      target_devices: one or more emulators to run the test on.
        The default test name will run on the last emulator provided.
        If additional emulators are listed, additional test targets will be
        created by appending the emulator name to the original test name
      **kwargs: Any keyword arguments to be passed.

    Returns:
      each android_application_test per emulator image.
    """

    deps = kwargs.pop("deps", [])
    multidex = kwargs.pop("multidex", "native")
    srcs = kwargs.pop("srcs", [])
    test_class = kwargs.pop("test_class", infer_test_class_from_srcs(name, srcs))

    # create a BUILD target for the last element of target_devices, with the basic test name
    target_device = target_devices[-1]
    android_application_test(
        name = name,
        srcs = srcs,
        deps = deps,
        multidex = multidex,
        local_resource_files = _EMPTY_LOCAL_RESOURCE_FILES,
        args = _COMMON_LOGCAT_ARGS,
        target_devices = [target_device],
        test_class = test_class,
        **kwargs
    )

    # if there are multiple target_devices, create named BUILD targets for all the other ones except
    # the last one.
    if len(target_devices) > 1:
        for target_device in target_devices[:-1]:
            target_device_name = target_device.replace("//tools/android/emulated_devices/", "").replace(":", "_")
            android_application_test(
                name = name + "_" + target_device_name,
                srcs = srcs,
                deps = deps,
                multidex = multidex,
                local_resource_files = _EMPTY_LOCAL_RESOURCE_FILES,
                args = _COMMON_LOGCAT_ARGS,
                target_devices = [target_device],
                test_class = test_class,
                **kwargs
            )

# Wrapper around check_dependencies.
def dependencies_test(name, allowlist = [], **kwargs):
    """Generate a dependencies_test for MDD.

    Args:
      name: The test name.
      allowlist: The excluded targets under the package.
      **kwargs: Any keyword arguments to be passed.
    """
    all_builds = []
    for r in native.existing_rules().values():
        allowlisted = False
        for build in allowlist:
            # Ignore the leading colon in build.
            if build[1:] in r["name"]:
                allowlisted = True
                break
        if not allowlisted:
            all_builds.append(r["name"])
    check_dependencies(
        name = name,
        of = [":" + build for build in all_builds],
        **kwargs
    )