#!/usr/bin/env python3 """Static wallpaper integration on desktop; never stops the existing wallpaper daemon.""" import json import os from pathlib import Path import struct import subprocess import sys import tempfile import time from urllib.parse import quote import zlib if os.environ.get('ADAPTIVE_WALLPAPER_TEST_BUS') != '1': os.execvpe('dbus-run-session', ['dbus-run-session', '--', sys.executable, __file__], dict(os.environ, ADAPTIVE_WALLPAPER_TEST_BUS='1')) ROOT = Path(__file__).resolve().parents[1] def image(path, color): def chunk(kind, data): return struct.pack('!I', len(data)) + kind + data + struct.pack('!I', zlib.crc32(kind + data)) raw = (b'\x00' + bytes(color) * 64) * 64 path.write_bytes(b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', struct.pack('!2I5B', 64, 64, 8, 2, 0, 0, 0)) + chunk(b'IDAT', zlib.compress(raw)) + chunk(b'IEND', b'')) def url(path): return 'file://' + quote(str(path), safe="/!~*'()") def wait_for(predicate, description, timeout=10): end = time.monotonic() + timeout while time.monotonic() < end: if predicate(): return time.sleep(.1) raise AssertionError(description) with tempfile.TemporaryDirectory(prefix='adaptive-wallpaper-') as tmp: tmp = Path(tmp) first = tmp / "обои a '#%.png" second = tmp / 'second.png' image(first, (35, 45, 65)) image(second, (65, 45, 35)) env = dict(os.environ, QT_QPA_PLATFORM='wayland', QT_QUICK_CONTROLS_STYLE='Material', XDG_STATE_HOME=str(tmp), XDG_CACHE_HOME=str(tmp), ADAPTIVE_NOTIFICATIONS='0') log_path = tmp / 'shell.log' log = log_path.open('w') process = None def start(): return subprocess.Popen(['quickshell', '-p', str(ROOT), '--no-color'], env=env, stdout=log, stderr=subprocess.STDOUT) def ipc(*args): result = subprocess.run(['quickshell', 'ipc', '--pid', str(process.pid), 'call', 'shell', *args], env=env, text=True, capture_output=True, timeout=30) if result.returncode: raise RuntimeError(result.stderr) return result.stdout def status(): return json.loads(ipc('status')) def ready(): if process.poll() is not None: raise RuntimeError(log_path.read_text()) try: return status()['preferencesWritable'] except (ValueError, RuntimeError): return False def stop(): process.terminate() try: process.wait(timeout=3) except subprocess.TimeoutExpired: process.kill(); process.wait() def layers(): result = subprocess.run(['hyprctl', '-j', 'layers'], env=env, capture_output=True, text=True, check=True, timeout=10) def walk(node): if isinstance(node, dict): if node.get('namespace') == 'adaptive-wallpaper' and node.get('pid') == process.pid: yield node for child in node.values(): yield from walk(child) elif isinstance(node, list): for child in node: yield from walk(child) return list(walk(json.loads(result.stdout))) try: process = start() wait_for(ready, 'shell readiness', 60) monitors = json.loads(subprocess.run(['hyprctl', '-j', 'monitors'], env=env, capture_output=True, text=True, check=True, timeout=10).stdout) monitor = next((m for m in monitors if m.get('focused')), monitors[0])['name'] ipc('wallpapers') assert all(s['state'] == 'disabled' for s in status()['wallpapers'].values()) assert not layers(), 'Unconfigured wallpapers must not map a surface' ipc('setWallpaper', '*', str(first)) wait_for(lambda: len(status()['wallpapers']) == len(monitors) and all(s['state'] == 'ready' and s['displayed'] == url(first) for s in status()['wallpapers'].values()), 'common wallpaper ready') wait_for(lambda: len(layers()) == len(monitors), 'background layer on each monitor') ipc('reload') time.sleep(.5) wait_for(ready, 'reload readiness', 60) wait_for(lambda: len(status()['wallpapers']) == len(monitors) and all(s['state'] == 'ready' and s['displayed'] == url(first) for s in status()['wallpapers'].values()), 'wallpaper ready after QML reload') print('PASS common wallpaper, special paths, background layers and QML reload', flush=True) ipc('setWallpaper', monitor, str(second)) wait_for(lambda: status()['wallpapers'][monitor]['displayed'] == url(second), 'per-output override') assert all(s['displayed'] == url(first) for name, s in status()['wallpapers'].items() if name != monitor) ipc('setWallpaper', monitor, 'https://example.com/image.png') assert status()['wallpaperError'], 'Remote source must be rejected' assert status()['wallpapers'][monitor]['displayed'] == url(second) ipc('setWallpaper', monitor, str(tmp / 'missing.png')) wait_for(lambda: status()['wallpapers'][monitor]['state'] == 'error', 'invalid file reported') assert status()['wallpapers'][monitor]['displayed'] == url(second), 'Keep last good wallpaper' print('PASS output override, remote rejection and missing-file fallback', flush=True) ipc('setWallpaper', monitor, '') wait_for(lambda: status()['wallpapers'][monitor]['state'] == 'disabled', 'override disabled') wait_for(lambda: len(layers()) == len(monitors) - 1, 'disabled surface removed') ipc('inheritWallpaper', monitor) wait_for(lambda: status()['wallpapers'][monitor]['displayed'] == url(first), 'inherit common wallpaper') ipc('setWallpaper', 'TEST-DISCONNECTED', str(second)) prefs = next(tmp.rglob('preferences.json')) wait_for(lambda: json.loads(prefs.read_text()).get('wallpapers', {}).get('TEST-DISCONNECTED') == url(second), 'disconnected output preference saved') stop() process = start() wait_for(ready, 'restarted shell readiness', 60) wait_for(lambda: status()['wallpapers'][monitor]['displayed'] == url(first), 'wallpaper restored') assert json.loads(prefs.read_text())['wallpapers']['TEST-DISCONNECTED'] == url(second) ipc('setWallpaper', '*', '') wait_for(lambda: not layers(), 'all test backgrounds removed') print('PASS disable, inherit, disconnected preference and restart persistence', flush=True) finally: if process and process.poll() is None: print('Final wallpaper state:', status(), flush=True) stop() log.close() logs = log_path.read_text() print(logs) for marker in ['Failed to load configuration', 'ReferenceError', 'TypeError', 'Binding loop', 'Cannot assign', 'Cannot create delegate']: assert marker not in logs, marker