blob: b6677d45fc46d62ab61b6e78be6b66f5ddab9a5f [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",
57 "//build/config/android:hide_native_jni_exports",
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 if (is_component_build || is_asan) {
64 data_deps += [ "//build/android:cpplib_stripped" ]
65 }
66 }
67
68 create_native_executable_dist(_dist_target) {
69 testonly = true
70 dist_dir = "$root_out_dir/$target_name"
71 binary = _exec_output
72 deps = [
73 ":$_exec_target",
74 ]
75 if (defined(invoker.extra_dist_files)) {
76 extra_files = invoker.extra_dist_files
77 }
78 }
79 } else {
80 _library_target = "_${target_name}__library"
81 _apk_target = "${target_name}_apk"
82 _apk_specific_vars = [
83 "android_manifest",
jbudorickf6ee8202016-12-05 06:50:50 -080084 "android_manifest_dep",
dsinclair685bb882016-04-20 07:32:39 -070085 "enable_multidex",
jbudorick7f3a15f2016-06-10 06:28:40 -070086 "proguard_configs",
87 "proguard_enabled",
dsinclair685bb882016-04-20 07:32:39 -070088 "use_default_launcher",
89 "write_asset_list",
jbudorick7f3a15f2016-06-10 06:28:40 -070090 "use_native_activity",
dsinclair685bb882016-04-20 07:32:39 -070091 ]
92 shared_library(_library_target) {
93 # Configs will always be defined since we set_defaults in BUILDCONFIG.gn.
94 configs = [] # Prevent list overwriting warning.
95 configs = invoker.configs
96 testonly = true
97
98 deps = []
jbudorickf6ee8202016-12-05 06:50:50 -080099 forward_variables_from(
100 invoker,
101 "*",
102 _apk_specific_vars + _wrapper_script_vars + [ "visibility" ])
dsinclair685bb882016-04-20 07:32:39 -0700103
104 if (!defined(invoker.use_default_launcher) ||
105 invoker.use_default_launcher) {
106 deps += [ "//testing/android/native_test:native_test_native_code" ]
107 }
108 }
109 unittest_apk(_apk_target) {
110 forward_variables_from(invoker, _apk_specific_vars + [ "deps" ])
jbudorickf6ee8202016-12-05 06:50:50 -0800111 shared_library = ":$_library_target"
dsinclair685bb882016-04-20 07:32:39 -0700112 apk_name = invoker.target_name
113 if (defined(invoker.output_name)) {
114 apk_name = invoker.output_name
dsinclair685bb882016-04-20 07:32:39 -0700115 install_script_name = "install_${invoker.output_name}"
116 }
jbudorick7f3a15f2016-06-10 06:28:40 -0700117
118 # TODO(agrieve): Remove this data_dep once bots don't build the _apk
119 # target (post-GYP).
120 # It's a bit backwards for the apk to depend on the runner script, since
121 # the apk is conceptually a runtime_dep of the script. However, it is
122 # currently necessary because the bots build this _apk target directly
123 # rather than the group() below.
124 data_deps = [
125 ":$_test_runner_target",
126 ]
dsinclair685bb882016-04-20 07:32:39 -0700127 }
128
129 # Incremental test targets work only for .apks.
130 _incremental_test_runner_target =
131 "${_output_name}_incremental__test_runner_script"
132 test_runner_script(_incremental_test_runner_target) {
jbudorickf6ee8202016-12-05 06:50:50 -0800133 forward_variables_from(invoker,
134 _wrapper_script_vars + [
135 "data",
136 "data_deps",
137 "deps",
138 "public_deps",
139 ])
dsinclair685bb882016-04-20 07:32:39 -0700140 apk_target = ":$_apk_target"
141 test_name = "${_output_name}_incremental"
142 test_type = "gtest"
143 test_suite = _output_name
144 incremental_install = true
145 }
146 group("${target_name}_incremental") {
147 testonly = true
148 datadeps = [
149 ":$_incremental_test_runner_target",
150 ]
151 deps = [
152 ":${_apk_target}_incremental",
153 ]
154 }
155 }
156
157 _test_runner_target = "${_output_name}__test_runner_script"
158 test_runner_script(_test_runner_target) {
jbudorickf6ee8202016-12-05 06:50:50 -0800159 forward_variables_from(invoker,
160 _wrapper_script_vars + [
161 "data",
162 "data_deps",
163 "deps",
164 "public_deps",
165 ])
jbudorick7f3a15f2016-06-10 06:28:40 -0700166
dsinclair685bb882016-04-20 07:32:39 -0700167 if (_use_raw_android_executable) {
168 executable_dist_dir = "$root_out_dir/$_dist_target"
169 } else {
170 apk_target = ":$_apk_target"
171 }
172 test_name = _output_name
173 test_type = "gtest"
174 test_suite = _output_name
175 }
176
177 group(target_name) {
178 testonly = true
179 deps = [
180 ":$_test_runner_target",
181 ]
182 if (_use_raw_android_executable) {
183 deps += [ ":$_dist_target" ]
184 } else {
185 deps += [ ":$_apk_target" ]
186 }
187 }
188
jbudorick7f3a15f2016-06-10 06:28:40 -0700189 # TODO(GYP_GONE): Delete this after we've converted everything to GN.
dsinclair685bb882016-04-20 07:32:39 -0700190 # The _run targets exist only for compatibility w/ GYP.
191 group("${target_name}_apk_run") {
192 testonly = true
193 deps = [
194 ":${invoker.target_name}",
195 ]
196 }
197 } else if (is_ios) {
198 import("//build/config/ios/rules.gni")
199
200 _test_target = target_name
201 _resources_bundle_data = target_name + "_resources_bundle_data"
202
203 bundle_data(_resources_bundle_data) {
jbudorickf6ee8202016-12-05 06:50:50 -0800204 visibility = [ ":$_test_target" ]
dsinclair685bb882016-04-20 07:32:39 -0700205 sources = [
206 "//testing/gtest_ios/Default.png",
207 ]
208 outputs = [
209 "{{bundle_resources_dir}}/{{source_file_part}}",
210 ]
211 }
212
jbudorick7f3a15f2016-06-10 06:28:40 -0700213 ios_app_bundle(_test_target) {
dsinclair685bb882016-04-20 07:32:39 -0700214 testonly = true
dsinclair685bb882016-04-20 07:32:39 -0700215
216 # See above call.
217 set_sources_assignment_filter([])
jbudorick7f3a15f2016-06-10 06:28:40 -0700218 forward_variables_from(invoker, "*", [ "testonly" ])
dsinclair685bb882016-04-20 07:32:39 -0700219
jbudorick7f3a15f2016-06-10 06:28:40 -0700220 # Provide sensible defaults in case invoker did not define any of those
221 # required variables.
222 if (!defined(info_plist) && !defined(info_plist_target)) {
223 info_plist = "//testing/gtest_ios/unittest-Info.plist"
224 }
jbudorick7f3a15f2016-06-10 06:28:40 -0700225
jbudorickf6ee8202016-12-05 06:50:50 -0800226 _bundle_id_suffix = target_name
227 if (ios_automatically_manage_certs) {
228 # Use the same bundle identifier for all unit tests when managing
229 # certificates automatically as the number of free certs is limited.
230 _bundle_id_suffix = "generic-unit-test"
jbudorick7f3a15f2016-06-10 06:28:40 -0700231 }
jbudorick7f3a15f2016-06-10 06:28:40 -0700232 if (!defined(extra_substitutions)) {
233 extra_substitutions = []
234 }
jbudorickf6ee8202016-12-05 06:50:50 -0800235 extra_substitutions += [ "GTEST_BUNDLE_ID_SUFFIX=$_bundle_id_suffix" ]
dsinclair685bb882016-04-20 07:32:39 -0700236
237 if (!defined(deps)) {
238 deps = []
239 }
240 deps += [
dsinclair685bb882016-04-20 07:32:39 -0700241 # All shared libraries must have the sanitizer deps to properly link in
242 # asan mode (this target will be empty in other cases).
243 "//build/config/sanitizers:deps",
244 ]
jbudorickf6ee8202016-12-05 06:50:50 -0800245 if (!defined(bundle_deps)) {
246 bundle_deps = []
247 }
248 bundle_deps += [ ":$_resources_bundle_data" ]
dsinclair685bb882016-04-20 07:32:39 -0700249 }
250 } else {
251 executable(target_name) {
252 deps = []
253 forward_variables_from(invoker, "*")
254
255 testonly = true
256 deps += [
257 # All shared libraries must have the sanitizer deps to properly link in
258 # asan mode (this target will be empty in other cases).
259 "//build/config/sanitizers:deps",
260
261 # Give tests the default manifest on Windows (a no-op elsewhere).
262 "//build/win:default_exe_manifest",
263 ]
264 }
265
jbudorick7f3a15f2016-06-10 06:28:40 -0700266 # TODO(GYP_GONE): Delete this after we've converted everything to GN.
dsinclair685bb882016-04-20 07:32:39 -0700267 # The _run targets exist only for compatibility with GYP.
268 group("${target_name}_run") {
269 testonly = true
270 deps = [
271 ":${invoker.target_name}",
272 ]
273 }
jbudorick7f3a15f2016-06-10 06:28:40 -0700274
275 if (defined(invoker.output_name) && target_name != invoker.output_name) {
276 group("${invoker.output_name}_run") {
277 testonly = true
278 deps = [
279 ":${invoker.target_name}",
280 ]
281 }
282 }
dsinclair685bb882016-04-20 07:32:39 -0700283 }
284}
thestigc65e11e2016-08-30 21:56:33 -0700285
286# Test defaults.
287set_defaults("test") {
288 if (is_android) {
289 configs = default_shared_library_configs
290 } else {
291 configs = default_executable_configs
292 }
293}