#!/bin/sh
set -ex

export GTK_A11Y=NONE

cleanup(){
    kill -9 "$xpid" || :
    rm "$exe" || :
}
trap cleanup 0

exe=$(dirname "$0")/3buttons
#shellcheck disable=SC2046
cc "$exe.c" -o "$exe" $(pkg-config --cflags --libs libpanel-1 gtk4)

#Run program in virtual X framebuffer.
export DISPLAY=:71
Xvfb "$DISPLAY" &
xpid=$!
"$exe" &
pid=$!

#Wait a bit for server to be set up and window, to be mapped
sleep 5
#The panel has merely 3 buttons. Clicking a button destroys that button.
#If 0 buttons exist, the program terminates with 0.
#Therefore, after three <Tab> <Enter>, the program terminates
i=0
nbuttons=3
while [ "$i" -lt "$nbuttons" ]; do
    test -d "/proc/$pid"
    xdotool key Tab Return
    sleep 5
    i=$((i+1))
done
#Confirm the program terminated
! kill -0 "$pid"
