diff --git a/tests/test_binding_inheritance.py b/tests/test_binding_inheritance.py index 9c8775d0d..264a594a8 100644 --- a/tests/test_binding_inheritance.py +++ b/tests/test_binding_inheritance.py @@ -243,3 +243,42 @@ async def test_focused_child_widget_with_movement_bindings_no_inherit() -> None: async with AppWithWidgetWithBindingsNoInherit().run_test() as pilot: await pilot.press("x", *MOVEMENT_KEYS, "x") assert pilot.app.pressed_keys == ["locally_x", *[f"locally_{key}" for key in MOVEMENT_KEYS], "locally_x"] + +############################################################################## +class FocusableWidgetWithNoBindingsNoInherit(Static, can_focus=True, inherit_bindings=False): + """A widget that can receive focus but has no bindings and doesn't inherit bindings.""" + +class ScreenWithMovementBindingsNoInheritChild(Screen): + """A screen that binds keys, including movement keys.""" + + BINDINGS = [ + Binding("x", "screen_record('x')", "x"), + *[Binding(key, f"screen_record('{key}')", key) for key in MOVEMENT_KEYS] + ] + + async def action_screen_record(self, key: str) -> None: + # Sneaky forward reference. Just for the purposes of testing. + await self.app.action_record(f"screen_{key}") + + def compose(self) -> ComposeResult: + yield FocusableWidgetWithNoBindingsNoInherit() + + def on_mount(self) -> None: + self.query_one(FocusableWidgetWithNoBindingsNoInherit).focus() + +class AppWithScreenWithBindingsWidgetNoBindingsNoInherit(AppKeyRecorder): + """An app with a non-default screen that handles movement key bindings, child no-inherit.""" + + SCREENS = {"main":ScreenWithMovementBindingsNoInheritChild} + + def on_mount(self) -> None: + self.push_screen("main") + +@pytest.mark.xfail( + reason="A child widget that doesn't inherit bindings, but has no bindings, incorrectly defers to its parent class [issue#1351]" +) +async def test_focused_child_widget_no_inherit_with_movement_bindings_on_screen() -> None: + """A focused child widget, that doesn't inherit bindings, with movement bindings in the screen, should trigger screen actions.""" + async with AppWithScreenWithBindingsWidgetNoBindingsNoInherit().run_test() as pilot: + await pilot.press("x", *MOVEMENT_KEYS, "x") + assert pilot.app.pressed_keys == ["screen_x", *[f"screen_{key}" for key in MOVEMENT_KEYS], "screen_x"]