# Copyright 2014 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.

import("//media/media_options.gni")
import("//testing/libfuzzer/fuzzer_test.gni")
import("//third_party/libaom/options.gni")

source_set("run_all_unittests") {
  testonly = true
  sources = [
    "run_all_unittests.cc",
  ]
  configs += [ "//media:media_config" ]
  deps = [
    "//base",
    "//base/test:test_support",
    "//media:test_support",
    "//mojo/core/embedder",
  ]

  if (is_android) {
    deps += [ "//ui/gl" ]
  }
}

source_set("pipeline_integration_test_base") {
  testonly = true

  if (media_use_ffmpeg) {
    sources = [
      "fake_encrypted_media.cc",
      "fake_encrypted_media.h",
      "mock_media_source.cc",
      "mock_media_source.h",
      "pipeline_integration_test_base.cc",
      "pipeline_integration_test_base.h",
    ]

    configs += [ "//media:media_config" ]

    deps = [
      "//base",
      "//base/test:test_support",
      "//media:test_support",
      "//testing/gmock",
      "//testing/gtest",
      "//third_party/libaom:av1_buildflags",
      "//url",
    ]
  }
}

source_set("pipeline_integration_tests") {
  testonly = true

  # Even if FFmpeg is enabled on Android we don't want these.
  # TODO(watk): Refactor tests that could be made to run on Android. See
  # http://crbug.com/570762
  if (media_use_ffmpeg && !is_android) {
    sources = [
      "pipeline_integration_test.cc",
    ]

    configs += [ "//build/config/compiler:no_size_t_to_int_warning" ]

    deps = [
      ":pipeline_integration_test_base",
      "//base",
      "//media:test_support",
      "//media/mojo/clients",
      "//services/service_manager/public/cpp:service_test_support",
      "//third_party/libaom:av1_buildflags",

      # Needed for the opus_config
      "//third_party/opus",
      "//url",

      # TODO(dalecurtis): Required since the gmock header is included in the
      # header for pipeline_integration_test_base.h.  This should be moved into
      # the .cc file to avoid the extra dependency here.
      "//testing/gmock",
    ]
  }
}

source_set("pipeline_integration_perftests") {
  testonly = true

  if (media_use_ffmpeg) {
    sources = [
      "pipeline_integration_perftest.cc",
    ]

    deps = [
      ":pipeline_integration_test_base",
      "//media:test_support",
      "//testing/gtest",
      "//testing/perf",

      # TODO(dalecurtis): Required since the gmock header is included in the
      # header for pipeline_integration_test_base.h.  This should be moved into
      # the .cc file to avoid the extra dependency here.
      "//testing/gmock",
    ]
  }
}

source_set("mojo_pipeline_integration_tests") {
  testonly = true

  if (media_use_ffmpeg && !is_android) {
    sources = [
      "pipeline_integration_test.cc",
    ]

    defines = [ "MOJO_RENDERER" ]

    configs += [ "//build/config/compiler:no_size_t_to_int_warning" ]

    deps = [
      ":pipeline_integration_test_base",
      "//base",
      "//base/test:test_support",
      "//media:test_support",
      "//media/mojo/clients",
      "//media/mojo/interfaces",
      "//media/mojo/services",
      "//services/service_manager/public/cpp:service_test_support",
      "//testing/gtest",
      "//third_party/libaom:av1_buildflags",
      "//ui/gfx:test_support",
      "//ui/gfx/geometry",

      # TODO(dalecurtis): Required since the gmock header is included in the
      # header for pipeline_integration_test_base.h.  This should be moved
      # into the .cc file to avoid the extra dependency here.
      "//testing/gmock",
    ]
  }
}

# Keep these aligned with FuzzerVariant in pipeline_integration_fuzzertest.c
pipeline_integration_fuzzer_variants = [
  "SRC",  # A SRC= version (not MSE) pipeline fuzzer test

  # MSE pipeline fuzzer test variants. Note, while it would be nice to have one
  # generic MSE fuzzer, we use distinct fuzzers corresponding 1:1 to a set of
  # mimetype+codec strings for now because:
  # a) the SourceBuffer implementation must be constructed to expect a
  #    particular bytestream type and (set of) codec(s); it does not
  #    auto-detect those from the appended media, and
  # b) maintaining the association of a (mutated) corpus item with the same MSE
  #    SourceBuffer configuration requires associating that item with the same
  #    mimetype+codec string. The benefits are simpler fuzzers and the ability
  #    to slice per-fuzzer coverage stats. The costs include more fuzzers, risk
  #    of new bytestream types or codec fuzzing gaps, and more potential
  #    duplicate bugs from different fuzzers for common issues exposed across
  #    mimetype+codec string variants.
  "WEBM_OPUS",
  "WEBM_VORBIS",
  "WEBM_VP8",
  "WEBM_VP9",
  "WEBM_OPUS_VP9",
  "MP4_FLAC",
  "MP3",

  # See below for additional variants depending on build configuration.
]

if (enable_av1_decoder) {
  pipeline_integration_fuzzer_variants += [ "MP4_AV1" ]
}

if (proprietary_codecs) {
  pipeline_integration_fuzzer_variants += [
    "ADTS",
    "MP4_AACLC",
    "MP4_AACSBR",

    # Though neither StreamParserFactory, MP4StreamParser, nor
    # SourceBufferState::Init differentiate kinds of AVC, we use "AVC1" here to
    # retain corpus associated with this fuzzer target name.
    "MP4_AVC1",
    "MP4_AACLC_AVC",
  ]
  if (enable_mse_mpeg2ts_stream_parser) {
    pipeline_integration_fuzzer_variants += [
      "MP2T_AACLC",
      "MP2T_AACSBR",
      "MP2T_AVC",
      "MP2T_MP3",
      "MP2T_AACLC_AVC",
    ]
  }
}

foreach(variant, pipeline_integration_fuzzer_variants) {
  if (variant == "SRC") {
    test_name = "media_pipeline_integration_fuzzer"
  } else {
    test_name = "mediasource_${variant}_pipeline_integration_fuzzer"
  }

  fuzzer_test(test_name) {
    sources = [
      "pipeline_integration_fuzzertest.cc",
    ]
    deps = [
      ":pipeline_integration_test_base",
      "//base",
      "//media",

      # TODO(dalecurtis): Required since the gmock header is included in the
      # header for pipeline_integration_test_base.h.  This should be
      # moved into the .cc file to avoid the extra dependency here.
      "//testing/gmock",
      "//third_party/libaom:av1_buildflags",
      "//ui/gfx:test_support",
    ]

    libfuzzer_options = [
      # This is done to suppress tons of log messages generated by gmock asserts.
      "close_fd_mask=1",
    ]

    defines = [ "PIPELINE_FUZZER_VARIANT=${variant}" ]

    seed_corpus = "//media/test/data/"
  }
}
