dsinclair | 685bb88 | 2016-04-20 07:32:39 -0700 | [diff] [blame] | 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(). |
jbudorick | 7f3a15f | 2016-06-10 06:28:40 -0700 | [diff] [blame] | 13 | # use_native_activity: Test implements ANativeActivity_onCreate(). |
dsinclair | 685bb88 | 2016-04-20 07:32:39 -0700 | [diff] [blame] | 14 | template("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 | |
jbudorick | 7f3a15f | 2016-06-10 06:28:40 -0700 | [diff] [blame] | 29 | _test_runner_target = "${_output_name}__test_runner_script" |
jbudorick | f6ee820 | 2016-12-05 06:50:50 -0800 | [diff] [blame] | 30 | _wrapper_script_vars = [ |
jbudorick | 7f3a15f | 2016-06-10 06:28:40 -0700 | [diff] [blame] | 31 | "ignore_all_data_deps", |
jbudorick | f6ee820 | 2016-12-05 06:50:50 -0800 | [diff] [blame] | 32 | "shard_timeout", |
jbudorick | 7f3a15f | 2016-06-10 06:28:40 -0700 | [diff] [blame] | 33 | ] |
jbudorick | 7f3a15f | 2016-06-10 06:28:40 -0700 | [diff] [blame] | 34 | |
| 35 | assert(_use_raw_android_executable || enable_java_templates) |
| 36 | |
dsinclair | 685bb88 | 2016-04-20 07:32:39 -0700 | [diff] [blame] | 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 = [] |
jbudorick | f6ee820 | 2016-12-05 06:50:50 -0800 | [diff] [blame] | 47 | forward_variables_from(invoker, |
| 48 | "*", |
| 49 | _wrapper_script_vars + [ "extra_dist_files" ]) |
dsinclair | 685bb88 | 2016-04-20 07:32:39 -0700 | [diff] [blame] | 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", |
Lei Zhang | fba1651 | 2017-03-15 16:34:16 -0700 | [diff] [blame] | 56 | "//build/config/android:hide_all_but_jni_onload", |
dsinclair | 685bb88 | 2016-04-20 07:32:39 -0700 | [diff] [blame] | 57 | ] |
Lei Zhang | fba1651 | 2017-03-15 16:34:16 -0700 | [diff] [blame] | 58 | configs += [ |
| 59 | "//build/config:executable_config", |
| 60 | "//build/config/android:hide_all_but_jni", |
| 61 | ] |
dsinclair | 685bb88 | 2016-04-20 07:32:39 -0700 | [diff] [blame] | 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 | if (is_component_build || is_asan) { |
| 66 | data_deps += [ "//build/android:cpplib_stripped" ] |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | create_native_executable_dist(_dist_target) { |
| 71 | testonly = true |
| 72 | dist_dir = "$root_out_dir/$target_name" |
| 73 | binary = _exec_output |
| 74 | deps = [ |
| 75 | ":$_exec_target", |
| 76 | ] |
| 77 | if (defined(invoker.extra_dist_files)) { |
| 78 | extra_files = invoker.extra_dist_files |
| 79 | } |
| 80 | } |
| 81 | } else { |
| 82 | _library_target = "_${target_name}__library" |
| 83 | _apk_target = "${target_name}_apk" |
| 84 | _apk_specific_vars = [ |
| 85 | "android_manifest", |
jbudorick | f6ee820 | 2016-12-05 06:50:50 -0800 | [diff] [blame] | 86 | "android_manifest_dep", |
dsinclair | 685bb88 | 2016-04-20 07:32:39 -0700 | [diff] [blame] | 87 | "enable_multidex", |
jbudorick | 7f3a15f | 2016-06-10 06:28:40 -0700 | [diff] [blame] | 88 | "proguard_configs", |
| 89 | "proguard_enabled", |
dsinclair | 685bb88 | 2016-04-20 07:32:39 -0700 | [diff] [blame] | 90 | "use_default_launcher", |
| 91 | "write_asset_list", |
jbudorick | 7f3a15f | 2016-06-10 06:28:40 -0700 | [diff] [blame] | 92 | "use_native_activity", |
dsinclair | 685bb88 | 2016-04-20 07:32:39 -0700 | [diff] [blame] | 93 | ] |
| 94 | shared_library(_library_target) { |
| 95 | # Configs will always be defined since we set_defaults in BUILDCONFIG.gn. |
| 96 | configs = [] # Prevent list overwriting warning. |
| 97 | configs = invoker.configs |
| 98 | testonly = true |
| 99 | |
| 100 | deps = [] |
jbudorick | f6ee820 | 2016-12-05 06:50:50 -0800 | [diff] [blame] | 101 | forward_variables_from( |
| 102 | invoker, |
| 103 | "*", |
| 104 | _apk_specific_vars + _wrapper_script_vars + [ "visibility" ]) |
dsinclair | 685bb88 | 2016-04-20 07:32:39 -0700 | [diff] [blame] | 105 | |
| 106 | if (!defined(invoker.use_default_launcher) || |
| 107 | invoker.use_default_launcher) { |
| 108 | deps += [ "//testing/android/native_test:native_test_native_code" ] |
| 109 | } |
| 110 | } |
| 111 | unittest_apk(_apk_target) { |
| 112 | forward_variables_from(invoker, _apk_specific_vars + [ "deps" ]) |
jbudorick | f6ee820 | 2016-12-05 06:50:50 -0800 | [diff] [blame] | 113 | shared_library = ":$_library_target" |
dsinclair | 685bb88 | 2016-04-20 07:32:39 -0700 | [diff] [blame] | 114 | apk_name = invoker.target_name |
| 115 | if (defined(invoker.output_name)) { |
| 116 | apk_name = invoker.output_name |
dsinclair | 685bb88 | 2016-04-20 07:32:39 -0700 | [diff] [blame] | 117 | install_script_name = "install_${invoker.output_name}" |
| 118 | } |
jbudorick | 7f3a15f | 2016-06-10 06:28:40 -0700 | [diff] [blame] | 119 | |
| 120 | # TODO(agrieve): Remove this data_dep once bots don't build the _apk |
| 121 | # target (post-GYP). |
| 122 | # It's a bit backwards for the apk to depend on the runner script, since |
| 123 | # the apk is conceptually a runtime_dep of the script. However, it is |
| 124 | # currently necessary because the bots build this _apk target directly |
| 125 | # rather than the group() below. |
| 126 | data_deps = [ |
| 127 | ":$_test_runner_target", |
| 128 | ] |
dsinclair | 685bb88 | 2016-04-20 07:32:39 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | # Incremental test targets work only for .apks. |
| 132 | _incremental_test_runner_target = |
| 133 | "${_output_name}_incremental__test_runner_script" |
| 134 | test_runner_script(_incremental_test_runner_target) { |
jbudorick | f6ee820 | 2016-12-05 06:50:50 -0800 | [diff] [blame] | 135 | forward_variables_from(invoker, |
| 136 | _wrapper_script_vars + [ |
| 137 | "data", |
| 138 | "data_deps", |
| 139 | "deps", |
| 140 | "public_deps", |
| 141 | ]) |
dsinclair | 685bb88 | 2016-04-20 07:32:39 -0700 | [diff] [blame] | 142 | apk_target = ":$_apk_target" |
| 143 | test_name = "${_output_name}_incremental" |
| 144 | test_type = "gtest" |
| 145 | test_suite = _output_name |
| 146 | incremental_install = true |
| 147 | } |
| 148 | group("${target_name}_incremental") { |
| 149 | testonly = true |
| 150 | datadeps = [ |
| 151 | ":$_incremental_test_runner_target", |
| 152 | ] |
| 153 | deps = [ |
| 154 | ":${_apk_target}_incremental", |
| 155 | ] |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | _test_runner_target = "${_output_name}__test_runner_script" |
| 160 | test_runner_script(_test_runner_target) { |
jbudorick | f6ee820 | 2016-12-05 06:50:50 -0800 | [diff] [blame] | 161 | forward_variables_from(invoker, |
| 162 | _wrapper_script_vars + [ |
| 163 | "data", |
| 164 | "data_deps", |
| 165 | "deps", |
| 166 | "public_deps", |
| 167 | ]) |
jbudorick | 7f3a15f | 2016-06-10 06:28:40 -0700 | [diff] [blame] | 168 | |
dsinclair | 685bb88 | 2016-04-20 07:32:39 -0700 | [diff] [blame] | 169 | if (_use_raw_android_executable) { |
| 170 | executable_dist_dir = "$root_out_dir/$_dist_target" |
| 171 | } else { |
| 172 | apk_target = ":$_apk_target" |
| 173 | } |
| 174 | test_name = _output_name |
| 175 | test_type = "gtest" |
| 176 | test_suite = _output_name |
| 177 | } |
| 178 | |
| 179 | group(target_name) { |
| 180 | testonly = true |
| 181 | deps = [ |
| 182 | ":$_test_runner_target", |
| 183 | ] |
| 184 | if (_use_raw_android_executable) { |
| 185 | deps += [ ":$_dist_target" ] |
| 186 | } else { |
| 187 | deps += [ ":$_apk_target" ] |
| 188 | } |
| 189 | } |
dsinclair | 685bb88 | 2016-04-20 07:32:39 -0700 | [diff] [blame] | 190 | } else if (is_ios) { |
| 191 | import("//build/config/ios/rules.gni") |
| 192 | |
| 193 | _test_target = target_name |
| 194 | _resources_bundle_data = target_name + "_resources_bundle_data" |
| 195 | |
| 196 | bundle_data(_resources_bundle_data) { |
jbudorick | f6ee820 | 2016-12-05 06:50:50 -0800 | [diff] [blame] | 197 | visibility = [ ":$_test_target" ] |
dsinclair | 685bb88 | 2016-04-20 07:32:39 -0700 | [diff] [blame] | 198 | sources = [ |
| 199 | "//testing/gtest_ios/Default.png", |
| 200 | ] |
| 201 | outputs = [ |
| 202 | "{{bundle_resources_dir}}/{{source_file_part}}", |
| 203 | ] |
| 204 | } |
| 205 | |
jbudorick | 7f3a15f | 2016-06-10 06:28:40 -0700 | [diff] [blame] | 206 | ios_app_bundle(_test_target) { |
dsinclair | 685bb88 | 2016-04-20 07:32:39 -0700 | [diff] [blame] | 207 | testonly = true |
dsinclair | 685bb88 | 2016-04-20 07:32:39 -0700 | [diff] [blame] | 208 | |
| 209 | # See above call. |
| 210 | set_sources_assignment_filter([]) |
jbudorick | 7f3a15f | 2016-06-10 06:28:40 -0700 | [diff] [blame] | 211 | forward_variables_from(invoker, "*", [ "testonly" ]) |
dsinclair | 685bb88 | 2016-04-20 07:32:39 -0700 | [diff] [blame] | 212 | |
jbudorick | 7f3a15f | 2016-06-10 06:28:40 -0700 | [diff] [blame] | 213 | # Provide sensible defaults in case invoker did not define any of those |
| 214 | # required variables. |
| 215 | if (!defined(info_plist) && !defined(info_plist_target)) { |
| 216 | info_plist = "//testing/gtest_ios/unittest-Info.plist" |
| 217 | } |
jbudorick | 7f3a15f | 2016-06-10 06:28:40 -0700 | [diff] [blame] | 218 | |
jbudorick | f6ee820 | 2016-12-05 06:50:50 -0800 | [diff] [blame] | 219 | _bundle_id_suffix = target_name |
| 220 | if (ios_automatically_manage_certs) { |
| 221 | # Use the same bundle identifier for all unit tests when managing |
| 222 | # certificates automatically as the number of free certs is limited. |
| 223 | _bundle_id_suffix = "generic-unit-test" |
jbudorick | 7f3a15f | 2016-06-10 06:28:40 -0700 | [diff] [blame] | 224 | } |
jbudorick | 7f3a15f | 2016-06-10 06:28:40 -0700 | [diff] [blame] | 225 | if (!defined(extra_substitutions)) { |
| 226 | extra_substitutions = [] |
| 227 | } |
jbudorick | f6ee820 | 2016-12-05 06:50:50 -0800 | [diff] [blame] | 228 | extra_substitutions += [ "GTEST_BUNDLE_ID_SUFFIX=$_bundle_id_suffix" ] |
dsinclair | 685bb88 | 2016-04-20 07:32:39 -0700 | [diff] [blame] | 229 | |
| 230 | if (!defined(deps)) { |
| 231 | deps = [] |
| 232 | } |
Tom Anderson | 41e3c35 | 2017-05-19 15:14:35 -0700 | [diff] [blame] | 233 | deps += [ "//build/config:exe_and_shlib_deps" ] |
jbudorick | f6ee820 | 2016-12-05 06:50:50 -0800 | [diff] [blame] | 234 | if (!defined(bundle_deps)) { |
| 235 | bundle_deps = [] |
| 236 | } |
| 237 | bundle_deps += [ ":$_resources_bundle_data" ] |
dsinclair | 685bb88 | 2016-04-20 07:32:39 -0700 | [diff] [blame] | 238 | } |
| 239 | } else { |
| 240 | executable(target_name) { |
| 241 | deps = [] |
| 242 | forward_variables_from(invoker, "*") |
| 243 | |
| 244 | testonly = true |
| 245 | deps += [ |
Tom Anderson | 41e3c35 | 2017-05-19 15:14:35 -0700 | [diff] [blame] | 246 | "//build/config:exe_and_shlib_deps", |
dsinclair | 685bb88 | 2016-04-20 07:32:39 -0700 | [diff] [blame] | 247 | |
| 248 | # Give tests the default manifest on Windows (a no-op elsewhere). |
| 249 | "//build/win:default_exe_manifest", |
| 250 | ] |
| 251 | } |
| 252 | |
jbudorick | 7f3a15f | 2016-06-10 06:28:40 -0700 | [diff] [blame] | 253 | if (defined(invoker.output_name) && target_name != invoker.output_name) { |
| 254 | group("${invoker.output_name}_run") { |
| 255 | testonly = true |
| 256 | deps = [ |
| 257 | ":${invoker.target_name}", |
| 258 | ] |
| 259 | } |
| 260 | } |
dsinclair | 685bb88 | 2016-04-20 07:32:39 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
thestig | c65e11e | 2016-08-30 21:56:33 -0700 | [diff] [blame] | 263 | |
| 264 | # Test defaults. |
| 265 | set_defaults("test") { |
| 266 | if (is_android) { |
| 267 | configs = default_shared_library_configs |
| 268 | } else { |
| 269 | configs = default_executable_configs |
| 270 | } |
| 271 | } |