File: test_focus.py

package info (click to toggle)
textual 2.1.2-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 55,080 kB
  • sloc: python: 85,423; lisp: 1,669; makefile: 101
file content (452 lines) | stat: -rw-r--r-- 14,087 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import pytest

from textual.app import App, ComposeResult
from textual.containers import Container, ScrollableContainer
from textual.screen import Screen
from textual.widget import Widget
from textual.widgets import Button, Label


class Focusable(Widget, can_focus=True):
    pass


class NonFocusable(Widget, can_focus=False, can_focus_children=False):
    pass


class ChildrenFocusableOnly(Widget, can_focus=False, can_focus_children=True):
    pass


@pytest.fixture
def screen() -> Screen:
    app = App()

    with app._context():
        app.push_screen(Screen())

        screen = app.screen

        # The classes even/odd alternate along the focus chain.
        # The classes in/out identify nested widgets.
        screen._add_children(
            Focusable(id="foo", classes="a"),
            NonFocusable(id="bar"),
            Focusable(Focusable(id="Paul", classes="c"), id="container1", classes="b"),
            NonFocusable(Focusable(id="Jessica", classes="a"), id="container2"),
            Focusable(id="baz", classes="b"),
            ChildrenFocusableOnly(Focusable(id="child", classes="c")),
        )

        return screen


def test_focus_chain():
    app = App()
    with app._context():
        app.push_screen(Screen())

        screen = app.screen

        # Check empty focus chain
        assert not screen.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"),
            ChildrenFocusableOnly(Focusable(id="child")),
        )

        focus_chain = [widget.id for widget in screen.focus_chain]
        assert focus_chain == ["foo", "container1", "Paul", "baz", "child"]


def test_allow_focus():
    """Test allow_focus and allow_focus_children are called and the result used."""
    focusable_allow_focus_called = False
    non_focusable_allow_focus_called = False

    class Focusable(Widget, can_focus=False):
        def allow_focus(self) -> bool:
            nonlocal focusable_allow_focus_called
            focusable_allow_focus_called = True
            return True

    class NonFocusable(Container, can_focus=True):
        def allow_focus(self) -> bool:
            nonlocal non_focusable_allow_focus_called
            non_focusable_allow_focus_called = True
            return False

    class FocusableContainer(Container, can_focus_children=False):
        def allow_focus_children(self) -> bool:
            return True

    class NonFocusableContainer(Container, can_focus_children=True):
        def allow_focus_children(self) -> bool:
            return False

    app = App()

    with app._context():
        app.push_screen(Screen())

        app.screen._add_children(
            Focusable(id="foo"),
            NonFocusable(id="bar"),
            FocusableContainer(Button("egg", id="egg")),
            NonFocusableContainer(Button("EGG", id="qux")),
        )
        assert [widget.id for widget in app.screen.focus_chain] == ["foo", "egg"]
        assert focusable_allow_focus_called
        assert non_focusable_allow_focus_called


def test_focus_next_and_previous(screen: Screen):
    assert screen.focus_next().id == "foo"
    assert screen.focus_next().id == "container1"
    assert screen.focus_next().id == "Paul"
    assert screen.focus_next().id == "baz"
    assert screen.focus_next().id == "child"

    assert screen.focus_previous().id == "baz"
    assert screen.focus_previous().id == "Paul"
    assert screen.focus_previous().id == "container1"
    assert screen.focus_previous().id == "foo"


def test_focus_next_wrap_around(screen: Screen):
    """Ensure focusing the next widget wraps around the focus chain."""
    screen.set_focus(screen.query_one("#child"))
    assert screen.focused.id == "child"

    assert screen.focus_next().id == "foo"


def test_focus_previous_wrap_around(screen: Screen):
    """Ensure focusing the previous widget wraps around the focus chain."""
    screen.set_focus(screen.query_one("#foo"))
    assert screen.focused.id == "foo"

    assert screen.focus_previous().id == "child"


def test_wrap_around_selector(screen: Screen):
    """Ensure moving focus in both directions wraps around the focus chain."""
    screen.set_focus(screen.query_one("#foo"))
    assert screen.focused.id == "foo"

    assert screen.focus_previous("#Paul").id == "Paul"
    assert screen.focus_next("#foo").id == "foo"


