File: test_tabs.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 (512 lines) | stat: -rw-r--r-- 16,823 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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
from __future__ import annotations

import pytest

from textual import on
from textual.app import App, ComposeResult
from textual.widgets import Tab, Tabs
from textual.widgets._tabs import Underline


async def test_tab_label():
    """It should be possible to access a tab's label."""
    assert Tab("Pilot").label_text == "Pilot"


async def test_tab_relabel():
    """It should be possible to relabel a tab."""
    tab = Tab("Pilot")
    assert tab.label_text == "Pilot"
    tab.label = "Aeryn"
    assert tab.label_text == "Aeryn"


async def test_compose_empty_tabs():
    """It should be possible to create an empty Tabs."""

    class TabsApp(App[None]):
        def compose(self) -> ComposeResult:
            yield Tabs()

    async with TabsApp().run_test() as pilot:
        assert pilot.app.query_one(Tabs).tab_count == 0
        assert pilot.app.query_one(Tabs).active_tab is None


async def test_compose_tabs_from_strings():
    """It should be possible to create a Tabs from some strings."""

    class TabsApp(App[None]):
        def compose(self) -> ComposeResult:
            yield Tabs("John", "Aeryn", "Moya", "Pilot")

    async with TabsApp().run_test() as pilot:
        tabs = pilot.app.query_one(Tabs)
        assert tabs.tab_count == 4
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"


async def test_compose_tabs_from_tabs():
    """It should be possible to create a Tabs from some Tabs."""

    class TabsApp(App[None]):
        def compose(self) -> ComposeResult:
            yield Tabs(
                Tab("John"),
                Tab("Aeryn"),
                Tab("Moya"),
                Tab("Pilot"),
            )

    async with TabsApp().run_test() as pilot:
        tabs = pilot.app.query_one(Tabs)
        assert tabs.tab_count == 4
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"


async def test_add_tabs_later():
    """It should be possible to add tabs later on in the app's cycle."""

    class TabsApp(App[None]):
        def compose(self) -> ComposeResult:
            yield Tabs()

    async with TabsApp().run_test() as pilot:
        tabs = pilot.app.query_one(Tabs)
        assert tabs.tab_count == 0
        assert tabs.active_tab is None
        await tabs.add_tab("John")
        assert tabs.tab_count == 1
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"
        await tabs.add_tab("Aeryn")
        assert tabs.tab_count == 2
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"


async def test_add_tab_before():
    """It should be possible to add a tab before another tab."""

    class TabsApp(App[None]):
        def compose(self) -> ComposeResult:
            yield Tabs("Pilot")

    async with TabsApp().run_test() as pilot:
        tabs = pilot.app.query_one(Tabs)
        assert tabs.tab_count == 1
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"
        assert tabs.active == "tab-1"
        await tabs.add_tab("John", before="tab-1")
        assert tabs.tab_count == 2
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"
        assert tabs.active == "tab-1"
        await tabs.add_tab("John", before=tabs.active_tab)
        assert tabs.tab_count == 3
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"
        assert tabs.active == "tab-1"


async def test_add_tab_before_badly():
    """Test exceptions from badly adding a tab before another."""

    class TabsApp(App[None]):
        def compose(self) -> ComposeResult:
            yield Tabs("Pilot")

    async with TabsApp().run_test() as pilot:
        tabs = pilot.app.query_one(Tabs)
        assert tabs.tab_count == 1
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"
        assert tabs.active == "tab-1"
        with pytest.raises(Tabs.TabError):
            tabs.add_tab("John", before="this-is-not-a-tab")
        assert tabs.tab_count == 1
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"
        assert tabs.active == "tab-1"
        with pytest.raises(Tabs.TabError):
            tabs.add_tab("John", before=Tab("I just made this up"))
        assert tabs.tab_count == 1
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"
        assert tabs.active == "tab-1"


async def test_add_tab_after():
    """It should be possible to add a tab after another tab."""

    class TabsApp(App[None]):
        def compose(self) -> ComposeResult:
            yield Tabs("Pilot")

    async with TabsApp().run_test() as pilot:
        tabs = pilot.app.query_one(Tabs)
        assert tabs.tab_count == 1
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"
        assert tabs.active == "tab-1"
        await tabs.add_tab("John", after="tab-1")
        assert tabs.tab_count == 2
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"
        assert tabs.active == "tab-1"
        await tabs.add_tab("John", after=tabs.active_tab)
        assert tabs.tab_count == 3
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"
        assert tabs.active == "tab-1"


