You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
4.0 KiB
139 lines
4.0 KiB
import threading
|
|
import time
|
|
from datetime import datetime
|
|
|
|
from PIL import ImageChops
|
|
|
|
from ramscreen.__main__ import run
|
|
from ramscreen.config import Config
|
|
from ramscreen.meminfo import App, Memory
|
|
from ramscreen.render import render
|
|
|
|
GIB = 2**30
|
|
MEM = Memory(total=16 * GIB, available=8 * GIB, swap_total=2 * GIB, swap_free=GIB)
|
|
APPS = [App("firefox", GIB)]
|
|
NOW = datetime(2026, 10, 14, 12, 0)
|
|
FAST = Config(memory_interval=0, apps_interval=10)
|
|
|
|
|
|
class FakeLcd:
|
|
def __init__(self, open_failures=0, show_failures=()):
|
|
self.calls = []
|
|
self.shown = [] # (x, y, Bild)
|
|
self.open_failures = open_failures
|
|
self.show_failures = set(show_failures)
|
|
self._show_count = 0
|
|
|
|
def open(self):
|
|
self.calls.append("open")
|
|
if self.open_failures > 0:
|
|
self.open_failures -= 1
|
|
raise OSError("kein Gerät")
|
|
|
|
def close(self):
|
|
self.calls.append("close")
|
|
|
|
def set_orientation_portrait(self):
|
|
self.calls.append("orientation")
|
|
|
|
def set_brightness(self, percent):
|
|
self.calls.append(("brightness", percent))
|
|
|
|
def show(self, image, x, y):
|
|
index = self._show_count
|
|
self._show_count += 1
|
|
if index in self.show_failures:
|
|
raise OSError("Schreibfehler")
|
|
self.calls.append("show")
|
|
self.shown.append((x, y, image))
|
|
|
|
|
|
def stop_after(stop, iterations):
|
|
count = 0
|
|
|
|
def read_memory():
|
|
nonlocal count
|
|
count += 1
|
|
if count >= iterations:
|
|
stop.set()
|
|
return MEM
|
|
|
|
return read_memory
|
|
|
|
|
|
def run_fake(lcd, iterations, cfg=FAST, **kwargs):
|
|
stop = threading.Event()
|
|
kwargs.setdefault("top_apps", lambda n: APPS)
|
|
run(cfg, lcd, stop, read_memory=stop_after(stop, iterations), now=lambda: NOW,
|
|
reconnect_delay=0, **kwargs)
|
|
|
|
|
|
def sizes(lcd):
|
|
return [(x, y, img.size) for x, y, img in lcd.shown]
|
|
|
|
|
|
def test_normaler_ablauf():
|
|
lcd = FakeLcd()
|
|
run_fake(lcd, iterations=2)
|
|
assert lcd.calls[:3] == ["open", "orientation", ("brightness", 20)]
|
|
# 1. Durchlauf Vollbild, 2. Durchlauf unverändert → nichts, dann Endbildschirm als Vollbild
|
|
assert sizes(lcd) == [(0, 0, (320, 480)), (0, 0, (320, 480))]
|
|
assert lcd.calls[-1] == "close"
|
|
assert lcd.calls.count("open") == 1
|
|
|
|
|
|
def test_schreibfehler_fuehrt_zu_neuverbindung_und_vollbild():
|
|
lcd = FakeLcd(show_failures={0})
|
|
run_fake(lcd, iterations=2)
|
|
assert lcd.calls.count("open") == 2
|
|
assert lcd.calls[:6] == ["open", "orientation", ("brightness", 20), "close", "open", "orientation"]
|
|
assert sizes(lcd)[0] == (0, 0, (320, 480))
|
|
|
|
|
|
def test_display_beim_start_nicht_da():
|
|
lcd = FakeLcd(open_failures=2)
|
|
run_fake(lcd, iterations=1)
|
|
assert lcd.calls.count("open") == 3
|
|
assert sizes(lcd)[0] == (0, 0, (320, 480))
|
|
|
|
|
|
def test_stoppen_waehrend_wartezeit_beendet_sofort():
|
|
lcd = FakeLcd(open_failures=10**6)
|
|
stop = threading.Event()
|
|
t = threading.Thread(
|
|
target=run,
|
|
args=(FAST, lcd, stop),
|
|
kwargs={"read_memory": lambda: MEM, "top_apps": lambda n: APPS, "now": lambda: NOW, "reconnect_delay": 5},
|
|
)
|
|
t.start()
|
|
time.sleep(0.2)
|
|
stop.set()
|
|
t.join(timeout=2)
|
|
assert not t.is_alive()
|
|
assert "show" not in lcd.calls # nie verbunden → kein Endbildschirm
|
|
|
|
|
|
def test_programmliste_nur_im_eigenen_intervall():
|
|
calls = []
|
|
|
|
def top_apps(n):
|
|
calls.append(n)
|
|
return APPS
|
|
|
|
times = iter([0, 4, 8, 12, 16, 20])
|
|
run_fake(FakeLcd(), iterations=6, top_apps=top_apps, monotonic=lambda: next(times))
|
|
assert calls == [5, 5] # bei t=0 und t=12
|
|
|
|
|
|
def test_drehung_um_180_grad():
|
|
cfg = Config(memory_interval=0, rotate_180=True)
|
|
lcd = FakeLcd()
|
|
run_fake(lcd, iterations=1, cfg=cfg)
|
|
expected = render(MEM, APPS, NOW, cfg).rotate(180)
|
|
assert ImageChops.difference(lcd.shown[0][2], expected).getbbox() is None
|
|
|
|
|
|
def test_endbildschirm_fehler_wird_abgefangen():
|
|
lcd = FakeLcd(show_failures={1}) # 0 = erstes Bild, 1 = Endbildschirm
|
|
run_fake(lcd, iterations=1)
|
|
assert lcd.calls[-1] == "close"
|
|
|