#!/bin/bash # End to end test of multi-monitor capture and atlas packing. # # Runs a virtual X server with three RandR monitors defined over one screen, # paints a distinct pattern into each monitor's region, then has the host # capture it and checks that every display's pixels arrive at the right place # in the packed atlas. # # Usage: test-multimon.sh set -u HOST_BIN="${1:-host/build/looking-glass-host}" VERIFY_BIN="${2:-lg-verify-atlas}" WORK="${3:-/tmp/lg-multimon-test}" DISP=:99 SHM=/dev/shm/looking-glass-test SCREEN_W=1920 SCREEN_H=1080 MON_W=640 # each monitor gets its own primary colour so a misplaced copy is obvious COLOURS=(red green blue) NAMES=(TEST-1 TEST-2 TEST-3) cleanup() { [ -n "${HOST_PID:-}" ] && kill "$HOST_PID" 2>/dev/null [ -n "${XVFB_PID:-}" ] && kill "$XVFB_PID" 2>/dev/null rm -f "$SHM" } trap cleanup EXIT mkdir -p "$WORK" rm -rf "${WORK:?}"/* echo "== starting virtual X server ==" Xvfb $DISP -screen 0 ${SCREEN_W}x${SCREEN_H}x24 +extension RANDR > "$WORK/xvfb.log" 2>&1 & XVFB_PID=$! sleep 2 if ! DISPLAY=$DISP xrandr --query > /dev/null 2>&1; then echo "FAIL: virtual X server did not start" cat "$WORK/xvfb.log" exit 1 fi echo "== defining 3 monitors ==" for i in 0 1 2; do X=$((i * MON_W)) DISPLAY=$DISP xrandr --setmonitor "${NAMES[$i]}" \ ${MON_W}/170x${SCREEN_H}/280+${X}+0 none || { echo "FAIL: could not define monitor ${NAMES[$i]}"; exit 1; } done DISPLAY=$DISP xrandr --listmonitors echo "== painting a distinct pattern per monitor ==" # a gradient gives within-region variation, so a region that is left untouched # (flat colour) is distinguishable from one that was really copied ARGS=() for i in 0 1 2; do convert -size ${MON_W}x${SCREEN_H} \ gradient:"${COLOURS[$i]}"-black "$WORK/mon$i.png" ARGS+=("$WORK/mon$i.png") done convert "${ARGS[@]}" +append "$WORK/desktop.png" DISPLAY=$DISP display -window root "$WORK/desktop.png" 2>/dev/null & sleep 2 echo "== starting host ==" rm -f "$SHM" truncate -s 128M "$SHM" DISPLAY=$DISP "$HOST_BIN" app:capture=XCB app:shmFile="$SHM" \ > "$WORK/host.log" 2>&1 & HOST_PID=$! sleep 3 if ! kill -0 "$HOST_PID" 2>/dev/null; then echo "FAIL: host exited" tail -20 "$WORK/host.log" exit 1 fi grep -E "Multi-monitor|TEST-" "$WORK/host.log" || true echo echo "== verifying the packed atlas ==" "$VERIFY_BIN" "$SHM" "$WORK" VERIFY_RC=$? echo echo "== checking each display carries its own colour ==" RC=0 for i in 0 1 2; do PPM="$WORK/display-$i-${NAMES[$i]}.ppm" if [ ! -f "$PPM" ]; then echo " [FAIL] ${NAMES[$i]}: no image written" RC=1 continue fi # the dominant channel must be the colour painted into that monitor read -r R G B <<< "$(convert "$PPM" -resize 1x1! -format "%[fx:r] %[fx:g] %[fx:b]" info:)" case $i in 0) OK=$(awk -v r="$R" -v g="$G" -v b="$B" \ 'BEGIN{print (r>g && r>b) ? 1 : 0}') ; WANT=red ;; 1) OK=$(awk -v r="$R" -v g="$G" -v b="$B" \ 'BEGIN{print (g>r && g>b) ? 1 : 0}') ; WANT=green ;; 2) OK=$(awk -v r="$R" -v g="$G" -v b="$B" \ 'BEGIN{print (b>r && b>g) ? 1 : 0}') ; WANT=blue ;; esac if [ "$OK" = "1" ]; then echo " [PASS] ${NAMES[$i]} is $WANT dominant (r=$R g=$G b=$B)" else echo " [FAIL] ${NAMES[$i]} should be $WANT dominant (r=$R g=$G b=$B)" RC=1 fi done echo if [ $VERIFY_RC -eq 0 ] && [ $RC -eq 0 ]; then echo "MULTI-MONITOR TEST PASSED" exit 0 fi echo "MULTI-MONITOR TEST FAILED" exit 1