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.
128 lines
5.3 KiB
Python
128 lines
5.3 KiB
Python
"""Tests for lightgui.xframe.core: registry, hit-testing, handles."""
|
|
|
|
import unittest
|
|
|
|
from lightgui.xframe import core
|
|
from lightgui.xframe import palette
|
|
|
|
|
|
class RegistryTests(unittest.TestCase):
|
|
def test_all_palette_kinds_resolve(self):
|
|
for entry in palette.PALETTE:
|
|
cls = core.lookup(entry.name)
|
|
self.assertIsNotNone(cls, entry.name)
|
|
self.assertEqual(cls.ARCHETYPE, entry.archetype)
|
|
|
|
def test_unknown_kind_is_none(self):
|
|
self.assertIsNone(core.lookup("GtkNoSuchWidget"))
|
|
self.assertIsNone(core.create("GtkNoSuchWidget", "X_001"))
|
|
self.assertFalse(core.is_known_kind("GtkNoSuchWidget"))
|
|
|
|
def test_create_uses_registry_default_size(self):
|
|
widget = core.create("GtkButton", "GtkButton_001", 10, 20)
|
|
self.assertEqual((widget.width, widget.height), (150, 36))
|
|
self.assertEqual(widget.archetype, "Button")
|
|
widget = core.create("GtkAboutDialog", "GtkAboutDialog_001")
|
|
self.assertEqual((widget.width, widget.height), (300, 220))
|
|
|
|
def test_create_explicit_size(self):
|
|
widget = core.create("GtkButton", "B", 0, 0, 200, 50)
|
|
self.assertEqual((widget.width, widget.height), (200, 50))
|
|
|
|
def test_archetype_classes_cover_all_archetypes(self):
|
|
archetypes = {e.archetype for e in palette.PALETTE}
|
|
self.assertEqual(archetypes, set(core.ARCHETYPE_CLASSES))
|
|
|
|
|
|
class WidgetStateTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.widget = core.create("GtkButton", "GtkButton_001", 100, 80,
|
|
150, 36)
|
|
|
|
def test_setters_are_state_only(self):
|
|
self.widget.set_bounds(5, 6, 200, 40)
|
|
self.assertEqual((self.widget.x, self.widget.y,
|
|
self.widget.width, self.widget.height),
|
|
(5, 6, 200, 40))
|
|
# Phase 2: set_prop validates against the property table;
|
|
# a Button's caption prop is "label" (proposal SS2.1).
|
|
self.widget.set_prop("label", "Hello")
|
|
self.assertEqual(self.widget.text, "Hello")
|
|
self.assertEqual(self.widget.get_prop("label"), "Hello")
|
|
|
|
def test_set_bounds_clamps(self):
|
|
self.widget.set_bounds(-10, -5, 5, 3)
|
|
self.assertEqual((self.widget.x, self.widget.y), (0, 0))
|
|
self.assertEqual((self.widget.width, self.widget.height),
|
|
(core.MIN_WIDTH, core.MIN_HEIGHT))
|
|
|
|
def test_text_defaults_to_instance_name(self):
|
|
widget = core.create("GtkButton", "GtkButton_001")
|
|
self.assertEqual(widget.text, "GtkButton_001")
|
|
|
|
def test_contains(self):
|
|
self.assertTrue(self.widget.contains(100, 80))
|
|
self.assertTrue(self.widget.contains(250, 116))
|
|
self.assertFalse(self.widget.contains(99, 80))
|
|
self.assertFalse(self.widget.contains(251, 80))
|
|
self.assertFalse(self.widget.contains(100, 117))
|
|
|
|
|
|
class HandleGeometryTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.widget = core.create("GtkButton", "GtkButton_001", 100, 80,
|
|
150, 36)
|
|
self.widget.handles_on = True
|
|
|
|
def test_handle_size_is_13(self):
|
|
self.assertEqual(core.HANDLE_SIZE, 13)
|
|
self.assertEqual(core.HANDLE_HIT_PAD, 8)
|
|
|
|
def test_handle_centres(self):
|
|
# Corners on the widget corners, edges on the midpoints.
|
|
self.assertEqual(self.widget.handle_centre(0), (100, 80)) # NW
|
|
self.assertEqual(self.widget.handle_centre(2), (250, 80)) # NE
|
|
self.assertEqual(self.widget.handle_centre(4), (250, 116)) # SE
|
|
self.assertEqual(self.widget.handle_centre(6), (100, 116)) # SW
|
|
self.assertEqual(self.widget.handle_centre(1), (175, 80)) # N
|
|
self.assertEqual(self.widget.handle_centre(3), (250, 98)) # E
|
|
self.assertEqual(self.widget.handle_centre(5), (175, 116)) # S
|
|
self.assertEqual(self.widget.handle_centre(7), (100, 98)) # W
|
|
|
|
def test_handle_rect_is_13x13_centred(self):
|
|
x, y, w, h = self.widget.handle_rect(0)
|
|
self.assertEqual((w, h), (13, 13))
|
|
self.assertEqual((x, y), (100 - 6, 80 - 6))
|
|
|
|
def test_handle_at_with_pad(self):
|
|
# 8px pad: a point 8px outside the 13px square still grabs.
|
|
self.assertEqual(self.widget.handle_at(100 - 6 - 8, 80), 0)
|
|
self.assertEqual(self.widget.handle_at(100 - 6 - 9, 80), -1)
|
|
self.assertEqual(self.widget.handle_at(250, 98), 3)
|
|
self.assertEqual(self.widget.handle_at(175, 116), 5)
|
|
self.assertEqual(self.widget.handle_at(175, 100), -1)
|
|
|
|
def test_handle_at_off_when_handles_off(self):
|
|
self.widget.handles_on = False
|
|
self.assertEqual(self.widget.handle_at(100, 80), -1)
|
|
|
|
def test_apply_handle_drag_se(self):
|
|
self.widget.apply_handle_drag(4, 10, 5)
|
|
self.assertEqual((self.widget.width, self.widget.height),
|
|
(160, 41))
|
|
|
|
def test_apply_handle_drag_nw_moves_origin(self):
|
|
self.widget.apply_handle_drag(0, 10, 5)
|
|
self.assertEqual((self.widget.x, self.widget.y,
|
|
self.widget.width, self.widget.height),
|
|
(110, 85, 140, 31))
|
|
|
|
def test_apply_handle_drag_clamps_minimum(self):
|
|
self.widget.apply_handle_drag(4, -500, -500)
|
|
self.assertEqual((self.widget.width, self.widget.height),
|
|
(core.MIN_WIDTH, core.MIN_HEIGHT))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|