Files
anode/tests/test_theme_scale.py
T

144 lines
5.9 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""Verify .xth theme switching and fractional DPI scaling across toolkits without double-scaling."""
import glob
import os
import re
import subprocess
import tempfile
ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
THEMES_DIR = os.path.join(ROOT, "data", "themes")
APPLY_SCALE = os.path.join(ROOT, "scripts", "anode-apply-scale")
APPLY_THEME = os.path.join(ROOT, "scripts", "anode-apply-theme")
def run_env(env, cmd):
res = subprocess.run(cmd, env=env, capture_output=True, text=True)
assert res.returncode == 0, f"Command failed: {cmd}\nstdout={res.stdout}\nstderr={res.stderr}"
return res
def parse_xres(path):
out = {}
with open(path, "r", encoding="utf-8") as f:
for line in f:
s = line.strip()
if not s or s.startswith("!") or s.startswith("#"):
continue
if ":" in s:
k, v = s.split(":", 1)
out[k.strip()] = v.strip()
return out
def test_fractional_scales():
scales = [
("0.75", 72, "1", 11),
("1.00", 96, "1", 15),
("1.25", 120, "1", 19),
("1.50", 144, "1", 23),
("1.75", 168, "2", 26),
("2.00", 192, "2", 30),
("2.50", 240, "3", 38),
]
with tempfile.TemporaryDirectory() as tmp:
env = dict(os.environ)
env.pop("DISPLAY", None)
env["HOME"] = tmp
env["XDG_CONFIG_HOME"] = os.path.join(tmp, ".config")
env["ANODE_PREFIX"] = ROOT
env["ANODE_NO_SIGNAL"] = "1"
for s_str, exp_dpi, exp_gdk, exp_px in scales:
run_env(env, [APPLY_SCALE, s_str, "--touch=1"])
scale_env_p = os.path.join(tmp, ".config", "anode", "scale.env")
with open(scale_env_p, "r", encoding="utf-8") as f:
senv = f.read()
assert f'export GDK_SCALE="{exp_gdk}"' in senv, senv
assert 'export GDK_DPI_SCALE="1.0"' in senv, senv
assert f'export QT_SCALE_FACTOR="{s_str}"' in senv, senv
assert 'export QT_FONT_DPI="96"' in senv, senv
assert 'export QT_AUTO_SCREEN_SCALE_FACTOR="0"' in senv, senv
assert 'export QT_SCALE_FACTOR_ROUNDING_POLICY="PassThrough"' in senv, senv
xres = parse_xres(os.path.join(tmp, ".Xresources"))
assert int(xres["Xft.dpi"]) == exp_dpi, f"scale={s_str} dpi={xres['Xft.dpi']}"
assert int(xres["theme.baseFontSize"]) == exp_px, f"scale={s_str} base={xres['theme.baseFontSize']}"
assert xres["theme.fontFace"] == "Fira Code"
assert "Fira Code" in xres["st.font"] and f"pixelsize={exp_px}" in xres["st.font"]
assert "Fira Code" in xres["dwm.font"] and f"{exp_px}px" in xres["dwm.font"]
assert xres["*TkTheme"] == "gtkTtk"
assert xres["theme.touchMode"] == "1"
# Verify xsettingsd omits Xft/DPI so GTK3 does not double-scale
xs_p = os.path.join(tmp, ".config", "xsettingsd", "xsettingsd.conf")
with open(xs_p, "r", encoding="utf-8") as f:
xs = f.read()
assert "Xft/DPI" not in xs, f"Xft/DPI must not exist in xsettingsd.conf:\n{xs}"
assert f"Gdk/WindowScalingFactor {exp_gdk}" in xs, xs
# Verify Qt5 + Qt6 + Kvantum + Dunst + GTK2/3/4 files
for p in [
".gtkrc-2.0",
".config/gtk-3.0/gtk.css",
".config/gtk-3.0/settings.ini",
".config/gtk-4.0/gtk.css",
".config/gtk-4.0/settings.ini",
".config/qt5ct/qt5ct.conf",
".config/qt6ct/qt6ct.conf",
".config/Kvantum/kvantum.kvconfig",
".config/dunst/dunstrc",
]:
full = os.path.join(tmp, p)
assert os.path.isfile(full) and os.path.getsize(full) > 20, f"Missing/empty {p}"
def test_all_22_themes_and_legacy_192dpi_import():
themes = sorted(glob.glob(os.path.join(THEMES_DIR, "*.xth")))
assert len(themes) == 22, f"Expected 22 .xth themes, got {len(themes)}"
with tempfile.TemporaryDirectory() as tmp:
env = dict(os.environ)
env.pop("DISPLAY", None)
env["HOME"] = tmp
env["XDG_CONFIG_HOME"] = os.path.join(tmp, ".config")
env["ANODE_PREFIX"] = ROOT
env["ANODE_NO_SIGNAL"] = "1"
run_env(env, [APPLY_SCALE, "1.00", "--touch=0", "--no-apply"])
for th in themes:
run_env(env, [APPLY_THEME, th])
xres = parse_xres(os.path.join(tmp, ".Xresources"))
tname = xres["theme.name"]
assert xres["Xft.dpi"] == "96"
assert xres["theme.baseFontSize"] == "15"
assert xres["theme.fontFace"] == "Fira Code"
assert os.path.isfile(os.path.join(tmp, ".config", "qt6ct", "colors", f"ANODE-{tname}.conf"))
assert os.path.isfile(os.path.join(tmp, ".config", "qt5ct", "colors", f"ANODE-{tname}.conf"))
# Legacy 192-DPI import must normalize back to 96 DPI / 15px at 1.00x scale
legacy_xth = os.path.join(tmp, "LegacyHiDpi.xth")
with open(legacy_xth, "w", encoding="utf-8") as f:
f.write(
"theme.name: LegacyHiDpi\n"
"Xft.dpi: 192\n"
"Xcursor.size: 64\n"
"theme.fontFace: Fira Code\n"
"theme.baseFontSize: 32\n"
"dwm.borderpx: 4\n"
"theme.bgMain: #101010\n"
"theme.fgMain: #eeeeee\n"
"theme.accent: #55aaff\n"
)
run_env(env, [APPLY_THEME, legacy_xth])
xres = parse_xres(os.path.join(tmp, ".Xresources"))
assert xres["Xft.dpi"] == "96"
assert int(xres["theme.baseFontSize"]) == 16, xres["theme.baseFontSize"]
assert int(xres["dwm.borderpx"]) == 2, xres["dwm.borderpx"]
if __name__ == "__main__":
test_fractional_scales()
test_all_22_themes_and_legacy_192dpi_import()
print("test_theme_scale: ALL PASSED (7 fractional scales, 22 themes, legacy 192-DPI import)")