AtlatestRepositorysigil-desktop

sigil-desktop / tree / testtest-focus-live.sgl

1;;; window-focused? with a window open (0.14.0). Needs an X display the test
2;;; may move the input focus on, xdotool and xwininfo; run it on a headless
3;;; X server, never on someone's desktop, e.g.
4;;;
5;;; Xvfb :74 -screen 0 1700x900x24 +extension GLX -nolisten tcp &
6;;; env -u WAYLAND_DISPLAY DISPLAY=:74 guix shell xdotool xwininfo -- sigil test test/test-focus-live.sgl
7;;;
8;;; Without DISPLAY or without xdotool it says so and checks nothing; the
9;;; no-window answer is test-window.sgl's.
10;;;
11;;; Frame by frame: give the window the focus (xdotool, by its title; GLFW
12;;; also takes it when the window is shown, so this first give changes
13;;; nothing, and the last one is what proves xdotool reached the window)
14;;; and read #t; move the focus to the root window and read #f; give it
15;;; back and read #t again. On X11 GLFW asks the server (XGetInputFocus) at
16;;; each read, so each answer follows the X focus.
18(import (sigil test)
19 (sigil desktop)
20 (sigil list)
21 (sigil process))
23(define (display?) (and (getenv "DISPLAY") (not (getenv "WAYLAND_DISPLAY"))))
24(define (xdotool?) (and (display?) (= 0 (process-run "sh" "-c" "command -v xdotool >/dev/null && command -v xwininfo >/dev/null"))))
26(define TITLE "sigil-desktop focus test")
27(define (focus-window!)
28 (process-run "sh" "-c" (string-append "xdotool search --name '" TITLE "' | head -1 | xargs xdotool windowfocus")))
29(define (focus-root!)
30 (process-run "sh" "-c" "xdotool windowfocus $(xwininfo -root | awk '/Window id/ {print $4}')"))
32(define *seen* '())
33(define (see! name v) (set! *seen* (append *seen* (list (cons name v)))))
34(define (seen name) (let ((e (assoc name *seen*))) (and e (cdr e))))
36(define *n* 0)
37(define (frame dt)
38 (set! *n* (+ *n* 1))
39 (case *n*
40 ((2) (focus-window!))
41 ((6) (see! "focused, given" (window-focused?))
42 (focus-root!))
43 ((10) (see! "focused, taken away" (window-focused?))
44 (focus-window!))
45 ((14) (see! "focused, given back" (window-focused?))
46 (request-quit))
47 (else #f)))
49(when (xdotool?)
50 (app-run (lambda () #f) frame (lambda () #f) TITLE 640 360))
52(test-group "window-focused?, with a window open"
53 (if (not (xdotool?))
54 (test "no X display or no xdotool/xwininfo: nothing checked (see the header)"
55 (assert-true #t))
56 (begin
57 (test "given the focus, it is #t"
58 (assert-equal #t (seen "focused, given")))
59 (test "with the focus on the root window, it is #f"
60 (assert-equal #f (seen "focused, taken away"))
61 (assert-true (assoc "focused, taken away" *seen*)))
62 (test "given it back, it is #t again"
63 (assert-equal #t (seen "focused, given back"))))))