cmake_minimum_required(VERSION 3.10)

# Unit tests for the dde-file-manager-preview application library
# (previewlibrary + previewsingleapplication). Sources are recompiled with
# QT_DEBUG so PreviewLibrary::load() resolves "../libdfm-preview" relative to
# the current directory, which tests point at a sandbox that contains a fake
# libdfm-preview.so exporting the two C entry points.

set(_app_dir "${DFM_SOURCE_DIR}/apps/dde-file-manager-preview/filepreview")

# --- a fake libdfm-preview.so exporting initFilePreview / showFilePreviewDialog
set(_fake_lib_dir "${CMAKE_CURRENT_BINARY_DIR}/fakelibs")
add_library(cov-fake-dfm-preview SHARED test_fake_dfm_preview.cpp)
set_target_properties(cov-fake-dfm-preview PROPERTIES
    LIBRARY_OUTPUT_DIRECTORY "${_fake_lib_dir}")
target_link_libraries(cov-fake-dfm-preview PRIVATE Qt6::Core)

# --- the test binary itself
file(GLOB _ut_files CONFIGURE_DEPENDS
    "${CMAKE_CURRENT_SOURCE_DIR}/test_*.cpp"
    "${CMAKE_CURRENT_SOURCE_DIR}/main.cpp")

find_package(Qt6 COMPONENTS Widgets Network REQUIRED)
find_package(Dtk6 COMPONENTS Widget REQUIRED)

dfm_create_test_executable(ut-filepreview
    SOURCES ${_ut_files}
        "${_app_dir}/previewlibrary.cpp"
        "${_app_dir}/previewsingleapplication.cpp"
    LINK_LIBRARIES DFM6::base DFM6::framework Qt6::Widgets Qt6::Network Dtk6::Widget
        # the fake libdfm-preview.so is also linked directly so the tests can
        # read its counters (QLibrary then re-loads the very same library)
        cov-fake-dfm-preview
)

add_dependencies(ut-filepreview cov-fake-dfm-preview)

target_include_directories(ut-filepreview PRIVATE
    "${_app_dir}"
    # generated by the filepreview app target at configure time (VERSION etc.)
    "${CMAKE_BINARY_DIR}/src/apps/dde-file-manager-preview/filepreview")

target_compile_definitions(ut-filepreview PRIVATE
    QT_DEBUG
    UT_FAKE_DFM_PREVIEW_SO="$<TARGET_FILE:cov-fake-dfm-preview>"
)
