# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# ==============================================================================
# TEST SETUP
# ==============================================================================

# Define a test as an executable (or apk on Android) with the "testonly" flag
# set.
template("test") {
  if (is_android) {
    import("//build/config/android/config.gni")
    import("//build/config/android/rules.gni")

    main_target_name = target_name
    library_name = "_${target_name}__library"
    apk_name = "${target_name}_apk"

    shared_library(library_name) {
      # Configs will always be defined since we set_defaults for a component
      # in the main config. We want to use those rather than whatever came with
      # the nested shared/static library inside the component.
      configs = []  # Prevent list overwriting warning.
      configs = invoker.configs

      # See above call.
      set_sources_assignment_filter([])

      testonly = true

      if (defined(invoker.all_dependent_configs)) {
        all_dependent_configs = invoker.all_dependent_configs
      }
      if (defined(invoker.allow_circular_includes_from)) {
        allow_circular_includes_from = invoker.allow_circular_includes_from
      }
      if (defined(invoker.cflags)) {
        cflags = invoker.cflags
      }
      if (defined(invoker.cflags_c)) {
        cflags_c = invoker.cflags_c
      }
      if (defined(invoker.cflags_cc)) {
        cflags_cc = invoker.cflags_cc
      }
      if (defined(invoker.cflags_objc)) {
        cflags_objc = invoker.cflags_objc
      }
      if (defined(invoker.cflags_objcc)) {
        cflags_objcc = invoker.cflags_objcc
      }
      if (defined(invoker.check_includes)) {
        check_includes = invoker.check_includes
      }
      if (defined(invoker.data)) {
        data = invoker.data
      }
      if (defined(invoker.data_deps)) {
        data_deps = invoker.data_deps
      }
      if (defined(invoker.datadeps)) {
        datadeps = invoker.datadeps
      }
      if (defined(invoker.defines)) {
        defines = invoker.defines
      }
      deps = []
      if (!defined(invoker.use_launcher) || invoker.use_launcher) {
        deps += [ "//testing/android/native_test:native_test_native_code" ]
      }
      if (defined(invoker.deps)) {
        deps += invoker.deps
      }
      if (defined(invoker.direct_dependent_configs)) {
        direct_dependent_configs = invoker.direct_dependent_configs
      }
      if (defined(invoker.forward_dependent_configs_from)) {
        forward_dependent_configs_from = invoker.forward_dependent_configs_from
      }
      if (defined(invoker.include_dirs)) {
        include_dirs = invoker.include_dirs
      }
      if (defined(invoker.ldflags)) {
        ldflags = invoker.ldflags
      }
      if (defined(invoker.lib_dirs)) {
        lib_dirs = invoker.lib_dirs
      }
      if (defined(invoker.libs)) {
        libs = invoker.libs
      }
      if (defined(invoker.output_extension)) {
        output_extension = invoker.output_extension
      }
      if (defined(invoker.output_name)) {
        output_name = invoker.output_name
      }
      if (defined(invoker.public)) {
        public = invoker.public
      }
      if (defined(invoker.public_configs)) {
        public_configs = invoker.public_configs
      }
      if (defined(invoker.public_deps)) {
        public_deps = invoker.public_deps
      }
      if (defined(invoker.sources)) {
        sources = invoker.sources
      }
      if (defined(invoker.visibility)) {
        visibility = invoker.visibility
      }
    }

    unittest_apk(apk_name) {
      unittests_dep = ":$library_name"
      apk_name = main_target_name
      if (defined(invoker.output_name)) {
        apk_name = invoker.output_name
        unittests_binary = "lib${apk_name}.so"
      }
      deps = [
        ":$library_name",
      ]
      if (defined(invoker.apk_deps)) {
        deps += invoker.apk_deps
      }
      if (defined(invoker.apk_asset_location)) {
        asset_location = invoker.apk_asset_location
      }
    }

    test_name = main_target_name
    if (defined(invoker.output_name)) {
      test_name = invoker.output_name
    }
    test_runner_script_name = "${test_name}__test_runner_script"
    test_runner_script(test_runner_script_name) {
      test_name = test_name
      test_type = "gtest"
      test_suite = test_name
      if (defined(invoker.isolate_file)) {
        isolate_file = invoker.isolate_file
      }
    }

    group(target_name) {
      testonly = true
      datadeps = [
        ":$test_runner_script_name",
      ]
      deps = [
        ":$library_name",
        ":$apk_name",
      ]
    }
  } else {
    executable(target_name) {
      # See above.
      configs = []  # Prevent list overwriting warning.
      configs = invoker.configs

      # See above call.
      set_sources_assignment_filter([])

      testonly = true

      if (defined(invoker.all_dependent_configs)) {
        all_dependent_configs = invoker.all_dependent_configs
      }
      if (defined(invoker.allow_circular_includes_from)) {
        allow_circular_includes_from = invoker.allow_circular_includes_from
      }
      if (defined(invoker.cflags)) {
        cflags = invoker.cflags
      }
      if (defined(invoker.cflags_c)) {
        cflags_c = invoker.cflags_c
      }
      if (defined(invoker.cflags_cc)) {
        cflags_cc = invoker.cflags_cc
      }
      if (defined(invoker.cflags_objc)) {
        cflags_objc = invoker.cflags_objc
      }
      if (defined(invoker.cflags_objcc)) {
        cflags_objcc = invoker.cflags_objcc
      }
      if (defined(invoker.check_includes)) {
        check_includes = invoker.check_includes
      }
      if (defined(invoker.data)) {
        data = invoker.data
      }
      if (defined(invoker.data_deps)) {
        data_deps = invoker.data_deps
      }
      if (defined(invoker.datadeps)) {
        datadeps = invoker.datadeps
      }
      if (defined(invoker.defines)) {
        defines = invoker.defines
      }

      # All shared libraries must have the sanitizer deps to properly link in
      # asan mode (this target will be empty in other cases).
      if (defined(invoker.deps)) {
        deps = invoker.deps + [ "//build/config/sanitizers:deps" ]
      } else {
        deps = [
          "//build/config/sanitizers:deps",
        ]
      }
      if (defined(invoker.direct_dependent_configs)) {
        direct_dependent_configs = invoker.direct_dependent_configs
      }
      if (defined(invoker.forward_dependent_configs_from)) {
        forward_dependent_configs_from = invoker.forward_dependent_configs_from
      }
      if (defined(invoker.include_dirs)) {
        include_dirs = invoker.include_dirs
      }
      if (defined(invoker.ldflags)) {
        ldflags = invoker.ldflags
      }
      if (defined(invoker.lib_dirs)) {
        lib_dirs = invoker.lib_dirs
      }
      if (defined(invoker.libs)) {
        libs = invoker.libs
      }
      if (defined(invoker.output_extension)) {
        output_extension = invoker.output_extension
      }
      if (defined(invoker.output_name)) {
        output_name = invoker.output_name
      }
      if (defined(invoker.public)) {
        public = invoker.public
      }
      if (defined(invoker.public_configs)) {
        public_configs = invoker.public_configs
      }
      if (defined(invoker.public_deps)) {
        public_deps = invoker.public_deps
      }
      if (defined(invoker.sources)) {
        sources = invoker.sources
      }
      if (defined(invoker.visibility)) {
        visibility = invoker.visibility
      }
    }
  }
}
