From 0b07ab669f8cafe050ef848c3a506cb7ee3c8bc3 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Wed, 24 Jul 2024 16:45:13 +0100 Subject: [PATCH] add test --- src/textual/app.py | 9 +----- src/textual/pilot.py | 1 + tests/footer/test_footer.py | 58 +++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 tests/footer/test_footer.py diff --git a/src/textual/app.py b/src/textual/app.py index 4952cc91f9..1fa10ef0ce 100644 --- a/src/textual/app.py +++ b/src/textual/app.py @@ -3029,14 +3029,7 @@ def simulate_key(self, key: str) -> None: Args: key: Key to simulate. May also be the name of a key, e.g. "space". """ - event = events.Key(key, None) - self.post_message(event) - - # async def dispatch_simulated_key() -> None: - # if not (await self._check_bindings(event.key, priority=True)): - # await dispatch_key(self, event) - - # self.call_later(dispatch_simulated_key) + self.post_message(events.Key(key, None)) async def _check_bindings(self, key: str, priority: bool = False) -> bool: """Handle a key press. diff --git a/src/textual/pilot.py b/src/textual/pilot.py index 036b18a6b3..90c4baf0d2 100644 --- a/src/textual/pilot.py +++ b/src/textual/pilot.py @@ -342,6 +342,7 @@ async def _post_mouse_events( # E.g., the click event is preceded by MouseDown/MouseUp to emulate how # the driver works and emits a click event. widget_at, _ = app.get_widget_at(*offset) + print(widget_at) event = mouse_event_cls(**message_arguments) # Bypass event processing in App.on_event. Because App.on_event # is responsible for updating App.mouse_position, and because diff --git a/tests/footer/test_footer.py b/tests/footer/test_footer.py new file mode 100644 index 0000000000..d4ed2d59f8 --- /dev/null +++ b/tests/footer/test_footer.py @@ -0,0 +1,58 @@ +from textual.app import App, ComposeResult +from textual.binding import Binding +from textual.widget import Widget +from textual.widgets import Button, Footer + + +async def test_footer_bindings() -> None: + app_binding_count = 0 + + class TestWidget(Widget, can_focus=True): + BINDINGS = [ + Binding("b", "widget_binding", "Overridden Binding"), + ] + + DEFAULT_CSS = """ + TestWidget { + border: tall $background; + width: 50%; + height: 50%; + content-align: center middle; + + &:focus { + border: tall $accent; + } + } + """ + + def action_widget_binding(self) -> None: + assert False, "should never be called since there is a priority binding" + + class PriorityBindingApp(App): + BINDINGS = [ + Binding("b", "app_binding", "Priority Binding", priority=True), + ] + + CSS = """ + Screen { + align: center middle; + } + """ + + def compose(self) -> ComposeResult: + yield TestWidget() + yield Button("Move Focus") + yield Footer() + + def action_app_binding(self) -> None: + nonlocal app_binding_count + app_binding_count += 1 + + app = PriorityBindingApp() + async with app.run_test() as pilot: + await pilot.pause() + assert app_binding_count == 0 + await pilot.click("Footer", offset=(1, 0)) + assert app_binding_count == 1 + await pilot.click("Footer") + assert app_binding_count == 2