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.
157 lines
6.8 KiB
Python
157 lines
6.8 KiB
Python
"""Tests for lightgui.model: project model and .fdj round-trip."""
|
|
|
|
import json
|
|
import os
|
|
import tempfile
|
|
import unittest
|
|
|
|
from lightgui.model import Project
|
|
|
|
|
|
class ProjectModelTests(unittest.TestCase):
|
|
def test_add_assigns_unique_names(self):
|
|
project = Project()
|
|
first = project.add_widget("GtkButton", 10, 10)
|
|
second = project.add_widget("GtkButton", 20, 20)
|
|
label = project.add_widget("GtkLabel", 30, 30)
|
|
self.assertEqual(first.name, "GtkButton_001")
|
|
self.assertEqual(second.name, "GtkButton_002")
|
|
self.assertEqual(label.name, "GtkLabel_001")
|
|
self.assertEqual(len(project.widgets), 3)
|
|
|
|
def test_add_unknown_kind_returns_none(self):
|
|
project = Project()
|
|
self.assertIsNone(project.add_widget("GtkNoSuchWidget", 0, 0))
|
|
self.assertEqual(project.widgets, [])
|
|
|
|
def test_add_selects_and_seeds_text(self):
|
|
project = Project()
|
|
widget = project.add_widget("GtkButton", 10, 10)
|
|
self.assertIs(project.selected, widget)
|
|
# Phase 2: a Button's caption prop is "label" (SS2.1).
|
|
self.assertEqual(widget.get_prop("label"), "GtkButton_001")
|
|
self.assertEqual(widget.text, "GtkButton_001")
|
|
|
|
def test_remove_clears_selection(self):
|
|
project = Project()
|
|
widget = project.add_widget("GtkButton", 10, 10)
|
|
self.assertTrue(project.remove_widget(widget.id))
|
|
self.assertEqual(project.widgets, [])
|
|
self.assertIsNone(project.selected)
|
|
self.assertFalse(project.remove_widget(widget.id))
|
|
|
|
def test_move_and_resize(self):
|
|
project = Project()
|
|
widget = project.add_widget("GtkButton", 10, 10)
|
|
self.assertTrue(project.move_widget(widget.id, 50, 60))
|
|
self.assertEqual((widget.x, widget.y), (50, 60))
|
|
self.assertTrue(project.resize_widget(widget.id, 200, 40))
|
|
self.assertEqual((widget.width, widget.height), (200, 40))
|
|
self.assertFalse(project.move_widget(999, 0, 0))
|
|
|
|
def test_hit_test_topmost(self):
|
|
project = Project()
|
|
bottom = project.add_widget("GtkButton", 10, 10, 100, 100)
|
|
top = project.add_widget("GtkLabel", 50, 50, 100, 100)
|
|
self.assertIs(project.hit_test(60, 60), top)
|
|
self.assertIs(project.hit_test(15, 15), bottom)
|
|
self.assertIsNone(project.hit_test(500, 500))
|
|
|
|
|
|
class FdjTests(unittest.TestCase):
|
|
def test_round_trip(self):
|
|
project = Project()
|
|
project.title = "My App"
|
|
project.add_widget("GtkButton", 10, 20)
|
|
project.add_widget("GtkLabel", 200, 30, 180, 30)
|
|
project.widgets[1].set_prop("label", "Hello")
|
|
project.widgets[1].signals.append("clicked")
|
|
loaded = Project.load_text(project.save_text())
|
|
self.assertEqual(loaded.title, "My App")
|
|
self.assertEqual(loaded.class_name, "MyWindow")
|
|
self.assertEqual((loaded.win_width, loaded.win_height), (800, 600))
|
|
self.assertEqual(len(loaded.widgets), 2)
|
|
self.assertEqual(loaded.widgets[0].name, "GtkButton_001")
|
|
self.assertEqual(loaded.widgets[1].get_prop("label"), "Hello")
|
|
self.assertEqual(loaded.widgets[1].signals, ["clicked"])
|
|
self.assertEqual(loaded.save_text(), project.save_text())
|
|
|
|
def test_empty_project_bytes(self):
|
|
text = Project().save_text()
|
|
self.assertEqual(text, '{\n "format": "lightgui",\n "version": 1,\n'
|
|
' "window": {\n "title": "Main Window",\n'
|
|
' "class_name": "MyWindow",\n "width": 800,\n'
|
|
' "height": 600,\n "background": "",\n'
|
|
' "menubar": "",\n "border_style": 0,\n'
|
|
' "icon": "",\n "position": 0,\n'
|
|
' "window_state": 0\n },\n "widgets": []\n}\n')
|
|
self.assertEqual(Project.load_text(text).save_text(), text)
|
|
|
|
def test_load_is_valid_json_with_sorted_props(self):
|
|
project = Project()
|
|
widget = project.add_widget("GtkButton", 0, 0)
|
|
# Phase 2: set_prop only accepts table props; the saved
|
|
# props dict is still sorted by key (seeded "label" included).
|
|
widget.set_prop("glyph", "1")
|
|
widget.set_prop("foreground", "red")
|
|
data = json.loads(project.save_text())
|
|
self.assertEqual(list(data["widgets"][0]["props"]),
|
|
["foreground", "glyph", "label"])
|
|
|
|
def test_load_rejects_bad_format_and_version(self):
|
|
with self.assertRaises(ValueError):
|
|
Project.load_text('{"format": "other", "version": 1}')
|
|
with self.assertRaises(ValueError):
|
|
Project.load_text('{"format": "lightgui", "version": 2}')
|
|
with self.assertRaises(ValueError):
|
|
Project.load_text("not json")
|
|
|
|
def test_load_clamps_and_skips(self):
|
|
text = ('{"format": "lightgui", "version": 1, "window": '
|
|
'{"width": 5, "height": 99999, "border_style": 9}, '
|
|
'"widgets": [{"name": "W1", "type": "GtkButton", '
|
|
'"x": 0, "y": 0, "width": 3, "height": 2}, '
|
|
'{"name": "W2", "type": "LegacyButton", '
|
|
'"x": 0, "y": 0, "width": 100, "height": 30}, '
|
|
'{"name": "", "type": "GtkButton"}]}')
|
|
loaded = Project.load_text(text)
|
|
self.assertEqual((loaded.win_width, loaded.win_height), (800, 600))
|
|
self.assertEqual(loaded.border_style, 0)
|
|
self.assertEqual(len(loaded.widgets), 1)
|
|
self.assertEqual((loaded.widgets[0].width,
|
|
loaded.widgets[0].height), (10, 10))
|
|
self.assertEqual(loaded.skipped, ["LegacyButton"])
|
|
|
|
def test_name_counter_survives_load(self):
|
|
project = Project()
|
|
project.add_widget("GtkButton", 0, 0)
|
|
loaded = Project.load_text(project.save_text())
|
|
widget = loaded.add_widget("GtkButton", 0, 0)
|
|
self.assertEqual(widget.name, "GtkButton_002")
|
|
|
|
def test_save_and_load_file(self):
|
|
project = Project()
|
|
project.add_widget("GtkButton", 10, 10)
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
path = os.path.join(tmp, "test.fdj")
|
|
project.save_file(path)
|
|
loaded = Project.load_file(path)
|
|
self.assertEqual(len(loaded.widgets), 1)
|
|
self.assertEqual(loaded.widgets[0].name, "GtkButton_001")
|
|
|
|
def test_load_missing_file(self):
|
|
with self.assertRaises(ValueError):
|
|
Project.load_file("/nonexistent/dir/test.fdj")
|
|
|
|
def test_string_escaping_round_trip(self):
|
|
project = Project()
|
|
widget = project.add_widget("GtkLabel", 0, 0)
|
|
widget.set_prop("label", 'quote " backslash \\ newline\n tab\t')
|
|
loaded = Project.load_text(project.save_text())
|
|
self.assertEqual(loaded.widgets[0].get_prop("label"),
|
|
'quote " backslash \\ newline\n tab\t')
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|