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

import("//build/config/sanitizers/sanitizers.gni")
import("//build_overrides/build.gni")
import("../pdfium.gni")
import("test.gni")

source_set("test_support") {
  testonly = true
  sources = [
    "command_line_helpers.cpp",
    "command_line_helpers.h",
    "font_renamer.cpp",
    "font_renamer.h",
    "fx_string_testhelpers.cpp",
    "fx_string_testhelpers.h",
    "invalid_seekable_read_stream.cpp",
    "invalid_seekable_read_stream.h",
    "pseudo_retainable.h",
    "scoped_set_tz.cpp",
    "scoped_set_tz.h",
    "string_write_stream.cpp",
    "string_write_stream.h",
    "test_fonts.cpp",
    "test_fonts.h",
    "test_loader.cpp",
    "test_loader.h",
    "test_support.h",
    "utils/bitmap_saver.cpp",
    "utils/bitmap_saver.h",
    "utils/file_util.cpp",
    "utils/file_util.h",
    "utils/hash.cpp",
    "utils/hash.h",
    "utils/png_encode.cpp",
    "utils/png_encode.h",
    "utils/scoped_time_overrides.cpp",
    "utils/scoped_time_overrides.h",
  ]
  data = [ "resources/" ]
  public_deps = [
    ":path_service",
    "//third_party/test_fonts",
  ]
  deps = [
    "../:pdfium_public_headers",
    "../core/fdrm",
    "../core/fxcrt",
    "../core/fxge",
    "../fpdfsdk",
    "image_diff",
  ]
  configs += [
    "../:pdfium_strict_config",
    "../:pdfium_noshorten_config",
    ":pdfium_test_config",
  ]
  visibility = [ "../*" ]
  if (is_posix) {
    sources += [
      "scoped_locale.cc",
      "scoped_locale.h",
    ]
  }
  if (pdf_use_partition_alloc) {
    sources += [
      "allocator_shim_config.cpp",
      "allocator_shim_config.h",
    ]
    deps += [ "//base/allocator/partition_allocator/src/partition_alloc" ]
  }
  if (pdf_enable_v8) {
    sources += [
      "v8_initializer.cpp",
      "v8_initializer.h",
    ]
    deps += [
      "//v8",
      "//v8:v8_libplatform",
    ]
    configs += [ "//v8:external_startup_data" ]
  }
}

if (build_with_chromium && pdf_use_skia) {
  source_set("discardable_memory_allocator") {
    testonly = true
    sources = [
      "chromium_support/discardable_memory_allocator.cc",
      "chromium_support/discardable_memory_allocator.h",
    ]
    deps = [ "//base/test:test_support" ]
    configs += [ "../:pdfium_strict_config" ]
  }
}

source_set("pixel_diff_utils") {
  testonly = true
  sources = [
    "utils/pixel_diff_util.cpp",
    "utils/pixel_diff_util.h",
  ]
  configs += [ "../:pdfium_strict_config" ]
}

pdfium_unittest_source_set("unittests") {
  sources = [ "utils/pixel_diff_util_unittest.cpp" ]
  deps = [ ":pixel_diff_utils" ]
  pdfium_root_dir = "../"
}

source_set("path_service") {
  testonly = true
  sources = [
    "utils/path_service.cpp",
    "utils/path_service.h",
  ]
  deps = [ "../core/fxcrt" ]
  configs += [
    ":pdfium_in_chromium_config",
    "../:pdfium_strict_config",
    "../:pdfium_noshorten_config",
  ]
  visibility = [ "../*" ]
}

source_set("test_environments") {
  testonly = true
  sources = [
    "pdf_test_environment.cpp",
    "pdf_test_environment.h",
  ]
  deps = [
    ":test_support",
    "../core/fxcrt",
    "../core/fxge",
    "//testing/gtest",
  ]
  configs += [
    "../:pdfium_strict_config",
    "../:pdfium_noshorten_config",
  ]
  if (pdf_enable_v8) {
    sources += [
      "v8_test_environment.cpp",
      "v8_test_environment.h",
    ]
    deps += [
      "../fxjs",
      "//v8",
      "//v8:v8_libplatform",
    ]
    configs += [ "//v8:external_startup_data" ]
  }
  if (pdf_enable_xfa) {
    sources += [
      "xfa_test_environment.cpp",
      "xfa_test_environment.h",
    ]
    deps += [
      "../fxjs:gc",
      "../xfa/fgas/font",
    ]
  }
}

