File: test_escape_binding.py

package info (click to toggle)
textual 2.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 55,056 kB
  • sloc: python: 85,423; lisp: 1,669; makefile: 101
file content (55 lines) | stat: -rw-r--r-- 1,873 bytes parent folder | download
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
55
from textual.app import App, ComposeResult
from textual.screen import ModalScreen
from textual.widgets import Button, TextArea


class TextAreaDialog(ModalScreen):
    BINDINGS = [("escape", "dismiss")]

    def compose(self) -> ComposeResult:
        yield TextArea(
            tab_behavior="focus",  # the default
        )
        yield Button("Submit")


class TextAreaDialogApp(App):
    def on_mount(self) -> None:
        self.push_screen(TextAreaDialog())


async def test_escape_key_when_tab_behavior_is_focus():
    """Regression test for https://github.com/Textualize/textual/issues/4110

    When the `tab_behavior` of TextArea is the default to shift focus,
    pressing <Escape> should not shift focus but instead skip and allow any
    parent bindings to run.
    """

    app = TextAreaDialogApp()
    async with app.run_test() as pilot:
        # Sanity check
        assert isinstance(pilot.app.screen, TextAreaDialog)
        assert isinstance(pilot.app.focused, TextArea)

        # Pressing escape should dismiss the dialog screen, not focus the button
        await pilot.press("escape")
        assert not isinstance(pilot.app.screen, TextAreaDialog)


async def test_escape_key_when_tab_behavior_is_indent():
    """When the `tab_behavior` of TextArea is indent rather than switch focus,
    pressing <Escape> should instead shift focus.
    """

    app = TextAreaDialogApp()
    async with app.run_test() as pilot:
        # Sanity check
        assert isinstance(pilot.app.screen, TextAreaDialog)
        assert isinstance(pilot.app.focused, TextArea)

        pilot.app.query_one(TextArea).tab_behavior = "indent"
        # Pressing escape should focus the button, not dismiss the dialog screen
        await pilot.press("escape")
        assert isinstance(pilot.app.screen, TextAreaDialog)
        assert isinstance(pilot.app.focused, Button)