cmake_minimum_required(VERSION 3.20)
project(SacredStonesAndroidRenderTests C CXX)
if(NOT GBARECOMP_ROOT)
    message(FATAL_ERROR "Pass the pinned Android GBARECOMP_ROOT.")
endif()
add_subdirectory("${GBARECOMP_ROOT}" engine EXCLUDE_FROM_ALL)
add_executable(fe8_transition_tests ../android_transition_test.cpp)
target_link_libraries(fe8_transition_tests PRIVATE gbarecomp_gba)
target_compile_features(fe8_transition_tests PRIVATE cxx_std_20)
add_executable(android_display_tests ../android_display_test.cpp)
target_include_directories(android_display_tests PRIVATE "${GBARECOMP_ROOT}/src/runtime")
target_compile_features(android_display_tests PRIVATE cxx_std_20)

# Exercise the actual pad geometry and hit testing without opening SDL/audio.
set(pad_source "${GBARECOMP_ROOT}/src/runtime/host_window.cpp")
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${pad_source}")
file(READ "${pad_source}" pad_code)
string(FIND "${pad_code}" "struct PadCircle" pad_start)
string(FIND "${pad_code}" "bool touch_input_enabled" pad_end)
math(EXPR pad_length "${pad_end} - ${pad_start}")
if(pad_start LESS 0 OR pad_length LESS 1)
    message(FATAL_ERROR "Unable to locate upstream pad geometry for tests")
endif()
string(SUBSTRING "${pad_code}" ${pad_start} ${pad_length} pad_code)
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/pad_geometry.inc" "${pad_code}")
add_executable(android_pad_tests ../android_pad_test.cpp)
target_include_directories(android_pad_tests PRIVATE
    "${GBARECOMP_ROOT}/src/runtime" "${CMAKE_CURRENT_BINARY_DIR}")
target_compile_features(android_pad_tests PRIVATE cxx_std_20)

# Compile the real event mapper against a small menu-state test double.
file(READ "${pad_source}" menu_code)
string(FIND "${menu_code}" "bool runtime_ui_event(" menu_start)
if(menu_start LESS 0)
    message(FATAL_ERROR "Unable to locate upstream runtime menu event mapper")
endif()
string(SUBSTRING "${menu_code}" ${menu_start} -1 menu_code)
string(FIND "${menu_code}" "\n}  // namespace" menu_end)
if(menu_end LESS 1)
    message(FATAL_ERROR "Unable to locate end of upstream menu event mapper")
endif()
string(SUBSTRING "${menu_code}" 0 ${menu_end} menu_code)
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/menu_event.inc" "#if 1\n${menu_code}")
add_executable(android_menu_tests ../android_menu_test.cpp)
target_include_directories(android_menu_tests PRIVATE
    "${GBARECOMP_ROOT}/../recomp-ui/src" "${CMAKE_CURRENT_BINARY_DIR}"
    "${SDL2_INCLUDE_DIR}")
target_compile_features(android_menu_tests PRIVATE cxx_std_20)

# Exercise the real Android fit branch, including the distinction between
# cutout insets and control/gesture insets, with a synthetic SDL drawable.
file(READ "${pad_source}" fit_code)
string(FIND "${fit_code}" "PresentationLayout current_presentation_layout(" fit_start)
if(fit_start LESS 0)
    message(FATAL_ERROR "Unable to locate Android presentation mapper")
endif()
string(SUBSTRING "${fit_code}" ${fit_start} -1 fit_code)
string(FIND "${fit_code}" "void publish_touch_presentation(" fit_end)
if(fit_end LESS 1)
    message(FATAL_ERROR "Unable to locate end of Android presentation mapper")
endif()
string(SUBSTRING "${fit_code}" 0 ${fit_end} fit_code)
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/game_fit.inc" "${fit_code}")
add_executable(android_game_fit_tests ../android_game_fit_test.cpp)
target_include_directories(android_game_fit_tests PRIVATE
    "${GBARECOMP_ROOT}/src/runtime" "${CMAKE_CURRENT_BINARY_DIR}" "${SDL2_INCLUDE_DIR}")
target_compile_features(android_game_fit_tests PRIVATE cxx_std_20)
