chromium/chrome/test/data/webui/build_webui_tests.gni

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

import("//chrome/common/features.gni")
import("//tools/grit/preprocess_if_expr.gni")
import("//tools/typescript/webui_ts_library.gni")
import("//ui/webui/resources/tools/generate_grd.gni")

template("build_webui_tests") {
  not_needed([ "target_name" ])

  forward_variables_from(invoker, [ "files" ])

  preprocess_dir = "${target_gen_dir}/preprocessed"
  tsc_dir = "${target_gen_dir}/tsc"

  # Generated Mojo TS files.
  if (defined(invoker.mojo_files)) {
    assert(defined(invoker.mojo_files_deps))
    mojo_files = []
    foreach(mojo_file, invoker.mojo_files) {
      mojo_files += [ get_path_info(invoker.mojo_files, "file") ]
    }
  }

  if (defined(invoker.static_files)) {
    static_files_filter = [ "*.html" ]
    static_files = filter_include(invoker.static_files, static_files_filter)
    assert(static_files == invoker.static_files,
           "Static files must be .html files")
    preprocess_if_expr("preprocess_static_files") {
      visibility = [ ":build_grdp" ]
      defines = chrome_grit_defines
      in_folder = "."
      out_folder = preprocess_dir
      in_files = static_files
      out_manifest = "${target_gen_dir}/preprocess_static_files_manifest.json"
    }
  }

  preprocess_if_expr("preprocess") {
    visibility = [ ":build_ts" ]
    defines = chrome_grit_defines
    in_folder = "."
    out_folder = preprocess_dir
    in_files = files
  }

  if (defined(invoker.mojo_files_deps)) {
    copy("copy_mojo") {
      visibility = [ ":build_ts" ]
      deps = invoker.mojo_files_deps
      sources = invoker.mojo_files
      outputs = [ "${preprocess_dir}/{{source_file_part}}" ]
    }
  }

  webui_ts_library("build_ts") {
    composite = false
    if (defined(invoker.ts_composite)) {
      composite = invoker.ts_composite
    }
    if (!composite) {
      visibility = [ ":build_grdp" ]
    }
    root_dir = preprocess_dir
    out_dir = tsc_dir

    # Default tsconfig is set to
    # //chrome/test/data/webui/tsconfig_base_polymer.json for targets
    # with a Polymer or Lit dependency, and
    # //chrome/test/data/webui/tsconfig_base_polymer.json) for other targets.
    tsconfig_base = "//chrome/test/data/webui/tsconfig_base.json"
    if (defined(invoker.ts_tsconfig_base)) {
      tsconfig_base = invoker.ts_tsconfig_base
    } else if (defined(invoker.ts_deps)) {
      has_polymer_or_lit =
          filter_include(invoker.ts_deps,
                         [
                           "//third_party/polymer/v3_0:library",
                           "//third_party/lit/v3_0:build_ts",
                         ]) != []
      if (has_polymer_or_lit) {
        tsconfig_base = "//chrome/test/data/webui/tsconfig_base_polymer.json"
      }
    }

    path_mappings = []

    # Add invoker provided path_mappings before the default one, because the
    # order matters.
    if (defined(invoker.ts_path_mappings)) {
      path_mappings += invoker.ts_path_mappings
    }
    if (defined(invoker.is_chrome_untrusted) && invoker.is_chrome_untrusted) {
      path_mappings += [ "chrome-untrusted://webui-test/*|" + rebase_path(
                             "$root_gen_dir/chrome/test/data/webui/tsc/*",
                             target_gen_dir) ]
      webui_context_type = "untrusted"
    } else {
      path_mappings += [ "chrome://webui-test/*|" + rebase_path(
                             "$root_gen_dir/chrome/test/data/webui/tsc/*",
                             target_gen_dir) ]
      webui_context_type = "trusted"
    }
    in_files = files

    if (defined(invoker.ts_definitions)) {
      definitions = invoker.ts_definitions
    }
    deps = [ "//chrome/test/data/webui:build_ts" ]
    if (defined(invoker.ts_deps)) {
      deps += invoker.ts_deps
    }
    extra_deps = [ ":preprocess" ]

    if (defined(invoker.mojo_files)) {
      target_outputs = get_target_outputs(":copy_mojo")

      # Add all Mojo JS files produced by `:copy_mojo` as inputs to the TS
      # compiler.
      foreach(o, target_outputs) {
        in_files += [ rebase_path(o, preprocess_dir) ]
      }
      extra_deps += [ ":copy_mojo" ]
    }
  }

  # This target is public so that it can be depended on by
  # chrome/test/data/webui:build_grd which will place this grdp file into a grd
  # file.
  generate_grd("build_grdp") {
    resource_path_prefix = rebase_path(".", "//chrome/test/data/webui")
    if (defined(invoker.resource_path_prefix)) {
      resource_path_prefix = invoker.resource_path_prefix
    }
    grd_prefix = "webui_" + string_replace(resource_path_prefix, "/", "_")
    out_grd = "$target_gen_dir/resources.grdp"
    deps = [ ":build_ts" ]
    manifest_files =
        filter_include(get_target_outputs(":build_ts"), [ "*_manifest.json" ])
    if (defined(invoker.static_files)) {
      deps += [ ":preprocess_static_files" ]
      manifest_files +=
          [ "${target_gen_dir}/preprocess_static_files_manifest.json" ]
    }
  }
}