chromium/ios/third_party/earl_grey2/ios_eg2_test.gni

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

import("//build/config/ios/ios_test_runner_wrapper.gni")
import("//build/config/ios/rules.gni")
import("//ios/build/chrome_build.gni")
import("//ios/chrome/app/chrome_app.gni")
import("//ios/public/provider/chrome/browser/build_config.gni")

template("ios_eg2_test_app_host") {
  _target_type = "ios_app_bundle"
  if (defined(invoker.target_type)) {
    _target_type = invoker.target_type
  }

  target(_target_type, target_name) {
    testonly = true
    forward_variables_from(invoker, "*")

    configs += [ "//build/config/ios:xctest_config" ]

    if (!defined(bundle_deps)) {
      bundle_deps = []
    }
    bundle_deps += [ "//ios/third_party/earl_grey2:app_framework+bundle" ]
    if (!defined(include_provider_resources)) {
      include_provider_resources = true
    }
    if (include_provider_resources) {
      bundle_deps += ios_providers_resources_targets
    }
    if (!defined(deps)) {
      deps = []
    }
    deps += [ "//ios/third_party/earl_grey2:app_framework+link" ]

    # Xcode needs the following frameworks installed in the application (and
    # signed) for the XCTest to run, so install them using
    # extra_system_frameworks.
    extra_system_frameworks = [
      "$ios_sdk_platform_path/Developer/Library/Frameworks/XCTest.framework",
      "$ios_sdk_platform_path/Developer/Library/PrivateFrameworks/XCTAutomationSupport.framework",
    ]

    # Xcode 13 now depends on XCTestCore. To keep things future proof, copy over
    # everything that Xcode copies.
    if (xcode_version_int >= 1300) {
      extra_system_frameworks += [
        "$ios_sdk_platform_path/Developer/Library/PrivateFrameworks/XCTestCore.framework",
        "$ios_sdk_platform_path/Developer/Library/PrivateFrameworks/XCUIAutomation.framework",
        "$ios_sdk_platform_path/Developer/Library/PrivateFrameworks/XCUnit.framework",
        "$ios_sdk_platform_path/Developer/usr/lib/libXCTestSwiftSupport.dylib",
      ]
    }

    # XCTestSupport framework is required for running EG tests with Xcode 14.3 or later.
    if (xcode_version_int >= 1430) {
      extra_system_frameworks += [ "$ios_sdk_platform_path/Developer/Library/PrivateFrameworks/XCTestSupport.framework" ]
    }
  }
}

set_defaults("ios_eg2_test_app_host") {
  configs = default_executable_configs
}

# EarlGrey2 tests are just XCUITests that also depends on EarlGrey2.
template("ios_eg2_test") {
  assert(defined(invoker.xcode_test_application_name),
         "xcode_test_application_name must be defined for $target_name")
  assert(
      defined(invoker.deps),
      "deps must be defined for $target_name to include at least one earl grey test file.")

  _target_name = target_name
  _test_target = "${target_name}_test"
  ios_xcuitest_test(_test_target) {
    forward_variables_from(invoker,
                           [
                             "xcode_test_application_name",
                             "xctest_bundle_principal_class",
                             "runner_only_bundle_deps",
                             "bundle_deps",
                             "deps",
                             "data_deps",
                           ])

    if (!defined(deps)) {
      deps = []
    }
    deps += [ "//ios/testing/earl_grey:eg_test_support+eg2" ]

    # TODO(crbug.com/40120290) Because we change the target name, the subnodes
    # are going to append with the _test in the naming, which won't be backwards
    # compatible during migration from iOS recipe to Chromium.
    output_name = "${_target_name}"

    # Performance tests use EarlGrey and are run in google3 mobile
    # harness. The mobile harness test runner expects to be passed the test
    # module (not runner) and will fail if TestTraceProcessor is not present
    # in the module's Frameworks directory
    if (!ios_is_perf_test) {
      # Those frameworks are force-linked via //base/test:test_support, so
      # ensure the framework bundles are packaged in the test runner and
      # not the test module.
      if (!defined(runner_only_bundle_deps)) {
        runner_only_bundle_deps = []
      }
      runner_only_bundle_deps += [ "//base/test:test_trace_processor+bundle" ]
    }
  }

  ios_test_runner_wrapper(target_name) {
    forward_variables_from(invoker,
                           [
                             "clones",
                             "data",
                             "data_deps",
                             "deps",
                             "executable_args",
                             "retries",
                             "xcode_test_application_name",
                           ])
    _root_build_dir = rebase_path("${root_build_dir}", root_build_dir)

    if (!defined(data_deps)) {
      data_deps = []
    }

    # Include the top ios_eg2_test target, and the host app
    data_deps += [ ":${_test_target}" ]

    if (!defined(executable_args)) {
      executable_args = []
    }

    # EG2 tests app are bundled as *-Runner.app, while the host app is bundled
    # as *.app.
    executable_args += [
      "--app",
      "@WrappedPath(${_root_build_dir}/${target_name}-Runner.app)",
    ]
    executable_args += [
      "--host-app",
      "@WrappedPath(${_root_build_dir}/${xcode_test_application_name}.app)",
    ]
  }
}

set_defaults("ios_eg2_test") {
  configs = default_shared_library_configs
}