def test_no_focus_empty_selector(screen: Screen):
    """Ensure focus is cleared when selector matches nothing."""
    assert screen.focus_next("#bananas") is None
    assert screen.focus_previous("#bananas") is None

    screen.set_focus(screen.query_one("#foo"))
    assert screen.focused is not None
    assert screen.focus_next("#bananas") is None
    assert screen.focused is None

    screen.set_focus(screen.query_one("#foo"))
    assert screen.focused is not None
    assert screen.focus_previous("#bananas") is None
    assert screen.focused is None


def test_focus_next_and_previous_with_type_selector(screen: Screen):
    """Move focus with a selector that matches the currently focused node."""
    screen.set_focus(screen.query_one("#Paul"))
    assert screen.focused.id == "Paul"

    assert screen.focus_next(Focusable).id == "baz"
    assert screen.focus_next(Focusable).id == "child"

    assert screen.focus_previous(Focusable).id == "baz"
    assert screen.focus_previous(Focusable).id == "Paul"
    assert screen.focus_previous(Focusable).id == "container1"
    assert screen.focus_previous(Focusable).id == "foo"


def test_focus_next_and_previous_with_str_selector(screen: Screen):
    """Move focus with a selector that matches the currently focused node."""
    screen.set_focus(screen.query_one("#foo"))
    assert screen.focused.id == "foo"

    assert screen.focus_next(".a").id == "foo"
    assert screen.focus_next(".c").id == "Paul"
    assert screen.focus_next(".c").id == "child"

    assert screen.focus_previous(".c").id == "Paul"
    assert screen.focus_previous(".a").id == "foo"


def test_focus_next_and_previous_with_type_selector_without_self():
    """Test moving the focus with a selector that does not match the currently focused node."""
    app = App()
    with app._context():
        app.push_screen(Screen())

        screen = app.screen

        from textual.containers import Horizontal, VerticalScroll
        from textual.widgets import Button, Input, Switch

        screen._add_children(
            VerticalScroll(
                Horizontal(
                    Input(id="w3"),
                    Switch(id="w4"),
                    Input(id="w5"),
                    Button(id="w6"),
                    Switch(id="w7"),
                    id="w2",
                ),
                Horizontal(
                    Button(id="w9"),
                    Switch(id="w10"),
                    Button(id="w11"),
                    Input(id="w12"),
                    Input(id="w13"),
                    id="w8",
                ),
                id="w1",
            )
        )

        screen.set_focus(screen.query_one("#w3"))
        assert screen.focused.id == "w3"

        assert screen.focus_next(Button).id == "w6"
        assert screen.focus_next(Switch).id == "w7"
        assert screen.focus_next(Input).id == "w12"

        assert screen.focus_previous(Button).id == "w11"
        assert screen.focus_previous(Switch).id == "w10"
        assert screen.focus_previous(Button).id == "w9"
        assert screen.focus_previous(Input).id == "w5"


def test_focus_next_and_previous_with_str_selector_without_self(screen: Screen):
    """Test moving the focus with a selector that does not match the currently focused node."""
    screen.set_focus(screen.query_one("#foo"))
    assert screen.focused.id == "foo"

    assert screen.focus_next(".c").id == "Paul"
    assert screen.focus_next(".b").id == "baz"
    assert screen.focus_next(".c").id == "child"

    assert screen.focus_previous(".a").id == "foo"
    assert screen.focus_previous(".a").id == "foo"
    assert screen.focus_previous(".b").id == "baz"


