Files
lightgui_python/tests/test_draw.py
T
cnmcdee 7717194cbd Phase 2: faithful generic drawings + live-apply plumbing + tests
core.py: per-widget Cairo drawing inside the 14 archetypes, ported
from Rust archetypes.rs / C widget draws -- buttons (incl. link,
colour-swatch, lock, glyph, MenuButton direction arrow), toggles
(check/radio/switch/toggle-button/menu-check), labels (xalign/yalign,
wrap, accel stripping), entries (password masking, placeholder,
alignment, search/calc variants), text views (wrap modes, scrollbars,
cursor), ranges (scale with ticks/sel-range, scrollbar with thumb +
arrows, progress, spin, level bar), lists (combo, colour box, listbox,
checklistbox, tree, listview incl. icon style, menubar/menu, toolbar,
stack switcher), containers (frame/groupbox, notebook, calendar grid,
splitter, scrolled window, header/action bar, statusbar, window),
display (image scene, shape 0-9, separator, spinner, video,
drawing area), dialogs (title bar, file/color/about variants),
charts (line/bars/candles), spreadsheet grid. Drawings honour
background/foreground/border/font props; invisible draws nothing,
disabled dims. Pixel parity with C is NOT required.

Live-apply: Widget.set_prop validates against lightgui.props
(raises ValueError on unknown/bad/out-of-range props), updates
Name/geometry fields or props, marks the widget dirty and calls
on_change; CanvasState wires widget.on_change and clears dirty
after drawing. Widget setters: set_items (ComboBox/ListBox, prunes
CheckListBox checked-items), get/set_checked_items,
set_item_checked, set_password/set_show_password (masked until
revealed), set_direction (MenuButton), set_cursor_visible
(TextView), set_lower/set_page_increment (ScrollBar), set_value
(clamped), set_active, set_text (canonical caption prop).

model.py: caption seeding now uses the table's canonical prop
(label for Button/CheckBox, text for Entry); kinds with neither
get no seed.

tests: test_props.py (generic set on all 547 kinds, SS2.1-SS2.10
lists, validation, live-apply/dirty, setters, model-only flags)
and test_draw.py (headless Cairo smoke: every kind draws without
crashing and paints something; prop-driven branches). Five Phase 1
tests updated to the Phase 2 set_prop contract (label, not text,
for Button/Label; no arbitrary props). Full suite: 107 tests OK.
2026-12-01 02:42:21 +00:00

207 lines
8.1 KiB
Python

