#!/bin/sh

set -e
exec 2>&1
set -u

if [ -n "${DEB_HOST_GNU_TYPE:-}" ]; then
    CROSS_COMPILE="$DEB_HOST_GNU_TYPE-"
else
    CROSS_COMPILE=
fi

cd "$AUTOPKGTEST_TMP"

if [ $(dpkg-architecture -q DEB_HOST_ARCH_ENDIAN) != "little" ]; then
    # Mir doesn't handle rendering on big endian systems
    exit 77
fi

# This should be kept in sync with tests/acceptance-tests/wayland/CMakeLists.txt
known_failures="\
    BadBufferTest.test_truncated_shm_file \
    ClientSurfaceEventsTest.frame_timestamp_increases \
    ClientSurfaceEventsTest.surface_gets_enter_event \
    ClientSurfaceEventsTest.surface_gets_leave_event \
    SubsurfaceTest.place_above_simple \
    SubsurfaceTest.place_below_simple \
    TouchInputSubsurfaces/SubsurfaceMultilevelTest.subsurface_moves_after_both_sync_parent_and_grandparent_commit/0 \
    TouchInputSubsurfaces/SubsurfaceMultilevelTest.subsurface_with_desync_parent_moves_when_only_parent_committed/0 \
    TouchInputSubsurfaces/SubsurfaceTest.desync_subsurface_moves_when_only_parent_committed/0 \
    TouchInputSubsurfaces/SubsurfaceTest.one_subsurface_to_another_fallthrough/0 \
    TouchInputSubsurfaces/SubsurfaceTest.place_above_simple/0 \
    TouchInputSubsurfaces/SubsurfaceTest.place_below_simple/0 \
    TouchInputSubsurfaces/SubsurfaceTest.pointer_input_correctly_offset_for_subsurface/0 \
    TouchInputSubsurfaces/SubsurfaceTest.subsurface_does_not_move_when_parent_not_committed/0 \
    TouchInputSubsurfaces/SubsurfaceTest.subsurface_extends_parent_input_region/0 \
    TouchInputSubsurfaces/SubsurfaceTest.subsurface_moves_out_from_under_input_device/0 \
    TouchInputSubsurfaces/SubsurfaceTest.subsurface_moves_under_input_device_once/0 \
    TouchInputSubsurfaces/SubsurfaceTest.subsurface_moves_under_input_device_twice/0 \
    TouchInputSubsurfaces/SubsurfaceTest.sync_subsurface_moves_when_only_parent_committed/0 \
    WlShellSubsurfaces/SubsurfaceMultilevelTest.subsurface_moves_after_both_sync_parent_and_grandparent_commit/0 \
    WlShellSubsurfaces/SubsurfaceMultilevelTest.subsurface_with_desync_parent_moves_when_only_parent_committed/0 \
    WlShellSubsurfaces/SubsurfaceTest.desync_subsurface_moves_when_only_parent_committed/0 \
    WlShellSubsurfaces/SubsurfaceTest.one_subsurface_to_another_fallthrough/0 \
    WlShellSubsurfaces/SubsurfaceTest.place_above_simple/0 \
    WlShellSubsurfaces/SubsurfaceTest.place_below_simple/0 \
    WlShellSubsurfaces/SubsurfaceTest.pointer_input_correctly_offset_for_subsurface/0 \
    WlShellSubsurfaces/SubsurfaceTest.subsurface_does_not_move_when_parent_not_committed/0 \
    WlShellSubsurfaces/SubsurfaceTest.subsurface_extends_parent_input_region/0 \
    WlShellSubsurfaces/SubsurfaceTest.subsurface_moves_under_input_device_twice/0 \
    WlShellSubsurfaces/SubsurfaceTest.sync_subsurface_moves_when_only_parent_committed/0 \
    XdgShellStableSubsurfaces/SubsurfaceMultilevelTest.subsurface_moves_after_both_sync_parent_and_grandparent_commit/0 \
    XdgShellStableSubsurfaces/SubsurfaceMultilevelTest.subsurface_with_desync_parent_moves_when_only_parent_committed/0 \
    XdgShellStableSubsurfaces/SubsurfaceTest.desync_subsurface_moves_when_only_parent_committed/0 \
    XdgShellStableSubsurfaces/SubsurfaceTest.one_subsurface_to_another_fallthrough/0 \
    XdgShellStableSubsurfaces/SubsurfaceTest.place_above_simple/0 \
    XdgShellStableSubsurfaces/SubsurfaceTest.place_below_simple/0 \
    XdgShellStableSubsurfaces/SubsurfaceTest.pointer_input_correctly_offset_for_subsurface/0 \
    XdgShellStableSubsurfaces/SubsurfaceTest.subsurface_does_not_move_when_parent_not_committed/0 \
    XdgShellStableSubsurfaces/SubsurfaceTest.subsurface_extends_parent_input_region/0 \
    XdgShellStableSubsurfaces/SubsurfaceTest.subsurface_moves_under_input_device_twice/0 \
    XdgShellStableSubsurfaces/SubsurfaceTest.sync_subsurface_moves_when_only_parent_committed/0 \
    XdgShellV6Subsurfaces/SubsurfaceMultilevelTest.subsurface_moves_after_both_sync_parent_and_grandparent_commit/0 \
    XdgShellV6Subsurfaces/SubsurfaceMultilevelTest.subsurface_with_desync_parent_moves_when_only_parent_committed/0 \
    XdgShellV6Subsurfaces/SubsurfaceTest.desync_subsurface_moves_when_only_parent_committed/0 \
    XdgShellV6Subsurfaces/SubsurfaceTest.one_subsurface_to_another_fallthrough/0 \
    XdgShellV6Subsurfaces/SubsurfaceTest.place_above_simple/0 \
    XdgShellV6Subsurfaces/SubsurfaceTest.place_below_simple/0 \
    XdgShellV6Subsurfaces/SubsurfaceTest.pointer_input_correctly_offset_for_subsurface/0 \
    XdgShellV6Subsurfaces/SubsurfaceTest.subsurface_does_not_move_when_parent_not_committed/0 \
    XdgShellV6Subsurfaces/SubsurfaceTest.subsurface_extends_parent_input_region/0 \
    XdgShellV6Subsurfaces/SubsurfaceTest.subsurface_moves_under_input_device_twice/0 \
    XdgShellV6Subsurfaces/SubsurfaceTest.sync_subsurface_moves_when_only_parent_committed/0 \
    XdgSurfaceStableTest.attaching_buffer_to_unconfigured_xdg_surface_is_an_error \
    XdgSurfaceStableTest.creating_xdg_surface_from_wl_surface_with_attached_buffer_is_an_error \
    XdgSurfaceStableTest.creating_xdg_surface_from_wl_surface_with_committed_buffer_is_an_error \
    XdgSurfaceStableTest.creating_xdg_surface_from_wl_surface_with_existing_role_is_an_error \
    XdgToplevelStableTest.pointer_leaves_surface_during_interactive_move \
    XdgToplevelStableTest.pointer_leaves_surface_during_interactive_resize \
    XdgToplevelStableTest.touch_can_not_steal_pointer_based_move \
    XdgToplevelV6Test.pointer_leaves_surface_during_interactive_move \
    XdgToplevelV6Test.pointer_leaves_surface_during_interactive_resize"

filter=""
for failure in $known_failures
do
  filter="${filter}:${failure}"
done

$(${CROSS_COMPILE}pkg-config --variable test_runner wlcs) /usr/lib/$(dpkg-architecture -q DEB_HOST_MULTIARCH)/mir/miral_wlcs_integration.so --gtest_filter=-$filter
