find_package(Qt6 REQUIRED COMPONENTS Test)

# Unit tests run without SteamVR or a headset. They use the "offscreen" QPA platform;
# tests that need a GPU context QSKIP themselves when none is available.

qt_add_executable(tst_inputbridge tst_inputbridge.cpp)
target_link_libraries(tst_inputbridge PRIVATE vrdashboard_core Qt6::Test)
add_test(NAME inputbridge COMMAND tst_inputbridge)

qt_add_executable(tst_render tst_render.cpp)
target_link_libraries(tst_render PRIVATE vrdashboard_core Qt6::Test)
qt_add_qml_module(tst_render
    URI RenderTest
    VERSION 1.0
    QML_FILES TwoTone.qml Spinner.qml)
add_test(NAME render_opengl COMMAND tst_render)
set_tests_properties(render_opengl PROPERTIES ENVIRONMENT "VRDASH_TEST_API=opengl")
add_test(NAME render_vulkan COMMAND tst_render)
set_tests_properties(render_vulkan PROPERTIES ENVIRONMENT "VRDASH_TEST_API=vulkan")

set_tests_properties(inputbridge PROPERTIES ENVIRONMENT "QT_QPA_PLATFORM=offscreen")

# Two surfaces in one app (snapshot mode): set-up, hooks, rendering per surface.
qt_add_executable(tst_surfaces tst_surfaces.cpp)
target_link_libraries(tst_surfaces PRIVATE vrdashboard_core)
qt_add_qml_module(tst_surfaces
    URI SurfacesTest
    VERSION 1.0
    QML_FILES Red.qml Blue.qml)
add_test(NAME surfaces_opengl COMMAND tst_surfaces --graphics-api opengl)
add_test(NAME surfaces_vulkan COMMAND tst_surfaces --graphics-api vulkan)
add_test(NAME surfaces_cli_key COMMAND tst_surfaces --graphics-api opengl)
set_tests_properties(surfaces_cli_key PROPERTIES ENVIRONMENT "VRDASH_TEST_CLI_KEY=1")
add_test(NAME surfaces_prepare_fail COMMAND tst_surfaces --graphics-api opengl)
set_tests_properties(surfaces_prepare_fail PROPERTIES ENVIRONMENT "VRDASH_TEST_PREPARE_FAIL=1")
add_test(NAME surfaces_command COMMAND tst_surfaces)
set_tests_properties(surfaces_command PROPERTIES ENVIRONMENT "VRDASH_TEST_COMMAND=1")
add_test(NAME surfaces_desktop_novr COMMAND tst_surfaces)
set_tests_properties(surfaces_desktop_novr PROPERTIES
                     ENVIRONMENT "VRDASH_TEST_DESKTOP_NOVR=1;QT_QPA_PLATFORM=offscreen")
set_tests_properties(surfaces_opengl surfaces_vulkan surfaces_cli_key
                     PROPERTIES SKIP_RETURN_CODE 77)

# The app's settings file (docs/DESIGN.md, D34).
qt_add_executable(tst_config tst_config.cpp)
target_link_libraries(tst_config PRIVATE frameassistant_lib Qt6::Test)
add_test(NAME config COMMAND tst_config)

# HA entities and the HA device (D23, D31, D32); the device with a fake MQTT client.
qt_add_executable(tst_haentity tst_haentity.cpp)
target_link_libraries(tst_haentity PRIVATE frameassistant_lib Qt6::Test)
add_test(NAME haentity COMMAND tst_haentity)
qt_add_executable(tst_hadevice tst_hadevice.cpp FakeMqttClient.h)
target_link_libraries(tst_hadevice PRIVATE frameassistant_lib Qt6::Test)
add_test(NAME hadevice COMMAND tst_hadevice)

# MosquittoClient against a local mosquitto broker (D30 level 2); skips without one.
qt_add_executable(tst_mqtt tst_mqtt.cpp)
target_link_libraries(tst_mqtt PRIVATE frameassistant_lib Qt6::Test)
# TLS test certificates (D44).
target_compile_definitions(tst_mqtt PRIVATE TEST_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data")
add_test(NAME mqtt COMMAND tst_mqtt)
