qt_find_package(gRPC CONFIG)
qt_find_package(Protobuf CONFIG)

unset(extra_generator_args)
if(TARGET Qt::Qml)
    list(APPEND extra_generator_args QML)
endif()

# gRPC tests require reference gRPC and protobuf implementation
if(NOT TARGET gRPC::grpc_cpp_plugin
    OR NOT TARGET gRPC::grpc++
    OR NOT TARGET protobuf::libprotobuf
    OR NOT TARGET protobuf::protoc)
    message(AUTHOR_WARNING "QtGrpc tests require the reference gRPC C++ libraries.")
    return ()
endif()

qt_protobuf_generate(GENERATED_TARGET tst_grpc_tests_common_gen
    PROTO_FILES
        proto/simpletest.proto
        proto/testservice.proto
    ${extra_generator_args}
)

add_subdirectory(echoserver)
qt_internal_add_test(tst_grpc_client_basic
    SOURCES
        clienttest.cpp
    LIBRARIES
        Qt::Grpc
        Qt::Protobuf
    DEFINES
        QT_GRPC_SERVER_EXECUTABLE=\"$<TARGET_FILE:echoserver>\"
)
_qt_internal_link_protobuf_objects(tst_grpc_client_basic tst_grpc_tests_common_gen)

if(TARGET Qt::QuickTest)
    add_subdirectory(qml)
endif()

# TODO Qt6: Enable secure connection test later
#add_subdirectory(secureechoserver)
#qt_internal_add_test(grpc_client_secure
#    SOURCES
#        sslclienttest.cpp
#    LIBRARIES
#        Qt::Grpc
#        Qt::Protobuf
#    DEFINES
#        QT_GRPC_SERVER_EXECUTABLE=\"$<TARGET_FILE:secureechoserver>\"
#)
#_qt_internal_link_protobuf_objects(grpc_client_secure grpc_tests_common_gen)
