# Copyright 2020 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

import("//build_overrides/pigweed.gni")

import("$dir_pw_build/target_types.gni")
import("$dir_pw_compilation_testing/negative_compilation_test.gni")
import("$dir_pw_sync/backend.gni")
import("$dir_pw_thread/backend.gni")
import("$dir_pw_unit_test/test.gni")

config("public") {
  include_dirs = [ "public" ]
  visibility = [ ":*" ]
}

pw_source_set("server_api") {
  public_configs = [ ":public" ]
  public = [
    "public/pw_rpc/raw/internal/method.h",
    "public/pw_rpc/raw/internal/method_union.h",
    "public/pw_rpc/raw/server_reader_writer.h",
  ]
  sources = [ "method.cc" ]
  public_deps = [
    "..:server",
    dir_pw_bytes,
  ]
}

pw_source_set("client_api") {
  public_configs = [ ":public" ]
  public = [ "public/pw_rpc/raw/client_reader_writer.h" ]
  public_deps = [
    "..:client",
    dir_pw_bytes,
  ]
}

pw_source_set("fake_channel_output") {
  public = [ "public/pw_rpc/raw/fake_channel_output.h" ]
  public_configs = [ ":public" ]
  public_deps = [ "..:fake_channel_output" ]
}

pw_source_set("test_method_context") {
  public_configs = [ ":public" ]
  public = [ "public/pw_rpc/raw/test_method_context.h" ]
  public_deps = [
    ":fake_channel_output",
    ":server_api",
    "..:test_utils",
    dir_pw_assert,
    dir_pw_containers,
  ]
}

pw_source_set("client_testing") {
  public = [ "public/pw_rpc/raw/client_testing.h" ]
  sources = [ "client_testing.cc" ]
  public_deps = [
    ":fake_channel_output",
    "..:client",
  ]
  deps = [ "..:log_config" ]
}

pw_test_group("tests") {
  tests = [
    ":codegen_test",
    ":client_test",
    ":client_reader_writer_test",
    ":method_test",
    ":method_info_test",
    ":method_union_test",
    ":server_reader_writer_test",
    ":stub_generation_test",
    ":synchronous_call_test",
  ]
}

pw_test("codegen_test") {
  deps = [
    ":client_testing",
    ":test_method_context",
    "..:test_protos.pwpb",
    "..:test_protos.raw_rpc",
    dir_pw_protobuf,
  ]
  sources = [ "codegen_test.cc" ]
}

pw_test("client_test") {
  deps = [
    ":client_api",
    ":client_testing",
    "..:test_utils",
  ]
  sources = [ "client_test.cc" ]
}

pw_test("client_reader_writer_test") {
  deps = [
    ":client_api",
    ":client_testing",
    "..:test_protos.raw_rpc",
  ]
  sources = [ "client_reader_writer_test.cc" ]
}

pw_test("method_test") {
  deps = [
    ":server_api",
    ":service_nc_test",  # Pull in the service NC test through this test
    "$dir_pw_containers",
    "..:test_protos.pwpb",
    "..:test_protos.raw_rpc",
    "..:test_utils",
    dir_pw_protobuf,
  ]
  sources = [ "method_test.cc" ]
}

pw_test("method_info_test") {
  deps = [
    "..:common",
    "..:test_protos.raw_rpc",
    "..:test_utils",
  ]
  sources = [ "method_info_test.cc" ]
}

pw_test("method_union_test") {
  deps = [
    ":server_api",
    "..:test_protos.pwpb",
    "..:test_utils",
    dir_pw_protobuf,
  ]
  sources = [ "method_union_test.cc" ]
}

pw_test("server_reader_writer_test") {
  deps = [
    ":server_api",
    ":test_method_context",
    "..:test_protos.raw_rpc",
  ]
  sources = [ "server_reader_writer_test.cc" ]
}

pw_test("stub_generation_test") {
  deps = [ "..:test_protos.raw_rpc" ]
  sources = [ "stub_generation_test.cc" ]
}

pw_test("synchronous_call_test") {
  deps = [
    ":test_method_context",
    "$dir_pw_thread:thread",
    "$dir_pw_work_queue:pw_work_queue",
    "$dir_pw_work_queue:stl_test_thread",
    "$dir_pw_work_queue:test_thread",
    "..:synchronous_client_api",
    "..:test_protos.raw_rpc",
  ]
  sources = [ "synchronous_call_test.cc" ]
  enable_if = pw_sync_TIMED_THREAD_NOTIFICATION_BACKEND != ""

  # TODO: https://pwbug.dev/325509758 - Doesn't work on the Pico yet; hangs
  # indefinitely.
  if (pw_build_EXECUTABLE_TARGET_TYPE == "pico_executable") {
    enable_if = false
  }
}

pw_cc_negative_compilation_test("service_nc_test") {
  sources = [ "service_nc_test.cc" ]
  deps = [ "..:test_protos.raw_rpc" ]
}