async def test_focus_does_not_move_to_invisible_widgets():
    """Make sure invisible widgets don't get focused by accident.

    This is kind of a regression test for https://github.com/Textualize/textual/issues/3053,
    but not really.
    """

    class MyApp(App):
        CSS = "#inv { visibility: hidden; }"

        def compose(self):
            yield Button("one", id="one")
            yield Button("two", id="inv")
            yield Button("three", id="three")

    app = MyApp()
    async with app.run_test():
        assert app.focused.id == "one"
        assert app.screen.focus_next().id == "three"


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

    class MyApp(App):
        CSS = """
        #inv { visibility: hidden; }
        #three { visibility: visible; }
        """

        def compose(self):
            yield Button(id="one")
            with Container(id="inv"):
                yield Button(id="three")

    app = MyApp()
    async with app.run_test():
        assert app.focused.id == "one"
        assert app.screen.focus_next().id == "three"


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

    This is more or less a test for the interactions between #3053 and #3071.
    We want to make sure that the focus chain is computed correctly when going through
    a DOM with containers with all sorts of visibilities set.
    """

    class W(Widget):
        can_focus = True

    w1 = W(id="one")
    c2 = Container(id="two")
    w3 = W(id="three")
    c4 = Container(id="four")
    w5 = W(id="five")
    c6 = Container(id="six")
    w7 = W(id="seven")
    c8 = Container(id="eight")
    w9 = W(id="nine")
    w10 = W(id="ten")
    w11 = W(id="eleven")
    w12 = W(id="twelve")
    w13 = W(id="thirteen")

    class InheritedVisibilityApp(App[None]):
        CSS = """
        #four, #eight, #ten {
            visibility: visible;
        }

        #six, #thirteen {
            visibility: hidden;
        }
        """

        def compose(self):
            yield w1  # visible, inherited
            with c2:  # visible, inherited
                yield w3  # visible, inherited
                with c4:  # visible, set
                    yield w5  # visible, inherited
                    with c6:  # hidden, set
                        yield w7  # hidden, inherited
                        with c8:  # visible, set
                            yield w9  # visible, inherited
                        yield w10  # visible, set
                    yield w11  # visible, inherited
                yield w12  # visible, inherited
            yield w13  # invisible, set

    app = InheritedVisibilityApp()
    async with app.run_test():
        focus_chain = app.screen.focus_chain
        assert focus_chain == [
            w1,
            w3,
            w5,
            w9,
            w10,
            w11,
            w12,
        ]


async def test_mouse_down_gives_focus():
    class MyApp(App):
        AUTO_FOCUS = None

        def compose(self):
            yield Button()

    app = MyApp()
    async with app.run_test() as pilot:
        # Sanity check.
        assert app.focused is None

        await pilot.mouse_down(Button)
        assert isinstance(app.focused, Button)


async def test_mouse_up_does_not_give_focus():
    class MyApp(App):
        AUTO_FOCUS = None

        def compose(self):
            yield Button()

    app = MyApp()
    async with app.run_test() as pilot:
        # Sanity check.
        assert app.focused is None

        await pilot.mouse_up(Button)
        assert app.focused is None


async def test_focus_pseudo_class():
    """Test focus and blue pseudo classes"""

    # https://github.com/Textualize/textual/pull/3645
    class FocusApp(App):
        AUTO_FOCUS = None

        def compose(self) -> ComposeResult:
            yield Button("Hello")

    app = FocusApp()
    async with app.run_test() as pilot:
        button = app.query_one(Button)
        classes = list(button.get_pseudo_classes())
        # Blurred, not focused
        assert "blur" in classes
        assert "focus" not in classes

        # Focus the button
        button.focus()
        await pilot.pause()

        # Focused, not blurred
        classes = list(button.get_pseudo_classes())
        assert "blur" not in classes
        assert "focus" in classes


async def test_get_focusable_widget_at() -> None:
    """Check that clicking a non-focusable widget will focus any (focusable) ancestors."""

    class FocusApp(App):
        AUTO_FOCUS = None

        def compose(self) -> ComposeResult:
            with ScrollableContainer(id="focusable"):
                with Container():
                    yield Label("Foo", id="foo")
                    yield Label("Bar", id="bar")
            yield Label("Egg", id="egg")

    app = FocusApp()
    async with app.run_test() as pilot:
        # Nothing focused
        assert app.screen.focused is None
        # Click foo
        await pilot.click("#foo")
        # Confirm container is focused
        assert app.screen.focused is not None
        assert app.screen.focused.id == "focusable"
        # Reset focus
        app.screen.set_focus(None)
        assert app.screen.focused is None
        # Click bar
        await pilot.click("#bar")
        # Confirm container is focused
        assert app.screen.focused is not None
        assert app.screen.focused.id == "focusable"
        # Reset focus
        app.screen.set_focus(None)
        assert app.screen.focused is None
        # Click egg (outside of focusable widget)
        await pilot.click("#egg")
        # Confirm nothing focused
        assert app.screen.focused is None