• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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().
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/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
29    _test_runner_target = "${_output_name}__test_runner_script"
30    _wrapper_script_vars = [
31      "ignore_all_data_deps",
32      "shard_timeout",
33    ]
34
35    assert(_use_raw_android_executable || enable_java_templates)
36
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 = []
47        forward_variables_from(invoker,
48                               "*",
49                               _wrapper_script_vars + [ "extra_dist_files" ])
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",
56          "//build/config/android:hide_all_but_jni_onload",
57        ]
58        configs += [
59          "//build/config:executable_config",
60          "//build/config/android:hide_all_but_jni",
61        ]
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",
86        "android_manifest_dep",
87        "enable_multidex",
88        "proguard_configs",
89        "proguard_enabled",
90        "use_default_launcher",
91        "write_asset_list",
92        "use_native_activity",
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 = []
101        forward_variables_from(
102            invoker,
103            "*",
104            _apk_specific_vars + _wrapper_script_vars + [ "visibility" ])
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" ])
113        shared_library = ":$_library_target"
114        apk_name = invoker.target_name
115        if (defined(invoker.output_name)) {
116          apk_name = invoker.output_name
117          install_script_name = "install_${invoker.output_name}"
118        }
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        ]
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) {
135        forward_variables_from(invoker,
136                               _wrapper_script_vars + [
137                                     "data",
138                                     "data_deps",
139                                     "deps",
140                                     "public_deps",
141                                   ])
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) {
161      forward_variables_from(invoker,
162                             _wrapper_script_vars + [
163                                   "data",
164                                   "data_deps",
165                                   "deps",
166                                   "public_deps",
167                                 ])
168
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    }
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) {
197      visibility = [ ":$_test_target" ]
198      sources = [
199        "//testing/gtest_ios/Default.png",
200      ]
201      outputs = [
202        "{{bundle_resources_dir}}/{{source_file_part}}",
203      ]
204    }
205
206    ios_app_bundle(_test_target) {
207      testonly = true
208
209      # See above call.
210      set_sources_assignment_filter([])
211      forward_variables_from(invoker, "*", [ "testonly" ])
212
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      }
218
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"
224      }
225      if (!defined(extra_substitutions)) {
226        extra_substitutions = []
227      }
228      extra_substitutions += [ "GTEST_BUNDLE_ID_SUFFIX=$_bundle_id_suffix" ]
229
230      if (!defined(deps)) {
231        deps = []
232      }
233      deps += [ "//build/config:exe_and_shlib_deps" ]
234      if (!defined(bundle_deps)) {
235        bundle_deps = []
236      }
237      bundle_deps += [ ":$_resources_bundle_data" ]
238    }
239  } else {
240    executable(target_name) {
241      deps = []
242      forward_variables_from(invoker, "*")
243
244      testonly = true
245      deps += [
246        "//build/config:exe_and_shlib_deps",
247
248        # Give tests the default manifest on Windows (a no-op elsewhere).
249        "//build/win:default_exe_manifest",
250      ]
251    }
252
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    }
261  }
262}
263
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}
272