Files
tarot/tests/test_ui_binding_recursive.py

64 lines
1.6 KiB
Python
Raw Normal View History

2026-02-11 00:02:35 -08:00
import tkinter as tk
2025-12-05 03:41:16 -08:00
import pytest
2026-02-11 00:02:35 -08:00
2025-12-05 03:41:16 -08:00
from tarot.tarot_api import Tarot
2026-02-11 00:02:35 -08:00
from tarot.ui import CubeDisplay
2025-12-05 03:41:16 -08:00
def test_recursive_binding():
# Mock Tk root and widgets
class MockWidget:
def __init__(self):
self.children = []
self.bindings = {}
2026-02-11 00:02:35 -08:00
2025-12-05 03:41:16 -08:00
def bind(self, key, callback):
self.bindings[key] = callback
2026-02-11 00:02:35 -08:00
2025-12-05 03:41:16 -08:00
def winfo_children(self):
return self.children
2026-02-11 00:02:35 -08:00
2025-12-05 03:41:16 -08:00
def add_child(self, child):
self.children.append(child)
# Monkey patch tk
original_tk = tk.Tk
original_frame = tk.ttk.Frame
2026-02-11 00:02:35 -08:00
2025-12-05 03:41:16 -08:00
try:
# We don't need to mock everything, just enough to test _bind_recursive
2026-02-11 00:02:35 -08:00
2025-12-05 03:41:16 -08:00
cube = Tarot.cube
# We can instantiate CubeDisplay without showing it
display = CubeDisplay(cube)
2026-02-11 00:02:35 -08:00
2025-12-05 03:41:16 -08:00
# Create a mock widget tree
parent = MockWidget()
child1 = MockWidget()
child2 = MockWidget()
grandchild = MockWidget()
2026-02-11 00:02:35 -08:00
2025-12-05 03:41:16 -08:00
parent.add_child(child1)
parent.add_child(child2)
child1.add_child(grandchild)
2026-02-11 00:02:35 -08:00
2025-12-05 03:41:16 -08:00
# Run recursive binding
display._bind_recursive(parent)
2026-02-11 00:02:35 -08:00
2025-12-05 03:41:16 -08:00
# Verify bindings
assert "<ButtonPress-1>" in parent.bindings
assert "<B1-Motion>" in parent.bindings
2026-02-11 00:02:35 -08:00
2025-12-05 03:41:16 -08:00
assert "<ButtonPress-1>" in child1.bindings
assert "<B1-Motion>" in child1.bindings
2026-02-11 00:02:35 -08:00
2025-12-05 03:41:16 -08:00
assert "<ButtonPress-1>" in child2.bindings
assert "<B1-Motion>" in child2.bindings
2026-02-11 00:02:35 -08:00
2025-12-05 03:41:16 -08:00
assert "<ButtonPress-1>" in grandchild.bindings
assert "<B1-Motion>" in grandchild.bindings
2026-02-11 00:02:35 -08:00
2025-12-05 03:41:16 -08:00
finally:
pass