add_executable(ibus-config-test ibus-config.cpp)
target_link_libraries(ibus-config-test PRIVATE ngosen_ibus)
add_test(NAME ibus_config COMMAND ibus-config-test)
set_tests_properties(ibus_config PROPERTIES LABELS "unit;ibus")

add_executable(ibus-app-modes-test ibus-app-modes.cpp)
target_link_libraries(ibus-app-modes-test PRIVATE ngosen_ibus)
add_test(NAME ibus_app_modes COMMAND ibus-app-modes-test)
set_tests_properties(ibus_app_modes PROPERTIES LABELS "unit;ibus")

add_executable(ibus-host-test ibus-host.cpp)
target_link_libraries(ibus-host-test PRIVATE ngosen_ibus)
add_test(NAME ibus_host COMMAND ibus-host-test)
set_tests_properties(ibus_host PROPERTIES LABELS "unit;ibus")

add_executable(ibus-primary-test ibus-primary.cpp)
target_link_libraries(ibus-primary-test PRIVATE ngosen_ibus)
add_test(NAME ibus_primary COMMAND ibus-primary-test)
# Without the deadline the test hangs on the silent server; the timeout turns that into a failure.
set_tests_properties(ibus_primary PROPERTIES LABELS "unit;ibus" TIMEOUT 10)

# A fake compositor needs libwayland-server; the engine itself only loads libwayland-client at run time.
find_package(PkgConfig REQUIRED)
pkg_check_modules(WaylandServer IMPORTED_TARGET wayland-server)
if (WaylandServer_FOUND)
    add_executable(ibus-wayland-primary-test ibus-wayland-primary.cpp)
    target_link_libraries(ibus-wayland-primary-test PRIVATE ngosen_ibus PkgConfig::WaylandServer)
    add_test(NAME ibus_wayland_primary COMMAND ibus-wayland-primary-test)
    set_tests_properties(ibus_wayland_primary PROPERTIES LABELS "unit;ibus" TIMEOUT 60)
endif()

add_executable(ibus-program-name-test ibus-program-name.cpp)
target_link_libraries(ibus-program-name-test PRIVATE ngosen_ibus)
add_test(NAME ibus_program_name COMMAND ibus-program-name-test)
set_tests_properties(ibus_program_name PROPERTIES LABELS "unit;ibus")

add_executable(ibus-component-test ibus-component.cpp)
target_link_libraries(ibus-component-test PRIVATE ngosen_ibus)
target_compile_definitions(ibus-component-test PRIVATE NGOSEN_COMPONENT_XML_IN="${PROJECT_SOURCE_DIR}/data/ngosen.xml.in")
add_test(NAME ibus_component COMMAND ibus-component-test)
set_tests_properties(ibus_component PROPERTIES LABELS "unit;ibus")

add_test(NAME install_environment COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/install-environment.sh ${PROJECT_BINARY_DIR})
set_tests_properties(install_environment PROPERTIES LABELS "unit")

# The engine runs in its own process under ibus-daemon, so it is tested through a real daemon.
option(NGOSEN_IBUS_DAEMON_TESTS "Test the engine through ibus-daemon" ON)
if (NOT NGOSEN_IBUS_DAEMON_TESTS)
    return()
endif()

find_program(IBUS_DAEMON ibus-daemon)
find_program(DBUS_RUN_SESSION dbus-run-session)
find_program(XVFB Xvfb)
find_package(Python3 COMPONENTS Interpreter)
if (IBUS_DAEMON AND DBUS_RUN_SESSION AND XVFB AND Python3_FOUND)
    execute_process(COMMAND ${Python3_EXECUTABLE} -c "import ctypes, gi; ctypes.CDLL('libXtst.so.6'); gi.require_version('IBus', '1.0'); from gi.repository import IBus"
                    RESULT_VARIABLE NGOSEN_HAVE_PYTHON_IBUS OUTPUT_QUIET ERROR_QUIET)
endif()
if (NOT (IBUS_DAEMON AND DBUS_RUN_SESSION AND XVFB AND NGOSEN_HAVE_PYTHON_IBUS EQUAL 0))
    message(FATAL_ERROR "The engine tests need ibus-daemon, dbus-run-session, Xvfb, libXtst and python3-gi with IBus; "
                        "install them or pass -DNGOSEN_IBUS_DAEMON_TESTS=OFF")
endif()

add_executable(ibus-announce-test ibus-announce.cpp)
target_link_libraries(ibus-announce-test PRIVATE ngosen_ibus)
add_test(NAME ibus_announce COMMAND ${DBUS_RUN_SESSION} -- $<TARGET_FILE:ibus-announce-test>)
set_tests_properties(ibus_announce PROPERTIES LABELS "unit;ibus" TIMEOUT 30)

set(NGOSEN_IBUS_EXEC $<TARGET_FILE:ibus-engine-ngosen>)
set(NGOSEN_IBUS_ICON "")
set(component_dir ${CMAKE_CURRENT_BINARY_DIR}/component)
configure_file(${PROJECT_SOURCE_DIR}/data/ngosen.xml.in ${CMAKE_CURRENT_BINARY_DIR}/ngosen.xml.in @ONLY)
file(GENERATE OUTPUT ${component_dir}/ngosen.xml INPUT ${CMAKE_CURRENT_BINARY_DIR}/ngosen.xml.in)

add_test(NAME ibus_daemon_typing
         COMMAND ${DBUS_RUN_SESSION} -- sh ${CMAKE_CURRENT_SOURCE_DIR}/run-with-daemon.sh ${Python3_EXECUTABLE} ${component_dir}
                 ${PROJECT_SOURCE_DIR}/ngosen/data/dictionaries/vietnamese.cm.dict ${CMAKE_CURRENT_SOURCE_DIR}/ibus-daemon-typing.py)
set_tests_properties(ibus_daemon_typing PROPERTIES LABELS "integration;ibus" TIMEOUT 600
                     ENVIRONMENT "NGOSEN_IBUS_LOCALEDIR=${PROJECT_BINARY_DIR}/po/locale;NGOSEN_IBUS_ENGLISH_WORDS=${PROJECT_SOURCE_DIR}/ngosen/data/english-common.txt;NGOSEN_IBUS_KEEP_WORDS=${PROJECT_SOURCE_DIR}/ngosen/data/keep-words.txt;LC_ALL=en_US.UTF-8")

add_test(NAME ibus_daemon_typing_no_clicks
         COMMAND ${DBUS_RUN_SESSION} -- sh ${CMAKE_CURRENT_SOURCE_DIR}/run-with-daemon.sh ${Python3_EXECUTABLE} ${component_dir}
                 ${PROJECT_SOURCE_DIR}/ngosen/data/dictionaries/vietnamese.cm.dict ${CMAKE_CURRENT_SOURCE_DIR}/ibus-daemon-typing.py)
set_tests_properties(ibus_daemon_typing_no_clicks PROPERTIES LABELS "integration;ibus" TIMEOUT 120
                     ENVIRONMENT "NGOSEN_IBUS_LOCALEDIR=${PROJECT_BINARY_DIR}/po/locale;NGOSEN_IBUS_ENGLISH_WORDS=${PROJECT_SOURCE_DIR}/ngosen/data/english-common.txt;NGOSEN_IBUS_KEEP_WORDS=${PROJECT_SOURCE_DIR}/ngosen/data/keep-words.txt;LC_ALL=en_US.UTF-8;NGOSEN_TEST_NO_X=1")