source_set("unit_test_support") {
  testonly = true
  sources = []
  deps = [ "//testing/gtest" ]
  configs += [
    "../:pdfium_strict_config",
    "../:pdfium_noshorten_config",
  ]
  public_deps = [
    ":test_environments",
    ":test_support",
  ]
  if (is_posix) {
    sources += [
      "temporary_file_test.cpp",
      "temporary_file_test.h",
    ]
    deps += [ "../core/fxcrt" ]
  }
  if (pdf_enable_v8) {
    sources += [
      "fxv8_unittest.cpp",
      "fxv8_unittest.h",
    ]
    deps += [ "../fxjs" ]
    configs += [ "//v8:external_startup_data" ]
    if (pdf_enable_xfa) {
      sources += [
        "fxgc_unittest.cpp",
        "fxgc_unittest.h",
      ]
      deps += [ "../fxjs:gc" ]
    }
  }
}

source_set("embedder_test_support") {
  testonly = true
  sources = [
    "embedder_test.cpp",
    "embedder_test.h",
    "embedder_test_constants.h",
    "embedder_test_environment.cpp",
    "embedder_test_environment.h",
    "embedder_test_mock_delegate.h",
    "embedder_test_timer_handling_delegate.h",
    "fake_file_access.cpp",
    "fake_file_access.h",
    "range_set.cpp",
    "range_set.h",
    "utils/compare_coordinates.cc",
    "utils/compare_coordinates.h",
  ]
  deps = [
    ":pixel_diff_utils",
    "../:pdfium_public_headers",
    "../core/fdrm",
    "../core/fxge",
    "../fpdfsdk",
    "image_diff",
    "//testing/gmock",
    "//testing/gtest",
    "//third_party/simdutf",
  ]
  public_deps = [
    ":test_environments",
    ":test_support",
    "../core/fxcrt",
  ]
  configs += [
    "../:pdfium_strict_config",
    "../:pdfium_noshorten_config",
  ]
  visibility = [ "../*" ]
  if (pdf_enable_v8) {
    sources += [
      "external_engine_embedder_test.cpp",
      "external_engine_embedder_test.h",
      "js_embedder_test.cpp",
      "js_embedder_test.h",
    ]
    deps += [
      "../fxjs",
      "//v8",
      "//v8:v8_libplatform",
    ]
    configs += [ "//v8:external_startup_data" ]
    if (pdf_enable_xfa) {
      sources += [
        "xfa_js_embedder_test.cpp",
        "xfa_js_embedder_test.h",
      ]
      deps += [
        "../fpdfsdk",
        "../fpdfsdk/fpdfxfa",
        "../xfa/fxfa",
        "../xfa/fxfa/parser",
      ]
    }
  }
}

config("pdfium_in_chromium_config") {
  defines = []
  if (build_with_chromium) {
    defines += [ "BUILD_WITH_CHROMIUM" ]
  }
}

config("pdfium_test_config") {
  defines = []
  include_dirs = [ ".." ]
  if (pdf_use_skia) {
    defines += [ "PDF_ENABLE_SKIA" ]
  }
  if (is_asan) {
    defines += [ "PDF_ENABLE_ASAN" ]
  }
  if (pdf_use_partition_alloc) {
    defines += [ "PDF_USE_PARTITION_ALLOC" ]
  }
  if (enable_callgrind) {
    defines += [ "ENABLE_CALLGRIND" ]
  }
}

executable("pdfium_test") {
  testonly = true
  sources = [
    "helpers/dump.cc",
    "helpers/dump.h",
    "helpers/event.cc",
    "helpers/event.h",
    "helpers/page_renderer.cc",
    "helpers/page_renderer.h",
    "helpers/write.cc",
    "helpers/write.h",
    "pdfium_test.cc",
  ]

  # Note: One should write programs that depend on ../:pdfium. Whereas this
  # test program does not set a good example, and depends on PDFium internals,
  # as well as test support code, for convenience.
  deps = [
    ":test_support",
    "../:pdfium_public_headers",
    "../fpdfsdk",
    "image_diff",
    "//build/win:default_exe_manifest",
  ]
  configs += [
    ":pdfium_in_chromium_config",
    ":pdfium_test_config",
    "../:pdfium_common_config",
  ]

  if (is_win) {
    sources += [
      "helpers/win32/com_factory.cc",
      "helpers/win32/com_factory.h",
    ]
  }

  if (pdf_enable_v8) {
    deps += [
      "//v8:v8_headers",
      "//v8:v8_libplatform",
    ]
    include_dirs = [ "//v8" ]
    configs += [ "//v8:external_startup_data" ]
  }
  if (pdf_use_skia) {
    deps += [ "//skia" ]
    if (build_with_chromium) {
      deps += [ ":discardable_memory_allocator" ]
    }
  }

  if (!build_with_chromium) {
    assert_no_deps = [ "//testing/gtest/*" ]
  }
}

# Dummy group to keep satisfy references from //build.
group("test_scripts_shared") {
}
