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.
This commit is contained in:
1 parent
eeccee6c9a
commit
7717194cbd
9 files changed
+2537
-155
No files matched your search
+7
-1
@@ -98,10 +98,16 @@ class Project:
|
||||
self._next_id += 1
|
||||
self.widgets.append(widget)
|
||||
# Seed label/text to the instance name (Rust seed_defaults).
|
||||
# The canonical caption prop differs per table ("text" for
|
||||
# Button/Entry, "label" for CheckBox/RadioButton); kinds with
|
||||
# neither (Calendar, Notebook, Window, ...) get no seed.
|
||||
if widget.archetype in ("Button", "Label", "Toggle", "Entry",
|
||||
"Container", "Dialog", "Display"):
|
||||
if "text" not in widget.props and "label" not in widget.props:
|
||||
widget.set_prop("text", name)
|
||||
text_prop = ("text" if "text" in widget.prop_names()
|
||||
else "label")
|
||||
if text_prop in widget.prop_names():
|
||||
widget.set_prop(text_prop, name)
|
||||
self.select(widget.id)
|
||||
return widget
|
||||
|
||||
|
||||
+4
-4
@@ -36,10 +36,10 @@ EVENTS = ("OnClick", "OnChange", "OnKeyPress", "OnKeyDown", "OnKeyUp", "OnMouseD
|
||||
|
||||
GENERIC_PROPS = (
|
||||
PropDef("name", "Str", "", 0, 0, (), (), "Name", False),
|
||||
PropDef("left", "Int", "0", 0, 100000, (), (), "Left", False),
|
||||
PropDef("top", "Int", "0", 0, 100000, (), (), "Top", False),
|
||||
PropDef("width", "Int", "100", 10, 100000, (), (), "Width", False),
|
||||
PropDef("height", "Int", "30", 10, 100000, (), (), "Height", False),
|
||||
PropDef("left", "Int", "0", 0, 0, (), (), "Left", False),
|
||||
PropDef("top", "Int", "0", 0, 0, (), (), "Top", False),
|
||||
PropDef("width", "Int", "100", 0, 0, (), (), "Width", False),
|
||||
PropDef("height", "Int", "30", 0, 0, (), (), "Height", False),
|
||||
PropDef("background", "Color", "", 0, 0, (), (), "Color", False),
|
||||
PropDef("foreground", "Color", "", 0, 0, (), (), "Font.Color", False),
|
||||
PropDef("border-width", "Int", "0", 0, 100, (), (), "Border.Width", False),
|
||||
|
||||
@@ -102,9 +102,16 @@ class CanvasState:
|
||||
self._changed()
|
||||
|
||||
# --- placement / mutation ----------------------------------------------
|
||||
def _wire(self, widget):
|
||||
# Live-apply: a widget prop change notifies the canvas, which
|
||||
# redraws (Widget.set_prop is state-only; the host redraws).
|
||||
if widget is not None:
|
||||
widget.on_change = self._changed
|
||||
|
||||
def place(self, kind, x, y, width=None, height=None):
|
||||
widget = self.project.add_widget(kind, x, y, width, height)
|
||||
if widget is not None:
|
||||
self._wire(widget)
|
||||
self.handles_on = False
|
||||
self._changed()
|
||||
return widget
|
||||
@@ -172,6 +179,7 @@ class CanvasState:
|
||||
max(x - self._pending_default_w() // 2, 0),
|
||||
max(y - self._pending_default_h() // 2, 0))
|
||||
if placed is not None:
|
||||
self._wire(placed)
|
||||
self.handles_on = False
|
||||
self._changed()
|
||||
return True
|
||||
@@ -249,6 +257,8 @@ class Canvas(Gtk.DrawingArea):
|
||||
super().__init__()
|
||||
self.state = state if state is not None else CanvasState(project)
|
||||
self.state.on_change = self._on_state_change
|
||||
for widget in self.state.project.widgets:
|
||||
self.state._wire(widget)
|
||||
self.set_hexpand(True)
|
||||
self.set_vexpand(True)
|
||||
self.set_can_focus(True)
|
||||
@@ -336,6 +346,7 @@ class Canvas(Gtk.DrawingArea):
|
||||
cr.restore()
|
||||
for widget in self.state.project.widgets:
|
||||
widget.draw(cr)
|
||||
widget.dirty = False
|
||||
selected = self.state.selected
|
||||
if selected is not None:
|
||||
cr.save()
|
||||
|
||||
+1961
-134
File diff suppressed because it is too large.
Load diff
+4
-2
@@ -44,9 +44,11 @@ class WidgetStateTests(unittest.TestCase):
|
||||
self.assertEqual((self.widget.x, self.widget.y,
|
||||
self.widget.width, self.widget.height),
|
||||
(5, 6, 200, 40))
|
||||
self.widget.set_prop("text", "Hello")
|
||||
# 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("text"), "Hello")
|
||||
self.assertEqual(self.widget.get_prop("label"), "Hello")
|
||||
|
||||
def test_set_bounds_clamps(self):
|
||||
self.widget.set_bounds(-10, -5, 5, 3)
|
||||
|
||||
@@ -0,0 +1,206 @@
|
||||
"""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()
|
||||
+13
-8
@@ -28,7 +28,9 @@ class ProjectModelTests(unittest.TestCase):
|
||||
project = Project()
|
||||
widget = project.add_widget("GtkButton", 10, 10)
|
||||
self.assertIs(project.selected, widget)
|
||||
self.assertEqual(widget.get_prop("text"), "GtkButton_001")
|
||||
# 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()
|
||||
@@ -62,7 +64,7 @@ class FdjTests(unittest.TestCase):
|
||||
project.title = "My App"
|
||||
project.add_widget("GtkButton", 10, 20)
|
||||
project.add_widget("GtkLabel", 200, 30, 180, 30)
|
||||
project.widgets[1].set_prop("text", "Hello")
|
||||
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")
|
||||
@@ -70,7 +72,7 @@ class FdjTests(unittest.TestCase):
|
||||
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("text"), "Hello")
|
||||
self.assertEqual(loaded.widgets[1].get_prop("label"), "Hello")
|
||||
self.assertEqual(loaded.widgets[1].signals, ["clicked"])
|
||||
self.assertEqual(loaded.save_text(), project.save_text())
|
||||
|
||||
@@ -88,10 +90,13 @@ class FdjTests(unittest.TestCase):
|
||||
def test_load_is_valid_json_with_sorted_props(self):
|
||||
project = Project()
|
||||
widget = project.add_widget("GtkButton", 0, 0)
|
||||
widget.set_prop("zeta", "1")
|
||||
widget.set_prop("alpha", "2")
|
||||
# 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"]), ["alpha", "text", "zeta"])
|
||||
self.assertEqual(list(data["widgets"][0]["props"]),
|
||||
["foreground", "glyph", "label"])
|
||||
|
||||
def test_load_rejects_bad_format_and_version(self):
|
||||
with self.assertRaises(ValueError):
|
||||
@@ -141,9 +146,9 @@ class FdjTests(unittest.TestCase):
|
||||
def test_string_escaping_round_trip(self):
|
||||
project = Project()
|
||||
widget = project.add_widget("GtkLabel", 0, 0)
|
||||
widget.set_prop("text", 'quote " backslash \\ newline\n tab\t')
|
||||
widget.set_prop("label", 'quote " backslash \\ newline\n tab\t')
|
||||
loaded = Project.load_text(project.save_text())
|
||||
self.assertEqual(loaded.widgets[0].get_prop("text"),
|
||||
self.assertEqual(loaded.widgets[0].get_prop("label"),
|
||||
'quote " backslash \\ newline\n tab\t')
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,325 @@
|
||||
"""Tests for lightgui.props and Phase 2 live-apply plumbing.
|
||||
|
||||
Covers: every one of the 547 kinds has a property table with the
|
||||
generic set present; per-widget tables match the proposal SS2.1-SS2.10
|
||||
lists; set_prop validation rejects unknown props; live-apply changes
|
||||
widget state and marks the canvas dirty; set_items / password /
|
||||
direction / cursor-visible behaviour; model-only props are flagged.
|
||||
"""
|
||||
|
||||
import unittest
|
||||
|
||||
from lightgui import props
|
||||
from lightgui.xframe import core
|
||||
from lightgui.xframe import palette
|
||||
from lightgui.xframe.canvas import CanvasState
|
||||
|
||||
# Proposal SS1 generic set (internal C names): Name, geometry, Color,
|
||||
# Font (+ sub-props), Border (+ sub-props), Enabled, Visible, Hint,
|
||||
# ShowHint, Cursor, TabOrder, TabStop, Constraints, BorderSpacing,
|
||||
# AutoSize, Tag, Align, Anchors.
|
||||
GENERIC_REQUIRED = {
|
||||
"name", "left", "top", "width", "height",
|
||||
"background", "foreground",
|
||||
"font-name", "font-size", "font-bold", "font-italic",
|
||||
"font-underline", "font-strikeout",
|
||||
"border-width", "border-color", "border-radius",
|
||||
"enabled", "visible", "hint", "show-hint", "cursor",
|
||||
"tab-order", "tab-stop",
|
||||
"min-width", "min-height", "max-width", "max-height",
|
||||
"margin-left", "margin-top", "margin-right", "margin-bottom",
|
||||
"margin-around", "autosize", "tag", "align",
|
||||
"anchor-top", "anchor-left", "anchor-right", "anchor-bottom",
|
||||
}
|
||||
|
||||
# Proposal SS2.1-SS2.10 per-widget lists (internal C names).
|
||||
PER_WIDGET = {
|
||||
"Button": ["label", "default", "cancel", "kind", "layout", "glyph",
|
||||
"modal-result"],
|
||||
"Entry": ["text", "editable", "max-length", "placeholder-text",
|
||||
"password", "alignment", "char-case", "numbers-only",
|
||||
"hide-selection", "auto-select"],
|
||||
"Label": ["label", "xalign", "yalign", "wrap", "selectable",
|
||||
"transparent", "focus-control", "show-accel-char"],
|
||||
"CheckBox": ["label", "active", "inconsistent", "allow-grayed",
|
||||
"alignment", "value-checked", "value-unchecked"],
|
||||
"RadioButton": ["label", "active", "group"],
|
||||
"ComboBox": ["items", "active", "text", "style", "drop-down-count",
|
||||
"drop-down-width", "sorted", "read-only", "max-length"],
|
||||
"ListBox": ["items", "selection-mode", "item-index", "sorted",
|
||||
"columns", "item-height", "integral-height"],
|
||||
"CheckListBox": ["items", "checked-items", "item-index",
|
||||
"item-height", "sorted"],
|
||||
"ScrollBar": ["value", "lower", "upper", "step-increment",
|
||||
"page-increment", "page-size", "orientation",
|
||||
"large-change"],
|
||||
"TrackBar": ["value", "minimum", "maximum", "step", "page-step",
|
||||
"draw-value", "tick-marks", "frequency"],
|
||||
"TextView": ["text", "editable", "cursor-visible", "wrap-mode",
|
||||
"max-length", "scroll-bars", "want-tabs",
|
||||
"want-returns", "hide-selection", "alignment"],
|
||||
"Memo": ["text", "editable", "cursor-visible", "wrap-mode",
|
||||
"max-length", "scroll-bars", "want-tabs", "want-returns",
|
||||
"hide-selection", "alignment"],
|
||||
}
|
||||
|
||||
|
||||
class GenericSetTests(unittest.TestCase):
|
||||
def test_every_kind_has_generic_set(self):
|
||||
self.assertEqual(len(palette.PALETTE), 547)
|
||||
for entry in palette.PALETTE:
|
||||
names = props.prop_names(entry.name)
|
||||
missing = GENERIC_REQUIRED - names
|
||||
self.assertFalse(missing,
|
||||
"%s missing generic props: %s"
|
||||
% (entry.name, sorted(missing)))
|
||||
|
||||
def test_generic_set_is_same_for_all_kinds(self):
|
||||
for entry in palette.PALETTE:
|
||||
self.assertEqual(props.GENERIC_NAMES,
|
||||
props.GENERIC_NAMES
|
||||
& props.prop_names(entry.name))
|
||||
|
||||
def test_registry_names_resolve_like_kinds(self):
|
||||
self.assertEqual(props.props_for("GtkButton"),
|
||||
props.props_for("Button"))
|
||||
self.assertEqual(props.props_for("TCheckBox"),
|
||||
props.props_for("CheckBox"))
|
||||
self.assertEqual(props.props_for("GtkNoSuchWidget"),
|
||||
tuple(props.GENERIC_PROPS))
|
||||
|
||||
def test_stock_and_instrument_kinds_generic_only_for_now(self):
|
||||
# Phase 6 adds their tables; Phase 2 gives them the generic
|
||||
# set only (plus nothing custom).
|
||||
self.assertEqual(props.custom_props_for("IndRSI"), ())
|
||||
self.assertEqual(props.custom_props_for("InstLedRed"), ())
|
||||
self.assertEqual(props.custom_props_for("StockLineChart"), ())
|
||||
|
||||
|
||||
class PerWidgetTests(unittest.TestCase):
|
||||
def test_proposal_sections_2_1_to_2_10(self):
|
||||
for kind, required in PER_WIDGET.items():
|
||||
names = props.prop_names(kind)
|
||||
for name in required:
|
||||
self.assertIn(name, names,
|
||||
"%s table missing %s" % (kind, name))
|
||||
|
||||
def test_button_kind_enum_labels_match_c(self):
|
||||
prop = props.find_prop("Button", "kind")
|
||||
self.assertEqual(prop.enum_labels,
|
||||
("None", "OK", "Cancel", "Yes", "No", "Close",
|
||||
"Help", "Retry", "Ignore", "Abort"))
|
||||
self.assertEqual(prop.enum_values,
|
||||
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9))
|
||||
|
||||
def test_events_use_lazarus_names(self):
|
||||
for event in ("OnClick", "OnChange", "OnKeyPress", "OnMouseDown",
|
||||
"OnMouseUp", "OnMouseMove", "OnDblClick"):
|
||||
self.assertIn(event, props.EVENTS)
|
||||
|
||||
|
||||
class ModelOnlyTests(unittest.TestCase):
|
||||
def test_model_only_props_flagged(self):
|
||||
for name in ("font-underline", "font-strikeout", "cursor",
|
||||
"tab-order", "tab-stop", "tag", "align",
|
||||
"anchor-top", "hint", "show-hint", "glyph",
|
||||
"autosize", "margin-left"):
|
||||
self.assertTrue(props.is_model_only("GtkButton", name),
|
||||
name)
|
||||
|
||||
def test_live_props_not_model_only(self):
|
||||
for name in ("background", "foreground", "border-width",
|
||||
"font-name", "font-size", "font-bold", "enabled",
|
||||
"visible", "label", "value", "items", "active",
|
||||
"password", "direction", "cursor-visible",
|
||||
"lower", "page-increment"):
|
||||
kind = ("GtkScrollbar" if name in ("lower", "page-increment")
|
||||
else "GtkMenuButton" if name == "direction"
|
||||
else "GtkTextView" if name == "cursor-visible"
|
||||
else "GtkEntry" if name == "password"
|
||||
else "GtkButton")
|
||||
self.assertFalse(props.is_model_only(kind, name), name)
|
||||
|
||||
def test_register_custom_table_hook(self):
|
||||
# Phase 6 hook: a Stock kind gains a custom table, then the
|
||||
# hook is undone so other tests see the Phase 2 state.
|
||||
try:
|
||||
props.register_custom_table(
|
||||
"IndRSI", (props.PropDef(
|
||||
"period", "Int", "14", 1, 100, (), (),
|
||||
"Period", False),))
|
||||
self.assertIn("period", props.prop_names("IndRSI"))
|
||||
finally:
|
||||
props.register_custom_table("IndRSI", ())
|
||||
|
||||
|
||||
class ValidateTests(unittest.TestCase):
|
||||
def test_unknown_prop_rejected(self):
|
||||
with self.assertRaises(ValueError):
|
||||
props.validate("GtkButton", "no-such-prop", "x")
|
||||
|
||||
def test_bool_canonicalisation(self):
|
||||
self.assertEqual(props.validate("GtkButton", "enabled", "yes"),
|
||||
"true")
|
||||
self.assertEqual(props.validate("GtkButton", "enabled", False),
|
||||
"false")
|
||||
with self.assertRaises(ValueError):
|
||||
props.validate("GtkButton", "enabled", "maybe")
|
||||
|
||||
def test_int_range(self):
|
||||
self.assertEqual(
|
||||
props.validate("GtkButton", "modal-result", 5), "5")
|
||||
with self.assertRaises(ValueError):
|
||||
props.validate("GtkButton", "modal-result", 500)
|
||||
|
||||
def test_enum_label_and_value(self):
|
||||
self.assertEqual(props.validate("GtkButton", "kind", "OK"), "1")
|
||||
self.assertEqual(props.validate("GtkButton", "kind", 2), "2")
|
||||
with self.assertRaises(ValueError):
|
||||
props.validate("GtkButton", "kind", "Nope")
|
||||
|
||||
def test_color_canonicalisation(self):
|
||||
self.assertEqual(
|
||||
props.validate("GtkButton", "background", "#FF0000"),
|
||||
"#ff0000")
|
||||
self.assertEqual(
|
||||
props.validate("GtkButton", "background", "rgb(1, 2, 3)"),
|
||||
"rgb(1,2,3)")
|
||||
with self.assertRaises(ValueError):
|
||||
props.validate("GtkButton", "background", "not-a-color")
|
||||
|
||||
|
||||
class LiveApplyTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.button = core.create("GtkButton", "GtkButton_001", 0, 0)
|
||||
|
||||
def test_set_prop_rejects_unknown(self):
|
||||
with self.assertRaises(ValueError):
|
||||
self.button.set_prop("no-such-prop", "x")
|
||||
with self.assertRaises(ValueError):
|
||||
self.button.set_prop("modal-result", 500)
|
||||
|
||||
def test_set_prop_updates_state_and_marks_dirty(self):
|
||||
self.assertFalse(self.button.dirty)
|
||||
self.button.set_prop("label", "Hello")
|
||||
self.assertEqual(self.button.get_prop("label"), "Hello")
|
||||
self.assertEqual(self.button.text, "Hello")
|
||||
self.assertTrue(self.button.dirty)
|
||||
|
||||
def test_set_prop_notifies_host(self):
|
||||
calls = []
|
||||
self.button.on_change = lambda: calls.append(1)
|
||||
self.button.set_prop("label", "Hi")
|
||||
self.assertEqual(calls, [1])
|
||||
|
||||
def test_set_prop_geometry_updates_fields(self):
|
||||
self.button.set_prop("left", 12)
|
||||
self.button.set_prop("top", 34)
|
||||
self.button.set_prop("width", 200)
|
||||
self.button.set_prop("height", 50)
|
||||
self.assertEqual((self.button.x, self.button.y,
|
||||
self.button.width, self.button.height),
|
||||
(12, 34, 200, 50))
|
||||
self.button.set_prop("left", -5)
|
||||
self.button.set_prop("width", 3)
|
||||
self.assertEqual(self.button.x, 0)
|
||||
self.assertEqual(self.button.width, 10)
|
||||
self.button.set_prop("name", "Renamed")
|
||||
self.assertEqual(self.button.name, "Renamed")
|
||||
|
||||
def test_canvas_state_wiring_marks_canvas_dirty(self):
|
||||
state = CanvasState()
|
||||
state.place("GtkButton", 0, 0)
|
||||
widget = state.project.widgets[0]
|
||||
calls = []
|
||||
state.on_change = lambda: calls.append(1)
|
||||
calls.clear()
|
||||
widget.set_prop("label", "Placed")
|
||||
self.assertTrue(calls, "canvas on_change not called")
|
||||
self.assertTrue(widget.dirty)
|
||||
|
||||
def test_set_text_uses_canonical_prop(self):
|
||||
checkbox = core.create("GtkCheckButton", "GtkCheckButton_001")
|
||||
checkbox.set_text("Tick me")
|
||||
self.assertEqual(checkbox.get_prop("label"), "Tick me")
|
||||
self.assertEqual(checkbox.text, "Tick me")
|
||||
|
||||
|
||||
class WidgetSetterTests(unittest.TestCase):
|
||||
def test_set_items_list_and_string(self):
|
||||
combo = core.create("GtkComboBox", "GtkComboBox_001")
|
||||
combo.set_items(["Red", "Green", "Blue"])
|
||||
self.assertEqual(combo.get_items(), ["Red", "Green", "Blue"])
|
||||
self.assertEqual(combo.get_prop("items"), "Red|Green|Blue")
|
||||
self.assertTrue(combo.dirty)
|
||||
combo.set_items("One|Two")
|
||||
self.assertEqual(combo.get_items(), ["One", "Two"])
|
||||
|
||||
def test_set_items_prunes_checked_items(self):
|
||||
check = core.create("TCheckListBox", "TCheckListBox_001")
|
||||
check.set_items(["A", "B", "C"])
|
||||
check.set_checked_items(["A", "C"])
|
||||
check.set_items(["A", "B"])
|
||||
self.assertEqual(check.get_checked_items(), ["A"])
|
||||
|
||||
def test_checklistbox_checked_state(self):
|
||||
check = core.create("TCheckListBox", "TCheckListBox_001")
|
||||
check.set_items(["A", "B", "C"])
|
||||
check.set_item_checked("B", True)
|
||||
self.assertTrue(check.is_item_checked("B"))
|
||||
self.assertEqual(check.get_checked_items(), ["B"])
|
||||
check.set_item_checked("B", False)
|
||||
self.assertFalse(check.is_item_checked("B"))
|
||||
# Items not in the list cannot be checked.
|
||||
check.set_checked_items(["Z", "A"])
|
||||
self.assertEqual(check.get_checked_items(), ["A"])
|
||||
|
||||
def test_entry_password_masked_by_default(self):
|
||||
entry = core.create("GtkEntry", "GtkEntry_001")
|
||||
entry.set_prop("text", "secret")
|
||||
self.assertFalse(entry.get_password())
|
||||
entry.set_password(True)
|
||||
self.assertTrue(entry.get_password())
|
||||
self.assertEqual(entry._display_text(), "******")
|
||||
entry.set_show_password(True)
|
||||
self.assertEqual(entry._display_text(), "secret")
|
||||
entry.set_show_password(False)
|
||||
self.assertEqual(entry._display_text(), "******")
|
||||
|
||||
def test_menubutton_direction(self):
|
||||
button = core.create("GtkMenuButton", "GtkMenuButton_001")
|
||||
self.assertEqual(button.get_direction(), 1) # Down default
|
||||
button.set_direction("Left")
|
||||
self.assertEqual(button.get_direction(), 2)
|
||||
self.assertEqual(button.get_prop("direction"), "2")
|
||||
button.set_direction(0)
|
||||
self.assertEqual(button.get_direction(), 0)
|
||||
|
||||
def test_textview_cursor_visible(self):
|
||||
view = core.create("GtkTextView", "GtkTextView_001")
|
||||
self.assertTrue(view.get_cursor_visible())
|
||||
view.set_cursor_visible(False)
|
||||
self.assertFalse(view.get_cursor_visible())
|
||||
self.assertEqual(view.get_prop("cursor-visible"), "false")
|
||||
|
||||
def test_scrollbar_lower_and_page_increment(self):
|
||||
bar = core.create("GtkScrollbar", "GtkScrollbar_001")
|
||||
bar.set_lower(-50)
|
||||
self.assertEqual(bar.get_lower(), -50.0)
|
||||
bar.set_page_increment(25)
|
||||
self.assertEqual(bar.get_page_increment(), 25.0)
|
||||
self.assertEqual(bar.get_prop("lower"), "-50")
|
||||
self.assertEqual(bar.get_prop("page-increment"), "25")
|
||||
|
||||
def test_range_value_clamped(self):
|
||||
scale = core.create("GtkScale", "GtkScale_001")
|
||||
scale.set_prop("minimum", "0")
|
||||
scale.set_prop("maximum", "100")
|
||||
scale.set_value(150)
|
||||
self.assertEqual(scale.get_value(), 100.0)
|
||||
scale.set_value(-10)
|
||||
self.assertEqual(scale.get_value(), 0.0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
+6
-6
@@ -501,13 +501,13 @@ def main():
|
||||
{"name": "name", "kind": "Str", "default": "", "lo": 0.0,
|
||||
"hi": 0.0, "labels": None, "values": None},
|
||||
{"name": "left", "kind": "Int", "default": "0", "lo": 0.0,
|
||||
"hi": 100000.0, "labels": None, "values": None},
|
||||
"hi": 0.0, "labels": None, "values": None},
|
||||
{"name": "top", "kind": "Int", "default": "0", "lo": 0.0,
|
||||
"hi": 100000.0, "labels": None, "values": None},
|
||||
{"name": "width", "kind": "Int", "default": "100", "lo": 10.0,
|
||||
"hi": 100000.0, "labels": None, "values": None},
|
||||
{"name": "height", "kind": "Int", "default": "30", "lo": 10.0,
|
||||
"hi": 100000.0, "labels": None, "values": None},
|
||||
"hi": 0.0, "labels": None, "values": None},
|
||||
{"name": "width", "kind": "Int", "default": "100", "lo": 0.0,
|
||||
"hi": 0.0, "labels": None, "values": None},
|
||||
{"name": "height", "kind": "Int", "default": "30", "lo": 0.0,
|
||||
"hi": 0.0, "labels": None, "values": None},
|
||||
]
|
||||
generic.extend(tables["common"])
|
||||
for name, kind, default, lo, hi, labels in GENERIC_EXTRAS:
|
||||
|
||||
Reference in new issue
Block a user