blob: 01b51cc955e0c2d635d8605b4d2ffdab0e39dd31 [file] [log] [blame]
dsinclair685bb882016-04-20 07:32:39 -07001# 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().
jbudorick7f3a15f2016-06-10 06:28:40 -070013# use_native_activity: Test implements ANativeActivity_onCreate().
dsinclair685bb882016-04-20 07:32:39 -070014template("test") {
15 if (is_android) {
16 import("//build/config/android/config.gni")
17 import("//build/config/android/rules.gni")
jbudorick7f3a15f2016-06-10 06:28:40 -070018 import("//build/config/sanitizers/sanitizers.gni")
dsinclair685bb882016-04-20 07:32:39 -070019
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
jbudorick7f3a15f2016-06-10 06:28:40 -070030 _test_runner_target = "${_output_name}__test_runner_script"
jbudorickf6ee8202016-12-05 06:50:50 -080031 _wrapper_script_vars = [
jbudorick7f3a15f2016-06-10 06:28:40 -070032 "ignore_all_data_deps",
jbudorickf6ee8202016-12-05 06:50:50 -080033 "shard_timeout",
jbudorick7f3a15f2016-06-10 06:28:40 -070034 ]
jbudorick7f3a15f2016-06-10 06:28:40 -070035
36 assert(_use_raw_android_executable || enable_java_templates)
37
dsinclair685bb882016-04-20 07:32:39 -070038 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 = []
jbudorickf6ee8202016-12-05 06:50:50 -080048 forward_variables_from(invoker,
49 "*",
50 _wrapper_script_vars + [ "extra_dist_files" ])
dsinclair685bb882016-04-20 07:32:39 -070051 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",
Lei Zhangfba16512017-03-15 16:34:16 -070057 "//build/config/android:hide_all_but_jni_onload",
dsinclair685bb882016-04-20 07:32:39 -070058 ]
Lei Zhangfba16512017-03-15 16:34:16 -070059 configs += [
60 "//build/config:executable_config",
61 "//build/config/android:hide_all_but_jni",
62 ]
dsinclair685bb882016-04-20 07:32:39 -070063
64 # Don't output to the root or else conflict with the group() below.
65 output_name = rebase_path(_exec_output, root_out_dir)
66 if (is_component_build || is_asan) {
67 data_deps += [ "//build/android:cpplib_stripped" ]
68 }
69 }
70
71 create_native_executable_dist(_dist_target) {
72 testonly = true
73 dist_dir = "$root_out_dir/$target_name"
74 binary = _exec_output
75 deps = [
76 ":$_exec_target",
77 ]
78 if (defined(invoker.extra_dist_files)) {
79 extra_files = invoker.extra_dist_files
80 }
81 }
82 } else {
83 _library_target = "_${target_name}__library"
84 _apk_target = "${target_name}_apk"
85 _apk_specific_vars = [
86 "android_manifest",
jbudorickf6ee8202016-12-05 06:50:50 -080087 "android_manifest_dep",
dsinclair685bb882016-04-20 07:32:39 -070088 "enable_multidex",
jbudorick7f3a15f2016-06-10 06:28:40 -070089 "proguard_configs",
90 "proguard_enabled",
dsinclair685bb882016-04-20 07:32:39 -070091 "use_default_launcher",
92 "write_asset_list",
jbudorick7f3a15f2016-06-10 06:28:40 -070093 "use_native_activity",
dsinclair685bb882016-04-20 07:32:39 -070094 ]
95 shared_library(_library_target) {
96 # Configs will always be defined since we set_defaults in BUILDCONFIG.gn.
97 configs = [] # Prevent list overwriting warning.
98 configs = invoker.configs
99 testonly = true
100
101 deps = []
jbudorickf6ee8202016-12-05 06:50:50 -0800102 forward_variables_from(
103 invoker,
104 "*",
105 _apk_specific_vars + _wrapper_script_vars + [ "visibility" ])
dsinclair685bb882016-04-20 07:32:39 -0700106
107 if (!defined(invoker.use_default_launcher) ||
108 invoker.use_default_launcher) {
109 deps += [ "//testing/android/native_test:native_test_native_code" ]
110 }
111 }
112 unittest_apk(_apk_target) {
113 forward_variables_from(invoker, _apk_specific_vars + [ "deps" ])
jbudorickf6ee8202016-12-05 06:50:50 -0800114 shared_library = ":$_library_target"
dsinclair685bb882016-04-20 07:32:39 -0700115 apk_name = invoker.target_name
116 if (defined(invoker.output_name)) {
117 apk_name = invoker.output_name
dsinclair685bb882016-04-20 07:32:39 -0700118 install_script_name = "install_${invoker.output_name}"
119 }
jbudorick7f3a15f2016-06-10 06:28:40 -0700120
121 # TODO(agrieve): Remove this data_dep once bots don't build the _apk
122 # target (post-GYP).
123 # It's a bit backwards for the apk to depend on the runner script, since
124 # the apk is conceptually a runtime_dep of the script. However, it is
125 # currently necessary because the bots build this _apk target directly
126 # rather than the group() below.
127 data_deps = [
128 ":$_test_runner_target",
129 ]
dsinclair685bb882016-04-20 07:32:39 -0700130 }
131
132 # Incremental test targets work only for .apks.
133 _incremental_test_runner_target =
134 "${_output_name}_incremental__test_runner_script"
135 test_runner_script(_incremental_test_runner_target) {
jbudorickf6ee8202016-12-05 06:50:50 -0800136 forward_variables_from(invoker,
137 _wrapper_script_vars + [
138 "data",
139 "data_deps",
140 "deps",
141 "public_deps",
142 ])
dsinclair685bb882016-04-20 07:32:39 -0700143 apk_target = ":$_apk_target"
144 test_name = "${_output_name}_incremental"
145 test_type = "gtest"
146 test_suite = _output_name
147 incremental_install = true
148 }
149 group("${target_name}_incremental") {
150 testonly = true
151 datadeps = [
152 ":$_incremental_test_runner_target",
153 ]
154 deps = [
155 ":${_apk_target}_incremental",
156 ]
157 }
158 }
159
160 _test_runner_target = "${_output_name}__test_runner_script"
161 test_runner_script(_test_runner_target) {
jbudorickf6ee8202016-12-05 06:50:50 -0800162 forward_variables_from(invoker,
163 _wrapper_script_vars + [
164 "data",
165 "data_deps",
166 "deps",
167 "public_deps",
168 ])
jbudorick7f3a15f2016-06-10 06:28:40 -0700169
dsinclair685bb882016-04-20 07:32:39 -0700170 if (_use_raw_android_executable) {
171 executable_dist_dir = "$root_out_dir/$_dist_target"
172 } else {
173 apk_target = ":$_apk_target"
174 }
175 test_name = _output_name
176 test_type = "gtest"
177 test_suite = _output_name
178 }
179
180 group(target_name) {
181 testonly = true
182 deps = [
183 ":$_test_runner_target",
184 ]
185 if (_use_raw_android_executable) {
186 deps += [ ":$_dist_target" ]
187 } else {
188 deps += [ ":$_apk_target" ]
189 }
190 }
191
jbudorick7f3a15f2016-06-10 06:28:40 -0700192 # TODO(GYP_GONE): Delete this after we've converted everything to GN.
dsinclair685bb882016-04-20 07:32:39 -0700193 # The _run targets exist only for compatibility w/ GYP.
194 group("${target_name}_apk_run") {
195 testonly = true
196 deps = [
197 ":${invoker.target_name}",
198 ]
199 }
200 } else if (is_ios) {
201 import("//build/config/ios/rules.gni")
202
203 _test_target = target_name
204 _resources_bundle_data = target_name + "_resources_bundle_data"
205
206 bundle_data(_resources_bundle_data) {
jbudorickf6ee8202016-12-05 06:50:50 -0800207 visibility = [ ":$_test_target" ]
dsinclair685bb882016-04-20 07:32:39 -0700208 sources = [
209 "//testing/gtest_ios/Default.png",
210 ]
211 outputs = [
212 "{{bundle_resources_dir}}/{{source_file_part}}",
213 ]
214 }
215
jbudorick7f3a15f2016-06-10 06:28:40 -0700216 ios_app_bundle(_test_target) {
dsinclair685bb882016-04-20 07:32:39 -0700217 testonly = true
dsinclair685bb882016-04-20 07:32:39 -0700218
219 # See above call.
220 set_sources_assignment_filter([])
jbudorick7f3a15f2016-06-10 06:28:40 -0700221 forward_variables_from(invoker, "*", [ "testonly" ])
dsinclair685bb882016-04-20 07:32:39 -0700222
jbudorick7f3a15f2016-06-10 06:28:40 -0700223 # Provide sensible defaults in case invoker did not define any of those
224 # required variables.
225 if (!defined(info_plist) && !defined(info_plist_target)) {
226 info_plist = "//testing/gtest_ios/unittest-Info.plist"
227 }
jbudorick7f3a15f2016-06-10 06:28:40 -0700228
jbudorickf6ee8202016-12-05 06:50:50 -0800229 _bundle_id_suffix = target_name
230 if (ios_automatically_manage_certs) {
231 # Use the same bundle identifier for all unit tests when managing
232 # certificates automatically as the number of free certs is limited.
233 _bundle_id_suffix = "generic-unit-test"
jbudorick7f3a15f2016-06-10 06:28:40 -0700234 }
jbudorick7f3a15f2016-06-10 06:28:40 -0700235 if (!defined(extra_substitutions)) {
236 extra_substitutions = []
237 }
jbudorickf6ee8202016-12-05 06:50:50 -0800238 extra_substitutions += [ "GTEST_BUNDLE_ID_SUFFIX=$_bundle_id_suffix" ]
dsinclair685bb882016-04-20 07:32:39 -0700239
240 if (!defined(deps)) {
241 deps = []
242 }
243 deps += [
dsinclair685bb882016-04-20 07:32:39 -0700244 # All shared libraries must have the sanitizer deps to properly link in
245 # asan mode (this target will be empty in other cases).
246 "//build/config/sanitizers:deps",
247 ]
jbudorickf6ee8202016-12-05 06:50:50 -0800248 if (!defined(bundle_deps)) {
249 bundle_deps = []
250 }
251 bundle_deps += [ ":$_resources_bundle_data" ]
dsinclair685bb882016-04-20 07:32:39 -0700252 }
253 } else {
254 executable(target_name) {
255 deps = []
256 forward_variables_from(invoker, "*")
257
258 testonly = true
259 deps += [
260 # All shared libraries must have the sanitizer deps to properly link in
261 # asan mode (this target will be empty in other cases).
262 "//build/config/sanitizers:deps",
263
264 # Give tests the default manifest on Windows (a no-op elsewhere).
265 "//build/win:default_exe_manifest",
266 ]
267 }
268
jbudorick7f3a15f2016-06-10 06:28:40 -0700269 # TODO(GYP_GONE): Delete this after we've converted everything to GN.
dsinclair685bb882016-04-20 07:32:39 -0700270 # The _run targets exist only for compatibility with GYP.
271 group("${target_name}_run") {
272 testonly = true
273 deps = [
274 ":${invoker.target_name}",
275 ]
276 }
jbudorick7f3a15f2016-06-10 06:28:40 -0700277
278 if (defined(invoker.output_name) && target_name != invoker.output_name) {
279 group("${invoker.output_name}_run") {
280 testonly = true
281 deps = [
282 ":${invoker.target_name}",
283 ]
284 }
285 }
dsinclair685bb882016-04-20 07:32:39 -0700286 }
287}
thestigc65e11e2016-08-30 21:56:33 -0700288
289# Test defaults.
290set_defaults("test") {
291 if (is_android) {
292 configs = default_shared_library_configs
293 } else {
294 configs = default_executable_configs
295 }
296}