async def test_add_tab_after_badly():
    """Test exceptions from badly adding a tab after another."""

    class TabsApp(App[None]):
        def compose(self) -> ComposeResult:
            yield Tabs("Pilot")

    async with TabsApp().run_test() as pilot:
        tabs = pilot.app.query_one(Tabs)
        assert tabs.tab_count == 1
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"
        assert tabs.active == "tab-1"
        with pytest.raises(Tabs.TabError):
            tabs.add_tab("John", after="this-is-not-a-tab")
        assert tabs.tab_count == 1
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"
        assert tabs.active == "tab-1"
        with pytest.raises(Tabs.TabError):
            tabs.add_tab("John", after=Tab("I just made this up"))
        assert tabs.tab_count == 1
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"
        assert tabs.active == "tab-1"


async def test_add_tab_before_and_after():
    """Attempting to add a tab before and after another is an error."""

    class TabsApp(App[None]):
        def compose(self) -> ComposeResult:
            yield Tabs("Pilot")

    async with TabsApp().run_test() as pilot:
        tabs = pilot.app.query_one(Tabs)
        assert tabs.tab_count == 1
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"
        assert tabs.active == "tab-1"
        with pytest.raises(Tabs.TabError):
            tabs.add_tab("John", before="tab-1", after="tab-1")


async def test_remove_tabs():
    """It should be possible to remove tabs."""

    class TabsApp(App[None]):
        def compose(self) -> ComposeResult:
            yield Tabs("John", "Aeryn", "Moya", "Pilot")

    async with TabsApp().run_test() as pilot:
        tabs = pilot.app.query_one(Tabs)
        assert tabs.tab_count == 4
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"

        await tabs.remove_tab("tab-1")
        assert tabs.tab_count == 3
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-2"

        await tabs.remove_tab(tabs.query_one("#tab-2", Tab))
        assert tabs.tab_count == 2
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-3"

        await tabs.remove_tab("tab-does-not-exist")
        assert tabs.tab_count == 2
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-3"

        await tabs.remove_tab(None)
        assert tabs.tab_count == 2
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-3"

        await tabs.remove_tab("tab-3")
        await tabs.remove_tab("tab-4")
        assert tabs.tab_count == 0
        assert tabs.active_tab is None


async def test_remove_tabs_reversed():
    """It should be possible to remove tabs."""

    class TabsApp(App[None]):
        def compose(self) -> ComposeResult:
            yield Tabs("John", "Aeryn", "Moya", "Pilot")

    async with TabsApp().run_test() as pilot:
        tabs = pilot.app.query_one(Tabs)
        assert tabs.tab_count == 4
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"

        await tabs.remove_tab("tab-4")
        assert tabs.tab_count == 3
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"

        await tabs.remove_tab("tab-3")
        assert tabs.tab_count == 2
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"

        await tabs.remove_tab("tab-2")
        assert tabs.tab_count == 1
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"

        await tabs.remove_tab("tab-1")
        assert tabs.tab_count == 0
        assert tabs.active_tab is None


async def test_clear_tabs():
    """It should be possible to clear all tabs."""

    class TabsApp(App[None]):
        def compose(self) -> ComposeResult:
            yield Tabs("John", "Aeryn", "Moya", "Pilot")

    async with TabsApp().run_test() as pilot:
        tabs = pilot.app.query_one(Tabs)
        assert tabs.tab_count == 4
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"
        await tabs.clear()
        assert tabs.tab_count == 0
        assert tabs.active_tab is None


async def test_change_active_from_code():
    """It should be possible to change the active tab from code.."""

    class TabsApp(App[None]):
        def compose(self) -> ComposeResult:
            yield Tabs("John", "Aeryn", "Moya", "Pilot")

    async with TabsApp().run_test() as pilot:
        tabs = pilot.app.query_one(Tabs)
        assert tabs.tab_count == 4
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"
        assert tabs.active == tabs.active_tab.id

        tabs.active = "tab-2"
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-2"
        assert tabs.active == tabs.active_tab.id

        tabs.active = ""
        assert tabs.active_tab is None


async def test_navigate_tabs_with_keyboard():
    """It should be possible to navigate tabs with the keyboard."""

    class TabsApp(App[None]):
        def compose(self) -> ComposeResult:
            yield Tabs("John", "Aeryn", "Moya", "Pilot")

    async with TabsApp().run_test() as pilot:
        tabs = pilot.app.query_one(Tabs)
        assert tabs.tab_count == 4
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"
        assert tabs.active == tabs.active_tab.id

        await pilot.press("right")
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-2"
        assert tabs.active == tabs.active_tab.id

        await pilot.press("left")
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"
        assert tabs.active == tabs.active_tab.id

        await pilot.press(*(["left"] * tabs.tab_count))
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"
        assert tabs.active == tabs.active_tab.id


