# 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

      testonly = true

      # Don't use "*" to forward all variables since some (like output_name
      # and isolate_file) apply only to the APK below.
      deps = []
      forward_variables_from(invoker,
                             [
                               "all_dependent_configs",
                               "allow_circular_includes_from",
                               "cflags",
                               "cflags_c",
                               "cflags_cc",
                               "check_includes",
                               "data",
                               "data_deps",
                               "datadeps",
                               "defines",
                               "deps",
                               "include_dirs",
                               "ldflags",
                               "lib_dirs",
                               "libs",
                               "output_extension",
                               "output_name",
                               "public",
                               "public_configs",
                               "public_deps",
                               "sources",
                               "visibility",
                             ])

      if (!defined(invoker.use_default_launcher) ||
          invoker.use_default_launcher) {
        deps += [ "//testing/android/native_test:native_test_native_code" ]
      }
    }

    unittest_apk(apk_name) {
      forward_variables_from(invoker,
                             [
                               "android_manifest",
                               "deps",
                               "use_default_launcher",
                               "write_asset_list",
                             ])
      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" ]
    }

    _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
      }
    }
    incremental_test_runner_script_name =
        "${_test_name}_incremental__test_runner_script"
    test_runner_script(incremental_test_runner_script_name) {
      test_name = "${_test_name}_incremental"
      test_type = "gtest"
      test_suite = _test_name
      incremental_install = true
      if (defined(invoker.isolate_file)) {
        isolate_file = invoker.isolate_file
      }
    }

    group(target_name) {
      testonly = true
      datadeps = [
        ":$test_runner_script_name",
      ]
      deps = [
        ":$apk_name",
      ]
    }
    group("${target_name}_incremental") {
      testonly = true
      datadeps = [
        ":$incremental_test_runner_script_name",
      ]
      deps = [
        ":${apk_name}_incremental",
      ]
    }
  } else if (is_ios) {
    if (is_ios) {
      import("//build/config/ios/rules.gni")
    }

    ios_app(target_name) {
      # TODO(GYP): Make this configurable and only provide a default
      # that can be overridden.
      info_plist = "//testing/gtest_ios/unittest-Info.plist"
      app_name = target_name
      entitlements_path = "//testing/gtest_ios"
      code_signing_identity = ""
      testonly = true

      # See above call.
      set_sources_assignment_filter([])

      forward_variables_from(invoker,
                             [
                               "all_dependent_configs",
                               "allow_circular_includes_from",
                               "cflags",
                               "cflags_c",
                               "cflags_cc",
                               "cflags_objc",
                               "cflags_objcc",
                               "check_includes",
                               "configs",
                               "data",
                               "data_deps",
                               "defines",
                               "include_dirs",
                               "ldflags",
                               "libs",
                               "output_extension",
                               "output_name",
                               "public",
                               "public_configs",
                               "public_deps",
                               "sources",
                               "visibility",
                             ])

      if (defined(invoker.deps)) {
        deps = invoker.deps
      } else {
        deps = []
      }
      deps += [
        # All shared libraries must have the sanitizer deps to properly link in
        # asan mode (this target will be empty in other cases).
        "//build/config/sanitizers:deps",
      ]
    }
  } else {
    executable(target_name) {
      forward_variables_from(invoker, "*")

      testonly = true

      if (!defined(invoker.deps)) {
        deps = []
      }
      deps += [
        # All shared libraries must have the sanitizer deps to properly link in
        # asan mode (this target will be empty in other cases).
        "//build/config/sanitizers:deps",

        # Give tests the default manifest on Windows (a no-op elsewhere).
        "//build/win:default_exe_manifest",
      ]
    }
  }
}