"""Tests for lightgui.props and Phase 2 live-apply plumbing. Covers: every one of the 562 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), 562) 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_have_phase6_tables(self): # Phase 6 adds their tables (generic set first, then each # widget's own props from the C tables). The Ind* indicator # entries keep the generic set only -- they are out of scope. self.assertEqual(props.custom_props_for("IndRSI"), ()) self.assertTrue(props.custom_props_for("InstLedRed")) self.assertTrue(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()