119 lines
3.1 KiB
Python
119 lines
3.1 KiB
Python
import sys
|
|
import threading
|
|
import types
|
|
from enum import Enum
|
|
|
|
import pytest
|
|
from PIL import Image
|
|
|
|
import main
|
|
import registry
|
|
from lcdkit import BG
|
|
from view import Patch
|
|
|
|
|
|
class FakeLcd:
|
|
instances = []
|
|
|
|
def __init__(self, com_port="AUTO"):
|
|
self.com_port = com_port
|
|
self.calls, self.closed, self.orientation, self.brightness = [], False, None, None
|
|
FakeLcd.instances.append(self)
|
|
|
|
def InitializeComm(self): ...
|
|
|
|
def SetOrientation(self, o):
|
|
self.orientation = o
|
|
|
|
def SetBrightness(self, b):
|
|
self.brightness = b
|
|
|
|
def DisplayPILImage(self, image, x=0, y=0, image_width=None, image_height=None):
|
|
self.calls.append((image.size, x, y))
|
|
|
|
def closeSerial(self):
|
|
self.closed = True
|
|
|
|
|
|
class FakeOrientation(Enum):
|
|
PORTRAIT = 0
|
|
REVERSE_PORTRAIT = 1
|
|
LANDSCAPE = 2
|
|
REVERSE_LANDSCAPE = 3
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def fake_library(monkeypatch):
|
|
FakeLcd.instances = []
|
|
lib = types.ModuleType("library")
|
|
lcd = types.ModuleType("library.lcd")
|
|
comm = types.ModuleType("library.lcd.lcd_comm")
|
|
comm.Orientation = FakeOrientation
|
|
rev_a = types.ModuleType("library.lcd.lcd_comm_rev_a")
|
|
rev_a.LcdCommRevA = FakeLcd
|
|
for name, mod in (("library", lib), ("library.lcd", lcd), ("library.lcd.lcd_comm", comm),
|
|
("library.lcd.lcd_comm_rev_a", rev_a)):
|
|
monkeypatch.setitem(sys.modules, name, mod)
|
|
monkeypatch.setenv("SCREEN_DEVICE", "/dev/fake")
|
|
|
|
|
|
class StubView:
|
|
name = "stub"
|
|
|
|
def __init__(self):
|
|
self.started = self.stopped = False
|
|
|
|
def start(self):
|
|
self.started = True
|
|
|
|
def stop(self):
|
|
self.stopped = True
|
|
|
|
def frame_key(self, now):
|
|
return "k"
|
|
|
|
def render(self, now):
|
|
return Image.new("RGB", (480, 320), BG)
|
|
|
|
def patch(self, now):
|
|
return None
|
|
|
|
|
|
def stop_soon(seconds=0.4):
|
|
stop = threading.Event()
|
|
threading.Timer(seconds, stop.set).start()
|
|
return stop
|
|
|
|
|
|
def test_unknown_view_shows_error_and_stays_up_until_stopped(monkeypatch):
|
|
monkeypatch.setenv("VIEW", "bogus")
|
|
main.main(stop_soon())
|
|
lcd = FakeLcd.instances[0]
|
|
assert lcd.calls == [((480, 320), 0, 0)] # the error screen, drawn once
|
|
assert lcd.closed
|
|
|
|
|
|
def test_missing_view_variable_is_an_error_screen_too(monkeypatch):
|
|
monkeypatch.delenv("VIEW", raising=False)
|
|
main.main(stop_soon())
|
|
assert FakeLcd.instances[0].calls == [((480, 320), 0, 0)]
|
|
|
|
|
|
def test_known_view_runs_and_is_stopped_cleanly(monkeypatch):
|
|
view = StubView()
|
|
monkeypatch.setitem(registry.REGISTRY, "stub", lambda: view)
|
|
monkeypatch.setenv("VIEW", "stub")
|
|
main.main(stop_soon())
|
|
lcd = FakeLcd.instances[0]
|
|
assert view.started and view.stopped and lcd.closed
|
|
assert lcd.calls[0] == ((480, 320), 0, 0)
|
|
|
|
|
|
def test_settings_reach_the_screen(monkeypatch):
|
|
monkeypatch.setitem(registry.REGISTRY, "stub", StubView)
|
|
monkeypatch.setenv("VIEW", "stub")
|
|
monkeypatch.setenv("SCREEN_BRIGHTNESS", "70")
|
|
main.main(stop_soon(0.2))
|
|
lcd = FakeLcd.instances[0]
|
|
assert (lcd.com_port, lcd.orientation, lcd.brightness) == ("/dev/fake", FakeOrientation.LANDSCAPE, 70)
|