McRogueFace/tests/unit/test_grid_cell_events.py
John McCardle 5d41292bf6 Timer refactor: stopwatch-like semantics, mcrfpy.timers collection closes #173
Major Timer API improvements:
- Add `stopped` flag to Timer C++ class for proper state management
- Add `start()` method to restart stopped timers (preserves callback)
- Add `stop()` method that removes from engine but preserves callback
- Make `active` property read-write (True=start/resume, False=pause)
- Add `start=True` init parameter to create timers in stopped state
- Add `mcrfpy.timers` module-level collection (tuple of active timers)
- One-shot timers now set stopped=true instead of clearing callback
- Remove deprecated `setTimer()` and `delTimer()` module functions

Timer callbacks now receive (timer, runtime) instead of just (runtime).
Updated all tests to use new Timer API and callback signature.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-03 22:09:18 -05:00

128 lines
3.1 KiB
Python

#!/usr/bin/env python3
"""Test #142: Grid Cell Mouse Events"""
import sys
import mcrfpy
from mcrfpy import automation
def test_properties():
"""Test grid cell event properties exist and work"""
print("Testing grid cell event properties...")
test_props = mcrfpy.Scene("test_props")
ui = test_props.children
grid = mcrfpy.Grid(grid_size=(5, 5), pos=(100, 100), size=(200, 200))
ui.append(grid)
def cell_handler(x, y):
pass
# Test on_cell_enter
grid.on_cell_enter = cell_handler
assert grid.on_cell_enter == cell_handler
grid.on_cell_enter = None
assert grid.on_cell_enter is None
# Test on_cell_exit
grid.on_cell_exit = cell_handler
assert grid.on_cell_exit == cell_handler
grid.on_cell_exit = None
assert grid.on_cell_exit is None
# Test on_cell_click
grid.on_cell_click = cell_handler
assert grid.on_cell_click == cell_handler
grid.on_cell_click = None
assert grid.on_cell_click is None
# Test hovered_cell
assert grid.hovered_cell is None
print(" - Properties: PASS")
def test_cell_hover():
"""Test cell hover events"""
print("Testing cell hover events...")
test_hover = mcrfpy.Scene("test_hover")
ui = test_hover.children
test_hover.activate()
grid = mcrfpy.Grid(grid_size=(5, 5), pos=(100, 100), size=(200, 200))
ui.append(grid)
enter_events = []
exit_events = []
def on_enter(x, y):
enter_events.append((x, y))
def on_exit(x, y):
exit_events.append((x, y))
grid.on_cell_enter = on_enter
grid.on_cell_exit = on_exit
# Move into grid and between cells
automation.moveTo(150, 150)
automation.moveTo(200, 200)
def check_hover(timer, runtime):
print(f" Enter events: {len(enter_events)}, Exit events: {len(exit_events)}")
print(f" Hovered cell: {grid.hovered_cell}")
if len(enter_events) >= 1:
print(" - Hover: PASS")
else:
print(" - Hover: PARTIAL")
# Continue to click test
test_cell_click()
mcrfpy.Timer("check_hover", check_hover, 200, once=True)
def test_cell_click():
"""Test cell click events"""
print("Testing cell click events...")
test_click = mcrfpy.Scene("test_click")
ui = test_click.children
test_click.activate()
grid = mcrfpy.Grid(grid_size=(5, 5), pos=(100, 100), size=(200, 200))
ui.append(grid)
click_events = []
def on_click(x, y):
click_events.append((x, y))
grid.on_cell_click = on_click
automation.click(200, 200)
def check_click(timer, runtime):
print(f" Click events: {len(click_events)}")
if len(click_events) >= 1:
print(" - Click: PASS")
else:
print(" - Click: PARTIAL")
print("\n=== All grid cell event tests passed! ===")
sys.exit(0)
mcrfpy.Timer("check_click", check_click, 200, once=True)
if __name__ == "__main__":
try:
test_properties()
test_cell_hover() # Chains to test_cell_click
except Exception as e:
print(f"\nTEST FAILED: {e}")
import traceback
traceback.print_exc()
sys.exit(1)