|
| 1 | +from unittest.mock import Mock |
| 2 | + |
| 3 | +import arcade |
| 4 | +from arcade.gui import UIInteractiveSpriteWidget, UIBoxLayout |
| 5 | +from arcade.gui.events import UIOnClickEvent, UIMousePressEvent, UIMouseReleaseEvent |
| 6 | +from arcade.gui.widgets.layout import UIAnchorLayout |
| 7 | + |
| 8 | +from . import record_ui_events |
| 9 | + |
| 10 | + |
| 11 | +def _make_widget(**kwargs) -> UIInteractiveSpriteWidget: |
| 12 | + sprite = arcade.SpriteSolidColor(100, 100, color=arcade.color.RED) |
| 13 | + return UIInteractiveSpriteWidget(sprite=sprite, **kwargs) |
| 14 | + |
| 15 | + |
| 16 | +def test_click_fires_on_click(ui): |
| 17 | + """Clicking the widget should dispatch an on_click event.""" |
| 18 | + widget = _make_widget() |
| 19 | + ui.add(widget) |
| 20 | + |
| 21 | + with record_ui_events(widget, "on_click") as events: |
| 22 | + ui.click(widget.center_x, widget.center_y) |
| 23 | + |
| 24 | + assert len(events) == 1 |
| 25 | + assert isinstance(events[0], UIOnClickEvent) |
| 26 | + assert events[0].source is widget |
| 27 | + |
| 28 | + |
| 29 | +def test_click_outside_does_not_fire(ui): |
| 30 | + """Clicking outside the widget should not dispatch on_click.""" |
| 31 | + widget = _make_widget() |
| 32 | + ui.add(widget) |
| 33 | + |
| 34 | + with record_ui_events(widget, "on_click") as events: |
| 35 | + ui.click(widget.right + 50, widget.top + 50) |
| 36 | + |
| 37 | + assert len(events) == 0 |
| 38 | + |
| 39 | + |
| 40 | +def test_hovered_updates_on_mouse_move(ui): |
| 41 | + """Moving the mouse over the widget should set hovered=True.""" |
| 42 | + widget = _make_widget() |
| 43 | + ui.add(widget) |
| 44 | + |
| 45 | + assert widget.hovered is False |
| 46 | + ui.move_mouse(widget.center_x, widget.center_y) |
| 47 | + assert widget.hovered is True |
| 48 | + ui.move_mouse(widget.right + 50, widget.top + 50) |
| 49 | + assert widget.hovered is False |
| 50 | + |
| 51 | + |
| 52 | +def test_pressed_between_press_and_release(ui): |
| 53 | + """pressed should be True between mouse press and release.""" |
| 54 | + widget = _make_widget() |
| 55 | + ui.add(widget) |
| 56 | + |
| 57 | + assert widget.pressed is False |
| 58 | + ui.click_and_hold(widget.center_x, widget.center_y) |
| 59 | + assert widget.pressed is True |
| 60 | + ui.release(widget.center_x, widget.center_y) |
| 61 | + assert widget.pressed is False |
| 62 | + |
| 63 | + |
| 64 | +def test_disabled_blocks_click(ui): |
| 65 | + """Clicking a disabled widget should not fire on_click.""" |
| 66 | + widget = _make_widget() |
| 67 | + widget.disabled = True |
| 68 | + ui.add(widget) |
| 69 | + |
| 70 | + with record_ui_events(widget, "on_click") as events: |
| 71 | + ui.click(widget.center_x, widget.center_y) |
| 72 | + |
| 73 | + assert len(events) == 0 |
| 74 | + |
| 75 | + |
| 76 | +def test_widget_rect_matches_sprite_size(window): |
| 77 | + """Widget dimensions should default to sprite texture size.""" |
| 78 | + sprite = arcade.SpriteSolidColor(150, 75, color=arcade.color.BLUE) |
| 79 | + widget = UIInteractiveSpriteWidget(sprite=sprite) |
| 80 | + assert widget.width == 150 |
| 81 | + assert widget.height == 75 |
| 82 | + |
| 83 | + |
| 84 | +def test_explicit_size_overrides_sprite(window): |
| 85 | + """Explicit width/height should override sprite texture size.""" |
| 86 | + sprite = arcade.SpriteSolidColor(100, 100, color=arcade.color.RED) |
| 87 | + widget = UIInteractiveSpriteWidget(sprite=sprite, width=200, height=50) |
| 88 | + assert widget.width == 200 |
| 89 | + assert widget.height == 50 |
| 90 | + |
| 91 | + |
| 92 | +def test_works_in_box_layout(ui): |
| 93 | + """Widget should be usable inside a UIBoxLayout.""" |
| 94 | + layout = UIBoxLayout(vertical=False, space_between=10, size_hint=None) |
| 95 | + widget_a = _make_widget() |
| 96 | + widget_b = _make_widget() |
| 97 | + layout.add(widget_a) |
| 98 | + layout.add(widget_b) |
| 99 | + ui.add(layout) |
| 100 | + |
| 101 | + layout.do_layout() |
| 102 | + |
| 103 | + with record_ui_events(widget_a, "on_click") as events: |
| 104 | + ui.click(widget_a.center_x, widget_a.center_y) |
| 105 | + |
| 106 | + assert len(events) == 1 |
| 107 | + assert events[0].source is widget_a |
| 108 | + |
| 109 | + |
| 110 | +def test_works_in_anchor_layout(ui): |
| 111 | + """Widget should be usable inside a UIAnchorLayout.""" |
| 112 | + layout = UIAnchorLayout(width=500, height=500, size_hint=None) |
| 113 | + widget = _make_widget() |
| 114 | + layout.add(widget, anchor_x="center", anchor_y="center") |
| 115 | + ui.add(layout) |
| 116 | + |
| 117 | + layout.do_layout() |
| 118 | + |
| 119 | + with record_ui_events(widget, "on_click") as events: |
| 120 | + ui.click(widget.center_x, widget.center_y) |
| 121 | + |
| 122 | + assert len(events) == 1 |
| 123 | + |
| 124 | + |
| 125 | +def test_callback_via_event_decorator(ui): |
| 126 | + """Callback registration via @widget.event('on_click') should work.""" |
| 127 | + widget = _make_widget() |
| 128 | + callback = Mock() |
| 129 | + widget.push_handlers(on_click=callback) |
| 130 | + ui.add(widget) |
| 131 | + |
| 132 | + ui.click(widget.center_x, widget.center_y) |
| 133 | + |
| 134 | + assert callback.called |
| 135 | + assert isinstance(callback.call_args[0][0], UIOnClickEvent) |
| 136 | + |
| 137 | + |
| 138 | +def test_callback_via_assignment(ui): |
| 139 | + """Callback registration via widget.on_click = callback should work.""" |
| 140 | + widget = _make_widget() |
| 141 | + widget.on_click = Mock() |
| 142 | + ui.add(widget) |
| 143 | + |
| 144 | + ui.click(widget.center_x, widget.center_y) |
| 145 | + |
| 146 | + assert widget.on_click.called |
0 commit comments