#!/bin/sh
# Load images with the installed qiv on a virtual X server.
# "qiv --root FILE" loads FILE and sets it as the root window background.
# On Xvfb nothing is drawn on the root window, so this only checks that
# images are decoded; a file that is not an image must be rejected.
# qiv 3.0.4 exits 0 even when it cannot load the image, so a failure is
# detected by its "cannot load" message as well as by the exit status.
set -e

# Prints qiv's output; returns 0 if FILE was loaded, 1 if it was not.
load() {
    if out=$(xvfb-run -a -s "-screen 0 1024x768x24" timeout 60 \
            qiv --root "$1" 2>&1); then
        rc=0
    else
        rc=$?
    fi
    printf '%s\n' "$out"
    [ "$rc" -eq 0 ] && ! printf '%s\n' "$out" | grep -q 'cannot load'
}

fail=0
for img in intro.jpg qiv.png; do
    if load "$img"; then
        echo "PASS: $img loaded"
    else
        echo "FAIL: $img not loaded"
        fail=1
    fi
done

printf 'not an image\n' > "$AUTOPKGTEST_TMP/broken.jpg"
if load "$AUTOPKGTEST_TMP/broken.jpg"; then
    echo "FAIL: broken.jpg was accepted"
    fail=1
else
    echo "PASS: broken.jpg rejected"
fi
exit $fail
