AtlatestRepositorysigil-desktop
sigil-desktop / tree / testtest-cursor-live.sgl
1
;;; Cursor capture with a window open (0.12.0). Needs an X display the2
;;; test may move the pointer on, xdotool and xwininfo; run it on a headless3
;;; 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-cursor-live.sgl7
;;;8
;;; Without DISPLAY or without xdotool it says so and checks nothing.9
;;;10
;;; Frame by frame: put the pointer inside the window from outside (the11
;;; position reported on entering is a jump, not motion: the sum must stay 0;12
;;; and a capture that starts inside reports no jump, so nothing may be13
;;; dropped); capture; move the14
;;; pointer 40 right and 25 down with xdotool (XTEST) and read the motion15
;;; back from mouse-dx / mouse-dy, summed over every frame from the capture16
;;; on (a jump at the capture would show in the sum); press Escape, which17
;;; releases; move 10 right and 5 down uncaptured and read that back (no jump18
;;; at the release); capture again with escape-releases? #f and press Escape,19
;;; which must not release; move the input focus to the root window, which20
;;; releases it (cursor-captured? reads GLFW's own input mode); and release,21
;;; which must answer #f.23
(import (sigil test)24
(sigil desktop)25
(sigil list)26
(sigil process))28
(define (display?) (and (getenv "DISPLAY") (not (getenv "WAYLAND_DISPLAY"))))29
(define (xdotool?) (and (display?) (= 0 (process-run "sh" "-c" "command -v xdotool >/dev/null && command -v xwininfo >/dev/null"))))30
(define (xdo . args) (apply process-run "xdotool" args))31
(define TITLE "sigil-desktop cursor test")33
(define *seen* '())34
(define (see! name v) (set! *seen* (append *seen* (list (cons name v)))))35
(define (seen name) (let ((e (assoc name *seen*))) (and e (cdr e))))37
(define *dx* 0.0)38
(define *dy* 0.0)39
(define (sum!) (set! *dx* (+ *dx* (mouse-dx))) (set! *dy* (+ *dy* (mouse-dy))))40
(define (reset-sum!) (set! *dx* 0.0) (set! *dy* 0.0))42
(define *n* 0)43
(define (frame dt)44
(set! *n* (+ *n* 1))45
;; motion is summed in every frame after the capture (frame 3) up to 12,46
;; and after the release (frame 14) up to 2047
(when (or (and (> *n* 1) (<= *n* 3)) (and (> *n* 3) (<= *n* 12)) (and (> *n* 14) (<= *n* 20))) (sum!))48
(case *n*49
((1) (process-run "sh" "-c" (string-append "xdotool mousemove --window $(xdotool search --name '" TITLE "' | head -1) 320 180")))50
((3) (see! "motion entering" (list *dx* *dy*))51
(see! "captured? before" (cursor-captured?))52
(see! "capture" (set-cursor-captured! #t))53
(reset-sum!))54
((5) (see! "captured? after" (cursor-captured?))55
(xdo "mousemove_relative" "--" "40" "25"))56
((12) (see! "motion captured" (list *dx* *dy*))57
(xdo "key" "Escape"))58
((14) (see! "captured? after escape" (cursor-captured?))59
(reset-sum!))60
((16) (xdo "mousemove_relative" "--" "10" "5"))61
((20) (see! "motion released" (list *dx* *dy*))62
(see! "capture, escape kept" (set-cursor-captured! #t #f)))63
((22) (xdo "key" "Escape"))64
((25) (see! "captured? after kept escape" (cursor-captured?))65
(process-run "sh" "-c" "xdotool windowfocus $(xwininfo -root | awk '/Window id/ {print $4}')"))66
((29) (see! "captured? after focus loss" (cursor-captured?))67
(see! "capture again" (set-cursor-captured! #t))68
(see! "release" (set-cursor-captured! #f))69
(see! "captured? after release" (cursor-captured?))70
(request-quit))71
(else #f)))73
(when (xdotool?)74
(app-run (lambda () #f) frame (lambda () #f) TITLE 640 360))76
(define (near? a b) (< (abs (- a b)) 0.51))77
(define (motion? m x y) (and (pair? m) (near? (car m) x) (near? (cadr m) y)))79
(test-group "cursor capture, with a window open"80
(if (not (xdotool?))81
(test "no X display or no xdotool/xwininfo: nothing checked (see the header)"82
(assert-true #t))83
(begin84
(test "the pointer entering the window is not motion"85
(assert-true (motion? (seen "motion entering") 0.0 0.0)))86
(test "set-cursor-captured! #t captures it (pointer inside the window)"87
(assert-false (seen "captured? before"))88
(assert-true (seen "capture"))89
(assert-true (seen "captured? after")))90
(test "a 40,25 move reads back as 40,25, summed from the capture on (nothing dropped, no jump)"91
(assert-true (motion? (seen "motion captured") 40.0 25.0)))92
(test "Escape releases the capture"93
(assert-false (seen "captured? after escape")))94
(test "after the release, a 10,5 move reads back as 10,5 (no jump)"95
(assert-true (motion? (seen "motion released") 10.0 5.0)))96
(test "with escape-releases? #f, Escape keeps it"97
(assert-true (seen "capture, escape kept"))98
(assert-true (seen "captured? after kept escape")))99
(test "losing the focus releases it (GLFW's input mode, not a flag)"100
(assert-false (seen "captured? after focus loss")))101
(test "set-cursor-captured! #f releases and answers #f"102
(assert-true (seen "capture again"))103
(assert-false (seen "release"))104
(assert-false (seen "captured? after release"))))))