1# Copyright 2022 Google LLC 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"""Common methods to generate test suites.""" 15 16load("//devtools/deps/check:deps_check.bzl", "check_dependencies") 17load( 18 "@build_bazel_rules_android//android:rules.bzl", 19 "android_application_test", 20 "android_local_test", 21 "infer_test_class_from_srcs", 22) 23load("//devtools/build_cleaner/skylark:build_defs.bzl", "register_extension_info") 24 25_MANIFEST_VALUES = { 26 "minSdkVersion": "16", 27 "targetSdkVersion": "29", 28} 29 30def mdd_local_test(name, deps, manifest_values = _MANIFEST_VALUES, **kwargs): 31 """Generate 2 flavors for android_local_test, with default behavior and with all flags on. 32 33 The all on testing only works as expected if the test includes flagrule/FlagRule as a rule in 34 the test. 35 36 Args: 37 name: the test name 38 deps: a list of deps 39 manifest_values: manifest values to use. 40 **kwargs: Any other param that is supported by <internal>. 41 """ 42 43 android_local_test( 44 name = name, 45 deps = deps, 46 manifest_values = manifest_values, 47 **kwargs 48 ) 49 50# See <internal> 51register_extension_info( 52 extension = mdd_local_test, 53 label_regex_for_dep = "(all_on_)?{extension_name}", 54) 55 56# Run mdd_android_test on an assortment of emulators. 57# The last item in the list will be used as the default. 58_EMULATOR_IMAGES = [ 59 # Automotive 60 "//tools/android/emulated_devices/automotive:auto_29_x86", 61 "//tools/android/emulated_devices/automotive:auto_30_x86", 62 63 # Android Phone 64 "//tools/android/emulated_devices/generic_phone:google_21_x86_gms_stable", 65 "//tools/android/emulated_devices/generic_phone:google_22_x86_gms_stable", 66 "//tools/android/emulated_devices/generic_phone:google_23_x86_gms_stable", 67 "//tools/android/emulated_devices/generic_phone:google_24_x86_gms_stable", 68 "//tools/android/emulated_devices/generic_phone:google_25_x86_gms_stable", 69 "//tools/android/emulated_devices/generic_phone:google_26_x86_gms_stable", 70 "//tools/android/emulated_devices/generic_phone:google_27_x86_gms_stable", 71 "//tools/android/emulated_devices/generic_phone:google_28_x86_gms_stable", 72] 73 74# Logcat filter flags. 75_COMMON_LOGCAT_ARGS = [ 76 "--test_logcat_filter='MDD:V,MobileDataDownload:V'", 77] 78 79# This ensures that the `android_application_test` will merge the resources in its dependencies into the 80# test binary, just like an `android_binary` rule does. 81# This is a workaround for b/111061456. 82_EMPTY_LOCAL_RESOURCE_FILES = [] 83 84# Parameterized Integration Tests use TestParameterInjector (only supported at API level >= 24) 85# This list represents the emulator images that should be used rather than the default full list. 86PARAMETERIZED_EMULATOR_IMAGES = [ 87 # Automotive 88 "//tools/android/emulated_devices/automotive:auto_29_x86", 89 "//tools/android/emulated_devices/automotive:auto_30_x86", 90 91 # Android Phone 92 "//tools/android/emulated_devices/generic_phone:google_24_x86_gms_stable", 93 "//tools/android/emulated_devices/generic_phone:google_25_x86_gms_stable", 94 "//tools/android/emulated_devices/generic_phone:google_26_x86_gms_stable", 95 "//tools/android/emulated_devices/generic_phone:google_27_x86_gms_stable", 96 "//tools/android/emulated_devices/generic_phone:google_28_x86_gms_stable", 97 "//tools/android/emulated_devices/generic_phone:google_29_x86_gms_stable", 98 "//tools/android/emulated_devices/generic_phone:google_30_x86_gms_stable", 99] 100 101# Wrapper around android_application_test to generate targets for multiple emulator images. 102def mdd_android_test(name, target_devices = _EMULATOR_IMAGES, **kwargs): 103 """Generate an android_application_test for MDD. 104 105 Args: 106 name: the test name 107 target_devices: one or more emulators to run the test on. 108 The default test name will run on the last emulator provided. 109 If additional emulators are listed, additional test targets will be 110 created by appending the emulator name to the original test name 111 **kwargs: Any keyword arguments to be passed. 112 113 Returns: 114 each android_application_test per emulator image. 115 """ 116 117 deps = kwargs.pop("deps", []) 118 multidex = kwargs.pop("multidex", "native") 119 srcs = kwargs.pop("srcs", []) 120 test_class = kwargs.pop("test_class", infer_test_class_from_srcs(name, srcs)) 121 122 # create a BUILD target for the last element of target_devices, with the basic test name 123 target_device = target_devices[-1] 124 android_application_test( 125 name = name, 126 srcs = srcs, 127 deps = deps, 128 multidex = multidex, 129 local_resource_files = _EMPTY_LOCAL_RESOURCE_FILES, 130 args = _COMMON_LOGCAT_ARGS, 131 target_devices = [target_device], 132 test_class = test_class, 133 **kwargs 134 ) 135 136 # if there are multiple target_devices, create named BUILD targets for all the other ones except 137 # the last one. 138 if len(target_devices) > 1: 139 for target_device in target_devices[:-1]: 140 target_device_name = target_device.replace("//tools/android/emulated_devices/", "").replace(":", "_") 141 android_application_test( 142 name = name + "_" + target_device_name, 143 srcs = srcs, 144 deps = deps, 145 multidex = multidex, 146 local_resource_files = _EMPTY_LOCAL_RESOURCE_FILES, 147 args = _COMMON_LOGCAT_ARGS, 148 target_devices = [target_device], 149 test_class = test_class, 150 **kwargs 151 ) 152 153# Wrapper around check_dependencies. 154def dependencies_test(name, whitelist = [], **kwargs): 155 """Generate a dependencies_test for MDD. 156 157 Args: 158 name: The test name. 159 whitelist: The excluded targets under the package. 160 **kwargs: Any keyword arguments to be passed. 161 """ 162 all_builds = [] 163 for r in native.existing_rules().values(): 164 whitelisted = False 165 for build in whitelist: 166 # Ignore the leading colon in build. 167 if build[1:] in r["name"]: 168 whitelisted = True 169 break 170 if not whitelisted: 171 all_builds.append(r["name"]) 172 check_dependencies( 173 name = name, 174 of = [":" + build for build in all_builds], 175 **kwargs 176 ) 177