Home | History | Annotate | Download | only in testing
      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().
     14 template("test") {
     15   if (is_android) {
     16     import("//build/config/android/config.gni")
     17     import("//build/config/android/rules.gni")
     18     import("//build/config/sanitizers/sanitizers.gni")
     19 
     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 
     30     _test_runner_target = "${_output_name}__test_runner_script"
     31     _wrapper_script_vars = [
     32       "ignore_all_data_deps",
     33       "shard_timeout",
     34     ]
     35 
     36     assert(_use_raw_android_executable || enable_java_templates)
     37 
     38     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 = []
     48         forward_variables_from(invoker,
     49                                "*",
     50                                _wrapper_script_vars + [ "extra_dist_files" ])
     51         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",
     84         "android_manifest_dep",
     85         "enable_multidex",
     86         "proguard_configs",
     87         "proguard_enabled",
     88         "use_default_launcher",
     89         "write_asset_list",
     90         "use_native_activity",
     91       ]
     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 = []
     99         forward_variables_from(
    100             invoker,
    101             "*",
    102             _apk_specific_vars + _wrapper_script_vars + [ "visibility" ])
    103 
    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" ])
    111         shared_library = ":$_library_target"
    112         apk_name = invoker.target_name
    113         if (defined(invoker.output_name)) {
    114           apk_name = invoker.output_name
    115           install_script_name = "install_${invoker.output_name}"
    116         }
    117 
    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         ]
    127       }
    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) {
    133         forward_variables_from(invoker,
    134                                _wrapper_script_vars + [
    135                                      "data",
    136                                      "data_deps",
    137                                      "deps",
    138                                      "public_deps",
    139                                    ])
    140         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) {
    159       forward_variables_from(invoker,
    160                              _wrapper_script_vars + [
    161                                    "data",
    162                                    "data_deps",
    163                                    "deps",
    164                                    "public_deps",
    165                                  ])
    166 
    167       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 
    189     # TODO(GYP_GONE): Delete this after we've converted everything to GN.
    190     # 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) {
    204       visibility = [ ":$_test_target" ]
    205       sources = [
    206         "//testing/gtest_ios/Default.png",
    207       ]
    208       outputs = [
    209         "{{bundle_resources_dir}}/{{source_file_part}}",
    210       ]
    211     }
    212 
    213     ios_app_bundle(_test_target) {
    214       testonly = true
    215 
    216       # See above call.
    217       set_sources_assignment_filter([])
    218       forward_variables_from(invoker, "*", [ "testonly" ])
    219 
    220       # 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       }
    225 
    226       _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"
    231       }
    232       if (!defined(extra_substitutions)) {
    233         extra_substitutions = []
    234       }
    235       extra_substitutions += [ "GTEST_BUNDLE_ID_SUFFIX=$_bundle_id_suffix" ]
    236 
    237       if (!defined(deps)) {
    238         deps = []
    239       }
    240       deps += [
    241         # 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       ]
    245       if (!defined(bundle_deps)) {
    246         bundle_deps = []
    247       }
    248       bundle_deps += [ ":$_resources_bundle_data" ]
    249     }
    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 
    266     # TODO(GYP_GONE): Delete this after we've converted everything to GN.
    267     # 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     }
    274 
    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     }
    283   }
    284 }
    285 
    286 # Test defaults.
    287 set_defaults("test") {
    288   if (is_android) {
    289     configs = default_shared_library_configs
    290   } else {
    291     configs = default_executable_configs
    292   }
    293 }
    294