334 lines
14 KiB
Python
334 lines
14 KiB
Python
#!/usr/bin/env python3
|
|||
|
|
"""Headless Xvfb E2E integration tests across 1024x600, 1920x1080, and 3840x2160."""
|
||
|
|
|
||
|
|
import ctypes
|
||
|
|
import ctypes.util
|
||
|
|
import os
|
||
|
|
import shutil
|
||
|
|
import signal
|
||
|
|
import socket
|
||
|
|
import subprocess
|
||
|
|
import sys
|
||
|
|
import tempfile
|
||
|
|
import time
|
||
|
|
|
||
|
|
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
|
||
|
|
BUILD = os.path.join(ROOT, "build")
|
||
|
|
|
||
|
|
libx11_name = ctypes.util.find_library("X11") or "libX11.so.6"
|
||
|
|
X11 = ctypes.CDLL(libx11_name)
|
||
|
|
|
||
|
|
Window = ctypes.c_ulong
|
||
|
|
Atom = ctypes.c_ulong
|
||
|
|
Display_p = ctypes.c_void_p
|
||
|
|
|
||
|
|
|
||
|
|
class XWindowAttributes(ctypes.Structure):
|
||
|
|
_fields_ = [
|
||
|
|
("x", ctypes.c_int), ("y", ctypes.c_int),
|
||
|
|
("width", ctypes.c_int), ("height", ctypes.c_int),
|
||
|
|
("border_width", ctypes.c_int), ("depth", ctypes.c_int),
|
||
|
|
("visual", ctypes.c_void_p), ("root", Window),
|
||
|
|
("class", ctypes.c_int), ("bit_gravity", ctypes.c_int),
|
||
|
|
("win_gravity", ctypes.c_int), ("backing_store", ctypes.c_int),
|
||
|
|
("backing_planes", ctypes.c_ulong), ("backing_pixel", ctypes.c_ulong),
|
||
|
|
("save_under", ctypes.c_int), ("colormap", ctypes.c_ulong),
|
||
|
|
("map_installed", ctypes.c_int), ("map_state", ctypes.c_int),
|
||
|
|
("all_event_masks", ctypes.c_long), ("your_event_mask", ctypes.c_long),
|
||
|
|
("do_not_propagate_mask", ctypes.c_long), ("override_redirect", ctypes.c_int),
|
||
|
|
("screen", ctypes.c_void_p),
|
||
|
|
]
|
||
|
|
|
||
|
|
|
||
|
|
class XClientMessageData(ctypes.Union):
|
||
|
|
_fields_ = [("b", ctypes.c_char * 20), ("s", ctypes.c_short * 10), ("l", ctypes.c_long * 5)]
|
||
|
|
|
||
|
|
|
||
|
|
class XClientMessageEvent(ctypes.Structure):
|
||
|
|
_fields_ = [
|
||
|
|
("type", ctypes.c_int), ("serial", ctypes.c_ulong), ("send_event", ctypes.c_int),
|
||
|
|
("display", Display_p), ("window", Window), ("message_type", Atom),
|
||
|
|
("format", ctypes.c_int), ("data", XClientMessageData),
|
||
|
|
]
|
||
|
|
|
||
|
|
|
||
|
|
class XWMHints(ctypes.Structure):
|
||
|
|
_fields_ = [
|
||
|
|
("flags", ctypes.c_long), ("input", ctypes.c_int), ("initial_state", ctypes.c_int),
|
||
|
|
("icon_pixmap", ctypes.c_ulong), ("icon_window", Window),
|
||
|
|
("icon_x", ctypes.c_int), ("icon_y", ctypes.c_int),
|
||
|
|
("icon_mask", ctypes.c_ulong), ("window_group", Window),
|
||
|
|
]
|
||
|
|
|
||
|
|
|
||
|
|
X11.XOpenDisplay.argtypes = [ctypes.c_char_p]
|
||
|
|
X11.XOpenDisplay.restype = Display_p
|
||
|
|
X11.XCloseDisplay.argtypes = [Display_p]
|
||
|
|
X11.XDefaultRootWindow.argtypes = [Display_p]
|
||
|
|
X11.XDefaultRootWindow.restype = Window
|
||
|
|
X11.XInternAtom.argtypes = [Display_p, ctypes.c_char_p, ctypes.c_int]
|
||
|
|
X11.XInternAtom.restype = Atom
|
||
|
|
X11.XGetSelectionOwner.argtypes = [Display_p, Atom]
|
||
|
|
X11.XGetSelectionOwner.restype = Window
|
||
|
|
X11.XGetWindowProperty.argtypes = [
|
||
|
|
Display_p, Window, Atom, ctypes.c_long, ctypes.c_long, ctypes.c_int, Atom,
|
||
|
|
ctypes.POINTER(Atom), ctypes.POINTER(ctypes.c_int),
|
||
|
|
ctypes.POINTER(ctypes.c_ulong), ctypes.POINTER(ctypes.c_ulong),
|
||
|
|
ctypes.POINTER(ctypes.POINTER(ctypes.c_ubyte)),
|
||
|
|
]
|
||
|
|
X11.XGetWindowProperty.restype = ctypes.c_int
|
||
|
|
X11.XFree.argtypes = [ctypes.c_void_p]
|
||
|
|
X11.XCreateSimpleWindow.argtypes = [
|
||
|
|
Display_p, Window, ctypes.c_int, ctypes.c_int, ctypes.c_uint, ctypes.c_uint,
|
||
|
|
ctypes.c_uint, ctypes.c_ulong, ctypes.c_ulong,
|
||
|
|
]
|
||
|
|
X11.XCreateSimpleWindow.restype = Window
|
||
|
|
X11.XDestroyWindow.argtypes = [Display_p, Window]
|
||
|
|
X11.XChangeProperty.argtypes = [
|
||
|
|
Display_p, Window, Atom, Atom, ctypes.c_int, ctypes.c_int, ctypes.c_void_p, ctypes.c_int
|
||
|
|
]
|
||
|
|
X11.XSendEvent.argtypes = [Display_p, Window, ctypes.c_int, ctypes.c_long, ctypes.c_void_p]
|
||
|
|
X11.XFlush.argtypes = [Display_p]
|
||
|
|
X11.XSync.argtypes = [Display_p, ctypes.c_int]
|
||
|
|
X11.XQueryTree.argtypes = [
|
||
|
|
Display_p, Window, ctypes.POINTER(Window), ctypes.POINTER(Window),
|
||
|
|
ctypes.POINTER(ctypes.POINTER(Window)), ctypes.POINTER(ctypes.c_uint),
|
||
|
|
]
|
||
|
|
X11.XQueryTree.restype = ctypes.c_int
|
||
|
|
X11.XGetWindowAttributes.argtypes = [Display_p, Window, ctypes.POINTER(XWindowAttributes)]
|
||
|
|
X11.XGetWindowAttributes.restype = ctypes.c_int
|
||
|
|
X11.XGetWMHints.argtypes = [Display_p, Window]
|
||
|
|
X11.XGetWMHints.restype = ctypes.POINTER(XWMHints)
|
||
|
|
X11.XFetchName.argtypes = [Display_p, Window, ctypes.POINTER(ctypes.c_char_p)]
|
||
|
|
X11.XFetchName.restype = ctypes.c_int
|
||
|
|
|
||
|
|
|
||
|
|
def pick_display():
|
||
|
|
for d in range(70, 120):
|
||
|
|
if not os.path.exists(f"/tmp/.X{d}-lock") and not os.path.exists(f"/tmp/.X11-unix/X{d}"):
|
||
|
|
return f":{d}"
|
||
|
|
return ":99"
|
||
|
|
|
||
|
|
|
||
|
|
def get_cardinal_prop(dpy, win, atom_name):
|
||
|
|
atom = X11.XInternAtom(dpy, atom_name.encode(), 0)
|
||
|
|
da = Atom()
|
||
|
|
di = ctypes.c_int()
|
||
|
|
n = ctypes.c_ulong()
|
||
|
|
dl = ctypes.c_ulong()
|
||
|
|
prop = ctypes.POINTER(ctypes.c_ubyte)()
|
||
|
|
rc = X11.XGetWindowProperty(dpy, win, atom, 0, 16, 0, 0,
|
||
|
|
ctypes.byref(da), ctypes.byref(di),
|
||
|
|
ctypes.byref(n), ctypes.byref(dl), ctypes.byref(prop))
|
||
|
|
if rc != 0 or not prop:
|
||
|
|
return []
|
||
|
|
vals = []
|
||
|
|
if di.value == 32 and n.value > 0:
|
||
|
|
arr = ctypes.cast(prop, ctypes.POINTER(ctypes.c_ulong))
|
||
|
|
vals = [int(arr[i]) for i in range(n.value)]
|
||
|
|
X11.XFree(prop)
|
||
|
|
return vals
|
||
|
|
|
||
|
|
|
||
|
|
def query_children(dpy, parent):
|
||
|
|
r = Window()
|
||
|
|
p = Window()
|
||
|
|
ch = ctypes.POINTER(Window)()
|
||
|
|
n = ctypes.c_uint()
|
||
|
|
if X11.XQueryTree(dpy, parent, ctypes.byref(r), ctypes.byref(p), ctypes.byref(ch), ctypes.byref(n)):
|
||
|
|
out = [int(ch[i]) for i in range(n.value)]
|
||
|
|
if ch:
|
||
|
|
X11.XFree(ch)
|
||
|
|
return out
|
||
|
|
return []
|
||
|
|
|
||
|
|
|
||
|
|
def wait_until(pred, timeout=5.0):
|
||
|
|
end = time.monotonic() + timeout
|
||
|
|
while time.monotonic() < end:
|
||
|
|
res = pred()
|
||
|
|
if res:
|
||
|
|
return res
|
||
|
|
time.sleep(0.05)
|
||
|
|
return None
|
||
|
|
|
||
|
|
|
||
|
|
def terminate(proc):
|
||
|
|
if proc and proc.poll() is None:
|
||
|
|
proc.terminate()
|
||
|
|
try:
|
||
|
|
proc.wait(timeout=3)
|
||
|
|
except subprocess.TimeoutExpired:
|
||
|
|
proc.kill()
|
||
|
|
proc.wait(timeout=2)
|
||
|
|
|
||
|
|
|
||
|
|
def test_resolution(res_str, scale_str, width, height):
|
||
|
|
disp = pick_display()
|
||
|
|
with tempfile.TemporaryDirectory() as home:
|
||
|
|
env = dict(os.environ)
|
||
|
|
env["DISPLAY"] = disp
|
||
|
|
env["HOME"] = home
|
||
|
|
env["XDG_CONFIG_HOME"] = os.path.join(home, ".config")
|
||
|
|
env["ANODE_PREFIX"] = ROOT
|
||
|
|
env["ANODE_NO_SIGNAL"] = "1"
|
||
|
|
env["LD_LIBRARY_PATH"] = os.path.join(BUILD, "libgemui") + (":" + env["LD_LIBRARY_PATH"] if env.get("LD_LIBRARY_PATH") else "")
|
||
|
|
env["PATH"] = (
|
||
|
|
os.path.join(BUILD, "dwm") + ":" +
|
||
|
|
os.path.join(BUILD, "dmenu") + ":" +
|
||
|
|
os.path.join(BUILD, "st") + ":" +
|
||
|
|
os.path.join(BUILD, "apps") + ":" +
|
||
|
|
os.path.join(BUILD, "daemons") + ":" +
|
||
|
|
os.path.join(ROOT, "scripts") + ":" +
|
||
|
|
env.get("PATH", "")
|
||
|
|
)
|
||
|
|
|
||
|
|
# Configure theme + scale in sandbox
|
||
|
|
subprocess.run([os.path.join(ROOT, "scripts", "anode-apply-scale"), scale_str, "--touch=1"], env=env, check=True)
|
||
|
|
|
||
|
|
xvfb = subprocess.Popen(
|
||
|
|
["Xvfb", disp, "-screen", "0", f"{res_str}x24", "-nolisten", "tcp"],
|
||
|
|
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
||
|
|
)
|
||
|
|
dwm = None
|
||
|
|
dpy = None
|
||
|
|
procs = []
|
||
|
|
try:
|
||
|
|
dpy = wait_until(lambda: X11.XOpenDisplay(disp.encode()))
|
||
|
|
assert dpy, f"Failed to open Xvfb {disp}"
|
||
|
|
root = X11.XDefaultRootWindow(dpy)
|
||
|
|
|
||
|
|
dwm = subprocess.Popen([os.path.join(BUILD, "dwm", "dwm")], env=env)
|
||
|
|
procs.append(dwm)
|
||
|
|
|
||
|
|
tray_atom = X11.XInternAtom(dpy, b"_NET_SYSTEM_TRAY_S0", 0)
|
||
|
|
tray_win = wait_until(lambda: X11.XGetSelectionOwner(dpy, tray_atom))
|
||
|
|
assert tray_win, f"DWM failed to acquire _NET_SYSTEM_TRAY_S0 on {res_str}"
|
||
|
|
|
||
|
|
# Check initial _NET_WORKAREA and empty systray must be unmapped
|
||
|
|
wa0 = wait_until(lambda: get_cardinal_prop(dpy, root, "_NET_WORKAREA"))
|
||
|
|
assert len(wa0) >= 4 and wa0[2] == width and wa0[3] < height, f"Bad initial workarea: {wa0}"
|
||
|
|
tray_attr = XWindowAttributes()
|
||
|
|
X11.XGetWindowAttributes(dpy, tray_win, ctypes.byref(tray_attr))
|
||
|
|
assert tray_attr.map_state == 0, "Empty systray should stay unmapped!"
|
||
|
|
|
||
|
|
# Test XEmbed systray docking + _XEMBED_INFO unmap/remap + SIGUSR1 resize + undock
|
||
|
|
icon = X11.XCreateSimpleWindow(dpy, root, 0, 0, 24, 24, 0, 0, 0xffffff)
|
||
|
|
xembed_info = X11.XInternAtom(dpy, b"_XEMBED_INFO", 0)
|
||
|
|
info_arr = (ctypes.c_ulong * 2)(0, 1) # version=0, XEMBED_MAPPED=1
|
||
|
|
X11.XChangeProperty(dpy, icon, xembed_info, xembed_info, 32, 0, info_arr, 2)
|
||
|
|
op_atom = X11.XInternAtom(dpy, b"_NET_SYSTEM_TRAY_OPCODE", 0)
|
||
|
|
ev = XClientMessageEvent()
|
||
|
|
ev.type = 33 # ClientMessage
|
||
|
|
ev.display = dpy
|
||
|
|
ev.window = tray_win
|
||
|
|
ev.message_type = op_atom
|
||
|
|
ev.format = 32
|
||
|
|
ev.data.l[0] = 0
|
||
|
|
ev.data.l[1] = 0 # SYSTEM_TRAY_REQUEST_DOCK
|
||
|
|
ev.data.l[2] = icon
|
||
|
|
X11.XSendEvent(dpy, tray_win, 0, 0, ctypes.byref(ev))
|
||
|
|
X11.XSync(dpy, 0)
|
||
|
|
|
||
|
|
assert wait_until(lambda: icon in query_children(dpy, tray_win)), "Icon was not reparented into systray!"
|
||
|
|
X11.XGetWindowAttributes(dpy, tray_win, ctypes.byref(tray_attr))
|
||
|
|
assert tray_attr.map_state == 2, "Non-empty systray must be mapped (IsViewable)"
|
||
|
|
|
||
|
|
# Toggle _XEMBED_INFO unmapped -> tray unmaps
|
||
|
|
info_unmap = (ctypes.c_ulong * 2)(0, 0)
|
||
|
|
X11.XChangeProperty(dpy, icon, xembed_info, xembed_info, 32, 0, info_unmap, 2)
|
||
|
|
X11.XSync(dpy, 0)
|
||
|
|
assert wait_until(lambda: (X11.XGetWindowAttributes(dpy, tray_win, ctypes.byref(tray_attr)) or True) and tray_attr.map_state == 0), "Tray did not unmap when icon withdrew!"
|
||
|
|
|
||
|
|
# Remap via _XEMBED_INFO, reload scale via SIGUSR1, then destroy cleanly
|
||
|
|
X11.XChangeProperty(dpy, icon, xembed_info, xembed_info, 32, 0, info_arr, 2)
|
||
|
|
X11.XSync(dpy, 0)
|
||
|
|
assert wait_until(lambda: (X11.XGetWindowAttributes(dpy, tray_win, ctypes.byref(tray_attr)) or True) and tray_attr.map_state == 2)
|
||
|
|
pre_h = tray_attr.height
|
||
|
|
subprocess.run([os.path.join(ROOT, "scripts", "anode-apply-scale"), "2.00", "--touch=1"], env=env, check=True)
|
||
|
|
dwm.send_signal(signal.SIGUSR1)
|
||
|
|
time.sleep(0.25)
|
||
|
|
X11.XGetWindowAttributes(dpy, tray_win, ctypes.byref(tray_attr))
|
||
|
|
assert tray_attr.height >= pre_h, f"Tray height did not preserve/scale after SIGUSR1: {tray_attr.height} vs {pre_h}"
|
||
|
|
subprocess.run([os.path.join(ROOT, "scripts", "anode-apply-scale"), scale_str, "--touch=1"], env=env, check=True)
|
||
|
|
dwm.send_signal(signal.SIGUSR1)
|
||
|
|
time.sleep(0.2)
|
||
|
|
X11.XDestroyWindow(dpy, icon)
|
||
|
|
X11.XSync(dpy, 0)
|
||
|
|
assert wait_until(lambda: icon not in query_children(dpy, tray_win))
|
||
|
|
|
||
|
|
# Refresh wa0 after scale restore before testing anode-vkbd dock strut
|
||
|
|
wa0 = wait_until(lambda: get_cardinal_prop(dpy, root, "_NET_WORKAREA"))
|
||
|
|
vkbd = subprocess.Popen([os.path.join(BUILD, "apps", "anode-vkbd")], env=env)
|
||
|
|
procs.append(vkbd)
|
||
|
|
wa_vk = wait_until(lambda: (lambda w: w if w and len(w) >= 4 and w[3] < wa0[3] else None)(get_cardinal_prop(dpy, root, "_NET_WORKAREA")))
|
||
|
|
assert wa_vk, f"anode-vkbd did not reserve workarea strut on {res_str} (wa0={wa0})"
|
||
|
|
# Hide via SIGUSR1 -> workarea restores, then re-show via SIGUSR1 -> strut reserves again
|
||
|
|
vkbd.send_signal(signal.SIGUSR1)
|
||
|
|
assert wait_until(lambda: get_cardinal_prop(dpy, root, "_NET_WORKAREA") == wa0), "Workarea did not restore on vkbd hide"
|
||
|
|
vkbd.send_signal(signal.SIGUSR1)
|
||
|
|
assert wait_until(lambda: (lambda w: w if w and len(w) >= 4 and w[3] < wa0[3] else None)(get_cardinal_prop(dpy, root, "_NET_WORKAREA"))), "Workarea did not re-reserve on vkbd show"
|
||
|
|
terminate(vkbd)
|
||
|
|
|
||
|
|
# Test anode-statusd updates root WM_NAME
|
||
|
|
sd = subprocess.Popen([os.path.join(BUILD, "daemons", "anode-statusd")], env=env)
|
||
|
|
procs.append(sd)
|
||
|
|
def has_status():
|
||
|
|
nm = ctypes.c_char_p()
|
||
|
|
if X11.XFetchName(dpy, root, ctypes.byref(nm)) and nm.value:
|
||
|
|
s = nm.value.decode("utf-8", errors="ignore")
|
||
|
|
X11.XFree(nm)
|
||
|
|
if "|" in s:
|
||
|
|
return s
|
||
|
|
return None
|
||
|
|
assert wait_until(has_status), "anode-statusd did not publish status to root WM_NAME"
|
||
|
|
terminate(sd)
|
||
|
|
|
||
|
|
# Test core + GemUI utilities launch, map cleanly inside screen, and exit cleanly
|
||
|
|
for bin_path, extra_stdin in [
|
||
|
|
(os.path.join(BUILD, "st", "st"), None),
|
||
|
|
(os.path.join(BUILD, "dmenu", "dmenu"), b"alpha\nbeta\n"),
|
||
|
|
(os.path.join(BUILD, "apps", "anode-themes"), None),
|
||
|
|
(os.path.join(BUILD, "apps", "anode-power"), None),
|
||
|
|
(os.path.join(BUILD, "apps", "anode-display"), None),
|
||
|
|
(os.path.join(BUILD, "apps", "anode-audio"), None),
|
||
|
|
(os.path.join(BUILD, "apps", "anode-datetime"), None),
|
||
|
|
(os.path.join(BUILD, "apps", "anode-net"), None),
|
||
|
|
]:
|
||
|
|
before = set(query_children(dpy, root))
|
||
|
|
if extra_stdin:
|
||
|
|
p = subprocess.Popen([bin_path], env=env, stdin=subprocess.PIPE, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||
|
|
p.stdin.write(extra_stdin)
|
||
|
|
p.stdin.close()
|
||
|
|
else:
|
||
|
|
p = subprocess.Popen([bin_path], env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||
|
|
procs.append(p)
|
||
|
|
time.sleep(0.35)
|
||
|
|
assert p.poll() is None, f"{os.path.basename(bin_path)} exited prematurely on {res_str} with code {p.returncode}"
|
||
|
|
terminate(p)
|
||
|
|
|
||
|
|
finally:
|
||
|
|
for p in reversed(procs):
|
||
|
|
terminate(p)
|
||
|
|
if dpy:
|
||
|
|
X11.XCloseDisplay(dpy)
|
||
|
|
terminate(xvfb)
|
||
|
|
|
||
|
|
|
||
|
|
def main():
|
||
|
|
if not shutil.which("Xvfb"):
|
||
|
|
print("test_x11_xvfb: Xvfb not installed (skipping)")
|
||
|
|
return 0
|
||
|
|
cases = [
|
||
|
|
("1024x600", "1.00", 1024, 600),
|
||
|
|
("1920x1080", "1.25", 1920, 1080),
|
||
|
|
("3840x2160", "2.00", 3840, 2160),
|
||
|
|
]
|
||
|
|
for res, sc, w, h in cases:
|
||
|
|
test_resolution(res, sc, w, h)
|
||
|
|
print(f"test_x11_xvfb: {res} @ {sc}x OK")
|
||
|
|
return 0
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
sys.exit(main())
|