async def test_navigate_empty_tabs_with_keyboard():
    """It should be possible to navigate an empty tabs with the keyboard."""

    class TabsApp(App[None]):
        def compose(self) -> ComposeResult:
            yield Tabs()

    async with TabsApp().run_test() as pilot:
        tabs = pilot.app.query_one(Tabs)
        assert tabs.tab_count == 0
        assert tabs.active_tab is None
        assert tabs.active == ""

        await pilot.press("right")
        assert tabs.active_tab is None
        assert tabs.active == ""

        await pilot.press("left")
        assert tabs.active_tab is None
        assert tabs.active == ""


async def test_navigate_tabs_with_mouse():
    """It should be possible to navigate tabs with the mouse."""

    class TabsApp(App[None]):
        def compose(self) -> ComposeResult:
            yield Tabs("John", "Aeryn", "Moya", "Pilot")

    async with TabsApp().run_test() as pilot:
        tabs = pilot.app.query_one(Tabs)
        assert tabs.tab_count == 4
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"

        await pilot.click("#tab-2")
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-2"

        await pilot.click("Underline", offset=(2, 0))
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "tab-1"


class TabsMessageCatchApp(App[None]):
    def __init__(self) -> None:
        super().__init__()
        self.intended_handlers: list[str] = []

    def compose(self) -> ComposeResult:
        yield Tabs("John", "Aeryn", "Moya", "Pilot")

    @on(Tabs.Cleared)
    @on(Tabs.TabActivated)
    @on(Underline.Clicked)
    @on(Tab.Clicked)
    def log_message(
        self, event: Tabs.Cleared | Tabs.TabActivated | Underline.Clicked | Tab.Clicked
    ) -> None:
        self.intended_handlers.append(event.handler_name)

    @on(Tabs.TabActivated)
    @on(Tabs.Cleared)
    def check_control(self, event: Tabs.TabActivated) -> None:
        assert event.control is event.tabs


async def test_startup_messages():
    """On startup there should be a tab activated message."""
    async with TabsMessageCatchApp().run_test() as pilot:
        assert pilot.app.intended_handlers == ["on_tabs_tab_activated"]


async def test_change_tab_with_code_messages():
    """Changing tab in code should result in an activated tab message."""
    async with TabsMessageCatchApp().run_test() as pilot:
        pilot.app.query_one(Tabs).active = "tab-2"
        await pilot.pause()
        assert pilot.app.intended_handlers == [
            "on_tabs_tab_activated",
            "on_tabs_tab_activated",
        ]


async def test_remove_tabs_messages():
    """Removing tabs should result in various messages."""
    async with TabsMessageCatchApp().run_test() as pilot:
        tabs = pilot.app.query_one(Tabs)
        for n in range(4):
            await tabs.remove_tab(f"tab-{n+1}")

        await pilot.pause()
        assert pilot.app.intended_handlers == [
            "on_tabs_tab_activated",
            "on_tabs_tab_activated",
            "on_tabs_tab_activated",
            "on_tabs_tab_activated",
            "on_tabs_cleared",
        ]


async def test_reverse_remove_tabs_messages():
    """Removing tabs should result in various messages."""
    async with TabsMessageCatchApp().run_test() as pilot:
        tabs = pilot.app.query_one(Tabs)
        for n in reversed(range(4)):
            await tabs.remove_tab(f"tab-{n+1}")

        await pilot.pause()
        assert pilot.app.intended_handlers == [
            "on_tabs_tab_activated",
            "on_tabs_cleared",
        ]


async def test_keyboard_navigation_messages():
    """Keyboard navigation should result in the expected messages."""
    async with TabsMessageCatchApp().run_test() as pilot:
        await pilot.press("right")
        await pilot.pause()
        await pilot.press("left")
        await pilot.pause()
        assert pilot.app.intended_handlers == [
            "on_tabs_tab_activated",
            "on_tabs_tab_activated",
            "on_tabs_tab_activated",
        ]


async def test_mouse_navigation_messages():
    """Mouse navigation should result in the expected messages."""
    async with TabsMessageCatchApp().run_test() as pilot:
        await pilot.click("#tab-2")
        await pilot.pause()
        await pilot.click("Underline", offset=(2, 0))
        await pilot.pause()
        assert pilot.app.intended_handlers == [
            "on_tabs_tab_activated",
            "on_tabs_tab_activated",
            "on_tabs_tab_activated",
        ]


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

    class DisabledTabApp(App):
        def compose(self) -> ComposeResult:
            yield Tabs(
                Tab("Enabled", id="enabled"),
                Tab("Disabled", id="disabled", disabled=True),
            )

    app = DisabledTabApp()
    async with app.run_test() as pilot:
        # Click the underline beneath the disabled tab
        await pilot.click(Tabs, offset=(14, 2))
        tabs = pilot.app.query_one(Tabs)
        assert tabs.active_tab is not None
        assert tabs.active_tab.id == "enabled"