guest_test.sh 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. #!/bin/bash
  2. # guest_test.sh - run inside a VM's desktop session by release_check.sh (or by hand): starts each test wallpaper with the
  3. # engine under test, takes a screenshot, checks the engine survived and stopped cleanly. One "PASS|FAIL <check>" line per
  4. # check, screenshots in $OUT. Needs the session environment (sessenv) sourced first.
  5. # LWE_DIR folder with lwe-hints/ (the engine), assets/ and wp-<workshop id>/ folders
  6. # LWE_ARGS how the engine is told where to draw, e.g. "--screen-root Virtual-1" or "--window 0x0x1920x1080"
  7. # SHOT_CMD screenshot command with $SHOT_FILE as the file, e.g. 'gnome-screenshot -f $SHOT_FILE'; the value
  8. # host asks the host instead (COSMIC's portal asks for permission on every call): the script creates
  9. # $SHOT_FILE.want and waits for $SHOT_FILE, host_shots.sh takes the picture through the VM's display
  10. # WALLPAPERS ids to run, default: scene with parallax, scene with particles, two web items
  11. # SETTLE seconds to wait for the first frames (default 25), the checks that do not compare frames at a fixed time
  12. # go on as soon as the engine says it draws (a few seconds more, web pages need longer)
  13. # ONLY space separated check groups to run (wallpapers behavior video cycle automute automuteignore transition multi windowbg), default all except automuteignore (AUTOMUTE_IGNORE=1)
  14. # MINIMIZE_CMD minimizes the test window (X11): the engine paused by a fullscreen window must resume once it is hidden
  15. # MULTI_SHOT_CMD screenshot command that captures every monitor (the VM was started with a second output): turns on
  16. # the multi monitor check, the wallpaper must show on both screens
  17. # resolved: the engine's command line shows the real path (/var/home on Fedora Atomic systems)
  18. LWE_DIR=$(realpath "${LWE_DIR:-$HOME/lwe}")
  19. ENGINE=$LWE_DIR/lwe-hints/linux-wallpaperengine
  20. export LWE_WEB_STATS=1
  21. OUT=${OUT:-/tmp/lwe-test}
  22. WALLPAPERS=${WALLPAPERS-"1111948150 1120596121 1078208425 834237690"}
  23. SETTLE=${SETTLE:-25}
  24. mkdir -p "$OUT"
  25. rm -f "$OUT/done"
  26. status=0
  27. want() { [ -z "$ONLY" ] || [[ " $ONLY " == *" $1 "* ]]; }
  28. settle() {
  29. local log=$1 id=$2 extra=4 waited=0 web=0
  30. if grep -qi '"type"[[:space:]]*:[[:space:]]*"web"' "$LWE_DIR/wp-$id/project.json" 2>/dev/null; then web=1; fi
  31. while [ "$waited" -lt "$SETTLE" ] && ! grep -qE "drawing at scale|First frame drawn" "$log" 2>/dev/null; do sleep 1; waited=$((waited + 1)); done
  32. if [ $web = 1 ]; then
  33. # LWE_WEB_STATS says when the page painted something; a page that never does is given the old fixed wait
  34. while [ "$waited" -lt "$SETTLE" ] && ! grep -q "first page content after" "$log" 2>/dev/null; do sleep 1; waited=$((waited + 1)); done
  35. extra=3
  36. fi
  37. [ $((SETTLE - waited)) -lt "$extra" ] && extra=$((SETTLE - waited))
  38. [ "$extra" -gt 0 ] && sleep "$extra"
  39. return 0
  40. }
  41. ready() {
  42. local waited=0
  43. while [ "$waited" -lt "$SETTLE" ] && ! grep -qE "drawing at scale|First frame drawn" "$1" 2>/dev/null; do sleep 1; waited=$((waited + 1)); done
  44. sleep 2
  45. }
  46. timed() { local group=$1 t0=$SECONDS; shift; "$@"; echo "TIME $group $((SECONDS - t0))"; }
  47. pass() { echo "PASS $1"; }
  48. fail() { echo "FAIL $1"; status=1; }
  49. engine_pids() { pgrep -f "^$LWE_DIR/lwe-hints/linux-wallpaperengine.[b]in"; }
  50. take_shot() {
  51. if [ "$SHOT_CMD" = host ]; then
  52. touch "$1.want"
  53. for _ in $(seq 120); do [ -s "$1" ] && break; sleep 1; done
  54. else
  55. eval "$SHOT_CMD" > "$2" 2>&1
  56. fi
  57. }
  58. stop_engine() {
  59. for p in $(engine_pids); do kill "$p"; done
  60. for _ in $(seq 20); do engine_pids >/dev/null || return 0; sleep 0.5; done
  61. for p in $(engine_pids); do kill -9 "$p"; done
  62. return 1
  63. }
  64. stop_engine
  65. total=0 bad_start=0
  66. if want wallpapers; then
  67. t_wallpapers=$SECONDS
  68. sleep 2
  69. SHOT_FILE=$OUT/baseline.png
  70. rm -f "$SHOT_FILE"
  71. take_shot "$SHOT_FILE" "$OUT/baseline.log"
  72. if [ -s "$SHOT_FILE" ]; then pass "baseline screenshot taken"; else fail "no baseline screenshot"; fi
  73. for id in $WALLPAPERS; do
  74. dir=$LWE_DIR/wp-$id
  75. [ -d "$dir" ] || { fail "$id: wallpaper folder missing"; continue; }
  76. log=$OUT/engine-$id.log
  77. # shellcheck disable=SC2086
  78. (setsid nohup "$ENGINE" --assets-dir "$LWE_DIR/assets" $LWE_ARGS "$dir" > "$log" 2>&1 < /dev/null &)
  79. settle "$log" "$id"
  80. total=$((total + 1))
  81. if engine_pids >/dev/null; then pass "$id: engine running"; else fail "$id: engine exited"; bad_start=$((bad_start + 1)); fi
  82. if grep -qiE "segmentation|SIGSEGV|terminate called|Aborted|core dumped" "$log"; then fail "$id: crash text in the log"; else pass "$id: no crash text in the log"; fi
  83. if grep -qi '"type"[[:space:]]*:[[:space:]]*"web"' "$dir/project.json" 2>/dev/null; then
  84. # --web-frames auto picks dmabuf frames on a Wayland session with EGL; a fallback means it did not work there
  85. if grep -q "restarting the web host with shared memory frames" "$log"; then fail "$id: dmabuf web frames fell back to shared memory"
  86. elif grep -q "frames arrive as dmabufs" "$log"; then pass "$id: web frames arrive as dmabufs"
  87. else pass "$id: web frames through shared memory"; fi
  88. fi
  89. SHOT_FILE=$OUT/shot-$id.png
  90. rm -f "$SHOT_FILE"
  91. take_shot "$SHOT_FILE" "$OUT/shot-$id.log"
  92. if [ -s "$SHOT_FILE" ]; then pass "$id: screenshot taken"; else fail "$id: no screenshot"; fi
  93. if stop_engine; then pass "$id: engine stopped on SIGTERM"; else fail "$id: engine needed SIGKILL"; fi
  94. if pgrep -f "[l]inux-wallpaperengine-web-helper" >/dev/null; then fail "$id: web helper left behind"; else pass "$id: no web helper left behind"; fi
  95. done
  96. if [ "$total" -gt 0 ] && [ "$bad_start" -eq "$total" ]; then
  97. echo "FATAL: the engine did not stay running on any of the $total wallpapers, the other checks are skipped"
  98. touch "$OUT/done"; exit 1
  99. fi
  100. echo "TIME wallpapers $((SECONDS - t_wallpapers))"
  101. fi
  102. # the test window's own command line is the pattern: toolkits start helpers and detach, the shell's pid is not enough
  103. stop_fullscreen_window() {
  104. [ -n "$STOP_CMD" ] && pkill -f -- "^${STOP_CMD%% *}.*${STOP_CMD#* }" 2>/dev/null
  105. pkill -f "lwe-fullscreen-test|fullscreen_window.py|maximize_kde.sh|konsole -e sleep" 2>/dev/null
  106. return 0
  107. }
  108. # wait_for_log <file> <text> <seconds> <lines of it seen before>: true once more lines than that contain the text
  109. wait_for_log() {
  110. for _ in $(seq "$3"); do
  111. [ "$(grep -c "$2" "$1")" -gt "$4" ] && return 0
  112. sleep 1
  113. done
  114. return 1
  115. }
  116. # pause_check <label> <command>: a window opened by the command must pause the engine (a "Paused: windows" log line) and
  117. # closing it must resume it. An empty command is skipped.
  118. pause_check() {
  119. local label=$1 cmd=$2 log=$OUT/engine-behavior.log
  120. if [ -z "$cmd" ]; then echo "SKIP $label pause: no command for this desktop"; return; fi
  121. STOP_CMD=$cmd
  122. stop_fullscreen_window
  123. local paused_before resumed_before fs_pid
  124. paused_before=$(grep -c "Paused: windows" "$log"); resumed_before=$(grep -c "Resumed" "$log")
  125. eval "$cmd" > "$OUT/$label-window.log" 2>&1 &
  126. fs_pid=$!
  127. sleep 2
  128. if [ "$SHOT_CMD" = host ]; then
  129. touch "$OUT/focus.click"
  130. for _ in $(seq 10); do [ -e "$OUT/focus.click" ] || break; sleep 1; done
  131. fi
  132. if ! kill -0 "$fs_pid" 2>/dev/null; then
  133. fail "$label pause: the test window did not start ($(head -c 150 "$OUT/$label-window.log" | tr '\n' ' '))"
  134. elif grep -q "Fullscreen detection not supported" "$log"; then
  135. echo "SKIP $label pause: the compositor has no window state detection"
  136. elif wait_for_log "$log" "Paused: windows" 45 "$paused_before"; then
  137. pass "$label pause: engine paused"
  138. local minimized_resumed=0
  139. if [ "$label" = fullscreen ] && [ -n "$MINIMIZE_CMD" ]; then
  140. if command -v xdotool >/dev/null; then
  141. eval "$MINIMIZE_CMD" > "$OUT/minimize.log" 2>&1
  142. if wait_for_log "$log" "Resumed" 20 "$resumed_before"; then pass "$label pause: engine resumed once the window was minimized"; minimized_resumed=1
  143. else fail "$label pause: still paused with the window minimized"; fi
  144. resumed_before=$(grep -c "Resumed" "$log")
  145. else
  146. echo "SKIP $label pause: no xdotool to minimize the window"
  147. fi
  148. fi
  149. kill "$fs_pid" 2>/dev/null; stop_fullscreen_window
  150. if [ $minimized_resumed = 1 ]; then :
  151. elif wait_for_log "$log" "Resumed" 45 "$resumed_before"; then pass "$label pause: engine resumed"; else fail "$label pause: no resume after the window closed"; fi
  152. else
  153. fail "$label pause: no pause within 45s of a $label window"
  154. {
  155. echo "--- state at the failed $label check, $(date +%T)"
  156. echo "window processes:"; pgrep -fa "konsole|fullscreen_window|maximize_kde" | cut -c1-120
  157. echo "engine bus names:"; busctl --user list 2>/dev/null | grep -i linuxwallpaper
  158. echo "kwin scripts left in the runtime dir:"; ls "$XDG_RUNTIME_DIR" 2>/dev/null | grep -E "^lwe-"
  159. echo "engine log tail:"; grep -v '^ALSA' "$log" | tail -5 | cut -c1-160
  160. if command -v gdbus >/dev/null && gdbus introspect --session --dest org.kde.KWin --object-path /Scripting >/dev/null 2>&1; then
  161. echo "KWin's window list:"
  162. diag=$(mktemp /tmp/lwe-diag-XXXXXX.js)
  163. echo 'for (const w of workspace.windowList()) print("LWEDIAG", w.resourceClass, w.caption, "fs=" + w.fullScreen, "max=" + (w.maximizeMode), "min=" + w.minimized, "out=" + (w.output ? w.output.name : "none"), "desktops=" + w.desktops.length, "normal=" + w.normalWindow);' > "$diag"
  164. id=$(gdbus call --session --dest org.kde.KWin --object-path /Scripting --method org.kde.kwin.Scripting.loadScript "$diag" lwe-diag | tr -dc 0-9)
  165. gdbus call --session --dest org.kde.KWin --object-path "/Scripting/Script$id" --method org.kde.kwin.Script.run >/dev/null 2>&1
  166. sleep 1
  167. journalctl --user -b --no-pager 2>/dev/null | grep LWEDIAG | tail -12 | cut -c1-220
  168. gdbus call --session --dest org.kde.KWin --object-path /Scripting --method org.kde.kwin.Scripting.unloadScript lwe-diag >/dev/null 2>&1
  169. rm -f "$diag"
  170. fi
  171. } >> "$OUT/pause-diagnostics.txt" 2>&1
  172. fi
  173. kill "$fs_pid" 2>/dev/null; stop_fullscreen_window
  174. sleep 3
  175. }
  176. hotswap_video_check() {
  177. local A=3471294034 B=3818199837 ctl=$OUT/control-video pid
  178. if [ ! -d "$LWE_DIR/wp-$A" ] || [ ! -d "$LWE_DIR/wp-$B" ]; then echo "SKIP hotswap video: wallpapers $A and $B missing"; return; fi
  179. : > "$ctl"
  180. # shellcheck disable=SC2086
  181. (setsid nohup "$ENGINE" --assets-dir "$LWE_DIR/assets" $LWE_ARGS --control-file "$ctl" "$LWE_DIR/wp-$A" > "$OUT/engine-swapvideo.log" 2>&1 < /dev/null &)
  182. ready "$OUT/engine-swapvideo.log"
  183. pid=$(engine_pids | head -1)
  184. if [ -z "$pid" ]; then fail "hotswap video: engine did not start"; return; fi
  185. echo "path=$LWE_DIR/wp-$B" > "$ctl"
  186. kill -USR1 "$pid"
  187. sleep 20
  188. SHOT_FILE=$OUT/swap-video.png; rm -f "$SHOT_FILE"; take_shot "$SHOT_FILE" "$OUT/swap-video.log"
  189. stop_engine
  190. # shellcheck disable=SC2086
  191. (setsid nohup "$ENGINE" --assets-dir "$LWE_DIR/assets" $LWE_ARGS "$LWE_DIR/wp-$B" > "$OUT/engine-freshvideo.log" 2>&1 < /dev/null &)
  192. ready "$OUT/engine-freshvideo.log"
  193. SHOT_FILE=$OUT/fresh-video.png; rm -f "$SHOT_FILE"; take_shot "$SHOT_FILE" "$OUT/fresh-video.log"
  194. stop_engine
  195. }
  196. peak_in() { awk -v a="$1" -v b="$2" '$1 >= a && $1 < b && $2 > m { m = $2 } END { print m + 0 }' "$OUT/audio-timeline.txt"; }
  197. automute_helpers() {
  198. cat > "$OUT/peak.py" <<'PY'
  199. import struct, subprocess, sys, time
  200. p = subprocess.Popen(["parec", "-d", "@DEFAULT_SINK@.monitor", "--format=s16le", "--rate=8000", "--channels=1",
  201. "--latency-msec=50"], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
  202. t0 = time.time()
  203. while time.time() - t0 < float(sys.argv[1]):
  204. d = p.stdout.read(800)
  205. if not d:
  206. break
  207. s = struct.unpack("<%dh" % (len(d) // 2), d)
  208. print("%.1f %d" % (time.time() - t0, max(abs(x) for x in s)), flush=True)
  209. p.kill()
  210. PY
  211. python3 -c "
  212. import wave
  213. w = wave.open('$OUT/silence.wav', 'w'); w.setnchannels(2); w.setsampwidth(2); w.setframerate(44100)
  214. w.writeframes(b'\0' * 44100 * 4 * 12); w.close()"
  215. }
  216. automute_check() {
  217. local id=3471294034 win_pid="" base muted back pmuted pback
  218. if [ ! -d "$LWE_DIR/wp-$id" ]; then echo "SKIP automute: wallpaper $id missing"; return; fi
  219. if ! command -v parec >/dev/null || ! command -v paplay >/dev/null || ! pactl info >/dev/null 2>&1; then
  220. echo "SKIP automute: no pulse tools or server in this guest"; return
  221. fi
  222. automute_helpers
  223. # shellcheck disable=SC2086
  224. (setsid nohup "$ENGINE" --assets-dir "$LWE_DIR/assets" $LWE_ARGS --automute "$LWE_DIR/wp-$id" > "$OUT/engine-automute.log" 2>&1 < /dev/null &)
  225. ready "$OUT/engine-automute.log"
  226. python3 "$OUT/peak.py" 24 > "$OUT/audio-timeline.txt" 2>/dev/null &
  227. local mon=$!
  228. sleep 5
  229. paplay "$OUT/silence.wav" &
  230. wait "$mon" 2>/dev/null
  231. pkill -x paplay 2>/dev/null
  232. if [ -n "$FULLSCREEN_CMD" ]; then
  233. # the second player while a fullscreen window has the wallpaper paused: only the engine's log shows it, so no
  234. # fixed waits, each step goes on as soon as the log says the last one happened
  235. local log=$OUT/engine-automute.log stops
  236. stops=$(grep -c "Automute: other apps stopped" "$log")
  237. STOP_CMD=$FULLSCREEN_CMD; stop_fullscreen_window
  238. eval "$FULLSCREEN_CMD" > "$OUT/automute-window.log" 2>&1 &
  239. win_pid=$!
  240. wait_for_log "$log" "Paused: windows" 15 0
  241. python3 -c "
  242. import wave
  243. w = wave.open('$OUT/silence-short.wav', 'w'); w.setnchannels(2); w.setsampwidth(2); w.setframerate(44100)
  244. w.writeframes(b'\\0' * 44100 * 4 * 5); w.close()"
  245. paplay "$OUT/silence-short.wav" &
  246. sleep 3
  247. kill "$win_pid" 2>/dev/null; stop_fullscreen_window
  248. wait_for_log "$log" "Automute: other apps stopped" 12 "$stops"
  249. fi
  250. stop_engine
  251. pkill -x paplay 2>/dev/null
  252. base=$(peak_in 1 5)
  253. if [ "$base" -lt 50 ]; then echo "SKIP automute: the wallpaper's music is not audible on this guest's default sink (peak $base)"; return; fi
  254. muted=$(peak_in 8 16); back=$(peak_in 18 22)
  255. if [ "$muted" -le 30 ]; then pass "automute: muted while another app plays (peak $muted, $base before)"; else fail "automute: not muted while another app plays (peak $muted)"; fi
  256. if [ "$back" -gt $((base / 4)) ]; then pass "automute: music back within 5s of the other app stopping (peak $back)"; else fail "automute: music not back 1.5-5s after the other app stopped (peak $back)"; fi
  257. if [ -n "$FULLSCREEN_CMD" ]; then
  258. # the scene's own music is silent while the wallpaper is paused, so what is checked is the engine's state: the log
  259. # lines of the second player come while the "Paused: windows" state holds
  260. local log=$OUT/engine-automute.log paused_line started stopped first_stop resumed
  261. paused_line=$(grep -n "Paused: windows" "$log" | head -1 | cut -d: -f1)
  262. first_stop=$(grep -n "Automute: other apps stopped" "$log" | head -1 | cut -d: -f1)
  263. resumed=$(grep -n "^Resumed" "$log" | head -1 | cut -d: -f1)
  264. started=$(grep -n "Automute: another app is playing" "$log" | tail -1 | cut -d: -f1)
  265. stopped=$(grep -n "Automute: other apps stopped" "$log" | tail -1 | cut -d: -f1)
  266. # the sound state flips between the first stop and the resume, whichever of that line and "Paused" the engine logs first
  267. if [ -z "$paused_line" ]; then echo "SKIP automute while paused: the engine was not paused by the window"
  268. elif [ "$(grep -c "Automute: another app is playing" "$log")" -ge 2 ] && [ "$started" -gt "${first_stop:-0}" ] && [ "$started" -lt "${resumed:-999999}" ]; then pass "automute: another app's sound noticed while the wallpaper is paused"
  269. else fail "automute: another app's sound not noticed while the wallpaper is paused"; fi
  270. if [ -n "$paused_line" ] && [ -n "$stopped" ] && [ "$stopped" -gt "$started" ]; then pass "automute: the other app stopping noticed while the wallpaper is paused"
  271. else fail "automute: the other app stopping not noticed while the wallpaper is paused"; fi
  272. fi
  273. }
  274. automute_ignore_check() {
  275. local id=3471294034 base during
  276. if [ ! -d "$LWE_DIR/wp-$id" ]; then echo "SKIP automute ignore: wallpaper $id missing"; return; fi
  277. if ! command -v parec >/dev/null || ! command -v paplay >/dev/null || ! pactl info >/dev/null 2>&1; then
  278. echo "SKIP automute ignore: no pulse tools or server in this guest"; return
  279. fi
  280. automute_helpers
  281. # shellcheck disable=SC2086
  282. (setsid nohup "$ENGINE" --assets-dir "$LWE_DIR/assets" $LWE_ARGS "$LWE_DIR/wp-$id" > "$OUT/engine-automute-ignore.log" 2>&1 < /dev/null &)
  283. ready "$OUT/engine-automute-ignore.log"
  284. python3 "$OUT/peak.py" 24 > "$OUT/audio-timeline.txt" 2>/dev/null &
  285. local mon=$!
  286. sleep 5
  287. paplay "$OUT/silence.wav" &
  288. wait "$mon" 2>/dev/null
  289. stop_engine
  290. pkill -x paplay 2>/dev/null
  291. base=$(peak_in 1 5)
  292. if [ "$base" -lt 50 ]; then echo "SKIP automute ignore: the wallpaper's music is not audible on this guest's default sink (peak $base)"; return; fi
  293. during=$(peak_in 8 16)
  294. if [ "$during" -gt $((base / 4)) ]; then pass "automute ignore: without --automute the music keeps playing while another app plays (peak $during, $base before)"
  295. else fail "automute ignore: the music went quiet without --automute (peak $during)"; fi
  296. if grep -q "Automute:" "$OUT/engine-automute-ignore.log"; then fail "automute ignore: the engine watched other apps' sound without --automute"
  297. else pass "automute ignore: no automute detector without the flag"; fi
  298. }
  299. transition_check() {
  300. local A=1120596121 B=1111948150 ctl=$OUT/control-transition pid
  301. if [ ! -d "$LWE_DIR/wp-$A" ] || [ ! -d "$LWE_DIR/wp-$B" ]; then echo "SKIP transition: wallpapers $A and $B missing"; return; fi
  302. rm -f "$OUT"/tr-half*.ppm
  303. : > "$ctl"
  304. # shellcheck disable=SC2086
  305. (LWE_TRANSITION_DUMP="$OUT/tr-half.ppm" setsid nohup "$ENGINE" --assets-dir "$LWE_DIR/assets" $LWE_ARGS --control-file "$ctl" \
  306. --transition fade --transition-duration 3000 "$LWE_DIR/wp-$A" > "$OUT/engine-transition.log" 2>&1 < /dev/null &)
  307. ready "$OUT/engine-transition.log"
  308. pid=$(engine_pids | head -1)
  309. if [ -z "$pid" ]; then fail "transition: engine did not start"; return; fi
  310. echo "path=$LWE_DIR/wp-$B" > "$ctl"
  311. kill -USR1 "$pid"
  312. for _ in $(seq 30); do [ -s "$OUT/tr-half.ppm" ] && break; sleep 1; done
  313. sleep 3
  314. if engine_pids >/dev/null; then pass "transition: the engine survived a fade"; else fail "transition: the engine died during a fade"; fi
  315. stop_engine
  316. if [ -s "$OUT/tr-half.ppm" ]; then pass "transition: the output was drawn halfway through the fade"; else fail "transition: nothing was drawn halfway through the fade"; fi
  317. }
  318. hotswap_cycle_check() {
  319. local order="1120596121 1367456719 1078208425 1111948150 834237690 1367456719 1120596121"
  320. local ctl=$OUT/control-cycle log=$OUT/engine-cycle.log id pid n=0 swaps type helpers bad bad_before wait_s
  321. for id in $order; do
  322. if [ ! -d "$LWE_DIR/wp-$id" ]; then echo "SKIP hotswap cycle: wallpaper $id missing"; return; fi
  323. done
  324. : > "$ctl"
  325. # shellcheck disable=SC2086
  326. (setsid nohup "$ENGINE" --assets-dir "$LWE_DIR/assets" $LWE_ARGS --control-file "$ctl" "$LWE_DIR/wp-${order%% *}" > "$log" 2>&1 < /dev/null &)
  327. settle "$log" "${order%% *}"
  328. pid=$(engine_pids | head -1)
  329. if [ -z "$pid" ]; then fail "hotswap cycle: engine did not start"; return; fi
  330. for id in ${order#* }; do
  331. n=$((n + 1))
  332. [ -n "${CYCLE_UPTO:-}" ] && [ "$n" -gt "$CYCLE_UPTO" ] && break
  333. if grep -qi '"type"[[:space:]]*:[[:space:]]*"web"' "$LWE_DIR/wp-$id/project.json"; then type=web
  334. elif grep -qi '"type"[[:space:]]*:[[:space:]]*"video"' "$LWE_DIR/wp-$id/project.json"; then type=video
  335. else type=scene; fi
  336. contents=$(grep -c "first page content after" "$log")
  337. swaps=$(grep -c "Hotswapping wallpaper to" "$log")
  338. bad_before=$(grep -ciE "terminate called|segmentation|fatal|exception" "$log")
  339. echo "path=$LWE_DIR/wp-$id" > "$ctl"
  340. kill -USR1 "$pid"
  341. for _ in $(seq 40); do [ "$(grep -c "Hotswapping wallpaper to" "$log")" -gt "$swaps" ] && break; sleep 0.5; done
  342. if [ "$type" = web ]; then
  343. wait_for_log "$log" "first page content after" 20 "$contents"
  344. sleep 3
  345. else
  346. sleep 5
  347. fi
  348. if ! kill -0 "$pid" 2>/dev/null; then fail "hotswap cycle step $n ($type $id): the engine exited"; return; fi
  349. if [ "$(grep -c "Hotswapping wallpaper to" "$log")" -le "$swaps" ]; then fail "hotswap cycle step $n ($type $id): no hotswap in the log"; stop_engine; return; fi
  350. helpers=$(pgrep -fc "[l]inux-wallpaperengine-web-helper")
  351. if [ "$type" = web ] && [ "$helpers" -eq 0 ]; then fail "hotswap cycle step $n ($type $id): no web host is running"; stop_engine; return; fi
  352. if [ "$type" != web ] && [ "$helpers" -gt 0 ]; then
  353. for _ in $(seq 16); do sleep 0.5; helpers=$(pgrep -fc "[l]inux-wallpaperengine-web-helper"); [ "$helpers" -eq 0 ] && break; done
  354. if [ "$helpers" -gt 0 ]; then fail "hotswap cycle step $n ($type $id): $helpers web host processes left behind"; stop_engine; return; fi
  355. fi
  356. bad=$(grep -ciE "terminate called|segmentation|fatal|exception" "$log")
  357. if [ "$bad" -gt "$bad_before" ]; then fail "hotswap cycle step $n ($type $id): $((bad - bad_before)) new error lines in the log"; stop_engine; return; fi
  358. SHOT_FILE=$OUT/shot-cycle-$n-$id.png; rm -f "$SHOT_FILE"; take_shot "$SHOT_FILE" "$OUT/cycle-$n.log"
  359. pass "hotswap cycle step $n: $type $id"
  360. done
  361. stop_engine
  362. }
  363. window_background_check() {
  364. local id=1120596121 log=$OUT/engine-windowbg.log geometry
  365. [ "${XDG_SESSION_TYPE:-}" = x11 ] && command -v xrandr >/dev/null || return 0
  366. [ -d "$LWE_DIR/wp-$id" ] || { echo "SKIP window background: wallpaper $id missing"; return 0; }
  367. geometry=$(xrandr | sed -n 's/^[^ ]* connected \(primary \)\?\([0-9]*\)x\([0-9]*\)+\([0-9]*\)+\([0-9]*\).*/\4x\5x\2x\3/p' | head -1)
  368. [ -n "$geometry" ] || { echo "SKIP window background: no output geometry from xrandr"; return 0; }
  369. # shellcheck disable=SC2086
  370. (setsid nohup "$ENGINE" --assets-dir "$LWE_DIR/assets" --window "$geometry" --window-background "$LWE_DIR/wp-$id" > "$log" 2>&1 < /dev/null &)
  371. settle "$log" "$id"
  372. if engine_pids >/dev/null; then pass "window background: engine running"; else fail "window background: engine exited"; return 0; fi
  373. SHOT_FILE=$OUT/shot-windowbg-$id.png; rm -f "$SHOT_FILE"; take_shot "$SHOT_FILE" "$OUT/windowbg.log"
  374. if [ -s "$SHOT_FILE" ]; then pass "window background: screenshot taken"; else fail "window background: no screenshot"; fi
  375. if stop_engine; then pass "window background: engine stopped on SIGTERM"; else fail "window background: engine needed SIGKILL"; fi
  376. }
  377. multi_monitor_check() {
  378. local id=1111948150 w1 w2
  379. [ -n "$MULTI_SHOT_CMD" ] || return
  380. if ! command -v xrandr >/dev/null; then echo "SKIP multi monitor: no xrandr"; return; fi
  381. for f in /sys/class/drm/card*-Virtual-2/status; do echo on | sudo tee "$f" >/dev/null 2>&1; done
  382. sleep 2
  383. w1=$(xrandr | sed -n 's/^Virtual-1 connected[^0-9]*\([0-9]*\)x[0-9]*+.*/\1/p' | head -1)
  384. if [ -z "$w1" ] || ! xrandr | grep -q '^Virtual-2 connected'; then echo "SKIP multi monitor: the guest has no second output"; return; fi
  385. xrandr --output Virtual-1 --auto --pos 0x0 --output Virtual-2 --auto --pos "${w1}x0"
  386. sleep 4
  387. w2=$(xrandr | sed -n 's/^Virtual-2 connected[^0-9]*\([0-9]*\)x[0-9]*+.*/\1/p' | head -1)
  388. echo "$w1 $w2" > "$OUT/multi-geometry"
  389. SHOT_FILE=$OUT/multi-baseline.png; rm -f "$SHOT_FILE"; eval "$MULTI_SHOT_CMD" > "$OUT/multi-baseline.log" 2>&1
  390. # shellcheck disable=SC2086
  391. (setsid nohup "$ENGINE" --assets-dir "$LWE_DIR/assets" --screen-root Virtual-1 --screen-root Virtual-2 "$LWE_DIR/wp-$id" > "$OUT/engine-multi.log" 2>&1 < /dev/null &)
  392. settle "$OUT/engine-multi.log" "$id"
  393. if engine_pids >/dev/null; then pass "multi monitor: engine running on two screens"; else fail "multi monitor: engine exited"; fi
  394. SHOT_FILE=$OUT/multi-shot.png; rm -f "$SHOT_FILE"; eval "$MULTI_SHOT_CMD" > "$OUT/multi-shot.log" 2>&1
  395. if [ -s "$SHOT_FILE" ] && [ -s "$OUT/multi-baseline.png" ]; then pass "multi monitor: screenshots of both screens taken"; else fail "multi monitor: no screenshot ($(head -c 120 "$OUT/multi-shot.log" | tr '\n' ' '))"; fi
  396. stop_engine
  397. xrandr --output Virtual-2 --off
  398. }
  399. # behaviour checks, BEHAVIOR=1: hotswap, pause on fullscreen, SIGKILL leaves no helper
  400. if [ "${BEHAVIOR:-0}" = 1 ] && want behavior; then
  401. t_behavior=$SECONDS
  402. A=${HOTSWAP_FROM:-1120596121}
  403. B=${HOTSWAP_TO:-1111948150}
  404. ctl=$OUT/control
  405. : > "$ctl"
  406. # shellcheck disable=SC2086
  407. (setsid nohup "$ENGINE" --assets-dir "$LWE_DIR/assets" $LWE_ARGS --control-file "$ctl" "$LWE_DIR/wp-$A" > "$OUT/engine-behavior.log" 2>&1 < /dev/null &)
  408. settle "$OUT/engine-behavior.log" "$A"
  409. pid=$(engine_pids | head -1)
  410. if [ -z "$pid" ]; then fail "behavior: engine did not start"; else
  411. SHOT_FILE=$OUT/hot-before.png; rm -f "$SHOT_FILE"; take_shot "$SHOT_FILE" "$OUT/hot-before.log"
  412. echo "path=$LWE_DIR/wp-$B" > "$ctl"
  413. kill -USR1 "$pid"
  414. sleep 15
  415. if grep -q "Hotswapping wallpaper to" "$OUT/engine-behavior.log"; then pass "hotswap: engine took the new wallpaper"; else fail "hotswap: no hotswap in the log"; fi
  416. if kill -0 "$pid" 2>/dev/null; then pass "hotswap: same process still running"; else fail "hotswap: engine exited"; fi
  417. SHOT_FILE=$OUT/hot-after.png; rm -f "$SHOT_FILE"; take_shot "$SHOT_FILE" "$OUT/hot-after.log"
  418. # back to the first wallpaper so the CPU numbers are of a scene with particles
  419. echo "path=$LWE_DIR/wp-$A" > "$ctl"; kill -USR1 "$pid"; sleep 12
  420. pause_check "fullscreen" "$FULLSCREEN_CMD"
  421. pause_check "maximized" "$MAXIMIZE_CMD"
  422. stop_engine
  423. fi
  424. # SIGKILL: the web host must not outlive the engine
  425. # shellcheck disable=SC2086
  426. (setsid nohup "$ENGINE" --assets-dir "$LWE_DIR/assets" $LWE_ARGS "$LWE_DIR/wp-1078208425" > "$OUT/engine-kill.log" 2>&1 < /dev/null &)
  427. settle "$OUT/engine-kill.log" 1078208425
  428. pid=$(engine_pids | head -1)
  429. helpers_before=$(pgrep -fc "[l]inux-wallpaperengine-web-helper")
  430. kill -9 "$pid" 2>/dev/null
  431. sleep 8
  432. helpers_after=$(pgrep -fc "[l]inux-wallpaperengine-web-helper")
  433. if [ "$helpers_before" -gt 0 ] && [ "$helpers_after" -eq 0 ]; then pass "SIGKILL: $helpers_before web host processes ended with the engine"
  434. elif [ "$helpers_before" -eq 0 ]; then fail "SIGKILL: no web host was running to begin with"
  435. else fail "SIGKILL: $helpers_after of $helpers_before web host processes left behind"; pkill -f "linux-wallpaperengine-web-helper"; fi
  436. echo "TIME behavior $((SECONDS - t_behavior))"
  437. fi
  438. if [ "${BEHAVIOR:-0}" = 1 ]; then
  439. want video && timed video hotswap_video_check
  440. want cycle && timed cycle hotswap_cycle_check
  441. want automute && timed automute automute_check
  442. if [ -n "$ONLY" ]; then want automuteignore && timed automuteignore automute_ignore_check
  443. elif [ "${AUTOMUTE_IGNORE:-0}" = 1 ]; then timed automuteignore automute_ignore_check; fi
  444. want transition && timed transition transition_check
  445. want multi && timed multi multi_monitor_check
  446. want windowbg && timed windowbg window_background_check
  447. fi
  448. touch "$OUT/done"
  449. exit $status