1# Copyright 2015 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# ============================================================================== 6# TEST SETUP 7# ============================================================================== 8 9# Define a test as an executable (or apk on Android) with the "testonly" flag 10# set. 11# Variable: 12# use_raw_android_executable: Use executable() rather than android_apk(). 13# use_native_activity: Test implements ANativeActivity_onCreate(). 14template("test") { 15 if (is_android) { 16 import("//build/config/android/config.gni") 17 import("//build/config/android/rules.gni") 18 19 _use_raw_android_executable = defined(invoker.use_raw_android_executable) && 20 invoker.use_raw_android_executable 21 22 # output_name is used to allow targets with the same name but in different 23 # packages to still produce unique runner scripts. 24 _output_name = invoker.target_name 25 if (defined(invoker.output_name)) { 26 _output_name = invoker.output_name 27 } 28 29 _test_runner_target = "${_output_name}__test_runner_script" 30 _wrapper_script_vars = [ 31 "ignore_all_data_deps", 32 "shard_timeout", 33 ] 34 35 assert(_use_raw_android_executable || enable_java_templates) 36 37 if (_use_raw_android_executable) { 38 _exec_target = "${target_name}__exec" 39 _dist_target = "${target_name}__dist" 40 _exec_output = 41 "$target_out_dir/${invoker.target_name}/${invoker.target_name}" 42 43 executable(_exec_target) { 44 # Configs will always be defined since we set_defaults in BUILDCONFIG.gn. 45 configs = [] 46 data_deps = [] 47 forward_variables_from(invoker, 48 "*", 49 _wrapper_script_vars + [ "extra_dist_files" ]) 50 testonly = true 51 52 # Thanks to the set_defaults() for test(), configs are initialized with 53 # the default shared_library configs rather than executable configs. 54 configs -= [ 55 "//build/config:shared_library_config", 56 "//build/config/android:hide_all_but_jni_onload", 57 ] 58 configs += [ 59 "//build/config:executable_config", 60 "//build/config/android:hide_all_but_jni", 61 ] 62 63 # Don't output to the root or else conflict with the group() below. 64 output_name = rebase_path(_exec_output, root_out_dir) 65 } 66 67 create_native_executable_dist(_dist_target) { 68 testonly = true 69 dist_dir = "$root_out_dir/$target_name" 70 binary = _exec_output 71 deps = [ 72 ":$_exec_target", 73 ] 74 if (defined(invoker.extra_dist_files)) { 75 extra_files = invoker.extra_dist_files 76 } 77 } 78 } else { 79 _library_target = "_${target_name}__library" 80 _apk_target = "${target_name}_apk" 81 _apk_specific_vars = [ 82 "android_manifest", 83 "android_manifest_dep", 84 "enable_multidex", 85 "proguard_configs", 86 "proguard_enabled", 87 "use_default_launcher", 88 "write_asset_list", 89 "use_native_activity", 90 ] 91 shared_library(_library_target) { 92 # Configs will always be defined since we set_defaults in BUILDCONFIG.gn. 93 configs = [] # Prevent list overwriting warning. 94 configs = invoker.configs 95 testonly = true 96 97 deps = [] 98 forward_variables_from( 99 invoker, 100 "*", 101 _apk_specific_vars + _wrapper_script_vars + [ "visibility" ]) 102 103 if (!defined(invoker.use_default_launcher) || 104 invoker.use_default_launcher) { 105 deps += [ "//testing/android/native_test:native_test_native_code" ] 106 } 107 } 108 unittest_apk(_apk_target) { 109 forward_variables_from(invoker, _apk_specific_vars + [ "deps" ]) 110 shared_library = ":$_library_target" 111 apk_name = invoker.target_name 112 if (defined(invoker.output_name)) { 113 apk_name = invoker.output_name 114 install_script_name = "install_${invoker.output_name}" 115 } 116 117 # TODO(agrieve): Remove this data_dep once bots don't build the _apk 118 # target (post-GYP). 119 # It's a bit backwards for the apk to depend on the runner script, since 120 # the apk is conceptually a runtime_dep of the script. However, it is 121 # currently necessary because the bots build this _apk target directly 122 # rather than the group() below. 123 data_deps = [ 124 ":$_test_runner_target", 125 ] 126 } 127 128 # Incremental test targets work only for .apks. 129 _incremental_test_runner_target = 130 "${_output_name}_incremental__test_runner_script" 131 test_runner_script(_incremental_test_runner_target) { 132 forward_variables_from(invoker, 133 _wrapper_script_vars + [ 134 "data", 135 "data_deps", 136 "deps", 137 "public_deps", 138 ]) 139 apk_target = ":$_apk_target" 140 test_name = "${_output_name}_incremental" 141 test_type = "gtest" 142 test_suite = _output_name 143 incremental_install = true 144 } 145 group("${target_name}_incremental") { 146 testonly = true 147 datadeps = [ 148 ":$_incremental_test_runner_target", 149 ] 150 deps = [ 151 ":${_apk_target}_incremental", 152 ] 153 } 154 } 155 156 _test_runner_target = "${_output_name}__test_runner_script" 157 test_runner_script(_test_runner_target) { 158 forward_variables_from(invoker, 159 _wrapper_script_vars + [ 160 "data", 161 "data_deps", 162 "deps", 163 "public_deps", 164 ]) 165 166 if (_use_raw_android_executable) { 167 executable_dist_dir = "$root_out_dir/$_dist_target" 168 } else { 169 apk_target = ":$_apk_target" 170 } 171 test_name = _output_name 172 test_type = "gtest" 173 test_suite = _output_name 174 } 175 176 group(target_name) { 177 testonly = true 178 deps = [ 179 ":$_test_runner_target", 180 ] 181 if (_use_raw_android_executable) { 182 deps += [ ":$_dist_target" ] 183 } else { 184 deps += [ ":$_apk_target" ] 185 } 186 } 187 } else if (is_ios) { 188 import("//build/config/ios/rules.gni") 189 190 _test_target = target_name 191 _resources_bundle_data = target_name + "_resources_bundle_data" 192 193 bundle_data(_resources_bundle_data) { 194 visibility = [ ":$_test_target" ] 195 sources = [ 196 "//testing/gtest_ios/Default.png", 197 ] 198 outputs = [ 199 "{{bundle_resources_dir}}/{{source_file_part}}", 200 ] 201 } 202 203 ios_app_bundle(_test_target) { 204 testonly = true 205 206 # See above call. 207 set_sources_assignment_filter([]) 208 forward_variables_from(invoker, "*", [ "testonly" ]) 209 210 # Provide sensible defaults in case invoker did not define any of those 211 # required variables. 212 if (!defined(info_plist) && !defined(info_plist_target)) { 213 info_plist = "//testing/gtest_ios/unittest-Info.plist" 214 } 215 216 _bundle_id_suffix = target_name 217 if (ios_automatically_manage_certs) { 218 # Use the same bundle identifier for all unit tests when managing 219 # certificates automatically as the number of free certs is limited. 220 _bundle_id_suffix = "generic-unit-test" 221 } 222 if (!defined(extra_substitutions)) { 223 extra_substitutions = [] 224 } 225 extra_substitutions += [ "GTEST_BUNDLE_ID_SUFFIX=$_bundle_id_suffix" ] 226 227 if (!defined(bundle_deps)) { 228 bundle_deps = [] 229 } 230 bundle_deps += [ ":$_resources_bundle_data" ] 231 } 232 } else { 233 executable(target_name) { 234 deps = [] 235 forward_variables_from(invoker, "*") 236 237 testonly = true 238 deps += [ 239 # Give tests the default manifest on Windows (a no-op elsewhere). 240 "//build/win:default_exe_manifest", 241 ] 242 } 243 244 if (defined(invoker.output_name) && target_name != invoker.output_name) { 245 group("${invoker.output_name}_run") { 246 testonly = true 247 deps = [ 248 ":${invoker.target_name}", 249 ] 250 } 251 } 252 } 253} 254 255# Test defaults. 256set_defaults("test") { 257 if (is_android) { 258 configs = default_shared_library_configs 259 } else { 260 configs = default_executable_configs 261 } 262} 263 264template("pdfium_unittest_source_set") { 265 source_set(target_name) { 266 _pdfium_root_dir = rebase_path(invoker.pdfium_root_dir, ".") 267 268 testonly = true 269 sources = invoker.sources 270 configs += [ _pdfium_root_dir + ":pdfium_core_config" ] 271 if (defined(invoker.configs)) { 272 configs += invoker.configs 273 } 274 deps = [ 275 _pdfium_root_dir + ":pdfium_unittest_deps", 276 ] 277 if (defined(invoker.deps)) { 278 deps += invoker.deps 279 } 280 visibility = [ _pdfium_root_dir + ":*" ] 281 forward_variables_from(invoker, [ "cflags" ]) 282 } 283} 284 285template("pdfium_embeddertest_source_set") { 286 source_set(target_name) { 287 _pdfium_root_dir = rebase_path(invoker.pdfium_root_dir, ".") 288 289 testonly = true 290 sources = invoker.sources 291 configs += [ _pdfium_root_dir + ":pdfium_core_config" ] 292 if (defined(invoker.configs)) { 293 configs += invoker.configs 294 } 295 deps = [ 296 _pdfium_root_dir + ":pdfium_embeddertest_deps", 297 ] 298 if (defined(invoker.deps)) { 299 deps += invoker.deps 300 } 301 visibility = [ _pdfium_root_dir + ":*" ] 302 forward_variables_from(invoker, [ "cflags" ]) 303 } 304} 305