"""Headless render smoke tests (Phase 2).
Every palette kind is drawn to an offscreen Cairo surface to catch
draw crashes, plus targeted draws for the prop-driven branches
(password entries, combos, checklists, scrollbars, notebooks,
calendars, shapes, progress bars, trees, list views, menu buttons).
No display is needed: cairo.ImageSurface only. Recognisability is
verified visually under Xvfb (showcase screenshots), not here.
"""
import unittest
import cairo
from lightgui.xframe import core
from lightgui.xframe import palette
def _draw(widget, width=None, height=None):
"""Draw *widget* on a fresh white surface; return the surface."""
surface = cairo.ImageSurface(
cairo.FORMAT_ARGB32,
max(widget.width + 4, width or 0),
max(widget.height + 4, height or 0))
cr = cairo.Context(surface)
cr.set_source_rgb(1, 1, 1)
cr.paint()
widget.draw(cr)
return surface
def _non_white_pixels(surface):
data = surface.get_data()
stride = surface.get_stride()
count = 0
for y in range(surface.get_height()):
row = data[y * stride:(y + 1) * stride]
for x in range(0, len(row), 4):
b, g, r = row[x], row[x + 1], row[x + 2]
if (r, g, b) != (255, 255, 255):
count += 1
return count
class RenderSmokeTests(unittest.TestCase):
def test_every_kind_draws_without_crashing(self):
for entry in palette.PALETTE:
widget = core.create(entry.name, entry.name + "_001")
self.assertIsNotNone(widget, entry.name)
try:
_draw(widget)
except Exception as exc: # noqa: BLE001 - report the kind
self.fail("%s draw crashed: %r" % (entry.name, exc))
def test_every_kind_draws_something(self):
# A visible widget must paint at least one non-white pixel
# (its border, text, or fill) at its default size.
blank = []
for entry in palette.PALETTE:
widget = core.create(entry.name, entry.name + "_001")
surface = _draw(widget)
if _non_white_pixels(surface) == 0:
blank.append(entry.name)
self.assertFalse(blank, "kinds drawing nothing: %s" % blank[:10])
def test_invisible_widget_draws_nothing(self):
widget = core.create("GtkButton", "GtkButton_001")
widget.set_prop("visible", "false")
surface = _draw(widget)
self.assertEqual(_non_white_pixels(surface), 0)
def test_disabled_widget_still_draws(self):
widget = core.create("GtkButton", "GtkButton_001")
widget.set_prop("enabled", "false")
surface = _draw(widget)
self.assertGreater(_non_white_pixels(surface), 0)
class PropDrivenDrawTests(unittest.TestCase):
def _widget(self, kind, width=200, height=120, **props):
widget = core.create(kind, kind + "_001", 0, 0, width, height)
for name, value in props.items():
widget.set_prop(name, value)
return widget
def test_password_entry_masks_text(self):
entry = self._widget("GtkEntry", text="secret", password=True)
_draw(entry)
entry.set_prop("show-password", "true")
_draw(entry)
def test_combo_with_items_and_styles(self):
combo = self._widget("GtkComboBox", items="Red|Green|Blue",
active=1, text="Green")
_draw(combo)
combo.set_prop("style", "Simple")
_draw(combo)
def test_checklistbox_checked_rows(self):
check = self._widget("TCheckListBox", width=200, height=120,
items="A|B|C")
check.set_checked_items(["A", "C"])
_draw(check)
def test_scrollbar_orientations(self):
for orientation in (0, 1):
bar = self._widget("GtkScrollbar", width=200, height=30,
**{"value": 30, "lower": 0, "upper": 100,
"page-size": 20,
"orientation": orientation})
_draw(bar)
def test_scale_variants(self):
for kind, extra in (
("GtkScale", {"value": 70, "tick-marks": 3,
"frequency": 10, "draw-value": True}),
("TTrackBar", {"orientation": 1, "value": 40}),
("GtkProgressBar", {"fraction": 0.45,
"show-text": True}),
("GtkSpinButton", {"value": 42, "digits": 2}),
("GtkLevelBar", {"value": 80})):
_draw(self._widget(kind, width=200, height=40, **extra))
def test_notebook_and_calendar(self):
notebook = self._widget("GtkNotebook", width=260, height=180,
pages="One|Two|Three", page=1,
**{"tab-position": 0})
_draw(notebook)
notebook.set_prop("tab-position", "Bottom")
_draw(notebook)
calendar = self._widget("GtkCalendar", width=220, height=180,
date="2026-11-30",
**{"show-week-numbers": True})
_draw(calendar)
calendar.set_prop("show-heading", "false")
_draw(calendar)
def test_tree_and_list_views(self):
tree = self._widget("GtkTreeView", width=260, height=180,
items="Root/Child 1;Root/Child 2;Other",
**{"grid-lines": True, "item-index": 1})
_draw(tree)
view = self._widget("GtkListView", width=260, height=180,
columns="Name|Value",
items="Row 1,Value 1;Row 2,Value 2",
**{"view-style": 0, "grid-lines": True})
_draw(view)
view.set_prop("view-style", "Icon")
_draw(view)
def test_shapes(self):
for shape in range(10):
widget = self._widget("TShape", width=120, height=90,
shape=shape)
_draw(widget)
def test_menubutton_directions(self):
for direction in ("Up", "Down", "Left", "Right", "None"):
button = self._widget("GtkMenuButton", direction=direction)
_draw(button)
def test_textview_wrap_and_scrollbars(self):
view = self._widget("GtkTextView", width=260, height=180,
text="First line\nSecond line of text here",
**{"wrap-mode": "Word",
"scroll-bars": "Both",
"cursor-visible": True})
_draw(view)
view.set_prop("wrap-mode", "None")
view.set_cursor_visible(False)
_draw(view)
def test_buttons_and_toggles(self):
for kind, extra in (
("GtkButton", {"default": True}),
("GtkLinkButton", {}),
("GtkColorButton", {"color": "rgb(200,30,30)"}),
("GtkLockButton", {}),
("GtkCheckButton", {"active": True}),
("GtkCheckButton", {"inconsistent": True,
"allow-grayed": True}),
("GtkRadioButton", {"active": True}),
("GtkSwitch", {"active": True}),
("GtkToggleButton", {"active": True, "flat": True}),
("GtkLabel", {"xalign": 1, "wrap": True}),
("GtkImage", {"file": "photo.png"}),
("GtkStatusbar", {"text": "Ready",
"size-grip": True}),
("GtkStatusbar", {"panels": "A|B|C"}),
("GtkFrame", {"label": "Group", "label-xalign": 0.5}),
("GtkDialog", {"title": "Confirm"})):
_draw(self._widget(kind, **extra))
def test_background_border_and_font_props_change_pixels(self):
plain = self._widget("GtkButton")
styled = self._widget("GtkButton", background="#336699",
foreground="#ffffff",
**{"font-size": 18, "font-bold": True,
"border-radius": 10})
self.assertNotEqual(
_non_white_pixels(_draw(plain)),
_non_white_pixels(_draw(styled)))
if __name__ == "__main__":
unittest.main()