File size: 1,317 Bytes
2c3c408 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | from textual.app import App
from textual.screen import Screen
from textual.widget import Widget
class Focusable(Widget, can_focus=True):
pass
class NonFocusable(Widget, can_focus=False, can_focus_children=False):
pass
async def test_focus_chain():
app = App()
app._set_active()
app.push_screen(Screen())
# Check empty focus chain
assert not app.focus_chain
app.screen._add_children(
Focusable(id="foo"),
NonFocusable(id="bar"),
Focusable(Focusable(id="Paul"), id="container1"),
NonFocusable(Focusable(id="Jessica"), id="container2"),
Focusable(id="baz"),
)
focused = [widget.id for widget in app.focus_chain]
assert focused == ["foo", "Paul", "baz"]
async def test_focus_next_and_previous():
app = App()
app._set_active()
app.push_screen(Screen())
app.screen._add_children(
Focusable(id="foo"),
NonFocusable(id="bar"),
Focusable(Focusable(id="Paul"), id="container1"),
NonFocusable(Focusable(id="Jessica"), id="container2"),
Focusable(id="baz"),
)
assert app.focus_next().id == "foo"
assert app.focus_next().id == "Paul"
assert app.focus_next().id == "baz"
assert app.focus_previous().id == "Paul"
assert app.focus_previous().id == "foo"
|