blob: 3b9eddc4ee906350349f9a7cc85141385c545c93 [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")
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
jbudorick7f3a15f2016-06-10 06:28:40 -070029 _test_runner_target = "${_output_name}__test_runner_script"
jbudorickf6ee8202016-12-05 06:50:50 -080030 _wrapper_script_vars = [
jbudorick7f3a15f2016-06-10 06:28:40 -070031 "ignore_all_data_deps",
jbudorickf6ee8202016-12-05 06:50:50 -080032 "shard_timeout",
jbudorick7f3a15f2016-06-10 06:28:40 -070033 ]
jbudorick7f3a15f2016-06-10 06:28:40 -070034
35 assert(_use_raw_android_executable || enable_java_templates)
36
dsinclair685bb882016-04-20 07:32:39 -070037 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 = []
jbudorickf6ee8202016-12-05 06:50:50 -080047 forward_variables_from(invoker,
48 "*",
49 _wrapper_script_vars + [ "extra_dist_files" ])
dsinclair685bb882016-04-20 07:32:39 -070050 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 Zhangfba16512017-03-15 16:34:16 -070056 "//build/config/android:hide_all_but_jni_onload",
dsinclair685bb882016-04-20 07:32:39 -070057 ]
Lei Zhangfba16512017-03-15 16:34:16 -070058 configs += [
59 "//build/config:executable_config",
60 "//build/config/android:hide_all_but_jni",
61 ]
dsinclair685bb882016-04-20 07:32:39 -070062
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",
jbudorickf6ee8202016-12-05 06:50:50 -080086 "android_manifest_dep",
dsinclair685bb882016-04-20 07:32:39 -070087 "enable_multidex",
jbudorick7f3a15f2016-06-10 06:28:40 -070088 "proguard_configs",
89 "proguard_enabled",
dsinclair685bb882016-04-20 07:32:39 -070090 "use_default_launcher",
91 "write_asset_list",
jbudorick7f3a15f2016-06-10 06:28:40 -070092 "use_native_activity",
dsinclair685bb882016-04-20 07:32:39 -070093 ]
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 = []
jbudorickf6ee8202016-12-05 06:50:50 -0800101 forward_variables_from(
102 invoker,
103 "*",
104 _apk_specific_vars + _wrapper_script_vars + [ "visibility" ])
dsinclair685bb882016-04-20 07:32:39 -0700105
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" ])
jbudorickf6ee8202016-12-05 06:50:50 -0800113 shared_library = ":$_library_target"
dsinclair685bb882016-04-20 07:32:39 -0700114 apk_name = invoker.target_name
115 if (defined(invoker.output_name)) {
116 apk_name = invoker.output_name
dsinclair685bb882016-04-20 07:32:39 -0700117 install_script_name = "install_${invoker.output_name}"
118 }
jbudorick7f3a15f2016-06-10 06:28:40 -0700119
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 ]
dsinclair685bb882016-04-20 07:32:39 -0700129 }
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) {
jbudorickf6ee8202016-12-05 06:50:50 -0800135 forward_variables_from(invoker,
136 _wrapper_script_vars + [
137 "data",
138 "data_deps",
139 "deps",
140 "public_deps",
141 ])
dsinclair685bb882016-04-20 07:32:39 -0700142 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) {
jbudorickf6ee8202016-12-05 06:50:50 -0800161 forward_variables_from(invoker,
162 _wrapper_script_vars + [
163 "data",
164 "data_deps",
165 "deps",
166 "public_deps",
167 ])
jbudorick7f3a15f2016-06-10 06:28:40 -0700168
dsinclair685bb882016-04-20 07:32:39 -0700169 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 }
dsinclair685bb882016-04-20 07:32:39 -0700190 } 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) {
jbudorickf6ee8202016-12-05 06:50:50 -0800197 visibility = [ ":$_test_target" ]
dsinclair685bb882016-04-20 07:32:39 -0700198 sources = [
199 "//testing/gtest_ios/Default.png",
200 ]
201 outputs = [
202 "{{bundle_resources_dir}}/{{source_file_part}}",
203 ]
204 }
205
jbudorick7f3a15f2016-06-10 06:28:40 -0700206 ios_app_bundle(_test_target) {
dsinclair685bb882016-04-20 07:32:39 -0700207 testonly = true
dsinclair685bb882016-04-20 07:32:39 -0700208
209 # See above call.
210 set_sources_assignment_filter([])
jbudorick7f3a15f2016-06-10 06:28:40 -0700211 forward_variables_from(invoker, "*", [ "testonly" ])
dsinclair685bb882016-04-20 07:32:39 -0700212
jbudorick7f3a15f2016-06-10 06:28:40 -0700213 # 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 }
jbudorick7f3a15f2016-06-10 06:28:40 -0700218
jbudorickf6ee8202016-12-05 06:50:50 -0800219 _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"
jbudorick7f3a15f2016-06-10 06:28:40 -0700224 }
jbudorick7f3a15f2016-06-10 06:28:40 -0700225 if (!defined(extra_substitutions)) {
226 extra_substitutions = []
227 }
jbudorickf6ee8202016-12-05 06:50:50 -0800228 extra_substitutions += [ "GTEST_BUNDLE_ID_SUFFIX=$_bundle_id_suffix" ]
dsinclair685bb882016-04-20 07:32:39 -0700229
230 if (!defined(deps)) {
231 deps = []
232 }
Tom Anderson41e3c352017-05-19 15:14:35 -0700233 deps += [ "//build/config:exe_and_shlib_deps" ]
jbudorickf6ee8202016-12-05 06:50:50 -0800234 if (!defined(bundle_deps)) {
235 bundle_deps = []
236 }
237 bundle_deps += [ ":$_resources_bundle_data" ]
dsinclair685bb882016-04-20 07:32:39 -0700238 }
239 } else {
240 executable(target_name) {
241 deps = []
242 forward_variables_from(invoker, "*")
243
244 testonly = true
245 deps += [
Tom Anderson41e3c352017-05-19 15:14:35 -0700246 "//build/config:exe_and_shlib_deps",
dsinclair685bb882016-04-20 07:32:39 -0700247
248 # Give tests the default manifest on Windows (a no-op elsewhere).
249 "//build/win:default_exe_manifest",
250 ]
251 }
252
jbudorick7f3a15f2016-06-10 06:28:40 -0700253 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 }
dsinclair685bb882016-04-20 07:32:39 -0700261 }
262}
thestigc65e11e2016-08-30 21:56:33 -0700263
264# Test defaults.
265set_defaults("test") {
266 if (is_android) {
267 configs = default_shared_library_configs
268 } else {
269 configs = default_executable_configs
270 }
271}