1# Copyright 2015 The PDFium Authors 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/internal_rules.gni") 18 import("//build/config/android/rules.gni") 19 20 _use_raw_android_executable = defined(invoker.use_raw_android_executable) && 21 invoker.use_raw_android_executable 22 23 # output_name is used to allow targets with the same name but in different 24 # packages to still produce unique runner scripts. 25 _output_name = invoker.target_name 26 if (defined(invoker.output_name)) { 27 _output_name = invoker.output_name 28 } 29 30 _test_runner_target = "${_output_name}__test_runner_script" 31 _wrapper_script_vars = [ 32 "ignore_all_data_deps", 33 "shard_timeout", 34 ] 35 36 assert(_use_raw_android_executable || enable_java_templates) 37 38 if (_use_raw_android_executable) { 39 _exec_target = "${target_name}__exec" 40 _dist_target = "${target_name}__dist" 41 _exec_output = 42 "$target_out_dir/${invoker.target_name}/${invoker.target_name}" 43 44 executable(_exec_target) { 45 # Configs will always be defined since we set_defaults in BUILDCONFIG.gn. 46 configs = [] 47 data_deps = [] 48 forward_variables_from(invoker, 49 "*", 50 _wrapper_script_vars + [ "extra_dist_files" ]) 51 testonly = true 52 53 # Thanks to the set_defaults() for test(), configs are initialized with 54 # the default shared_library configs rather than executable configs. 55 configs -= [ 56 "//build/config:shared_library_config", 57 "//build/config/android:hide_all_but_jni", 58 ] 59 configs += [ "//build/config:executable_config" ] 60 61 # Don't output to the root or else conflict with the group() below. 62 output_name = rebase_path(_exec_output, root_out_dir) 63 } 64 65 create_native_executable_dist(_dist_target) { 66 testonly = true 67 dist_dir = "$root_out_dir/$target_name" 68 binary = _exec_output 69 deps = [ ":$_exec_target" ] 70 if (defined(invoker.extra_dist_files)) { 71 extra_files = invoker.extra_dist_files 72 } 73 } 74 } else { 75 _library_target = "${target_name}__library" 76 _apk_target = "${target_name}__apk" 77 _apk_specific_vars = [ 78 "android_manifest", 79 "android_manifest_dep", 80 "enable_multidex", 81 "product_config_java_packages", 82 "min_sdk_version", 83 "proguard_configs", 84 "proguard_enabled", 85 "shared_resources", 86 "srcjar_deps", 87 "target_sdk_version", 88 "use_default_launcher", 89 "use_native_activity", 90 ] 91 92 # Adds the unwind tables from unstripped binary as an asset file in the 93 # apk, if |add_unwind_tables_in_apk| is specified by the test. 94 if (defined(invoker.add_unwind_tables_in_apk) && 95 invoker.add_unwind_tables_in_apk) { 96 _unwind_table_asset_name = "${target_name}_unwind_assets" 97 unwind_table_asset(_unwind_table_asset_name) { 98 testonly = true 99 library_target = _library_target 100 deps = [ ":$_library_target" ] 101 } 102 } 103 104 shared_library(_library_target) { 105 # Configs will always be defined since we set_defaults in BUILDCONFIG.gn. 106 configs = [] # Prevent list overwriting warning. 107 configs = invoker.configs 108 testonly = true 109 110 deps = [] 111 forward_variables_from( 112 invoker, 113 "*", 114 _apk_specific_vars + _wrapper_script_vars + [ "visibility" ]) 115 116 if (!defined(invoker.use_default_launcher) || 117 invoker.use_default_launcher) { 118 deps += [ "//testing/android/native_test:native_test_native_code" ] 119 } 120 } 121 unittest_apk(_apk_target) { 122 forward_variables_from(invoker, _apk_specific_vars) 123 shared_library = ":$_library_target" 124 apk_name = invoker.target_name 125 if (defined(invoker.output_name)) { 126 apk_name = invoker.output_name 127 install_script_name = "install_${invoker.output_name}" 128 } 129 130 if (defined(invoker.deps)) { 131 deps = invoker.deps 132 } else { 133 deps = [] 134 } 135 136 # Add the Java classes so that each target does not have to do it. 137 deps += [ "//base/test:test_support_java" ] 138 139 if (defined(_unwind_table_asset_name)) { 140 deps += [ ":${_unwind_table_asset_name}" ] 141 } 142 } 143 } 144 145 test_runner_script(_test_runner_target) { 146 forward_variables_from(invoker, _wrapper_script_vars) 147 148 if (_use_raw_android_executable) { 149 executable_dist_dir = "$root_out_dir/$_dist_target" 150 data_deps = [ ":$_exec_target" ] 151 } else { 152 apk_target = ":$_apk_target" 153 incremental_apk = incremental_install 154 155 # Dep needed for the test runner .runtime_deps file to pick up data 156 # deps from the forward_variables_from(invoker, "*") on the library. 157 data_deps = [ ":$_library_target" ] 158 } 159 test_name = _output_name 160 test_suite = _output_name 161 test_type = "gtest" 162 } 163 164 # Create a wrapper script rather than using a group() in order to ensure 165 # "ninja $target_name" always works. If this was a group(), then GN would 166 # not create a top-level alias for it if a target exists in another 167 # directory with the same $target_name. 168 # Also - bots run this script directly for "components_perftests". 169 generate_wrapper(target_name) { 170 testonly = true 171 executable = "$root_build_dir/bin/run_$_output_name" 172 wrapper_script = "$root_build_dir/$_output_name" 173 deps = [ ":$_test_runner_target" ] 174 if (_use_raw_android_executable) { 175 deps += [ ":$_dist_target" ] 176 } else { 177 # Dep needed for the swarming .isolate file to pick up data 178 # deps from the forward_variables_from(invoker, "*") on the library. 179 deps += [ 180 ":$_apk_target", 181 ":$_library_target", 182 ] 183 } 184 } 185 } else if (is_ios) { 186 import("//build/config/ios/rules.gni") 187 188 _test_target = target_name 189 _resources_bundle_data = target_name + "_resources_bundle_data" 190 191 bundle_data(_resources_bundle_data) { 192 visibility = [ ":$_test_target" ] 193 sources = [ "//testing/gtest_ios/Default.png" ] 194 outputs = [ "{{bundle_resources_dir}}/{{source_file_part}}" ] 195 } 196 197 ios_app_bundle(_test_target) { 198 testonly = true 199 200 # See above call. 201 forward_variables_from(invoker, "*", [ "testonly" ]) 202 203 # Provide sensible defaults in case invoker did not define any of those 204 # required variables. 205 if (!defined(info_plist) && !defined(info_plist_target)) { 206 info_plist = "//testing/gtest_ios/unittest-Info.plist" 207 } 208 209 _bundle_id_suffix = target_name 210 if (ios_automatically_manage_certs) { 211 # Use the same bundle identifier for all unit tests when managing 212 # certificates automatically as the number of free certs is limited. 213 _bundle_id_suffix = "generic-unit-test" 214 } 215 if (!defined(extra_substitutions)) { 216 extra_substitutions = [] 217 } 218 extra_substitutions += [ "GTEST_BUNDLE_ID_SUFFIX=$_bundle_id_suffix" ] 219 220 if (!defined(bundle_deps)) { 221 bundle_deps = [] 222 } 223 bundle_deps += [ ":$_resources_bundle_data" ] 224 } 225 } else { 226 executable(target_name) { 227 deps = [] 228 forward_variables_from(invoker, "*") 229 230 testonly = true 231 deps += [ 232 # Give tests the default manifest on Windows (a no-op elsewhere). 233 "//build/win:default_exe_manifest", 234 ] 235 } 236 237 if (defined(invoker.output_name) && target_name != invoker.output_name) { 238 group("${invoker.output_name}_run") { 239 testonly = true 240 deps = [ ":${invoker.target_name}" ] 241 } 242 } 243 } 244} 245 246# Test defaults. 247set_defaults("test") { 248 if (is_android) { 249 configs = default_shared_library_configs 250 configs -= [ "//build/config/android:hide_all_but_jni_onload" ] 251 configs += [ "//build/config/android:hide_all_but_jni" ] 252 } else { 253 configs = default_executable_configs 254 } 255} 256 257template("pdfium_unittest_source_set") { 258 source_set(target_name) { 259 _pdfium_root_dir = rebase_path(invoker.pdfium_root_dir, ".") 260 261 testonly = true 262 sources = invoker.sources 263 configs += [ _pdfium_root_dir + ":pdfium_core_config" ] 264 if (defined(invoker.configs)) { 265 configs += invoker.configs 266 } 267 deps = [ _pdfium_root_dir + ":pdfium_unittest_deps" ] 268 if (defined(invoker.deps)) { 269 deps += invoker.deps 270 } 271 visibility = [ _pdfium_root_dir + ":*" ] 272 forward_variables_from(invoker, [ "cflags" ]) 273 } 274} 275 276template("pdfium_embeddertest_source_set") { 277 source_set(target_name) { 278 _pdfium_root_dir = rebase_path(invoker.pdfium_root_dir, ".") 279 280 testonly = true 281 sources = invoker.sources 282 configs += [ _pdfium_root_dir + ":pdfium_core_config" ] 283 if (defined(invoker.configs)) { 284 configs += invoker.configs 285 } 286 deps = [ _pdfium_root_dir + ":pdfium_embeddertest_deps" ] 287 if (defined(invoker.deps)) { 288 deps += invoker.deps 289 } 290 visibility = [ _pdfium_root_dir + ":*" ] 291 forward_variables_from(invoker, [ "cflags" ]) 292 } 293} 294