File: test_app_focus_blur.py

package info (click to toggle)
textual 2.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,084 kB
  • sloc: python: 85,423; lisp: 1,669; makefile: 101
file content (79 lines) | stat: -rw-r--r-- 2,840 bytes parent folder | download | duplicates (2)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""Test the workings of reacting to AppFocus and AppBlur."""

from textual.app import App, ComposeResult
from textual.events import AppBlur, AppFocus
from textual.widgets import Input


class FocusBlurApp(App[None]):

    AUTO_FOCUS = "#input-4"

    def compose(self) -> ComposeResult:
        for n in range(10):
            yield Input(id=f"input-{n}")


async def test_app_blur() -> None:
    """Test that AppBlur removes focus."""
    async with FocusBlurApp().run_test() as pilot:
        assert pilot.app.focused is not None
        assert pilot.app.focused.id == "input-4"
        pilot.app.post_message(AppBlur())
        await pilot.pause()
        assert pilot.app.focused is None


async def test_app_focus_restores_focus() -> None:
    """Test that AppFocus restores the correct focus."""
    async with FocusBlurApp().run_test() as pilot:
        assert pilot.app.focused is not None
        assert pilot.app.focused.id == "input-4"
        pilot.app.post_message(AppBlur())
        await pilot.pause()
        assert pilot.app.focused is None
        pilot.app.post_message(AppFocus())
        await pilot.pause()
        assert pilot.app.focused is not None
        assert pilot.app.focused.id == "input-4"


async def test_app_focus_restores_none_focus() -> None:
    """Test that AppFocus doesn't set focus if nothing was focused."""
    async with FocusBlurApp().run_test() as pilot:
        pilot.app.screen.focused = None
        pilot.app.post_message(AppBlur())
        await pilot.pause()
        assert pilot.app.focused is None
        pilot.app.post_message(AppFocus())
        await pilot.pause()
        assert pilot.app.focused is None


async def test_app_focus_handles_missing_widget() -> None:
    """Test that AppFocus works even when the last-focused widget has gone away."""
    async with FocusBlurApp().run_test() as pilot:
        assert pilot.app.focused is not None
        assert pilot.app.focused.id == "input-4"
        pilot.app.post_message(AppBlur())
        await pilot.pause()
        assert pilot.app.focused is None
        await pilot.app.query_one("#input-4").remove()
        pilot.app.post_message(AppFocus())
        await pilot.pause()
        assert pilot.app.focused is None


async def test_app_focus_defers_to_new_focus() -> None:
    """Test that AppFocus doesn't undo a fresh focus done while the app is in AppBlur state."""
    async with FocusBlurApp().run_test() as pilot:
        assert pilot.app.focused is not None
        assert pilot.app.focused.id == "input-4"
        pilot.app.post_message(AppBlur())
        await pilot.pause()
        assert pilot.app.focused is None
        pilot.app.query_one("#input-1").focus()
        await pilot.pause()
        pilot.app.post_message(AppFocus())
        await pilot.pause()
        assert pilot.app.focused.id == "input-1"